@tsed/mongoose 6.114.17 → 6.115.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,13 +10,15 @@ const MongooseService_1 = require("./services/MongooseService");
10
10
  * @ignore
11
11
  */
12
12
  let MongooseModule = class MongooseModule {
13
+ mongooseService;
14
+ settings;
13
15
  constructor(mongooseService, settings) {
14
16
  this.mongooseService = mongooseService;
15
17
  this.settings = settings;
16
18
  // auto configure the cache manager when mongoose is used with @tsed/mongoose
17
19
  const cache = this.settings.get("cache");
18
20
  // istanbul ignore next
19
- if (cache === null || cache === void 0 ? void 0 : cache.mongoose) {
21
+ if (cache?.mongoose) {
20
22
  cache.connection = this.mongooseService.get();
21
23
  }
22
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModule.js","sourceRoot":"","sources":["../../src/MongooseModule.ts"],"names":[],"mappings":";;;;AAAA,iCAA+C;AAC/C,iCAA2C;AAC3C,wEAAoE;AACpE,gEAA2D;AAE3D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IACzB,YAAoB,eAAgC,EAA2B,QAAuB;QAAlF,oBAAe,GAAf,eAAe,CAAiB;QAA2B,aAAQ,GAAR,QAAQ,CAAe;QACpG,6EAA6E;QAC7E,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAM,OAAO,CAAC,CAAC;QAE9C,uBAAuB;QACvB,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,QAAQ,EAAE;YACnB,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,CAAC;SAC/C;IACH,CAAC;IAED,OAAO,KAAyB,CAAC;IAEjC,UAAU;QACR,OAAO,IAAI,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC;IACjD,CAAC;CACF,CAAA;AAhBY,cAAc;IAH1B,IAAA,WAAM,EAAC;QACN,OAAO,EAAE,CAAC,0CAAoB,CAAC;KAChC,CAAC;IAEuD,mBAAA,IAAA,kBAAa,GAAE,CAAA;6CAAjC,iCAAe;GADzC,cAAc,CAgB1B;AAhBY,wCAAc","sourcesContent":["import {Configuration, OnInit} from \"@tsed/di\";\nimport {Module, OnDestroy} from \"@tsed/di\";\nimport {MONGOOSE_CONNECTIONS} from \"./services/MongooseConnections\";\nimport {MongooseService} from \"./services/MongooseService\";\n\n/**\n * @ignore\n */\n@Module({\n imports: [MONGOOSE_CONNECTIONS]\n})\nexport class MongooseModule implements OnDestroy, OnInit {\n constructor(private mongooseService: MongooseService, @Configuration() private settings: Configuration) {\n // auto configure the cache manager when mongoose is used with @tsed/mongoose\n const cache = this.settings.get<any>(\"cache\");\n\n // istanbul ignore next\n if (cache?.mongoose) {\n cache.connection = this.mongooseService.get();\n }\n }\n\n $onInit(): void | Promise<any> {}\n\n $onDestroy(): Promise<any> | void {\n return this.mongooseService.closeConnections();\n }\n}\n"]}
1
+ {"version":3,"file":"MongooseModule.js","sourceRoot":"","sources":["../../src/MongooseModule.ts"],"names":[],"mappings":";;;;AAAA,iCAA+C;AAC/C,iCAA2C;AAC3C,wEAAoE;AACpE,gEAA2D;AAE3D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IACL;IAA2D;IAA/E,YAAoB,eAAgC,EAA2B,QAAuB;QAAlF,oBAAe,GAAf,eAAe,CAAiB;QAA2B,aAAQ,GAAR,QAAQ,CAAe;QACpG,6EAA6E;QAC7E,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAM,OAAO,CAAC,CAAC;QAE9C,uBAAuB;QACvB,IAAI,KAAK,EAAE,QAAQ,EAAE;YACnB,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,CAAC;SAC/C;IACH,CAAC;IAED,OAAO,KAAyB,CAAC;IAEjC,UAAU;QACR,OAAO,IAAI,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC;IACjD,CAAC;CACF,CAAA;AAhBY,cAAc;IAH1B,IAAA,WAAM,EAAC;QACN,OAAO,EAAE,CAAC,0CAAoB,CAAC;KAChC,CAAC;IAEuD,mBAAA,IAAA,kBAAa,GAAE,CAAA;6CAAjC,iCAAe;GADzC,cAAc,CAgB1B;AAhBY,wCAAc","sourcesContent":["import {Configuration, OnInit} from \"@tsed/di\";\nimport {Module, OnDestroy} from \"@tsed/di\";\nimport {MONGOOSE_CONNECTIONS} from \"./services/MongooseConnections\";\nimport {MongooseService} from \"./services/MongooseService\";\n\n/**\n * @ignore\n */\n@Module({\n imports: [MONGOOSE_CONNECTIONS]\n})\nexport class MongooseModule implements OnDestroy, OnInit {\n constructor(private mongooseService: MongooseService, @Configuration() private settings: Configuration) {\n // auto configure the cache manager when mongoose is used with @tsed/mongoose\n const cache = this.settings.get<any>(\"cache\");\n\n // istanbul ignore next\n if (cache?.mongoose) {\n cache.connection = this.mongooseService.get();\n }\n }\n\n $onInit(): void | Promise<any> {}\n\n $onDestroy(): Promise<any> | void {\n return this.mongooseService.closeConnections();\n }\n}\n"]}
@@ -11,7 +11,7 @@ function isRef(value) {
11
11
  return (value && value._bsontype) || (0, core_1.isString)(value);
12
12
  }
13
13
  function getType(refPath, ctx) {
14
- return ((ctx === null || ctx === void 0 ? void 0 : ctx.self[refPath]) && MongooseModels_1.MongooseModels.get(ctx.self[refPath])) || Object;
14
+ return (ctx?.self[refPath] && MongooseModels_1.MongooseModels.get(ctx.self[refPath])) || Object;
15
15
  }
16
16
  /**
17
17
  * Define a property as mongoose reference to other Model (decorated with @Model).
@@ -1 +1 @@
1
- {"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":";;;AAAA,qCAAyF;AACzF,yCAA4F;AAC5F,uCAAkD;AAClD,sDAAuD;AACvD,mDAAqF;AACrF,iEAA4D;AAE5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,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AAC,EAAC,CAAC,CAAC;IACjE,CAAC,CAAC,EACF,IAAA,cAAK,EAAC,IAAA,eAAM,GAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC;AAzBD,gCAyBC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
1
+ {"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":";;;AAAA,qCAAyF;AACzF,yCAA4F;AAC5F,uCAAkD;AAClD,sDAAuD;AACvD,mDAAqF;AACrF,iEAA4D;AAE5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,IAAA,eAAQ,EAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,OAAO,CAAC,OAAe,EAAE,GAAoB;IACpD,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,+BAAc,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAW,CAAC,CAAC,IAAI,MAAM,CAAC;AAC3F,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,SAAgB,UAAU,CAAC,OAAe,EAAE,GAAG,KAAkB;IAC/D,OAAO,IAAA,oBAAa,EAClB,IAAA,iBAAQ,EAAC,MAAM,CAAC,EAChB,IAAA,gBAAO,EAAC,0BAA0B,CAAC,EACnC,IAAA,oBAAW,EAAC,uBAAuB,CAAC,EACpC,IAAA,iBAAU,EAAC,2BAAe,EAAE;QAC1B,IAAI,EAAE,iBAAc,CAAC,KAAK,CAAC,QAAQ;QACnC,OAAO;KACR,CAAC,EACF,IAAA,2BAAa,EAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAC3B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,IAAA,yBAAW,EAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAC,CAAC,CAAC;IACnE,CAAC,CAAC,EACF,IAAA,yBAAW,EAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,IAAA,uBAAS,EAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAC,CAAC,CAAC;IACjE,CAAC,CAAC,EACF,IAAA,cAAK,EAAC,IAAA,eAAM,GAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC;AAzBD,gCAyBC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
@@ -10,10 +10,9 @@ function asPromise(c) {
10
10
  return c && c.asPromise ? c.asPromise() : c;
11
11
  }
12
12
  let MongooseService = class MongooseService {
13
- constructor() {
14
- this.connections = new Map();
15
- this.defaultConnection = "default";
16
- }
13
+ connections = new Map();
14
+ defaultConnection = "default";
15
+ logger;
17
16
  /**
18
17
  *
19
18
  * @returns {Promise<"mongoose".Connection>}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";;;;AAAA,iCAA4C;AAC5C,yCAAoC;AACpC,gEAAgC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,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H,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,IAAA,WAAM,GAAE;sCACD,eAAM;+CAAC;AALJ,eAAe;IAD3B,IAAA,eAAU,GAAE;GACA,eAAe,CA4D3B;AA5DY,0CAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";;;;AAAA,iCAA4C;AAC5C,yCAAoC;AACpC,gEAAgC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,MAAa,eAAe;IACjB,WAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;IAC3D,iBAAiB,GAAW,SAAS,CAAC;IAG9C,MAAM,CAAS;IAEf;;;OAGG;IACH,KAAK,CAAC,OAAO,CAAC,EAAU,EAAE,GAAW,EAAE,iBAAiC,EAAE,SAAS,GAAG,KAAK;QACzF,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SAC5B;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QAEnE,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,kBAAQ,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;YACtF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;YAErC,IAAI,EAAE,KAAK,SAAS,IAAI,SAAS,EAAE;gBACjC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;aAC7B;YAED,OAAO,UAAU,CAAC;SACnB;QAAC,OAAO,EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,IAAA,WAAM,GAAE;sCACD,eAAM;+CAAC;AALJ,eAAe;IAD3B,IAAA,eAAU,GAAE;GACA,eAAe,CA4D3B;AA5DY,0CAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
@@ -35,12 +35,11 @@ function hasVersionField(schema, versionKey) {
35
35
  return versionKey in schema.paths;
36
36
  }
37
37
  function createSchema(target, options = {}) {
38
- var _a;
39
38
  const schemaOptionsFromStore = core_1.Store.from(target).get(constants_1.MONGOOSE_SCHEMA_OPTIONS) || {};
40
39
  options.schemaOptions = { ...options.schemaOptions, ...schemaOptionsFromStore };
41
40
  const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
42
41
  (0, schemaOptions_1.schemaOptions)(target, options);
43
- const outputVersionKey = hasVersionField(schema, (_a = options.schemaOptions) === null || _a === void 0 ? void 0 : _a.versionKey);
42
+ const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);
44
43
  schema.methods.toClass = function toClass() {
45
44
  return (0, json_mapper_1.deserialize)(this.toObject({
46
45
  versionKey: outputVersionKey,
@@ -1 +1 @@
1
- {"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,mDAAyD;AACzD,yCAA4D;AAC5D,6CAAuC;AACvC,gEAA8F;AAC9F,sDAAgF;AAGhF,qDAAgD;AAChD,mDAA8C;AAE9C,MAAM,sBAAsB,GAAG,CAAC,KAAK,CAAC,CAAC;AAOvC;;GAEG;AACH,SAAS,WAAW,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAyB,EAAE,OAAuB;IACtF,MAAM,cAAc,GAAG,IAAI,kBAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5D,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;QAC/C,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACtC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,MAA0C;IAC9D,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,eAAe,CAAC,MAAuB,EAAE,UAA6B;IAC7E,8CAA8C;IAC9C,IAAI,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IAE9B,gDAAgD;IAChD,OAAQ,UAAqB,IAAI,MAAM,CAAC,KAAK,CAAC;AAChD,CAAC;AAED,SAAgB,YAAY,CAAC,MAAiB,EAAE,UAAiC,EAAE;;IACjF,MAAM,sBAAsB,GAAG,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,mCAAuB,CAAC,IAAI,EAAE,CAAC;IACrF,OAAO,CAAC,aAAa,GAAG,EAAC,GAAG,OAAO,CAAC,aAAa,EAAE,G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AC5C,iBAAiB,GAAG,EAAC,GAAG,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAQ,EAAC,CAAC;KACrF;IAED,iBAAiB,GAAG,IAAA,kBAAW,EAAC,EAAC,GAAG,iBAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,IAAI,UAAU,CAAC,OAAO,EAAE;YACtB,OAAO,CAAC,iBAAiB,CAAC,CAAC;SAC5B;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAA,aAAM,EAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;SACpG;QAED,OAAO,EAAC,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,iBAAiB,EAAoC,CAAC;KAC9E;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AArDD,0DAqDC","sourcesContent":["import {cleanObject, nameOf, Store, Type} from \"@tsed/core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return {type: Map, of: schemaTypeOptions} as unknown as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
1
+ {"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,mDAAyD;AACzD,yCAA4D;AAC5D,6CAAuC;AACvC,gEAA8F;AAC9F,sDAAgF;AAGhF,qDAAgD;AAChD,mDAA8C;AAE9C,MAAM,sBAAsB,GAAG,CAAC,KAAK,CAAC,CAAC;AAOvC;;GAEG;AACH,SAAS,WAAW,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAyB,EAAE,OAAuB;IACtF,MAAM,cAAc,GAAG,IAAI,kBAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5D,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;QAC/C,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACtC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,MAA0C;IAC9D,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,eAAe,CAAC,MAAuB,EAAE,UAA6B;IAC7E,8CAA8C;IAC9C,IAAI,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IAE9B,gDAAgD;IAChD,OAAQ,UAAqB,IAAI,MAAM,CAAC,KAAK,CAAC;AAChD,CAAC;AAED,SAAgB,YAAY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;YACtB,GAAG;YACH,GAAG;YACH,SAAS;YACT,SAAS;YACT,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC;YACxB,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SAC/B,CAAC;KACH;SAAM,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE;QACjC,4CAA4C;QAC5C,iBAAiB,GAAG,EAAC,GAAG,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAQ,EAAC,CAAC;KACrF;IAED,iBAAiB,GAAG,IAAA,kBAAW,EAAC,EAAC,GAAG,iBAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,IAAI,UAAU,CAAC,OAAO,EAAE;YACtB,OAAO,CAAC,iBAAiB,CAAC,CAAC;SAC5B;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAA,aAAM,EAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;SACpG;QAED,OAAO,EAAC,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,iBAAiB,EAAoC,CAAC;KAC9E;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AArDD,0DAqDC","sourcesContent":["import {cleanObject, nameOf, Store, Type} from \"@tsed/core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return {type: Map, of: schemaTypeOptions} as unknown as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
@@ -7,13 +7,15 @@ import { MongooseService } from "./services/MongooseService.js";
7
7
  * @ignore
8
8
  */
9
9
  let MongooseModule = class MongooseModule {
10
+ mongooseService;
11
+ settings;
10
12
  constructor(mongooseService, settings) {
11
13
  this.mongooseService = mongooseService;
12
14
  this.settings = settings;
13
15
  // auto configure the cache manager when mongoose is used with @tsed/mongoose
14
16
  const cache = this.settings.get("cache");
15
17
  // istanbul ignore next
16
- if (cache === null || cache === void 0 ? void 0 : cache.mongoose) {
18
+ if (cache?.mongoose) {
17
19
  cache.connection = this.mongooseService.get();
18
20
  }
19
21
  }
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModule.js","sourceRoot":"","sources":["../../src/MongooseModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,aAAa,EAAS,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAC,MAAM,EAAY,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,oBAAoB,EAAC,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAC;AAE3D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IACzB,YAAoB,eAAgC,EAA2B,QAAuB;QAAlF,oBAAe,GAAf,eAAe,CAAiB;QAA2B,aAAQ,GAAR,QAAQ,CAAe;QACpG,6EAA6E;QAC7E,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAM,OAAO,CAAC,CAAC;QAE9C,uBAAuB;QACvB,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,QAAQ,EAAE;YACnB,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,CAAC;SAC/C;IACH,CAAC;IAED,OAAO,KAAyB,CAAC;IAEjC,UAAU;QACR,OAAO,IAAI,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC;IACjD,CAAC;CACF,CAAA;AAhBY,cAAc;IAH1B,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,oBAAoB,CAAC;KAChC,CAAC;IAEuD,WAAA,aAAa,EAAE,CAAA;qCAAjC,eAAe;GADzC,cAAc,CAgB1B;SAhBY,cAAc","sourcesContent":["import {Configuration, OnInit} from \"@tsed/di\";\nimport {Module, OnDestroy} from \"@tsed/di\";\nimport {MONGOOSE_CONNECTIONS} from \"./services/MongooseConnections\";\nimport {MongooseService} from \"./services/MongooseService\";\n\n/**\n * @ignore\n */\n@Module({\n imports: [MONGOOSE_CONNECTIONS]\n})\nexport class MongooseModule implements OnDestroy, OnInit {\n constructor(private mongooseService: MongooseService, @Configuration() private settings: Configuration) {\n // auto configure the cache manager when mongoose is used with @tsed/mongoose\n const cache = this.settings.get<any>(\"cache\");\n\n // istanbul ignore next\n if (cache?.mongoose) {\n cache.connection = this.mongooseService.get();\n }\n }\n\n $onInit(): void | Promise<any> {}\n\n $onDestroy(): Promise<any> | void {\n return this.mongooseService.closeConnections();\n }\n}\n"]}
1
+ {"version":3,"file":"MongooseModule.js","sourceRoot":"","sources":["../../src/MongooseModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,aAAa,EAAS,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAC,MAAM,EAAY,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,oBAAoB,EAAC,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAC;AAE3D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IACL;IAA2D;IAA/E,YAAoB,eAAgC,EAA2B,QAAuB;QAAlF,oBAAe,GAAf,eAAe,CAAiB;QAA2B,aAAQ,GAAR,QAAQ,CAAe;QACpG,6EAA6E;QAC7E,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAM,OAAO,CAAC,CAAC;QAE9C,uBAAuB;QACvB,IAAI,KAAK,EAAE,QAAQ,EAAE;YACnB,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,CAAC;SAC/C;IACH,CAAC;IAED,OAAO,KAAyB,CAAC;IAEjC,UAAU;QACR,OAAO,IAAI,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC;IACjD,CAAC;CACF,CAAA;AAhBY,cAAc;IAH1B,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,oBAAoB,CAAC;KAChC,CAAC;IAEuD,WAAA,aAAa,EAAE,CAAA;qCAAjC,eAAe;GADzC,cAAc,CAgB1B;SAhBY,cAAc","sourcesContent":["import {Configuration, OnInit} from \"@tsed/di\";\nimport {Module, OnDestroy} from \"@tsed/di\";\nimport {MONGOOSE_CONNECTIONS} from \"./services/MongooseConnections\";\nimport {MongooseService} from \"./services/MongooseService\";\n\n/**\n * @ignore\n */\n@Module({\n imports: [MONGOOSE_CONNECTIONS]\n})\nexport class MongooseModule implements OnDestroy, OnInit {\n constructor(private mongooseService: MongooseService, @Configuration() private settings: Configuration) {\n // auto configure the cache manager when mongoose is used with @tsed/mongoose\n const cache = this.settings.get<any>(\"cache\");\n\n // istanbul ignore next\n if (cache?.mongoose) {\n cache.connection = this.mongooseService.get();\n }\n }\n\n $onInit(): void | Promise<any> {}\n\n $onDestroy(): Promise<any> | void {\n return this.mongooseService.closeConnections();\n }\n}\n"]}
@@ -8,7 +8,7 @@ function isRef(value) {
8
8
  return (value && value._bsontype) || isString(value);
9
9
  }
10
10
  function getType(refPath, ctx) {
11
- return ((ctx === null || ctx === void 0 ? void 0 : ctx.self[refPath]) && MongooseModels.get(ctx.self[refPath])) || Object;
11
+ return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath])) || Object;
12
12
  }
13
13
  /**
14
14
  * Define a property as mongoose reference to other Model (decorated with @Model).
@@ -1 +1 @@
1
- {"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,QAAQ,EAAE,UAAU,EAAQ,aAAa,EAAC,MAAM,YAAY,CAAC;AACzF,OAAO,EAAC,WAAW,EAAE,OAAO,EAAmB,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAC,MAAM,IAAI,cAAc,EAAC,MAAM,UAAU,CAAC;AAClD,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAC,WAAW,EAAE,aAAa,EAAE,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAE5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,OAAO,CAAC,OAAe,EAAE,GAAoB;IACpD,OAAO,CAAC,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,CAAC,OAAO,CAAC,KAAI,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAW,CAAC,CAAC,IAAI,MAAM,CAAC;AAC3F,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,UAAU,CAAC,OAAe,EAAE,GAAG,KAAkB;IAC/D,OAAO,aAAa,CAClB,QAAQ,CAAC,MAAM,CAAC,EAChB,OAAO,CAAC,0BAA0B,CAAC,EACnC,WAAW,CAAC,uBAAuB,CAAC,EACpC,UAAU,CAAC,eAAe,EAAE;QAC1B,IAAI,EAAE,cAAc,CAAC,KAAK,CAAC,QAAQ;QACnC,OAAO;KACR,CAAC,EACF,aAAa,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAC3B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,WAAW,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAC,CAAC,CAAC;IACnE,CAAC,CAAC,EACF,WAAW,CAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,SAAS,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAC,CAAC,CAAC;IACjE,CAAC,CAAC,EACF,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
1
+ {"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,QAAQ,EAAE,UAAU,EAAQ,aAAa,EAAC,MAAM,YAAY,CAAC;AACzF,OAAO,EAAC,WAAW,EAAE,OAAO,EAAmB,KAAK,EAA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nE,CAAC,CAAC,EACF,WAAW,CAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,SAAS,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAC,CAAC,CAAC;IACjE,CAAC,CAAC,EACF,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
@@ -7,10 +7,9 @@ function asPromise(c) {
7
7
  return c && c.asPromise ? c.asPromise() : c;
8
8
  }
9
9
  let MongooseService = class MongooseService {
10
- constructor() {
11
- this.connections = new Map();
12
- this.defaultConnection = "default";
13
- }
10
+ connections = new Map();
11
+ defaultConnection = "default";
12
+ logger;
14
13
  /**
15
14
  *
16
15
  * @returns {Promise<"mongoose".Connection>}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,QAAQ,MAAM,UAAU,CAAC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,MAAa,eAAe;IAA5B;QACW,gBAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;QAC3D,sBAAiB,GAAW,SAAS,CAAC;IA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CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,MAAM,EAAE;8BACD,MAAM;+CAAC;AALJ,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CA4D3B;SA5DY,eAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,QAAQ,MAAM,UAAU,CAAC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,MAAa,eAAe;IACjB,WAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;IAC3D,iBAAiB,GAAW,SAAS,CAAC;IAG9C,MAAM,CAAS;IAEf;;;OAGG;IACH,KAAK,CAAC,OAAO,CAAC,EAAU,EAAE,GAAW,EAAE,iBAAiC,EAAE,SAAS,GAAG,KAAK;QACzF,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SAC5B;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QAEnE,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;YACtF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;YAErC,IAAI,EAAE,KAAK,SAAS,IAAI,SAAS,EAAE;gBACjC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;aAC7B;YAED,OAAO,UAAU,CAAC;SACnB;QAAC,OAAO,EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,MAAM,EAAE;8BACD,MAAM;+CAAC;AALJ,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CA4D3B;SA5DY,eAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
@@ -31,12 +31,11 @@ function hasVersionField(schema, versionKey) {
31
31
  return versionKey in schema.paths;
32
32
  }
33
33
  export function createSchema(target, options = {}) {
34
- var _a;
35
34
  const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};
36
35
  options.schemaOptions = { ...options.schemaOptions, ...schemaOptionsFromStore };
37
36
  const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
38
37
  schemaOptions(target, options);
39
- const outputVersionKey = hasVersionField(schema, (_a = options.schemaOptions) === null || _a === void 0 ? void 0 : _a.versionKey);
38
+ const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);
40
39
  schema.methods.toClass = function toClass() {
41
40
  return deserialize(this.toObject({
42
41
  versionKey: outputVersionKey,
@@ -1 +1 @@
1
- {"version":3,"file":"createSchema.js","sour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ACT,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC;YACxB,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SAC/B,CAAC;KACH;SAAM,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE;QACjC,4CAA4C;QAC5C,iBAAiB,GAAG,EAAC,GAAG,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAQ,EAAC,CAAC;KACrF;IAED,iBAAiB,GAAG,WAAW,CAAC,EAAC,GAAG,iBAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,IAAI,UAAU,CAAC,OAAO,EAAE;YACtB,OAAO,CAAC,iBAAiB,CAAC,CAAC;SAC5B;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;SACpG;QAED,OAAO,EAAC,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,iBAAiB,EAAoC,CAAC;KAC9E;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC","sourcesContent":["import {cleanObject, nameOf, Store, Type} from \"@tsed/core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return {type: Map, of: schemaTypeOptions} as unknown as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
1
+ {"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAC,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAC,aAAa,EAAkB,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AACvC,OAAO,QAAsE,MAAM,UAAU,CAAC;AAC9F,OAAO,EAAC,eAAe,EAAE,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AAGhF,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAE9C,MAAM,sBAAsB,GAAG,CAAC,KAAK,CAAC,CAAC;AAOvC;;GAEG;AACH,SAAS,WAAW,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAyB,EAAE,OAAuB;IACtF,MAAM,cAAc,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5D,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;QAC/C,cAAc,CAAC,OA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core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return {type: Map, of: schemaTypeOptions} as unknown as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/mongoose",
3
- "version": "6.114.17",
3
+ "version": "6.115.1",
4
4
  "description": "Mongoose package for Ts.ED framework",
5
5
  "private": false,
6
6
  "keywords": [
@@ -32,21 +32,21 @@
32
32
  "tslib": "2.4.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@tsed/ajv": "6.114.17",
36
- "@tsed/core": "6.114.17",
37
- "@tsed/di": "6.114.17",
38
- "@tsed/json-mapper": "6.114.17",
35
+ "@tsed/ajv": "6.115.1",
36
+ "@tsed/core": "6.115.1",
37
+ "@tsed/di": "6.115.1",
38
+ "@tsed/json-mapper": "6.115.1",
39
39
  "@tsed/logger": ">=6.1.4",
40
- "@tsed/schema": "6.114.17",
40
+ "@tsed/schema": "6.115.1",
41
41
  "mongoose": "^6.1.7"
42
42
  },
43
43
  "peerDependencies": {
44
- "@tsed/ajv": "^6.114.17",
45
- "@tsed/core": "^6.114.17",
46
- "@tsed/di": "^6.114.17",
47
- "@tsed/json-mapper": "^6.114.17",
44
+ "@tsed/ajv": "^6.115.1",
45
+ "@tsed/core": "^6.115.1",
46
+ "@tsed/di": "^6.115.1",
47
+ "@tsed/json-mapper": "^6.115.1",
48
48
  "@tsed/logger": ">=6.1.4",
49
- "@tsed/schema": "^6.114.17",
49
+ "@tsed/schema": "^6.115.1",
50
50
  "mongoose": ">=6.0.0"
51
51
  },
52
52
  "repository": "https://github.com/tsedio/tsed",