UNPKG

@minimaltech/node-infra

Version:

Minimal Technology NodeJS Infrastructure - Loopback 4 Framework

365 lines 17.4 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) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.defineCrudController = void 0; const core_1 = require("@loopback/core"); const repository_1 = require("@loopback/repository"); const rest_1 = require("@loopback/rest"); const common_1 = require("../../common"); const common_2 = require("./common"); const security_1 = require("@loopback/security"); // -------------------------------------------------------------------------------------------------------------- const defineCrudController = (opts) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const { entity: entityOptions, repository: repositoryOptions, controller: controllerOptions, schema: schemaOptions, doInjectCurrentUser, } = opts; const idPathParam = { name: 'id', in: 'path', schema: (0, common_2.getIdSchema)(entityOptions), }; class ReadController { constructor(repository) { var _a; this.repository = repository; this.defaultLimit = (_a = controllerOptions === null || controllerOptions === void 0 ? void 0 : controllerOptions.defaultLimit) !== null && _a !== void 0 ? _a : common_1.App.DEFAULT_QUERY_LIMIT; } // ---------------------------------------------------------------------------------------------------------- find(filter) { return this.repository.find((0, common_2.applyLimit)(filter)); } // ---------------------------------------------------------------------------------------------------------- findById(id, filter) { return this.repository.findById(id, (0, common_2.applyLimit)(filter)); } // ---------------------------------------------------------------------------------------------------------- findOne(filter) { return this.repository.findOne(filter); } // ---------------------------------------------------------------------------------------------------------- count(where) { return this.repository.count(where); } } __decorate([ (0, rest_1.get)('/', { responses: { '200': { description: `Array of ${entityOptions.name} model instances`, content: { 'application/json': { schema: { type: 'array', items: (_a = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.find) !== null && _a !== void 0 ? _a : (0, rest_1.getModelSchemaRef)(entityOptions, { includeRelations: true }), }, }, }, }, }, }), __param(0, rest_1.param.filter(entityOptions)), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], ReadController.prototype, "find", null); __decorate([ (0, rest_1.get)('/{id}', { responses: { '200': { description: `Find ${entityOptions.name} model instance`, content: { 'application/json': { schema: (_b = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.findById) !== null && _b !== void 0 ? _b : (0, rest_1.getModelSchemaRef)(entityOptions, { includeRelations: true }), }, }, }, }, }), __param(0, (0, rest_1.param)(idPathParam)), __param(1, rest_1.param.query.object('filter', (0, rest_1.getFilterSchemaFor)(entityOptions, { exclude: 'where' }))), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], ReadController.prototype, "findById", null); __decorate([ (0, rest_1.get)('/find-one', { responses: { '200': { description: `Find one ${entityOptions.name} model instance`, content: { 'application/json': { schema: (_c = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.findOne) !== null && _c !== void 0 ? _c : (0, rest_1.getModelSchemaRef)(entityOptions, { includeRelations: true }), }, }, }, }, }), __param(0, rest_1.param.query.object('filter', (0, rest_1.getFilterSchemaFor)(entityOptions))), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], ReadController.prototype, "findOne", null); __decorate([ (0, rest_1.get)('/count', { responses: { '200': { description: `Count number of ${entityOptions.name} model instance`, content: { 'application/json': { schema: repository_1.CountSchema, }, }, }, }, }), __param(0, rest_1.param.where(entityOptions)), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], ReadController.prototype, "count", null); if (controllerOptions.readonly) { if (repositoryOptions === null || repositoryOptions === void 0 ? void 0 : repositoryOptions.name) { (0, core_1.inject)(`repositories.${repositoryOptions.name}`)(ReadController, undefined, 0); } return ReadController; } class CrudController extends ReadController { constructor(repository, getCurrentUser) { super(repository); this.getCurrentUser = getCurrentUser; } // ---------------------------------------------------------------------------------------------------------- _getContextUser() { return new Promise((resolve, reject) => { if (!this.getCurrentUser) { resolve(null); return; } this.getCurrentUser().then(resolve).catch(reject); }); } // ---------------------------------------------------------------------------------------------------------- create(data) { return new Promise((resolve, reject) => { this._getContextUser() .then(currentUser => { this.repository .create(data, { authorId: currentUser === null || currentUser === void 0 ? void 0 : currentUser.userId, }) .then(resolve) .catch(reject); }) .catch(reject); }); } // ---------------------------------------------------------------------------------------------------------- updateAll(data, where) { return new Promise((resolve, reject) => { this._getContextUser().then(currentUser => { this.repository .updateAll(data, where, { authorId: currentUser === null || currentUser === void 0 ? void 0 : currentUser.userId, }) .then(resolve) .catch(reject); }); }); } // ---------------------------------------------------------------------------------------------------------- updateById(id, data) { return new Promise((resolve, reject) => { this._getContextUser().then(currentUser => { this.repository .updateWithReturn(id, data, { authorId: currentUser === null || currentUser === void 0 ? void 0 : currentUser.userId, }) .then(resolve) .catch(reject); }); }); } // ---------------------------------------------------------------------------------------------------------- replaceById(id, data) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { this._getContextUser().then(currentUser => { this.repository .replaceById(id, data, { authorId: currentUser === null || currentUser === void 0 ? void 0 : currentUser.userId, }) .then(() => { resolve(Object.assign(Object.assign({}, data), { id })); }) .catch(reject); }); }); }); } // ---------------------------------------------------------------------------------------------------------- deleteById(id) { return new Promise((resolve, reject) => { this.repository .deleteById(id) .then(() => { resolve({ id }); }) .catch(reject); }); } } __decorate([ (0, rest_1.post)('/', { responses: { '200': { description: `Create ${entityOptions.name} model instance`, content: { 'application/json': { schema: (_d = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.create) !== null && _d !== void 0 ? _d : (0, rest_1.getModelSchemaRef)(entityOptions), }, }, }, }, }), __param(0, (0, rest_1.requestBody)({ content: { 'application/json': { schema: (_e = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.createRequestBody) !== null && _e !== void 0 ? _e : (0, rest_1.getModelSchemaRef)(entityOptions, { title: `New ${entityOptions.name} payload`, exclude: ['id', 'createdAt', 'modifiedAt'], }), }, }, })), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], CrudController.prototype, "create", null); __decorate([ (0, rest_1.patch)('/', { responses: { '200': { description: `Number of updated ${entityOptions.name} models`, content: { 'application/json': { schema: repository_1.CountSchema, }, }, }, }, }), __param(0, (0, rest_1.requestBody)({ content: { 'application/json': { schema: (_f = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.updateAll) !== null && _f !== void 0 ? _f : (0, rest_1.getModelSchemaRef)(entityOptions, { title: `Partial fields of ${entityOptions.name}`, partial: true, }), }, }, })), __param(1, rest_1.param.where(entityOptions)), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CrudController.prototype, "updateAll", null); __decorate([ (0, rest_1.patch)('/{id}', { responses: { '200': { description: `Updated ${entityOptions.name} models`, content: { 'application/json': { schema: (_g = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.updateById) !== null && _g !== void 0 ? _g : (0, rest_1.getModelSchemaRef)(entityOptions, { title: `Updated ${entityOptions.name} models`, }), }, }, }, }, }), __param(0, (0, rest_1.param)(idPathParam)), __param(1, (0, rest_1.requestBody)({ content: { 'application/json': { schema: (_h = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.updateByIdRequestBody) !== null && _h !== void 0 ? _h : (0, rest_1.getModelSchemaRef)(entityOptions, { title: `Partial fields of ${entityOptions.name}`, partial: true, }), }, }, })), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CrudController.prototype, "updateById", null); __decorate([ (0, rest_1.put)('/{id}', { responses: { '204': { description: `${entityOptions.name} was replaced` }, }, }), __param(0, (0, rest_1.param)(idPathParam)), __param(1, (0, rest_1.requestBody)({ content: { 'application/json': { schema: (_j = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.replaceById) !== null && _j !== void 0 ? _j : (0, rest_1.getModelSchemaRef)(entityOptions, { title: `Fields of ${entityOptions.name}`, }), }, }, })), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], CrudController.prototype, "replaceById", null); __decorate([ (0, rest_1.del)('/{id}', { responses: { '200': { description: `${entityOptions.name} was deleted`, content: { 'application/json': { schema: (_k = schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.deleteById) !== null && _k !== void 0 ? _k : (0, rest_1.getModelSchemaRef)(entityOptions, { partial: true, title: `Deleted ${entityOptions.name} models`, }), }, }, }, }, }), __param(0, (0, rest_1.param)(idPathParam)), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], CrudController.prototype, "deleteById", null); if (repositoryOptions === null || repositoryOptions === void 0 ? void 0 : repositoryOptions.name) { (0, core_1.inject)(`repositories.${repositoryOptions.name}`)(CrudController, undefined, 0); } if (doInjectCurrentUser) { core_1.inject.getter(security_1.SecurityBindings.USER, { optional: true })(CrudController, undefined, 1); } return CrudController; }; exports.defineCrudController = defineCrudController; //# sourceMappingURL=crud.controller.js.map