mongodb-dynamic-api
Version:
Auto generated CRUD API for MongoDB using NestJS
46 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDuplicateOneService = void 0;
const class_transformer_1 = require("class-transformer");
const mixins_1 = require("../../mixins");
const services_1 = require("../../services");
class BaseDuplicateOneService extends services_1.BaseService {
constructor(model) {
super(model);
this.model = model;
}
async duplicateOne(id, partial) {
try {
const toDuplicate = await this.model
.findOne({
_id: id,
...(this.isSoftDeletable ? { isDeleted: false } : undefined),
})
.lean()
.exec();
if (!toDuplicate) {
this.handleDocumentNotFound();
}
const { _id } = await this.model.create((0, class_transformer_1.plainToInstance)(this.entity, {
...Object.entries(toDuplicate).reduce((acc, [key, value]) => {
if ((0, mixins_1.baseEntityKeysToExclude)().includes(key)) {
return acc;
}
return { ...acc, [key]: value };
}, {}),
...partial,
}));
const document = await this.model.findOne({ _id }).lean().exec();
if (this.callback) {
await this.callback(this.addDocumentId(document), this.callbackMethods);
}
return this.buildInstance(document);
}
catch (error) {
this.handleMongoErrors(error, false);
this.handleDuplicateKeyError(error);
}
}
}
exports.BaseDuplicateOneService = BaseDuplicateOneService;
//# sourceMappingURL=base-duplicate-one.service.js.map