UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

528 lines 18.6 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Entity = exports.systemProperty = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const data_1 = require("../../service/data"); const cacheData_1 = require("../cacheData"); exports.systemProperty = { id: { name: 'id', label: '主键', description: '主键', primaryKey: true, typeKey: '#/basicTypes/Long', type: 'integer', format: 'long', editable: false, display: { inTable: false, inFilter: false, inForm: false, inDetail: false, }, }, createdTime: { name: 'createdTime', label: '创建时间', description: '创建时间', typeKey: '#/basicTypes/DateTime', type: 'string', format: 'date-time', editable: false, display: { inTable: true, inFilter: false, inForm: false, inDetail: false, }, }, updatedTime: { name: 'updatedTime', label: '更新时间', description: '更新时间', typeKey: '#/basicTypes/DateTime', type: 'string', format: 'date-time', editable: false, display: { inTable: true, inFilter: false, inForm: false, inDetail: false, }, }, createdBy: { name: 'createdBy', label: '创建者', description: '创建者', typeKey: '#/basicTypes/String', type: 'string', format: '', editable: false, display: { inTable: false, inFilter: false, inForm: false, inDetail: false, }, }, updatedBy: { name: 'updatedBy', label: '更新者', description: '更新者', typeKey: '#/basicTypes/String', type: 'string', format: '', editable: false, display: { inTable: false, inFilter: false, inForm: false, inDetail: false, }, }, }; /** * 实体类 */ class Entity extends __1.Vertex { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.entity; /** * 实体 Id */ this.id = undefined; /** * dataTypes 中的唯一标识 */ this.schemaRef = undefined; /** * 实体名称 */ this.name = undefined; /** * 实体描述 */ this.description = undefined; /** * 实体类型(如enum) */ this.type = 'object'; /** * 实体属性列表 */ this.propertyList = []; /** * 实体索引列表 */ this.indexList = []; this.resolvers = []; /** * 所属服务 Id */ this.serviceId = undefined; /** * 所属服务类型 */ this.serviceType = undefined; /** * 所属服务 */ this.service = undefined; /** * 上个版本数据 */ this.lastVersionDef = undefined; /** * 父节点 */ this.dataNode = undefined; /** * 周边存在的名称 */ this.existingNames = []; source && this.assign(source); } /** * 按当前 id 加载实体数据 * @requires this.id */ async load() { const result = await data_1.entityService.loadDetail({ query: { id: this.id, }, config: { mock: __1.config.mock, }, }); this.assign(result); return this; } /** * 添加实体 */ async create(none, actionOptions) { __1.config.defaultApp?.emit('saving', true); Object.keys(exports.systemProperty).forEach((propertyKey) => { this.propertyList.push(new __1.EntityProperty(Object.assign({ root: this }, exports.systemProperty[propertyKey]))); }); const body = this.toJSON(); body.propertyList.forEach((property, index) => property._posIndex = index); __1.utils.logger.debug('添加实体', body); try { const result = await data_1.entityService.create({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Entity.create', operationDesc: actionOptions?.actionDesc || `添加实体"${this.name}"`, }, body, }); this.deepPick(result, ['id']); this.emit('created'); this.assign({ schemaRef: `#/${this.dataNode.service.id}/${result.id}`, }); __1.dataTypesMap[this.schemaRef] = this; // 先改成异步的 Promise.all([ this.syncInterfaces(), ]).then(() => { __1.updateDataTypeList(); this.dataNode.service.emit('dataTypesChange'); this.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); }); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } catch (err) { __1.config.defaultApp?.emit('saved'); throw err; } } /** * 修改实体 */ async update(none, actionOptions, then) { __1.config.defaultApp?.emit('saving', true); const body = this.toJSON('', ['propertyList', 'schemaRef']); __1.utils.logger.debug('修改实体', body); const result = await data_1.entityService.update({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Entity.update', operationDesc: actionOptions?.actionDesc || `修改实体"${this.name}"`, }, body, }); await then?.(); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除实体 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving', true); if (this.id) { await data_1.entityService.delete({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Entity.delete', operationDesc: actionOptions?.actionDesc || `删除实体"${this.name}"`, }, query: { id: this.id, }, }); } const index = this.dataNode.entities.indexOf(this); ~index && this.dataNode.entities.splice(index, 1); delete __1.dataTypesMap[this.schemaRef]; if (this.id) { await this.syncInterfaces(true); } __1.updateDataTypeList(); this.destroy(); this.dataNode.service.emit('dataTypesChange'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } /** * 设置实体名称 * @param name 名称 */ async setName(name) { const oldName = this.name; this.assign({ name }); await this.update(undefined, { actionDesc: `设置实体"${oldName}"的名称为"${name}"`, }, async () => { await this.syncInterfaces(); }); __1.updateDataTypeList(); this.dataNode.service.emit('dataTypesChange'); this.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); } /** * 设置实体描述 * @param description 描述 */ async setDescription(description) { this.assign({ description }); await this.update(undefined, { actionDesc: `设置实体"${this.name}"的描述为"${description}"`, }); } /** * 同步interfaces * interface、logic、params、returns、variabes采用plainAssign * 创建、删除实体,修改实体名称、增删改实体property需要同步 */ async syncInterfaces(isDelete) { if (isDelete) { const service = this.dataNode.service; const newInterfaces = service.interfaces.filter((itface) => itface.logic.entityId !== this.id); service.assign({ interfaces: newInterfaces }); } else { const service = this.dataNode.service; const interfaces = await data_1.interfaceService.loadList({ query: { serviceId: service.id, entityId: this.id, }, }); const entity = this; entity.assign({ resolvers: [] }); let newInterfaces = []; interfaces.forEach((itface) => { if (itface.serviceType === 'export') { return; } let tempItface = cacheData_1.vertexsMap.get(itface.id); if (tempItface) { tempItface.plainAssign(itface); if (tempItface.logic) { tempItface.logic.plainAssign(itface.logic); const params = []; const returns = []; const variables = []; itface.logic.params.forEach((param) => { const tempParam = cacheData_1.vertexsMap.get(param.id); if (tempParam) { tempParam.plainAssign(param); tempParam.genSchemaChildren(); params.push(tempParam); } else { params.push(__1.Param.from(param, tempItface.logic)); } }); tempItface.logic.assign({ params }); itface.logic.returns.forEach((ret) => { const tempRet = cacheData_1.vertexsMap.get(ret.id); if (tempRet) { tempRet.plainAssign(ret); tempRet.genSchemaChildren(); returns.push(tempRet); } else { returns.push(__1.Return.from(ret, tempItface.logic)); } }); tempItface.logic.assign({ returns }); itface.logic.variables.forEach((variable) => { const tempVariable = cacheData_1.vertexsMap.get(variable.id); if (tempVariable) { tempVariable.plainAssign(variable); tempVariable.genSchemaChildren(); variables.push(tempVariable); } else { variables.push(__1.Variable.from(variable, tempItface.logic)); } }); tempItface.logic.assign({ variables }); } } else { tempItface = __1.Interface.from(itface, service); tempItface.entityId = entity.id; newInterfaces.push(tempItface); } // 重新生成resolvers // const RESOLVER_NAMES = ['getAll', 'get', 'create', 'update', 'delete', 'count', 'import', 'export', 'batchDelete', 'batchCreate', 'batchUpdate']; // if (tempItface.logic && tempItface.logic.entityId) { // entity.assign({ resolvers: entity.resolvers || [] }); // const resolverName = RESOLVER_NAMES.find((name) => tempItface.name.startsWith(name)); // entity.resolvers.push({ // level: 'resolver', // name: resolverName, // interface: tempItface, // entity, // isLeaf: true, // }); // } }); newInterfaces = newInterfaces.concat(service.interfaces); service.assign({ interfaces: newInterfaces }); service.mountResolverOnEntity(); service.mountResolverOnInterface(); } this.dataNode.service.emit('interfacesChange'); } /** * 同步structures * 创建、删除实体,修改实体名称需要同步 */ async syncStructures(isDelete) { if (isDelete) { const service = this.dataNode.service; const newStructures = service.data.structures.filter((structure) => structure.entityId !== this.id); service.data.assign({ structures: newStructures }); Object.keys(__1.dataTypesMap).forEach((dataTypeKey) => { if (__1.dataTypesMap[dataTypeKey].entityId === this.id) { delete __1.dataTypesMap[dataTypeKey]; } }); } else { const service = this.dataNode.service; const structures = await data_1.structureService.loadList({ query: { id: service.id, }, }); const newStructures = []; structures.forEach((structure) => { let structureInstance = cacheData_1.vertexsMap.get(structure.id); if (structureInstance) { structureInstance.plainAssign(structure); } else { __1.convert2RefType(structure); structureInstance = new __1.Structure(structure); structureInstance.assign({ dataNode: this.dataNode, schemaRef: `#/${service.id}/${structure.id}`, }); const propertyList = structureInstance.propertyList; propertyList.forEach((property, index) => { property = propertyList[index] = new __1.StructureProperty(property); property.assign({ root: structureInstance }); }); structureInstance.assign({ resolvers: undefined }); __1.dataTypesMap[structureInstance.schemaRef] = structureInstance; } newStructures.push(structureInstance); }); service.data.assign({ structures: newStructures }); return newStructures; } } /** * 从后端 JSON 生成规范的 Entity 对象 */ static from(source, service) { __1.convert2RefType(source); const entity = new Entity(source); entity.assign({ dataNode: service.data, schemaRef: `#/${service.id}/${entity.id}`, }); const propertyList = entity.propertyList; propertyList.forEach((property, index) => { property = propertyList[index] = new __1.EntityProperty(property); property.assign({ root: entity }); }); entity.assign({ resolvers: undefined }); const indexList = entity.indexList; indexList.forEach((indexItem, index) => { indexItem = indexList[index] = new __1.EntityIndex(indexItem); indexItem.assign({ root: entity }); }); __1.dataTypesMap[entity.schemaRef] = entity; return entity; } } __decorate([ decorators_1.immutable() ], Entity.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "id", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "schemaRef", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "description", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "type", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "propertyList", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "indexList", void 0); __decorate([ decorators_1.excludedInJSON(), decorators_1.immutable() ], Entity.prototype, "resolvers", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "serviceId", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "serviceType", void 0); __decorate([ decorators_1.immutable() ], Entity.prototype, "service", void 0); __decorate([ decorators_1.immutable(), decorators_1.excludedInJSON() ], Entity.prototype, "lastVersionDef", void 0); __decorate([ decorators_1.circular(), decorators_1.immutable() ], Entity.prototype, "dataNode", void 0); __decorate([ decorators_1.excludedInJSON() ], Entity.prototype, "existingNames", void 0); __decorate([ decorators_1.action('添加实体') ], Entity.prototype, "create", null); __decorate([ decorators_1.action('删除实体') ], Entity.prototype, "delete", null); __decorate([ decorators_1.action('设置实体名称') ], Entity.prototype, "setName", null); __decorate([ decorators_1.action('设置实体描述') ], Entity.prototype, "setDescription", null); exports.Entity = Entity; exports.default = Entity; //# sourceMappingURL=Entity.js.map