diff --git a/app/src/data/entities/Entity.ts b/app/src/data/entities/Entity.ts index cdcbce6..48f23b9 100644 --- a/app/src/data/entities/Entity.ts +++ b/app/src/data/entities/Entity.ts @@ -44,6 +44,8 @@ export type EntityJSON = ReturnType; export const entityTypes = ["regular", "system", "generated"] as const; 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 option to disallow api deletes (or api actions in general) @@ -89,6 +91,14 @@ export class Entity< } 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: { diff --git a/app/src/data/entities/EntityManager.ts b/app/src/data/entities/EntityManager.ts index 0ab634a..6757170 100644 --- a/app/src/data/entities/EntityManager.ts +++ b/app/src/data/entities/EntityManager.ts @@ -118,12 +118,12 @@ export class EntityManager { ): Silent extends true ? Entity | undefined : Entity { // make sure to always retrieve by name 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 (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; @@ -236,7 +236,7 @@ export class EntityManager { } 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); } diff --git a/app/src/modules/ModuleHelper.ts b/app/src/modules/ModuleHelper.ts index 5088510..8561ebe 100644 --- a/app/src/modules/ModuleHelper.ts +++ b/app/src/modules/ModuleHelper.ts @@ -66,9 +66,14 @@ export class ModuleHelper { } ensureRelation(relation: EntityRelation) { - if (!this.em.relations.exists(relation)) { + try { + // most reliable way at the moment this.em.addRelation(relation); 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)) { } }