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.BaseCreateOneService = void 0; const class_transformer_1 = require("class-transformer"); const lodash_1 = require("lodash"); const services_1 = require("../../services"); class BaseCreateOneService extends services_1.BaseService { constructor(model) { super(model); this.model = model; } async createOne(partial) { try { const toCreate = this.beforeSaveCallback ? await this.beforeSaveCallback(undefined, { toCreate: (0, lodash_1.cloneDeep)(partial) }, this.callbackMethods) : partial; const { _id } = await this.model.create((0, class_transformer_1.plainToInstance)(this.entity, toCreate)); 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.handleDuplicateKeyError(error); } } } exports.BaseCreateOneService = BaseCreateOneService; //# sourceMappingURL=base-create-one.service.js.map