mongodb-dynamic-api
Version:
Auto generated CRUD API for MongoDB using NestJS
38 lines • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseReplaceOneService = void 0;
const class_transformer_1 = require("class-transformer");
const services_1 = require("../../services");
class BaseReplaceOneService extends services_1.BaseService {
constructor(model) {
super(model);
this.model = model;
}
async replaceOne(id, partial) {
try {
const document = await this.model
.findOneAndReplace({
_id: id,
...(this.isSoftDeletable ? { isDeleted: false } : undefined),
}, (0, class_transformer_1.plainToInstance)(this.entity, partial), {
new: true,
setDefaultsOnInsert: true,
})
.lean()
.exec();
if (!document) {
this.handleDocumentNotFound();
}
if (this.callback) {
await this.callback(document, this.callbackMethods);
}
return this.buildInstance(document);
}
catch (error) {
this.handleMongoErrors(error, false);
this.handleDuplicateKeyError(error);
}
}
}
exports.BaseReplaceOneService = BaseReplaceOneService;
//# sourceMappingURL=base-replace-one.service.js.map