mongodb-dynamic-api
Version:
Auto generated CRUD API for MongoDB using NestJS
42 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseUpdateOneService = void 0;
const lodash_1 = require("lodash");
const services_1 = require("../../services");
class BaseUpdateOneService extends services_1.BaseService {
constructor(model) {
super(model);
this.model = model;
}
async updateOne(id, partial) {
try {
const document = await this.model
.findOne({
_id: id,
...(this.isSoftDeletable ? { isDeleted: false } : undefined),
})
.lean()
.exec();
if (!document) {
this.handleDocumentNotFound();
}
const update = this.beforeSaveCallback
? await this.beforeSaveCallback(document, { id, update: (0, lodash_1.cloneDeep)(partial) }, this.callbackMethods)
: partial;
const updatedDocument = await this.model
.findOneAndUpdate({ _id: id }, { $set: update }, { new: true })
.lean()
.exec();
if (this.callback) {
await this.callback(updatedDocument, this.callbackMethods);
}
return this.buildInstance(updatedDocument);
}
catch (error) {
this.handleMongoErrors(error, false);
this.handleDuplicateKeyError(error);
}
}
}
exports.BaseUpdateOneService = BaseUpdateOneService;
//# sourceMappingURL=base-update-one.service.js.map