ncrudify
Version:
Configurable CRUD module for NestJS and Mongoose.
224 lines • 10.8 kB
JavaScript
"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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrudifyController = void 0;
const common_1 = require("@nestjs/common");
const crudify_service_1 = require("./crudify.service");
const config_service_1 = require("../config/config.service");
let CrudifyController = class CrudifyController {
constructor(crudService, configService) {
this.crudService = crudService;
this.configService = configService;
this.softDelete = Reflect.getMetadata("softDelete", this);
this.modelName = Reflect.getMetadata("modelName", this);
}
create(createDto) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "create");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "create");
return this.crudService.create(createDto);
}
createBulk(data) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "createBulk");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "createBulk");
return this.crudService.createBulk(data);
}
findAll(query) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "findAll");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "findAll");
return this.crudService.findAll(query);
}
count(query) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "count");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "count");
return this.crudService.count(query);
}
findOne(id) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "findOne");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "findOne");
return this.crudService.findOne({ _id: id });
}
put(id, updateDto) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "put");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "put");
return this.crudService.put(id, updateDto);
}
async updateBulk(body) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "updateBulk");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "updateBulk");
const { filter, updateDto } = body;
return this.crudService.updateBulk(filter, updateDto);
}
async restoreBulk(body) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "restoreBulk");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "restoreBulk");
const { filter } = body;
return this.crudService.restoreBulk(filter);
}
update(id, updateDto) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "update");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "update");
return this.crudService.update(id, updateDto);
}
async deleteBulk(body) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "deleteBulk");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "deleteBulk");
const { filter } = body;
if (this.softDelete)
return this.crudService.softDeleteBulk(filter);
return this.crudService.deleteBulk(filter);
}
delete(id) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "delete");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "delete");
if (this.softDelete)
return this.crudService.softDelete(id);
return this.crudService.delete(id);
}
deleteSoft(id) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "deleteSoft");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "deleteSoft");
return this.crudService.softDelete(id);
}
restore(id) {
var _a, _b;
const isEnabled = (_a = this.configService) === null || _a === void 0 ? void 0 : _a.isRouteEnabled(this.modelName, "restore");
if (!isEnabled)
return (_b = this.configService) === null || _b === void 0 ? void 0 : _b.notEnabledResponse(this.modelName, "restore");
return this.crudService.restore(id);
}
};
exports.CrudifyController = CrudifyController;
__decorate([
(0, common_1.Post)(),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "create", null);
__decorate([
(0, common_1.Post)("bulk"),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "createBulk", null);
__decorate([
(0, common_1.Get)(),
__param(0, (0, common_1.Query)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "findAll", null);
__decorate([
(0, common_1.Get)("count"),
__param(0, (0, common_1.Query)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "count", null);
__decorate([
(0, common_1.Get)(":id"),
__param(0, (0, common_1.Param)("id")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "findOne", null);
__decorate([
(0, common_1.Put)(":id"),
__param(0, (0, common_1.Param)("id")),
__param(1, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "put", null);
__decorate([
(0, common_1.Patch)("bulk"),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CrudifyController.prototype, "updateBulk", null);
__decorate([
(0, common_1.Patch)("bulk/restore"),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CrudifyController.prototype, "restoreBulk", null);
__decorate([
(0, common_1.Patch)(":id"),
__param(0, (0, common_1.Param)("id")),
__param(1, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "update", null);
__decorate([
(0, common_1.Delete)("bulk"),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CrudifyController.prototype, "deleteBulk", null);
__decorate([
(0, common_1.Delete)(":id"),
__param(0, (0, common_1.Param)("id")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "delete", null);
__decorate([
(0, common_1.Delete)(":id/soft"),
__param(0, (0, common_1.Param)("id")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "deleteSoft", null);
__decorate([
(0, common_1.Delete)(":id/restore"),
__param(0, (0, common_1.Param)("id")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0)
], CrudifyController.prototype, "restore", null);
exports.CrudifyController = CrudifyController = __decorate([
(0, common_1.Controller)(":entity"),
__metadata("design:paramtypes", [crudify_service_1.CrudifyService,
config_service_1.ConfigService])
], CrudifyController);
//# sourceMappingURL=crudify.controller.js.map