UNPKG

@wbget/midway-ats

Version:

midwayjs ats module

129 lines (128 loc) 4.71 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; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ATSService = void 0; const core_1 = require("@midwayjs/core"); const typeorm_1 = require("@midwayjs/typeorm"); const midway_uuid_int_1 = require("@wbget/midway-uuid-int"); const lodash_1 = require("lodash"); const typeorm_2 = require("typeorm"); const ats_entity_1 = require("../entity/ats.entity"); let ATSService = class ATSService { async init() { this.manager = this.dataSource.manager; } async addAtom() { const atom = this.manager.create(ats_entity_1.AtomEntity); atom.id = this.uuid.uuid() + ''; await this.manager.save(atom); return atom.id; } async delAtom(id) { await this.manager.delete(ats_entity_1.AtomEntity, { id }); } async createTrait(trait, id) { const entity = this.manager.create(trait); entity.id = id; return entity; } async saveTrait(trait) { await this.manager.save(trait); } async delTrait(trait, id) { await this.manager.delete(trait, id); } async getTrait(trait, options) { if ((0, lodash_1.isString)(options)) { return this.manager.findOne(trait, { where: { id: options }, }); } return this.manager.findOne(trait, options); } async getTraits(trait, options) { if (options === undefined) { return this.manager.find(trait); } if ((0, lodash_1.isArray)(options)) { return this.manager.find(trait, { where: { id: (0, typeorm_2.In)(options), }, }); } return this.manager.find(trait, options); } getRepository(trait) { return this.manager.getRepository(trait); } getManager() { return this.manager; } /** * * @param traits 按顺序过滤,传参数时请按照从最细到最宽的顺序传递 * @returns */ async getAtoms(traits, options = null) { if (traits.length === 0) { return []; } // TODO 加速查找 const atoms = await this.manager.find(traits[0], { where: options ? options[0] : null, select: ['id'], }); let ids = atoms.map(atom => atom.id); if (traits.length === 1) { return ids; } const others = traits.slice(1); let i = 0; for await (const entity of others) { if (options) { const option = options[i + 1]; const tids = await this.manager.find(entity, { select: ['id'], where: option ? [{ id: (0, typeorm_2.In)(ids) }, option] : { id: (0, typeorm_2.In)(ids) }, }); ids = tids.map(id => id.id); } else { const tids = await this.manager.find(entity, { select: ['id'], where: { id: (0, typeorm_2.In)(ids) }, }); ids = tids.map(id => id.id); } } return ids; } }; __decorate([ (0, typeorm_1.InjectDataSource)(), __metadata("design:type", typeorm_2.DataSource) ], ATSService.prototype, "dataSource", void 0); __decorate([ (0, core_1.Inject)(), __metadata("design:type", midway_uuid_int_1.UUIDIntService) ], ATSService.prototype, "uuid", void 0); __decorate([ (0, core_1.Init)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ATSService.prototype, "init", null); ATSService = __decorate([ (0, core_1.Provide)(), (0, core_1.Scope)(core_1.ScopeEnum.Singleton) ], ATSService); exports.ATSService = ATSService;