UNPKG

vulcain-corejs

Version:
212 lines (210 loc) 9.74 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const annotations_1 = require("../pipeline/annotations"); const abstractHandlers_1 = require("../pipeline/abstractHandlers"); const annotations_2 = require("../di/annotations"); const commandFactory_1 = require("../commands/command/commandFactory"); const abstractProviderCommand_1 = require("../commands/command/abstractProviderCommand"); let DefaultRepositoryCommand = class DefaultRepositoryCommand extends abstractProviderCommand_1.AbstractProviderCommand { initializeMetricsInfo() { // do nothing // since this command is generic, settings are made on every request } // Execute command runAsync(action, data) { this.setMetricsTags("Address=" + this.provider.address, "TargetSchema=" + this.schema.name, "TargetTenant=" + this.requestContext.tenant); return this[action + "Internal"](data); } create(entity) { return this.provider.createAsync(this.schema, entity); } createInternal(entity) { return __awaiter(this, void 0, void 0, function* () { if (entity && this.schema.description.hasSensibleData) entity = this.schema.encrypt(entity) || entity; entity = yield this.create(entity); if (entity && this.schema.description.hasSensibleData) entity = this.schema.decrypt(entity) || entity; return entity; }); } update(entity) { return __awaiter(this, void 0, void 0, function* () { let keyProperty = this.schema.getIdProperty(); let old = yield this.provider.getAsync(this.schema, entity[keyProperty]); if (!old) throw new Error("Entity doesn't exist for updating : " + entity[keyProperty]); return yield this.provider.updateAsync(this.schema, entity, old); }); } updateInternal(entity) { return __awaiter(this, void 0, void 0, function* () { // TODO move to provider if (entity && this.schema.description.hasSensibleData) entity = this.schema.encrypt(entity) || entity; entity = yield this.update(entity); if (entity && this.schema.description.hasSensibleData) entity = this.schema.decrypt(entity) || entity; return entity; }); } deleteInternal(entity) { return this.delete(entity); } delete(entity) { let keyProperty = this.schema.getIdProperty(); return this.provider.deleteAsync(this.schema, entity[keyProperty]); } get(id) { return __awaiter(this, void 0, void 0, function* () { let keyProperty = this.schema.getIdProperty(); let query = {}; query[keyProperty] = id; return yield this.provider.findOneAsync(this.schema, query); }); } getInternal(id) { return __awaiter(this, void 0, void 0, function* () { let entity = yield this.get(id); if (entity && this.schema.description.hasSensibleData) entity = this.schema.decrypt(entity) || entity; return entity; }); } all(options) { return this.provider.getAllAsync(this.schema, options); } allInternal(options) { return __awaiter(this, void 0, void 0, function* () { let list = yield this.all(options); if (list) { let result = []; for (let entity of list) { if (entity) { if (entity && this.schema.description.hasSensibleData) entity = this.schema.decrypt(entity) || entity; result.push(entity); } } return result; } return list; }); } }; DefaultRepositoryCommand = __decorate([ commandFactory_1.Command({ executionTimeoutInMilliseconds: 5000 }), __metadata("design:paramtypes", []) ], DefaultRepositoryCommand); exports.DefaultRepositoryCommand = DefaultRepositoryCommand; let DefaultActionHandler = class DefaultActionHandler extends abstractHandlers_1.AbstractActionHandler { constructor(container) { super(container); } createAsync(entity) { return __awaiter(this, void 0, void 0, function* () { if (!entity) throw new Error("Entity is required"); let cmd = yield this.requestContext.getCommandAsync("DefaultRepositoryCommand", this.metadata.schema); return cmd.executeAsync("create", entity); }); } updateAsync(entity) { return __awaiter(this, void 0, void 0, function* () { if (!entity) throw new Error("Entity is required"); let cmd = yield this.requestContext.getCommandAsync("DefaultRepositoryCommand", this.metadata.schema); return cmd.executeAsync("update", entity); }); } deleteAsync(entity) { return __awaiter(this, void 0, void 0, function* () { if (!entity) throw new Error("Entity is required"); let cmd = yield this.requestContext.getCommandAsync("DefaultRepositoryCommand", this.metadata.schema); return cmd.executeAsync("delete", entity); }); } }; __decorate([ annotations_1.Action({ action: "create", description: "Create a new entity", outputSchema: "" }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DefaultActionHandler.prototype, "createAsync", null); __decorate([ annotations_1.Action({ action: "update", description: "Update an entity", outputSchema: "" }) // Put outputSchema empty to take the default schema , __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DefaultActionHandler.prototype, "updateAsync", null); __decorate([ annotations_1.Action({ action: "delete", description: "Delete an entity", outputSchema: "boolean" }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DefaultActionHandler.prototype, "deleteAsync", null); DefaultActionHandler = __decorate([ __param(0, annotations_2.Inject("Container")), __metadata("design:paramtypes", [Object]) ], DefaultActionHandler); exports.DefaultActionHandler = DefaultActionHandler; let DefaultQueryHandler = class DefaultQueryHandler extends abstractHandlers_1.AbstractQueryHandler { constructor(container) { super(container); } getDefaultCommandAsync() { return this.requestContext.getCommandAsync("DefaultRepositoryCommand", this.metadata.schema); } getAsync(id) { return __awaiter(this, void 0, void 0, function* () { let cmd = yield this.getDefaultCommandAsync(); return cmd.executeAsync("get", id); }); } getAllAsync(query, maxByPage = 0, page) { return __awaiter(this, void 0, void 0, function* () { let options = { maxByPage: maxByPage || this.query && this.query.maxByPage || 0, page: page || this.query && this.query.page || 0, query: query || {} }; let cmd = yield this.getDefaultCommandAsync(); return cmd.executeAsync("all", options); }); } }; __decorate([ annotations_1.Query({ action: "get", description: "Get an entity by id" }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DefaultQueryHandler.prototype, "getAsync", null); __decorate([ annotations_1.Query({ action: "all", description: "Get all entities" }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Number, Number]), __metadata("design:returntype", Promise) ], DefaultQueryHandler.prototype, "getAllAsync", null); DefaultQueryHandler = __decorate([ __param(0, annotations_2.Inject("Container")), __metadata("design:paramtypes", [Object]) ], DefaultQueryHandler); exports.DefaultQueryHandler = DefaultQueryHandler; //# sourceMappingURL=crudHandlers.js.map