UNPKG

@mas-soft/mas-core-server

Version:

main application

157 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class ModelDefinition { constructor(nameOrDef) { if (typeof nameOrDef === 'string') { nameOrDef = { name: nameOrDef }; } const { name, properties, settings, relations } = nameOrDef; this.name = name; this.properties = {}; if (properties) { for (const p in properties) { this.addProperty(p, properties[p]); } } this.settings = settings || new Map(); this.relations = relations || {}; } addProperty(name, definitionOrType) { const definition = definitionOrType.type ? definitionOrType : { type: definitionOrType }; this.properties[name] = definition; return this; } addSetting(name, value) { this.settings[name] = value; return this; } addRelation(definition) { this.relations[definition.name] = definition; return this; } idProperties() { if (typeof this.settings.id === 'string') { return [this.settings.id]; } else if (Array.isArray(this.settings.id)) { return this.settings.id; } const idProps = Object.keys(this.properties).filter(prop => this.properties[prop].id); return idProps; } } exports.ModelDefinition = ModelDefinition; function asJSON(value) { if (value == null) return value; if (typeof value.toJSON === 'function') { return value.toJSON(); } if (Array.isArray(value)) { return value.map(item => asJSON(item)); } return value; } function asObject(value, options) { if (value == null) return value; if (typeof value.toObject === 'function') { return value.toObject(options); } if (typeof value.toJSON === 'function') { return value.toJSON(); } if (Array.isArray(value)) { return value.map(item => asObject(item, options)); } return value; } class Model { static get modelName() { return (this.definition && this.definition.name) || this.name; } toJSON() { const def = this.constructor.definition; if (def == null || def.settings.strict === false) { return this.toObject({ ignoreUnknownProperties: false }); } const json = {}; for (const p in def.properties) { if (p in this) { json[p] = asJSON(this[p]); } } return json; } toObject(options) { let obj; if (options && options.ignoreUnknownProperties === false) { obj = {}; for (const p in this) { let val = this[p]; obj[p] = asObject(val, options); } } else { obj = this.toJSON(); } return obj; } constructor(data) { Object.assign(this, data); } } exports.Model = Model; class ValueObject extends Model { } exports.ValueObject = ValueObject; class Entity extends Model { static getIdOf(entityOrData) { if (typeof entityOrData.getId === 'function') { return entityOrData.getId(); } const idName = this.definition.idName(); return entityOrData[idName]; } getId() { const definition = this.constructor.definition; const idProps = definition.idProperties(); if (idProps.length === 1) { return this[idProps[0]]; } if (!idProps.length) { throw new Error(`Invalid Entity ${this.constructor.name}:` + 'missing primary key (id) property'); } return this.getIdObject(); } getIdObject() { const definition = this.constructor.definition; const idProps = definition.idProperties(); const idObj = {}; for (const idProp of idProps) { idObj[idProp] = this[idProp]; } return idObj; } static buildWhereForId(id) { const where = {}; const idProps = this.definition.idProperties(); if (idProps.length === 1) { where[idProps[0]] = id; } else { for (const idProp of idProps) { where[idProp] = id[idProp]; } } return where; } } exports.Entity = Entity; class Event { } exports.Event = Event; //# sourceMappingURL=model.js.map