mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
Refactor entity handling to preserve config while overriding type
Reworked `ensureEntity` to replace entities while maintaining their configuration and allowing type adjustments. Updated tests to verify type persistence and synchronization of entity properties.
This commit is contained in:
@@ -140,7 +140,7 @@ export class Entity<
|
||||
return this.fields.find((field) => field.name === name);
|
||||
}
|
||||
|
||||
__experimental_replaceField(name: string, field: Field) {
|
||||
__replaceField(name: string, field: Field) {
|
||||
const index = this.fields.findIndex((f) => f.name === name);
|
||||
if (index === -1) {
|
||||
throw new Error(`Field "${name}" not found on entity "${this.name}"`);
|
||||
|
||||
@@ -99,6 +99,16 @@ export class EntityManager<TBD extends object = DefaultDB> {
|
||||
this.entities.push(entity);
|
||||
}
|
||||
|
||||
__replaceEntity(entity: Entity, name: string | undefined = entity.name) {
|
||||
const entityIndex = this._entities.findIndex((e) => e.name === name);
|
||||
|
||||
if (entityIndex === -1) {
|
||||
throw new Error(`Entity "${name}" not found and cannot be replaced`);
|
||||
}
|
||||
|
||||
this._entities[entityIndex] = entity;
|
||||
}
|
||||
|
||||
entity(e: Entity | keyof TBD | string): Entity {
|
||||
let entity: Entity | undefined;
|
||||
if (typeof e === "string") {
|
||||
|
||||
Reference in New Issue
Block a user