UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

568 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.EntityProperty = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const data_1 = require("../../service/data"); const basicTypes_1 = require("./basicTypes"); const BaseVariable_1 = require("../logic/BaseVariable"); const common_1 = require("../../service/common"); /** * 实体属性类 */ class EntityProperty extends __1.Vertex { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.property; /** * Id */ this.id = undefined; /** * 名称 */ this.name = undefined; /** * 标题 */ this.label = undefined; /** * 描述 */ this.description = undefined; /** * type */ this.type = undefined; /** * 数据格式 */ this.format = undefined; /** * 是否必须 */ this.required = undefined; /** * 数据类型 */ this.$ref = undefined; /** * 数据结构Id */ this.$refId = undefined; /** * 引用关系(多对一、一对多等) */ this.relationship = undefined; /** * 规则 */ this.rules = []; /** * UI 展示 */ this.display = { inDetail: true, inFilter: true, inForm: true, inTable: true, }; /** * 默认值 */ this.defaultValue = undefined; /** * 是否为主键 */ this.primaryKey = undefined; /** * 父级 Id */ this.entityId = undefined; /** * 父级引用 */ this.root = undefined; /** * 节点是否为叶子节点 * 前端 UI 状态 */ this.isLeaf = true; /** * 周边存在的名称 */ this.existingNames = []; /** * 数据类型Key,例子:"/#basicType/String",在前端使用 */ this.typeKey = undefined; /** * 外键引用实体 */ this.$relationEntity = undefined; /** * 外键引用实体字段 */ this.$relationProperty = undefined; /** * 外键删除规则 */ this.relationDelRule = undefined; /** * 上一版本规则 */ this.lastVersion = undefined; source && this.assign(source); } /** * 添加实体属性 */ async create(none, actionOptions) { __1.config.defaultApp?.emit('saving', true); const body = this.toJSON(); /// // convert2SchemaType(body); body._posIndex = this.root.propertyList.indexOf(this); /// __1.utils.logger.debug('添加实体属性', body); const result = await data_1.entityService.addProperty({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'EntityProperty.create', operationDesc: actionOptions?.actionDesc || `添加实体"${this.root.name}"属性"${this.name}"`, }, body, }); this.deepPick(result, ['id']); // 先改成异步的 this.root.syncInterfaces().then(() => { BaseVariable_1.updateAllVariablesChildrenSchema(); this.root.dataNode.service.emit('dataTypesChange'); this.root.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); }); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除实体属性 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving', true); try { if (this.id) { await data_1.entityService.deleteProperty({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'EntityProperty.delete', operationDesc: actionOptions?.actionDesc || `删除实体"${this.root.name}"属性"${this.name}"`, }, query: { id: this.id, }, }); } const index = this.root.propertyList.indexOf(this); ~index && this.root.propertyList.splice(index, 1); BaseVariable_1.updateAllVariablesChildrenSchema(); if (this.id) { this.root.syncInterfaces(); } this.destroy(); this.root.dataNode.service.emit('dataTypesChange'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } catch (err) { __1.config.defaultApp?.emit('saved'); throw err; } } /** * 修改实体属性 */ async update(none, actionOptions, then) { __1.config.defaultApp?.emit('saving', true); const body = this.toJSON(); delete body.display.id; __1.utils.logger.debug('修改实体属性', body); const result = await data_1.entityService.updateProperty({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'EntityPropperty.update', operationDesc: actionOptions?.actionDesc || `修改实体"${this.root.name}"属性"${this.name}"`, }, body, }); await then?.(); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 设置实体属性名称 * @param name 名称 */ async setName(name) { const oldName = this.name; this.assign({ name }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${oldName}"的名称为"${name}"`, }, async () => { await this.root.syncInterfaces(); }); this.root.dataNode.service.emit('dataTypesChange'); this.root.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); } /** * 设置实体属性标题 * @param label 标题 */ async setLabel(label) { this.assign({ label }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的标题为"${label}"`, }); } /** * 设置实体属性描述 * @param description 描述 */ async setDescription(description) { this.assign({ description }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的描述为"${description}"`, }); } /** * 查找schema 顶点被引用的逻辑顶点列表 */ async getSchemaUsage() { const result = await common_1.schemaService.getSchemaUsage({ query: { schemaId: this.id, valSource: 'entity_property', }, }); return result; } /** * 设置实体属性的数据类型 */ async setDataType(schema) { // 用于update失败后还原数据 const originalData = { $ref: this.$ref, type: this.type, format: this.format, defaultValue: this.defaultValue, relationship: this.relationship, rules: this.rules, $relationEntity: this.$relationEntity, $relationProperty: this.$relationProperty, }; this.assign({ $ref: undefined, type: undefined, format: undefined, }); this.assign(schema); this.assign({ defaultValue: basicTypes_1.getBasicTypeDefaultValue(), relationship: '', rules: [], $relationEntity: '', $relationProperty: '', }); try { await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的数据类型为"${schema.typeKey}"`, }); this.assign({ isDataTypeSetting: false }); await this.root.syncInterfaces(); BaseVariable_1.updateAllVariablesChildrenSchema(); this.root.dataNode.service.emit('dataTypesChange'); } catch (err) { this.assign(__1.convert2RefType(originalData)); __1.config.defaultApp?.emit('saved'); throw err; } } /** * 设置实体属性关系 */ async setRelationship(relationship) { this.assign({ relationship }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的关系为"${relationship}"`, }); this.root.dataNode.service.emit('dataTypesChange'); } /** * 设置实体属性外键 * reference: {$relationEntity, $relationProperty} */ async setReference(reference) { this.assign(reference); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的关联字段为"${reference.$relationProperty}"`, }); this.root.dataNode.service.emit('dataTypesChange'); } /** * 清除实体属性外键 */ async clearReference() { this.assign({ $relationEntity: undefined, $relationProperty: undefined, relationDelRule: undefined, }); await this.update(undefined, { actionDesc: `清除实体"${this.root.name}"的属性"${this.name}"的外键`, }); this.root.dataNode.service.emit('dataTypesChange'); } async setRelationDelRule(relationDelRule) { this.assign({ relationDelRule }); await this.update(undefined, { actionDesc: `设置实体属性外键删除规则`, }); } async setRules(rules) { this.assign({ rules }); await this.update(undefined, { actionDesc: `设置实体属性规则`, }); } /** * 设置实体属性的默认值 */ async setDefaultValue(defaultValue) { this.assign({ defaultValue }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}"的默认值为"${defaultValue}"`, }); this.root.dataNode.service.emit('dataTypesChange'); } /** * 设置实体属性是否必须 * @param required 必须 */ async setRequired(required) { this.assign({ required }); await this.update(undefined, { actionDesc: `设置实体"${this.root.name}"的属性"${this.name}""${required ? '必须' : '不必须'}"`, }); this.root.dataNode.service.emit('dataTypesChange'); } async setDisplay(display) { this.assign({ display }); await this.update(undefined, { actionDesc: '设置实体属性显示', }); } /** * 移动位置 * @param index 目标位置 */ async moveTo(index, oldIndex) { const propertyList = this.root.propertyList; if (oldIndex === undefined) oldIndex = propertyList.indexOf(this); propertyList.splice(oldIndex, 1); propertyList.splice(index, 0, this); await data_1.entityService.moveProperty({ headers: { appId: __1.config.defaultApp?.id, operationAction: null, operationDesc: null, }, query: { id: this.id, targetIndex: index, }, }); } /** * 上移实体属性 */ moveUp() { const propertyList = this.root.propertyList; const oldIndex = propertyList.indexOf(this); if (oldIndex === 0) return; return this.moveTo(oldIndex - 1, oldIndex); } /** * 属性下移 */ moveDown() { const propertyList = this.root.propertyList; const oldIndex = propertyList.indexOf(this); if (oldIndex === propertyList.length - 1) return; return this.moveTo(oldIndex + 1, oldIndex); } /** * 只更改Entity Property的名称 */ async updateLogicParamsForName(entity, property) { const itfacePrefix = ['getAll', 'count', 'export']; const interfaces = (entity.resolvers || []) .filter((resolver) => itfacePrefix.includes(resolver.name)) .map((resolver) => resolver.interface); const tasks = interfaces.map((itface) => { if (itface.logic) { return itface.logic.params.filter((param) => param.entityFieldId === this.id) .map((param) => { const name = param.name.split('.'); const newName = `${property.name}.${name[1]}`; return param.setName(newName); }); } else { return []; } }); await Promise.all(tasks); } } __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "id", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "label", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "description", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "type", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "format", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "required", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "$ref", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "$refId", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "relationship", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "rules", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "display", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "defaultValue", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "primaryKey", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "entityId", void 0); __decorate([ decorators_1.circular(), decorators_1.immutable() ], EntityProperty.prototype, "root", void 0); __decorate([ decorators_1.excludedInJSON() ], EntityProperty.prototype, "isLeaf", void 0); __decorate([ decorators_1.excludedInJSON() ], EntityProperty.prototype, "existingNames", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "typeKey", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "$relationEntity", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "$relationProperty", void 0); __decorate([ decorators_1.immutable() ], EntityProperty.prototype, "relationDelRule", void 0); __decorate([ decorators_1.immutable(), decorators_1.excludedInJSON() ], EntityProperty.prototype, "lastVersion", void 0); __decorate([ decorators_1.action('添加实体属性') ], EntityProperty.prototype, "create", null); __decorate([ decorators_1.action('删除实体属性') ], EntityProperty.prototype, "delete", null); __decorate([ decorators_1.action('设置实体属性名称') ], EntityProperty.prototype, "setName", null); __decorate([ decorators_1.action('设置实体属性标题') ], EntityProperty.prototype, "setLabel", null); __decorate([ decorators_1.action('设置实体属性描述') ], EntityProperty.prototype, "setDescription", null); __decorate([ decorators_1.action('设置实体属性的数据类型') ], EntityProperty.prototype, "setDataType", null); __decorate([ decorators_1.action('设置实体属性关系') ], EntityProperty.prototype, "setRelationship", null); __decorate([ decorators_1.action('设置实体属性外键') ], EntityProperty.prototype, "setReference", null); __decorate([ decorators_1.action('清除实体属性外键') ], EntityProperty.prototype, "clearReference", null); __decorate([ decorators_1.action('设置实体属性外键删除规则') ], EntityProperty.prototype, "setRelationDelRule", null); __decorate([ decorators_1.action('设置实体属性规则') ], EntityProperty.prototype, "setRules", null); __decorate([ decorators_1.action('设置实体属性的默认值') ], EntityProperty.prototype, "setDefaultValue", null); __decorate([ decorators_1.action('设置实体属性是否必须') ], EntityProperty.prototype, "setRequired", null); __decorate([ decorators_1.action('设置实体属性显示') ], EntityProperty.prototype, "setDisplay", null); __decorate([ decorators_1.action('上移实体属性') ], EntityProperty.prototype, "moveUp", null); __decorate([ decorators_1.action('下移实体属性') ], EntityProperty.prototype, "moveDown", null); exports.EntityProperty = EntityProperty; exports.default = EntityProperty; //# sourceMappingURL=EntityProperty.js.map