@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
413 lines • 19 kB
JavaScript
;
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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineServiceCrudController = 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 security_1 = require("@loopback/security");
const common_2 = require("./common");
// --------------------------------------------------------------------------------------------------------------
const defineServiceCrudController = (opts) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { entity: entityOptions, service: serviceOptions, controller: controllerOptions, schema: schemaOptions, options, } = opts;
const idPathParam = {
name: 'id',
in: 'path',
schema: (0, common_2.getIdSchema)(entityOptions),
};
class ReadController {
constructor(requestContext, service, getCurrentUser) {
var _a;
this.service = service;
this.getCurrentUser = getCurrentUser;
this.requestContext = requestContext;
this.defaultLimit = (_a = controllerOptions === null || controllerOptions === void 0 ? void 0 : controllerOptions.defaultLimit) !== null && _a !== void 0 ? _a : common_1.App.DEFAULT_QUERY_LIMIT;
}
// ----------------------------------------------------------------------------------------------------------
_getContextUser() {
return new Promise((resolve, reject) => {
if (!this.getCurrentUser) {
resolve(null);
return;
}
this.getCurrentUser().then(resolve).catch(reject);
});
}
// ----------------------------------------------------------------------------------------------------------
find(filter) {
return new Promise((resolve, reject) => {
this._getContextUser().then(currentUser => {
this.service
.find((0, common_2.applyLimit)(filter), {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
});
});
}
// ----------------------------------------------------------------------------------------------------------
findById(id, filter) {
return new Promise((resolve, reject) => {
this._getContextUser().then(currentUser => {
this.service
.findById(id, (0, common_2.applyLimit)(filter), {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
});
});
}
// ----------------------------------------------------------------------------------------------------------
findOne(filter) {
return new Promise((resolve, reject) => {
this._getContextUser().then(currentUser => {
this.service
.findOne((0, common_2.applyLimit)(filter), {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
});
});
}
// ----------------------------------------------------------------------------------------------------------
count(where) {
return new Promise((resolve, reject) => {
this._getContextUser().then(currentUser => {
this.service
.count(where !== null && where !== void 0 ? where : {}, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
});
});
}
}
__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) {
(0, core_1.inject)(rest_1.RestBindings.Http.CONTEXT)(ReadController, undefined, 0);
if (serviceOptions === null || serviceOptions === void 0 ? void 0 : serviceOptions.name) {
(0, core_1.inject)(`services.${serviceOptions.name}`)(ReadController, undefined, 1);
}
if (options === null || options === void 0 ? void 0 : options.doInjectCurrentUser) {
core_1.inject.getter(security_1.SecurityBindings.USER)(ReadController, undefined, 2);
}
return ReadController;
}
class CrudController extends ReadController {
constructor(requestContext, service, getCurrentUser) {
super(requestContext, service, getCurrentUser);
}
// ----------------------------------------------------------------------------------------------------------
create(data) {
return new Promise((resolve, reject) => {
this._getContextUser()
.then(currentUser => {
this.service
.create(data, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
})
.catch(reject);
});
}
// ----------------------------------------------------------------------------------------------------------
updateAll(data, where) {
return new Promise((resolve, reject) => {
this._getContextUser()
.then(currentUser => {
this.service
.updateAll(data, where !== null && where !== void 0 ? where : {}, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
})
.catch(reject);
});
}
// ----------------------------------------------------------------------------------------------------------
updateById(id, data) {
return new Promise((resolve, reject) => {
this._getContextUser()
.then(currentUser => {
this.service
.updateWithReturn(id, data, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
})
.catch(reject);
});
}
// ----------------------------------------------------------------------------------------------------------
replaceById(id, data) {
return new Promise((resolve, reject) => {
this._getContextUser()
.then(currentUser => {
this.service
.replaceById(id, data, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
})
.catch(reject);
});
}
// ----------------------------------------------------------------------------------------------------------
deleteById(id) {
return new Promise((resolve, reject) => {
this._getContextUser()
.then(currentUser => {
this.service
.deleteById(id, {
currentUser,
requestContext: this.requestContext,
})
.then(resolve)
.catch(reject);
})
.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);
(0, core_1.inject)(rest_1.RestBindings.Http.CONTEXT)(CrudController, undefined, 0);
if (serviceOptions === null || serviceOptions === void 0 ? void 0 : serviceOptions.name) {
(0, core_1.inject)(`services.${serviceOptions.name}`)(CrudController, undefined, 1);
}
if (options === null || options === void 0 ? void 0 : options.doInjectCurrentUser) {
core_1.inject.getter(security_1.SecurityBindings.USER)(CrudController, undefined, 2);
}
return CrudController;
};
exports.defineServiceCrudController = defineServiceCrudController;
//# sourceMappingURL=service-crud.controller.js.map