UNPKG

@eang/core

Version:

eang - model driven enterprise event processing

170 lines 5.53 kB
import { v7 as uuidv7 } from 'uuid'; class Entity { id; key; typeOf; instanceOf; name; data; tags; attributes; createdBy; createdAt; updatedBy; updatedAt; constructor(entityOpts) { this.id = entityOpts.id; this.key = entityOpts.key; this.typeOf = entityOpts.typeOf; this.instanceOf = entityOpts.instanceOf; this.name = entityOpts.name; this.data = entityOpts.data; this.tags = entityOpts.tags || {}; this.attributes = entityOpts.attributes || {}; this.createdBy = entityOpts.createdBy; this.createdAt = entityOpts.createdAt; this.updatedBy = entityOpts.updatedBy; this.updatedAt = entityOpts.updatedAt; } } export class Obj extends Entity { entityType = 'obj'; constructor(objOpts) { let id; let key; let typeOf; if (objOpts.id) { id = objOpts.id; const { key: parsedKey, typeOf: parsedTypeOf } = parseCombinedId(objOpts.id); key = parsedKey; typeOf = parsedTypeOf; } else { typeOf = objOpts.typeOf; key = objOpts.key || uuidv7(); id = typeOf + '/' + key; } super({ ...objOpts, id, key, typeOf }); } } export class Cnx extends Entity { entityType = 'cnx'; typeOf; fromObjId; fromObjKey; fromObjTypeOf; fromObjInstanceOf; toObjId; toObjKey; toObjTypeOf; toObjInstanceOf; constructor(cnxOpts) { let id; let key; let typeOf; if (cnxOpts.id) { id = cnxOpts.id; const { key: parsedKey, typeOf: parsedTypeOf } = parseCombinedId(cnxOpts.id); key = parsedKey; typeOf = parsedTypeOf ?? cnxOpts.typeOf; if (cnxOpts.typeOf && parsedTypeOf !== cnxOpts.typeOf) { throw new Error(`Type mismatch: expected ${cnxOpts.typeOf}, got ${parsedTypeOf}`); } } else if (cnxOpts.typeOf) { typeOf = cnxOpts.typeOf; key = cnxOpts.key || uuidv7(); id = typeOf + '/' + key; } else { throw new Error('Either id or typeOf must be provided for Cnx'); } super({ ...cnxOpts, id, key, typeOf }); this.typeOf = typeOf; if (cnxOpts.fromObjId) { this.fromObjId = cnxOpts.fromObjId; const { key: fromKey, typeOf: fromTypeOf } = parseCombinedId(cnxOpts.fromObjId); this.fromObjKey = fromKey; this.fromObjTypeOf = fromTypeOf; } else if (cnxOpts.fromObjKey && cnxOpts.fromObjTypeOf) { this.fromObjKey = cnxOpts.fromObjKey; this.fromObjTypeOf = cnxOpts.fromObjTypeOf; this.fromObjId = cnxOpts.fromObjTypeOf + '/' + cnxOpts.fromObjKey; } else { throw new Error('fromObjId or fromObjKey and fromObjTypeOf are required for Cnx'); } this.fromObjInstanceOf = cnxOpts.fromObjInstanceOf; if (cnxOpts.toObjId) { this.toObjId = cnxOpts.toObjId; const { key: toKey, typeOf: toTypeOf } = parseCombinedId(cnxOpts.toObjId); this.toObjKey = toKey; this.toObjTypeOf = toTypeOf; } else if (cnxOpts.toObjKey && cnxOpts.toObjTypeOf) { this.toObjKey = cnxOpts.toObjKey; this.toObjTypeOf = cnxOpts.toObjTypeOf; this.toObjId = cnxOpts.toObjTypeOf + '/' + cnxOpts.toObjKey; } else { throw new Error('toObjId or toObjKey and toObjTypeOf are required for Cnx'); } this.toObjInstanceOf = cnxOpts.toObjInstanceOf; } } export const EangEventEnum = { create: 'create', update: 'update', delete: 'delete', start: 'start', stop: 'stop' }; export class EntityEvent { entity; eventType; context; tenant; organizationalUnit; user; constructor(entity, eventType, context, options) { this.entity = entity; this.eventType = eventType; this.context = context; this.tenant = options?.tenant; this.organizationalUnit = options?.organizationalUnit; this.user = options?.user; } static parse(entity, eventType, context, options) { return new EntityEvent(entity, eventType, context, options); } static create(entity, options) { if (!entity.createdAt) { entity.createdAt = Date.now(); } return new EntityEvent(entity, EangEventEnum.create, undefined, options); } static start(entity, context, options) { if ('createdAt' in entity && !entity.createdAt) { entity.createdAt = Date.now(); } if (!('startedAt' in entity) || !entity.startedAt) { entity.startedAt = Date.now(); } return new EntityEvent(entity, EangEventEnum.start, context, options); } static stop(entity, context, options) { if (!('stoppedAt' in entity) || !entity.stoppedAt) { entity.stoppedAt = Date.now(); } return new EntityEvent(entity, EangEventEnum.stop, context, options); } } export function parseCombinedId(id) { const sepIdx = id.indexOf('/'); if (sepIdx === -1 || sepIdx === 0 || sepIdx === id.length - 1) { throw new Error(`Invalid ID format: ${id}`); } return { typeOf: id.slice(0, sepIdx), key: id.slice(sepIdx + 1) }; } //# sourceMappingURL=entity.js.map