UNPKG

@eang/core

Version:

eang - model driven enterprise event processing

214 lines 7.24 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.createdAt = Date.now(); // Default to now if not provided if (entityOpts.instanceOf !== undefined) { this.instanceOf = entityOpts.instanceOf; } if (entityOpts.name !== undefined) { this.name = entityOpts.name; } if (entityOpts.data !== undefined) { this.data = entityOpts.data; } this.tags = entityOpts.tags || {}; this.attributes = entityOpts.attributes || {}; if (entityOpts.createdBy !== undefined) { this.createdBy = entityOpts.createdBy; } if (entityOpts.createdAt !== undefined) { this.createdAt = entityOpts.createdAt; } if (entityOpts.updatedBy !== undefined) { this.updatedBy = entityOpts.updatedBy; } if (entityOpts.updatedAt !== undefined) { 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'); } const entityOpts = { id, key, typeOf }; if (cnxOpts.instanceOf !== undefined) entityOpts.instanceOf = cnxOpts.instanceOf; if (cnxOpts.name !== undefined) entityOpts.name = cnxOpts.name; if (cnxOpts.data !== undefined) entityOpts.data = cnxOpts.data; if (cnxOpts.tags !== undefined) entityOpts.tags = cnxOpts.tags; if (cnxOpts.attributes !== undefined) entityOpts.attributes = cnxOpts.attributes; if (cnxOpts.createdBy !== undefined) entityOpts.createdBy = cnxOpts.createdBy; if (cnxOpts.createdAt !== undefined) entityOpts.createdAt = cnxOpts.createdAt; if (cnxOpts.updatedBy !== undefined) entityOpts.updatedBy = cnxOpts.updatedBy; if (cnxOpts.updatedAt !== undefined) entityOpts.updatedAt = cnxOpts.updatedAt; super(entityOpts); 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'); } if (cnxOpts.fromObjInstanceOf !== undefined) { 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'); } if (cnxOpts.toObjInstanceOf !== undefined) { this.toObjInstanceOf = cnxOpts.toObjInstanceOf; } } } export const EangEventType = { 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; if (options?.tenant !== undefined) { this.tenant = options.tenant; } if (options?.organizationalUnit !== undefined) { this.organizationalUnit = options.organizationalUnit; } if (options?.user !== undefined) { 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, EangEventType.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, EangEventType.start, context, options); } static stop(entity, context, options) { if (!('stoppedAt' in entity) || !entity.stoppedAt) { entity.stoppedAt = Date.now(); } return new EntityEvent(entity, EangEventType.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