@eang/core
Version:
eang - model driven enterprise event processing
255 lines • 7.16 kB
JavaScript
import { Obj, parseCombinedId } from './entity.js';
export class OrganizationObj extends Obj {
typeOf = 'Organization';
constructor(opts) {
super({ ...opts, typeOf: 'Organization' });
}
}
export function isOrganizationObj(obj) {
return obj.typeOf === 'Organization';
}
export class ItemObj extends Obj {
typeOf = 'Item';
constructor(opts) {
super({ ...opts, typeOf: 'Item' });
}
}
export function isItemObj(obj) {
return obj.typeOf === 'Item';
}
export class GroupObj extends Obj {
typeOf = 'Group';
constructor(opts) {
super({ ...opts, typeOf: 'Group' });
}
}
export function isGroupObj(obj) {
return obj.typeOf === 'Group';
}
export class SystemObj extends Obj {
typeOf = 'System';
constructor(opts) {
super({ ...opts, typeOf: 'System' });
}
}
export function isSystemObj(obj) {
return obj.typeOf === 'System';
}
export class FolderObj extends Obj {
typeOf = 'Folder';
constructor(opts) {
super({ ...opts, typeOf: 'Folder' });
}
}
export function isFolderObj(obj) {
return obj.typeOf === 'Folder';
}
export class Security_PolicyObj extends Obj {
typeOf = 'Security_Policy';
constructor(opts) {
super({ ...opts, typeOf: 'Security_Policy' });
}
}
export function isSecurityPolicyObj(obj) {
return obj.typeOf === 'Security_Policy';
}
export class PersonObj extends Obj {
typeOf = 'Person';
username;
constructor(opts) {
super({ ...opts, typeOf: 'Person' });
this.username = opts.username;
}
}
export function isPersonObj(obj) {
return obj.typeOf === 'Person';
}
export class EventInstanceObj extends Obj {
typeOf = 'Event_Instance';
constructor(opts) {
super({ ...opts, typeOf: 'Event_Instance' });
}
}
export function isEventInstanceObj(obj) {
return obj.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;
if (opts.code !== undefined) {
this.code = opts.code;
}
if (opts.stack !== undefined) {
this.stack = opts.stack;
}
if (opts.cause !== undefined) {
this.cause = opts.cause;
}
this.severity = opts.severity || 'error';
}
}
export function isErrorEventInstanceObj(obj) {
return obj.typeOf === 'Error_Event_Instance';
}
export class ProcessInstanceObj extends Obj {
typeOf = 'Process_Instance';
startedAt;
stoppedAt;
constructor(opts) {
super({ ...opts, typeOf: 'Process_Instance' });
if (opts.startedAt !== undefined) {
this.startedAt = opts.startedAt;
}
if (opts.stoppedAt !== undefined) {
this.stoppedAt = opts.stoppedAt;
}
}
}
export function isProcessInstanceObj(obj) {
return obj.typeOf === 'Process_Instance';
}
export class FunctionInstanceObj extends Obj {
typeOf = 'Function_Instance';
startedAt;
stoppedAt;
constructor(opts) {
super({ ...opts, typeOf: 'Function_Instance' });
this.startedAt = opts.startedAt || Date.now();
if (opts.stoppedAt !== undefined) {
this.stoppedAt = opts.stoppedAt;
}
}
}
export function isFunctionInstanceObj(obj) {
return obj.typeOf === 'Function_Instance';
}
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' });
if (opts.timeout !== undefined) {
this.timeout = opts.timeout;
}
if (opts.memoryLimit !== undefined) {
this.memoryLimit = opts.memoryLimit;
}
if (opts.env !== undefined) {
this.env = opts.env;
}
if (opts.secrets !== undefined) {
this.secrets = opts.secrets;
}
}
}
export function isFunctionObj(obj) {
return obj.typeOf === 'Function';
}
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' });
if (opts.env !== undefined) {
this.env = opts.env;
}
if (opts.secrets !== undefined) {
this.secrets = opts.secrets;
}
}
}
export function isServiceObj(obj) {
return obj.typeOf === 'Service';
}
// Generic type guard
export function isObjOfType(obj, typeOf) {
return obj.typeOf === typeOf;
}
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,
Person: PersonObj
};
export function registerTypeOfClass(typeOf, cls) {
;
typeOfToClassMap[typeOf] =
cls;
}
export function createObj(data, typeOf, key) {
if (data.typeOf &&
typeof data.typeOf === 'string' &&
data.key &&
typeof data.key === 'string') {
// If data already has typeOf and key, use them directly
}
else if (typeOf && typeof typeOf === 'string' && key && typeof key === 'string') {
data.typeOf = typeOf;
data.key = key;
}
else if (data.id && typeof data.id === 'string') {
const { key, typeOf } = parseCombinedId(data.id);
data.typeOf = typeOf;
data.key = key;
}
else {
throw new Error('Either id or typeOf and key must be provided as strings to create an object');
}
const Cls = typeOfToClassMap[data.typeOf];
if (!Cls)
throw new Error(`Unknown typeOf: ${data.typeOf}`);
return new Cls(data);
}
//# sourceMappingURL=objects.js.map