UNPKG

mongo-ts-struct

Version:

Mongoose wrapper for Typescript supports

59 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const helpers_1 = require("../../../helpers"); const infer_type_1 = require("./../../infer-type"); const to_schema_1 = require("../../to-schema"); const mongoose_1 = require("mongoose"); const create_property_decorator_1 = require("./create-property-decorator"); // reflection inferred type function Prop(definition = {}) { return create_property_decorator_1.createPropertyDecorator('Prop', (targetPrototype, propertyName) => { return { type: supportedCtors(infer_type_1.InferType(targetPrototype, propertyName)), definition }; }); // return (targetPrototype: Object, propertyName: string): void => { // const name = targetPrototype.constructor.name // if(!MetadataAgent.has(targetPrototype, `isProcessed:${name}`)) { // const inferredTypeCtor = supportedCtors(InferType(targetPrototype, propertyName)); // MetadataAgent.assign(targetPrototype, [ // `schemaDefinitions.${propertyName}`, // { ...definition, type: inferredTypeCtor } // ]); // } // } } exports.Prop = Prop; /* function isTypedSchemaClass(typeCtor: Function): boolean { const name = typeCtor.name; return MetadataAgent.has(typeCtor, `isProcessed:${name}`); } */ // map the provided ctors function to the native mongoose ctors function supportedCtors(typeCtor) { const name = typeCtor.name; switch (name) { case 'String': return mongoose_1.Schema.Types.String; case 'Number': return mongoose_1.Schema.Types.Number; case 'Boolean': return mongoose_1.Schema.Types.Boolean; case 'Date': return mongoose_1.Schema.Types.Date; case 'Object': return mongoose_1.Schema.Types.Mixed; case 'Array': return mongoose_1.Schema.Types.Array; default: if (helpers_1.isTypedSchemaClass(typeCtor)) { return to_schema_1.toSchema(typeCtor); } else { throw new Error('Inferred field type not supported, you might want to use @Property instead.'); } } } //# sourceMappingURL=prop.js.map