UNPKG

@eang/core

Version:

eang - model driven enterprise event processing

210 lines 5.93 kB
import { Obj, parseCombinedId } from './entity.js'; export class OrganizationObj extends Obj { typeOf = 'Organization'; constructor(opts) { super({ ...opts, typeOf: 'Organization' }); } } export class ItemObj extends Obj { typeOf = 'Item'; constructor(opts) { super({ ...opts, typeOf: 'Item' }); } } export class GroupObj extends Obj { typeOf = 'Group'; constructor(opts) { super({ ...opts, typeOf: 'Group' }); } } export class SystemObj extends Obj { typeOf = 'System'; constructor(opts) { super({ ...opts, typeOf: 'System' }); } } export class FolderObj extends Obj { typeOf = 'Folder'; constructor(opts) { super({ ...opts, typeOf: 'Folder' }); } } export class Security_PolicyObj extends Obj { typeOf = 'Security_Policy'; constructor(opts) { super({ ...opts, typeOf: 'Security_Policy' }); } } export class EventInstanceObj extends Obj { typeOf = 'Event_Instance'; constructor(opts) { super({ ...opts, typeOf: 'Event_Instance' }); } } export class ErrorEventInstanceObj extends Obj { typeOf = 'Error_Event_Instance'; message; code; stack; cause; severity = 'error'; /** Error occurrence time is tracked via createdAt */ constructor(opts) { super({ ...opts, typeOf: 'Error_Event_Instance' }); this.message = opts.message; this.code = opts.code; this.stack = opts.stack; this.cause = opts.cause; this.severity = opts.severity || 'error'; } } export class ProcessInstanceObj extends Obj { typeOf = 'Process_Instance'; startedAt; stoppedAt; constructor(opts) { super({ ...opts, typeOf: 'Process_Instance' }); this.startedAt = opts.startedAt; this.stoppedAt = opts.stoppedAt; } } export class FunctionInstanceObj extends Obj { typeOf = 'Function_Instance'; startedAt; stoppedAt; constructor(opts) { super({ ...opts, typeOf: 'Function_Instance' }); this.startedAt = opts.startedAt; this.stoppedAt = opts.stoppedAt; } } export class FunctionObj extends Obj { typeOf = 'Function'; /** Function timeout in milliseconds */ timeout; /** Memory limit in MB */ memoryLimit; /** Environment variables specific to this function */ env; /** Key-value pairs of secrets available to this object */ secrets; constructor(opts) { super({ ...opts, typeOf: 'Function' }); this.timeout = opts.timeout; this.memoryLimit = opts.memoryLimit; this.env = opts.env; this.secrets = opts.secrets; } } export class ServiceObj extends Obj { typeOf = 'Service'; /** Service type (e.g., 'http', 'grpc', 'websocket') */ //type!: string /** Service port */ //port?: number /** Service host */ //host?: string /** Service health check endpoint */ //healthCheckEndpoint?: string /** Service version */ //version?: string /** Environment variables specific to this service */ env; /** Secrets available to this service */ secrets; /** Service scaling configuration */ // scaling?: { // minInstances?: number // maxInstances?: number // targetCPUUtilization?: number // } /** Service deployment configuration */ // deployment?: { // strategy?: 'rolling' | 'recreate' | 'blue-green' // replicas?: number // resources?: { // cpu?: string // memory?: string // } // } constructor(opts) { super({ ...opts, typeOf: 'Service' }); this.env = opts.env; this.secrets = opts.secrets; } } // --- Type Guards --- export function isOrganizationObj(obj) { return obj.typeOf === 'Organization'; } export function isItemObj(obj) { return obj.typeOf === 'Item'; } export function isEventInstanceObj(obj) { return obj.typeOf === 'Event_Instance'; } export function isErrorEventInstanceObj(obj) { return obj.typeOf === 'Error_Event_Instance'; } export function isProcessInstanceObj(obj) { return obj.typeOf === 'Process_Instance'; } export function isFunctionInstanceObj(obj) { return obj.typeOf === 'Function_Instance'; } export function isFunctionObj(obj) { return obj.typeOf === 'Function'; } export function isServiceObj(obj) { return obj.typeOf === 'Service'; } export function isGroupObj(obj) { return obj.typeOf === 'Group'; } export function isSystemObj(obj) { return obj.typeOf === 'System'; } export function isFolderObj(obj) { return obj.typeOf === 'Folder'; } export function isSecurityPolicyObj(obj) { return obj.typeOf === 'Security_Policy'; } // Generic type guard export function isObjOfType(obj, typeOf) { return obj.typeOf === typeOf; } // --- TypeOf to Class Map and Factory --- export const typeOfToClassMap = { Event_Instance: EventInstanceObj, Error_Event_Instance: ErrorEventInstanceObj, Process_Instance: ProcessInstanceObj, Function_Instance: FunctionInstanceObj, Function: FunctionObj, Service: ServiceObj, Item: ItemObj, Group: GroupObj, System: SystemObj, Folder: FolderObj, Security_Policy: Security_PolicyObj, Organization: OrganizationObj }; export function registerTypeOfClass(typeOf, cls) { typeOfToClassMap[typeOf] = cls; } export function createObjFromTypeOf(data, typeOf) { let type = data.typeOf || typeOf; if (!type && data.id) { const { typeOf: typeFromId } = parseCombinedId(data.id); type = typeFromId; } if (!type) throw new Error('typeOf is required to create an object'); if (typeof type !== 'string') throw new Error('typeOf must be a string'); const Cls = typeOfToClassMap[type]; if (!Cls) throw new Error(`Unknown typeOf: ${type}`); return new Cls(data); } //# sourceMappingURL=objects.js.map