fix: plugin schema reconciliation

This commit is contained in:
dswbx
2025-06-18 10:31:40 +02:00
parent 344d729320
commit b2086c4da7
3 changed files with 19 additions and 4 deletions

View File

@@ -44,6 +44,8 @@ export type EntityJSON = ReturnType<Entity["toJSON"]>;
export const entityTypes = ["regular", "system", "generated"] as const; export const entityTypes = ["regular", "system", "generated"] as const;
export type TEntityType = (typeof entityTypes)[number]; export type TEntityType = (typeof entityTypes)[number];
const ENTITY_SYMBOL = Symbol.for("bknd:entity");
/** /**
* @todo: add check for adding fields (primary and relation not allowed) * @todo: add check for adding fields (primary and relation not allowed)
* @todo: add option to disallow api deletes (or api actions in general) * @todo: add option to disallow api deletes (or api actions in general)
@@ -89,6 +91,14 @@ export class Entity<
} }
if (type) this.type = type; if (type) this.type = type;
this[ENTITY_SYMBOL] = true;
}
// this is currently required as there could be multiple variants
// we need to migrate to a mono repo
static isEntity(e: unknown): e is Entity {
if (!e) return false;
return e[ENTITY_SYMBOL] === true;
} }
static create(args: { static create(args: {

View File

@@ -118,12 +118,12 @@ export class EntityManager<TBD extends object = DefaultDB> {
): Silent extends true ? Entity | undefined : Entity { ): Silent extends true ? Entity | undefined : Entity {
// make sure to always retrieve by name // make sure to always retrieve by name
const entity = this.entities.find((entity) => const entity = this.entities.find((entity) =>
e instanceof Entity ? entity.name === e.name : entity.name === e, Entity.isEntity(e) ? entity.name === e.name : entity.name === e,
); );
if (!entity) { if (!entity) {
if (silent === true) return undefined as any; if (silent === true) return undefined as any;
throw new EntityNotDefinedException(e instanceof Entity ? e.name : (e as string)); throw new EntityNotDefinedException(Entity.isEntity(e) ? e.name : (e as string));
} }
return entity; return entity;
@@ -236,7 +236,7 @@ export class EntityManager<TBD extends object = DefaultDB> {
} }
getIndicesOf(_entity: Entity | string): EntityIndex[] { getIndicesOf(_entity: Entity | string): EntityIndex[] {
const entity = _entity instanceof Entity ? _entity : this.entity(_entity); const entity = Entity.isEntity(_entity) ? _entity : this.entity(_entity);
return this.indices.filter((index) => index.entity.name === entity.name); return this.indices.filter((index) => index.entity.name === entity.name);
} }

View File

@@ -66,9 +66,14 @@ export class ModuleHelper {
} }
ensureRelation(relation: EntityRelation) { ensureRelation(relation: EntityRelation) {
if (!this.em.relations.exists(relation)) { try {
// most reliable way at the moment
this.em.addRelation(relation); this.em.addRelation(relation);
this.flags.sync_required = true; this.flags.sync_required = true;
} catch (e) {}
// @todo: improve this function, seems like it still doesn't catch all cases
if (!this.em.relations.exists(relation)) {
} }
} }