mongodb-dynamic-api
Version:
Auto generated CRUD API for MongoDB using NestJS
36 lines • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseUpdateManyService = void 0;
const services_1 = require("../../services");
class BaseUpdateManyService extends services_1.BaseService {
constructor(model) {
super(model);
this.model = model;
}
async updateMany(ids, partial) {
try {
const toUpdateList = await this.model.find({ _id: { $in: ids } }).lean().exec();
if (toUpdateList?.length !== ids.length) {
this.handleDocumentNotFound();
}
await this.model
.updateMany({
_id: { $in: ids },
...(this.isSoftDeletable ? { isDeleted: false } : undefined),
}, partial)
.lean()
.exec();
const documents = await this.model.find({ _id: { $in: ids } }).lean().exec();
if (this.callback && documents.length) {
await Promise.all(documents.map((document) => this.callback(document, this.callbackMethods)));
}
return documents.map((d) => this.buildInstance(d));
}
catch (error) {
this.handleMongoErrors(error, false);
this.handleDuplicateKeyError(error);
}
}
}
exports.BaseUpdateManyService = BaseUpdateManyService;
//# sourceMappingURL=base-update-many.service.js.map