UNPKG

mongodb-dynamic-api

Version:

Auto generated CRUD API for MongoDB using NestJS

30 lines 1.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildSchemaFromEntity = void 0; const mongoose_1 = require("@nestjs/mongoose"); const decorators_1 = require("../decorators"); const interfaces_1 = require("../interfaces"); function buildSchemaFromEntity(entity) { const schemaOptions = Reflect.getOwnMetadata(decorators_1.DYNAMIC_API_SCHEMA_OPTIONS_METADATA, entity); const schema = mongoose_1.SchemaFactory.createForClass(entity); schema.set('timestamps', Object.getOwnPropertyNames(schema.paths).includes('createdAt') && Object.getOwnPropertyNames(schema.paths).includes('updatedAt')); if (schemaOptions?.indexes) { schemaOptions.indexes.forEach(({ fields, options }) => { schema.index(fields, options); }); } if (schemaOptions?.hooks?.length) { const isSoftDeletable = Object.getOwnPropertyNames(schema.paths).includes('deletedAt'); schemaOptions.hooks.forEach(({ type, method, callback, options }) => { const { query, softDeletableQuery } = interfaces_1.queryByRouteTypeMap.get(type); schema[method](isSoftDeletable && softDeletableQuery ? softDeletableQuery : query, { document: true, query: true, ...options }, callback); }); } if (schemaOptions?.customInit) { schemaOptions.customInit(schema); } return schema; } exports.buildSchemaFromEntity = buildSchemaFromEntity; //# sourceMappingURL=schema.helper.js.map