UNPKG

mongodb-dynamic-api

Version:

Auto generated CRUD API for MongoDB using NestJS

30 lines 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseCreateManyService = void 0; const class_transformer_1 = require("class-transformer"); const services_1 = require("../../services"); class BaseCreateManyService extends services_1.BaseService { constructor(model) { super(model); this.model = model; } async createMany(partials) { try { const created = await this.model.create(partials.map((p) => (0, class_transformer_1.plainToInstance)(this.entity, p))); const documents = await this.model .find({ _id: { $in: created.map(({ _id }) => _id.toString()) } }) .lean() .exec(); if (this.callback && documents.length) { await Promise.all(documents.map((document) => this.callback(this.addDocumentId(document), this.callbackMethods))); } return documents.map((d) => this.buildInstance(d)); } catch (error) { this.handleMongoErrors(error, false); this.handleDuplicateKeyError(error); } } } exports.BaseCreateManyService = BaseCreateManyService; //# sourceMappingURL=base-create-many.service.js.map