@tsed/mongoose 6.107.5 → 6.108.0

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.
Files changed (33) hide show
  1. package/jest.config.js +1 -1
  2. package/lib/cjs/MongooseModule.js +3 -3
  3. package/lib/cjs/MongooseModule.js.map +1 -1
  4. package/lib/cjs/decorators/model.js +1 -1
  5. package/lib/cjs/decorators/model.js.map +1 -1
  6. package/lib/cjs/decorators/numberDecimal.js +1 -1
  7. package/lib/cjs/index.js +44 -44
  8. package/lib/cjs/index.js.map +1 -1
  9. package/lib/cjs/interfaces/MongooseModel.js.map +1 -1
  10. package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -1
  11. package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -1
  12. package/lib/cjs/services/MongooseConnections.js +1 -5
  13. package/lib/cjs/services/MongooseConnections.js.map +1 -1
  14. package/lib/cjs/services/MongooseService.js +4 -4
  15. package/lib/cjs/services/MongooseService.js.map +1 -1
  16. package/lib/cjs/utils/createModel.js +6 -10
  17. package/lib/cjs/utils/createModel.js.map +1 -1
  18. package/lib/cjs/utils/createSchema.js +1 -1
  19. package/lib/cjs/utils/createSchema.js.map +1 -1
  20. package/lib/esm/decorators/model.js +1 -1
  21. package/lib/esm/decorators/model.js.map +1 -1
  22. package/lib/esm/interfaces/MongooseModel.js.map +1 -1
  23. package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -1
  24. package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -1
  25. package/lib/esm/services/MongooseConnections.js +1 -5
  26. package/lib/esm/services/MongooseConnections.js.map +1 -1
  27. package/lib/esm/utils/createModel.js +5 -9
  28. package/lib/esm/utils/createModel.js.map +1 -1
  29. package/lib/types/interfaces/MongooseModel.d.ts +1 -2
  30. package/lib/types/interfaces/MongooseModelOptions.d.ts +1 -1
  31. package/lib/types/interfaces/MongooseSchemaOptions.d.ts +1 -1
  32. package/lib/types/utils/createModel.d.ts +2 -2
  33. package/package.json +16 -16
package/jest.config.js CHANGED
@@ -5,7 +5,7 @@ module.exports = {
5
5
  ...require("@tsed/jest-config")(__dirname, "mongoose"),
6
6
  coverageThreshold: {
7
7
  global: {
8
- branches: 93.63,
8
+ branches: 93.49,
9
9
  functions: 98.9,
10
10
  lines: 98.9,
11
11
  statements: 98.93
@@ -25,12 +25,12 @@ let MongooseModule = class MongooseModule {
25
25
  return this.mongooseService.closeConnections();
26
26
  }
27
27
  };
28
- MongooseModule = (0, tslib_1.__decorate)([
28
+ MongooseModule = tslib_1.__decorate([
29
29
  (0, di_2.Module)({
30
30
  imports: [MongooseConnections_1.MONGOOSE_CONNECTIONS]
31
31
  }),
32
- (0, tslib_1.__param)(1, (0, di_1.Configuration)()),
33
- (0, tslib_1.__metadata)("design:paramtypes", [MongooseService_1.MongooseService, Object])
32
+ tslib_1.__param(1, (0, di_1.Configuration)()),
33
+ tslib_1.__metadata("design:paramtypes", [MongooseService_1.MongooseService, Object])
34
34
  ], MongooseModule);
35
35
  exports.MongooseModule = MongooseModule;
36
36
  //# sourceMappingURL=MongooseModule.js.map
@@ -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,wBAAA,IAAA,kBAAa,GAAE,CAAA;kDAAjC,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;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"]}
@@ -59,7 +59,7 @@ function Model(options = {}) {
59
59
  deps: [MongooseConnections_1.MONGOOSE_CONNECTIONS, token],
60
60
  useFactory(connections, schema) {
61
61
  (0, schemaOptions_1.applySchemaOptions)(schema, (0, schemaOptions_1.schemaOptions)(target));
62
- return (0, createModel_1.createModel)(target, schema, collectionName, options.collection, options.skipInit, connections.get(options.connection), options.discriminatorValue);
62
+ return (0, createModel_1.createModel)(target, schema, collectionName, options.collection, options.overwriteModels, connections.get(options.connection), options.discriminatorValue);
63
63
  }
64
64
  });
65
65
  };
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/decorators/model.ts"],"names":[],"mappings":";;;AAAA,iCAA0C;AAG1C,yEAAqE;AACrE,sDAAgE;AAChE,wDAAgD;AAChD,0DAAyE;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,SAAgB,KAAK,CAAC,UAAgC,EAAE;IACtD,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,MAAM,EAAC,KAAK,EAAE,cAAc,EAAC,GAAG,IAAA,2BAAa,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE/D,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;YACR,UAAU;gBACR,OAAO,IAAA,wBAAS,EAAC,MAAM,EAAE,OAAc,CAAC,CAAC;YAC3C,CAAC;SACF,CAAC,CAAC;QAEH,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,CAAC,0CAAoB,EAAE,KAAK,CAAC;YACnC,UAAU,CAAC,WAAiC,EAAE,MAAc;gBAC1D,IAAA,kCAAkB,EAAC,MAAM,EAAE,IAAA,6BAAa,EAAC,MAAM,CAAC,CAAC,CAAC;gBAClD,OAAO,IAAA,yBAAW,EAChB,MAAM,EACN,MAAM,EACN,cAAc,EACd,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,QAAQ,EAChB,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,kBAAkB,CAC3B,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AA9BD,sBA8BC","sourcesContent":["import {registerProvider} from \"@tsed/di\";\nimport {Schema} from \"mongoose\";\nimport {MongooseModelOptions} from \"../interfaces/MongooseModelOptions\";\nimport {MONGOOSE_CONNECTIONS} from \"../services/MongooseConnections\";\nimport {createModel, getModelToken} from \"../utils/createModel\";\nimport {getSchema} from \"../utils/createSchema\";\nimport {applySchemaOptions, schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with\n * `@Inject` annotation.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * Then inject the model into a service:\n *\n * ```typescript\n * class MyService {\n * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {\n * eventModel.findById().exec();\n * }\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n * - `name` (String): model name.\n * - `collection` (String): collection (optional, induced from model name).\n * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).\n *\n * @param {MongooseModelOptions} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @class\n */\nexport function Model(options: MongooseModelOptions = {}) {\n return (target: any) => {\n const {token, collectionName} = getModelToken(target, options);\n\n registerProvider({\n provide: token,\n deps: [],\n useFactory() {\n return getSchema(target, options as any);\n }\n });\n\n registerProvider({\n provide: target,\n type: \"mongoose:model\",\n deps: [MONGOOSE_CONNECTIONS, token],\n useFactory(connections: MONGOOSE_CONNECTIONS, schema: Schema) {\n applySchemaOptions(schema, schemaOptions(target));\n return createModel(\n target,\n schema,\n collectionName,\n options.collection,\n options.skipInit,\n connections.get(options.connection),\n options.discriminatorValue\n );\n }\n });\n };\n}\n"]}
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/decorators/model.ts"],"names":[],"mappings":";;;AAAA,iCAA0C;AAG1C,yEAAqE;AACrE,sDAAgE;AAChE,wDAAgD;AAChD,0DAAyE;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,SAAgB,KAAK,CAAC,UAAgC,EAAE;IACtD,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,MAAM,EAAC,KAAK,EAAE,cAAc,EAAC,GAAG,IAAA,2BAAa,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE/D,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;YACR,UAAU;gBACR,OAAO,IAAA,wBAAS,EAAC,MAAM,EAAE,OAAc,CAAC,CAAC;YAC3C,CAAC;SACF,CAAC,CAAC;QAEH,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,CAAC,0CAAoB,EAAE,KAAK,CAAC;YACnC,UAAU,CAAC,WAAiC,EAAE,MAAc;gBAC1D,IAAA,kCAAkB,EAAC,MAAM,EAAE,IAAA,6BAAa,EAAC,MAAM,CAAC,CAAC,CAAC;gBAClD,OAAO,IAAA,yBAAW,EAChB,MAAM,EACN,MAAM,EACN,cAAc,EACd,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,eAAe,EACvB,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,kBAAkB,CAC3B,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AA9BD,sBA8BC","sourcesContent":["import {registerProvider} from \"@tsed/di\";\nimport {Schema} from \"mongoose\";\nimport {MongooseModelOptions} from \"../interfaces/MongooseModelOptions\";\nimport {MONGOOSE_CONNECTIONS} from \"../services/MongooseConnections\";\nimport {createModel, getModelToken} from \"../utils/createModel\";\nimport {getSchema} from \"../utils/createSchema\";\nimport {applySchemaOptions, schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with\n * `@Inject` annotation.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * Then inject the model into a service:\n *\n * ```typescript\n * class MyService {\n * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {\n * eventModel.findById().exec();\n * }\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n * - `name` (String): model name.\n * - `collection` (String): collection (optional, induced from model name).\n * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).\n *\n * @param {MongooseModelOptions} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @class\n */\nexport function Model(options: MongooseModelOptions = {}) {\n return (target: any) => {\n const {token, collectionName} = getModelToken(target, options);\n\n registerProvider({\n provide: token,\n deps: [],\n useFactory() {\n return getSchema(target, options as any);\n }\n });\n\n registerProvider({\n provide: target,\n type: \"mongoose:model\",\n deps: [MONGOOSE_CONNECTIONS, token],\n useFactory(connections: MONGOOSE_CONNECTIONS, schema: Schema) {\n applySchemaOptions(schema, schemaOptions(target));\n return createModel(\n target,\n schema,\n collectionName,\n options.collection,\n options.overwriteModels,\n connections.get(options.connection),\n options.discriminatorValue\n );\n }\n });\n };\n}\n"]}
@@ -13,7 +13,7 @@ let DecimalFormat = class DecimalFormat {
13
13
  return (0, core_1.isString)(num) || (0, core_1.isNumber)(num);
14
14
  }
15
15
  };
16
- DecimalFormat = (0, tslib_1.__decorate)([
16
+ DecimalFormat = tslib_1.__decorate([
17
17
  (0, ajv_1.Formats)("decimal", { type: "number" })
18
18
  ], DecimalFormat);
19
19
  exports.DecimalFormat = DecimalFormat;
package/lib/cjs/index.js CHANGED
@@ -4,48 +4,48 @@
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const tslib_1 = require("tslib");
7
- (0, tslib_1.__exportStar)(require("./MongooseModule"), exports);
8
- (0, tslib_1.__exportStar)(require("./constants/constants"), exports);
9
- (0, tslib_1.__exportStar)(require("./decorators/auto"), exports);
10
- (0, tslib_1.__exportStar)(require("./decorators/discriminatorKey"), exports);
11
- (0, tslib_1.__exportStar)(require("./decorators/dynamicRef"), exports);
12
- (0, tslib_1.__exportStar)(require("./decorators/excludeIndexes"), exports);
13
- (0, tslib_1.__exportStar)(require("./decorators/expires"), exports);
14
- (0, tslib_1.__exportStar)(require("./decorators/immutable"), exports);
15
- (0, tslib_1.__exportStar)(require("./decorators/indexed"), exports);
16
- (0, tslib_1.__exportStar)(require("./decorators/lowercase"), exports);
17
- (0, tslib_1.__exportStar)(require("./decorators/model"), exports);
18
- (0, tslib_1.__exportStar)(require("./decorators/mongooseIndex"), exports);
19
- (0, tslib_1.__exportStar)(require("./decorators/mongooseIndexes"), exports);
20
- (0, tslib_1.__exportStar)(require("./decorators/mongoosePlugin"), exports);
21
- (0, tslib_1.__exportStar)(require("./decorators/numberDecimal"), exports);
22
- (0, tslib_1.__exportStar)(require("./decorators/objectID"), exports);
23
- (0, tslib_1.__exportStar)(require("./decorators/postHook"), exports);
24
- (0, tslib_1.__exportStar)(require("./decorators/preHook"), exports);
25
- (0, tslib_1.__exportStar)(require("./decorators/ref"), exports);
26
- (0, tslib_1.__exportStar)(require("./decorators/schema"), exports);
27
- (0, tslib_1.__exportStar)(require("./decorators/schemaIgnore"), exports);
28
- (0, tslib_1.__exportStar)(require("./decorators/select"), exports);
29
- (0, tslib_1.__exportStar)(require("./decorators/sparse"), exports);
30
- (0, tslib_1.__exportStar)(require("./decorators/text"), exports);
31
- (0, tslib_1.__exportStar)(require("./decorators/trim"), exports);
32
- (0, tslib_1.__exportStar)(require("./decorators/unique"), exports);
33
- (0, tslib_1.__exportStar)(require("./decorators/uppercase"), exports);
34
- (0, tslib_1.__exportStar)(require("./decorators/versionKey"), exports);
35
- (0, tslib_1.__exportStar)(require("./decorators/virtualRef"), exports);
36
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseConnectionOptions"), exports);
37
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseDocument"), exports);
38
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseModel"), exports);
39
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseModelOptions"), exports);
40
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseSchemaOptions"), exports);
41
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseSchemaTypes"), exports);
42
- (0, tslib_1.__exportStar)(require("./interfaces/MongooseVirtualRefOptions"), exports);
43
- (0, tslib_1.__exportStar)(require("./interfaces/interfaces"), exports);
44
- (0, tslib_1.__exportStar)(require("./registries/MongooseModels"), exports);
45
- (0, tslib_1.__exportStar)(require("./services/MongooseConnections"), exports);
46
- (0, tslib_1.__exportStar)(require("./services/MongooseService"), exports);
47
- (0, tslib_1.__exportStar)(require("./utils/createModel"), exports);
48
- (0, tslib_1.__exportStar)(require("./utils/createSchema"), exports);
49
- (0, tslib_1.__exportStar)(require("./utils/resolveRefType"), exports);
50
- (0, tslib_1.__exportStar)(require("./utils/schemaOptions"), exports);
7
+ tslib_1.__exportStar(require("./MongooseModule"), exports);
8
+ tslib_1.__exportStar(require("./constants/constants"), exports);
9
+ tslib_1.__exportStar(require("./decorators/auto"), exports);
10
+ tslib_1.__exportStar(require("./decorators/discriminatorKey"), exports);
11
+ tslib_1.__exportStar(require("./decorators/dynamicRef"), exports);
12
+ tslib_1.__exportStar(require("./decorators/excludeIndexes"), exports);
13
+ tslib_1.__exportStar(require("./decorators/expires"), exports);
14
+ tslib_1.__exportStar(require("./decorators/immutable"), exports);
15
+ tslib_1.__exportStar(require("./decorators/indexed"), exports);
16
+ tslib_1.__exportStar(require("./decorators/lowercase"), exports);
17
+ tslib_1.__exportStar(require("./decorators/model"), exports);
18
+ tslib_1.__exportStar(require("./decorators/mongooseIndex"), exports);
19
+ tslib_1.__exportStar(require("./decorators/mongooseIndexes"), exports);
20
+ tslib_1.__exportStar(require("./decorators/mongoosePlugin"), exports);
21
+ tslib_1.__exportStar(require("./decorators/numberDecimal"), exports);
22
+ tslib_1.__exportStar(require("./decorators/objectID"), exports);
23
+ tslib_1.__exportStar(require("./decorators/postHook"), exports);
24
+ tslib_1.__exportStar(require("./decorators/preHook"), exports);
25
+ tslib_1.__exportStar(require("./decorators/ref"), exports);
26
+ tslib_1.__exportStar(require("./decorators/schema"), exports);
27
+ tslib_1.__exportStar(require("./decorators/schemaIgnore"), exports);
28
+ tslib_1.__exportStar(require("./decorators/select"), exports);
29
+ tslib_1.__exportStar(require("./decorators/sparse"), exports);
30
+ tslib_1.__exportStar(require("./decorators/text"), exports);
31
+ tslib_1.__exportStar(require("./decorators/trim"), exports);
32
+ tslib_1.__exportStar(require("./decorators/unique"), exports);
33
+ tslib_1.__exportStar(require("./decorators/uppercase"), exports);
34
+ tslib_1.__exportStar(require("./decorators/versionKey"), exports);
35
+ tslib_1.__exportStar(require("./decorators/virtualRef"), exports);
36
+ tslib_1.__exportStar(require("./interfaces/MongooseConnectionOptions"), exports);
37
+ tslib_1.__exportStar(require("./interfaces/MongooseDocument"), exports);
38
+ tslib_1.__exportStar(require("./interfaces/MongooseModel"), exports);
39
+ tslib_1.__exportStar(require("./interfaces/MongooseModelOptions"), exports);
40
+ tslib_1.__exportStar(require("./interfaces/MongooseSchemaOptions"), exports);
41
+ tslib_1.__exportStar(require("./interfaces/MongooseSchemaTypes"), exports);
42
+ tslib_1.__exportStar(require("./interfaces/MongooseVirtualRefOptions"), exports);
43
+ tslib_1.__exportStar(require("./interfaces/interfaces"), exports);
44
+ tslib_1.__exportStar(require("./registries/MongooseModels"), exports);
45
+ tslib_1.__exportStar(require("./services/MongooseConnections"), exports);
46
+ tslib_1.__exportStar(require("./services/MongooseService"), exports);
47
+ tslib_1.__exportStar(require("./utils/createModel"), exports);
48
+ tslib_1.__exportStar(require("./utils/createSchema"), exports);
49
+ tslib_1.__exportStar(require("./utils/resolveRefType"), exports);
50
+ tslib_1.__exportStar(require("./utils/schemaOptions"), exports);
51
51
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,gEAAiC;AACjC,qEAAsC;AACtC,iEAAkC;AAClC,6EAA8C;AAC9C,uEAAwC;AACxC,2EAA4C;AAC5C,oEAAqC;AACrC,sEAAuC;AACvC,oEAAqC;AACrC,sEAAuC;AACvC,kEAAmC;AACnC,0EAA2C;AAC3C,4EAA6C;AAC7C,2EAA4C;AAC5C,0EAA2C;AAC3C,qEAAsC;AACtC,qEAAsC;AACtC,oEAAqC;AACrC,gEAAiC;AACjC,mEAAoC;AACpC,yEAA0C;AAC1C,mEAAoC;AACpC,mEAAoC;AACpC,iEAAkC;AAClC,iEAAkC;AAClC,mEAAoC;AACpC,sEAAuC;AACvC,uEAAwC;AACxC,uEAAwC;AACxC,sFAAuD;AACvD,6EAA8C;AAC9C,0EAA2C;AAC3C,iFAAkD;AAClD,kFAAmD;AACnD,gFAAiD;AACjD,sFAAuD;AACvD,uEAAwC;AACxC,2EAA4C;AAC5C,8EAA+C;AAC/C,0EAA2C;AAC3C,mEAAoC;AACpC,oEAAqC;AACrC,sEAAuC;AACvC,qEAAsC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./MongooseModule\";\nexport * from \"./constants/constants\";\nexport * from \"./decorators/auto\";\nexport * from \"./decorators/discriminatorKey\";\nexport * from \"./decorators/dynamicRef\";\nexport * from \"./decorators/excludeIndexes\";\nexport * from \"./decorators/expires\";\nexport * from \"./decorators/immutable\";\nexport * from \"./decorators/indexed\";\nexport * from \"./decorators/lowercase\";\nexport * from \"./decorators/model\";\nexport * from \"./decorators/mongooseIndex\";\nexport * from \"./decorators/mongooseIndexes\";\nexport * from \"./decorators/mongoosePlugin\";\nexport * from \"./decorators/numberDecimal\";\nexport * from \"./decorators/objectID\";\nexport * from \"./decorators/postHook\";\nexport * from \"./decorators/preHook\";\nexport * from \"./decorators/ref\";\nexport * from \"./decorators/schema\";\nexport * from \"./decorators/schemaIgnore\";\nexport * from \"./decorators/select\";\nexport * from \"./decorators/sparse\";\nexport * from \"./decorators/text\";\nexport * from \"./decorators/trim\";\nexport * from \"./decorators/unique\";\nexport * from \"./decorators/uppercase\";\nexport * from \"./decorators/versionKey\";\nexport * from \"./decorators/virtualRef\";\nexport * from \"./interfaces/MongooseConnectionOptions\";\nexport * from \"./interfaces/MongooseDocument\";\nexport * from \"./interfaces/MongooseModel\";\nexport * from \"./interfaces/MongooseModelOptions\";\nexport * from \"./interfaces/MongooseSchemaOptions\";\nexport * from \"./interfaces/MongooseSchemaTypes\";\nexport * from \"./interfaces/MongooseVirtualRefOptions\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./registries/MongooseModels\";\nexport * from \"./services/MongooseConnections\";\nexport * from \"./services/MongooseService\";\nexport * from \"./utils/createModel\";\nexport * from \"./utils/createSchema\";\nexport * from \"./utils/resolveRefType\";\nexport * from \"./utils/schemaOptions\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,2DAAiC;AACjC,gEAAsC;AACtC,4DAAkC;AAClC,wEAA8C;AAC9C,kEAAwC;AACxC,sEAA4C;AAC5C,+DAAqC;AACrC,iEAAuC;AACvC,+DAAqC;AACrC,iEAAuC;AACvC,6DAAmC;AACnC,qEAA2C;AAC3C,uEAA6C;AAC7C,sEAA4C;AAC5C,qEAA2C;AAC3C,gEAAsC;AACtC,gEAAsC;AACtC,+DAAqC;AACrC,2DAAiC;AACjC,8DAAoC;AACpC,oEAA0C;AAC1C,8DAAoC;AACpC,8DAAoC;AACpC,4DAAkC;AAClC,4DAAkC;AAClC,8DAAoC;AACpC,iEAAuC;AACvC,kEAAwC;AACxC,kEAAwC;AACxC,iFAAuD;AACvD,wEAA8C;AAC9C,qEAA2C;AAC3C,4EAAkD;AAClD,6EAAmD;AACnD,2EAAiD;AACjD,iFAAuD;AACvD,kEAAwC;AACxC,sEAA4C;AAC5C,yEAA+C;AAC/C,qEAA2C;AAC3C,8DAAoC;AACpC,+DAAqC;AACrC,iEAAuC;AACvC,gEAAsC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./MongooseModule\";\nexport * from \"./constants/constants\";\nexport * from \"./decorators/auto\";\nexport * from \"./decorators/discriminatorKey\";\nexport * from \"./decorators/dynamicRef\";\nexport * from \"./decorators/excludeIndexes\";\nexport * from \"./decorators/expires\";\nexport * from \"./decorators/immutable\";\nexport * from \"./decorators/indexed\";\nexport * from \"./decorators/lowercase\";\nexport * from \"./decorators/model\";\nexport * from \"./decorators/mongooseIndex\";\nexport * from \"./decorators/mongooseIndexes\";\nexport * from \"./decorators/mongoosePlugin\";\nexport * from \"./decorators/numberDecimal\";\nexport * from \"./decorators/objectID\";\nexport * from \"./decorators/postHook\";\nexport * from \"./decorators/preHook\";\nexport * from \"./decorators/ref\";\nexport * from \"./decorators/schema\";\nexport * from \"./decorators/schemaIgnore\";\nexport * from \"./decorators/select\";\nexport * from \"./decorators/sparse\";\nexport * from \"./decorators/text\";\nexport * from \"./decorators/trim\";\nexport * from \"./decorators/unique\";\nexport * from \"./decorators/uppercase\";\nexport * from \"./decorators/versionKey\";\nexport * from \"./decorators/virtualRef\";\nexport * from \"./interfaces/MongooseConnectionOptions\";\nexport * from \"./interfaces/MongooseDocument\";\nexport * from \"./interfaces/MongooseModel\";\nexport * from \"./interfaces/MongooseModelOptions\";\nexport * from \"./interfaces/MongooseSchemaOptions\";\nexport * from \"./interfaces/MongooseSchemaTypes\";\nexport * from \"./interfaces/MongooseVirtualRefOptions\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./registries/MongooseModels\";\nexport * from \"./services/MongooseConnections\";\nexport * from \"./services/MongooseService\";\nexport * from \"./utils/createModel\";\nexport * from \"./utils/createSchema\";\nexport * from \"./utils/resolveRefType\";\nexport * from \"./utils/schemaOptions\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {JsonDeserializerOptions} from \"@tsed/json-mapper\";\nimport {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport interface MongooseModel<T> extends Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>> {}\n"]}
1
+ {"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport type MongooseModel<T> = Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>>;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n skipInit?: boolean;\n discriminatorValue?: string;\n}\n"]}
1
+ {"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\nimport {CompileModelOptions} from \"mongoose\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n overwriteModels?: boolean;\n discriminatorValue?: string;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: object;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
1
+ {"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: Record<any, any>;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
@@ -38,11 +38,7 @@ function mapOptions(options) {
38
38
  const settings = mapOptions(configuration.get("mongoose"));
39
39
  let isDefault = true;
40
40
  for (const current of settings) {
41
- await mongooseService.connect(current.id, current.url, current.connectionOptions || {
42
- useCreateIndex: true,
43
- useNewUrlParser: true,
44
- useUnifiedTopology: true
45
- }, isDefault);
41
+ await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);
46
42
  isDefault = false;
47
43
  }
48
44
  return mongooseService;
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseConnections.js","sourceRoot":"","sources":["../../../src/services/MongooseConnections.ts"],"names":[],"mappings":";;;AAAA,iCAAyD;AACzD,qCAAmC;AAEnC,iEAA4D;AAE5D;;GAEG;AACH,yCAAyC;AAC5B,QAAA,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAMvE,SAAS,UAAU,CAAC,OAA4E;IAC9F,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,CAAC;KACX;IAED,IAAI,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,EAAE;QACrB,MAAM,EAAC,GAAG,EAAE,iBAAiB,EAAC,GAAG,OAAO,IAAI,EAAE,CAAC;QAE/C,OAAO;YACL;gBACE,EAAE,EAAE,SAAS;gBACb,GAAG;gBACH,iBAAiB;aAClB;SACF,CAAC;KACH;IAED,OAAQ,OAAuC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC/D,OAAO;YACL,GAAG,QAAQ;YACX,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;SAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,IAAA,qBAAgB,EAAC;IACf,OAAO,EAAE,4BAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,kBAAa,EAAE,iCAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAC3B,OAAO,CAAC,EAAE,EACV,OAAO,CAAC,GAAG,EACX,OAAO,CAAC,iBAAiB,IAAI;gBAC3B,cAAc,EAAE,IAAI;gBACpB,eAAe,EAAE,IAAI;gBACrB,kBAAkB,EAAE,IAAI;aACzB,EACD,SAAS,CACV,CAAC;YAEF,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces/MongooseConnectionOptions\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(\n current.id,\n current.url,\n current.connectionOptions || {\n useCreateIndex: true,\n useNewUrlParser: true,\n useUnifiedTopology: true\n },\n isDefault\n );\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
1
+ {"version":3,"file":"MongooseConnections.js","sourceRoot":"","sources":["../../../src/services/MongooseConnections.ts"],"names":[],"mappings":";;;AAAA,iCAAyD;AACzD,qCAAmC;AAEnC,iEAA4D;AAE5D;;GAEG;AACH,yCAAyC;AAC5B,QAAA,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAMvE,SAAS,UAAU,CAAC,OAA4E;IAC9F,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,CAAC;KACX;IAED,IAAI,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,EAAE;QACrB,MAAM,EAAC,GAAG,EAAE,iBAAiB,EAAC,GAAG,OAAO,IAAI,EAAE,CAAC;QAE/C,OAAO;YACL;gBACE,EAAE,EAAE,SAAS;gBACb,GAAG;gBACH,iBAAiB;aAClB;SACF,CAAC;KACH;IAED,OAAQ,OAAuC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC/D,OAAO;YACL,GAAG,QAAQ;YACX,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;SAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,IAAA,qBAAgB,EAAC;IACf,OAAO,EAAE,4BAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,kBAAa,EAAE,iCAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,IAAI,EAAE,EAAE,SAAS,CAAC,CAAC;YAEnG,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces/MongooseConnectionOptions\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
@@ -4,7 +4,7 @@ exports.MongooseService = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const di_1 = require("@tsed/di");
6
6
  const logger_1 = require("@tsed/logger");
7
- const mongoose_1 = (0, tslib_1.__importDefault)(require("mongoose"));
7
+ const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
8
8
  // istanbul ignore next
9
9
  function asPromise(c) {
10
10
  return c && c.asPromise ? c.asPromise() : c;
@@ -62,11 +62,11 @@ let MongooseService = class MongooseService {
62
62
  }
63
63
  }
64
64
  };
65
- (0, tslib_1.__decorate)([
65
+ tslib_1.__decorate([
66
66
  (0, di_1.Inject)(),
67
- (0, tslib_1.__metadata)("design:type", logger_1.Logger)
67
+ tslib_1.__metadata("design:type", logger_1.Logger)
68
68
  ], MongooseService.prototype, "logger", void 0);
69
- MongooseService = (0, tslib_1.__decorate)([
69
+ MongooseService = tslib_1.__decorate([
70
70
  (0, di_1.Injectable)()
71
71
  ], MongooseService);
72
72
  exports.MongooseService = MongooseService;
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../../src/services/MongooseService.ts"],"names":[],"mappings":";;;;AAAA,iCAA4C;AAC5C,yCAAoC;AACpC,qEAAgC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,C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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;2CACD,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;IAA5B;QACW,gBAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;QAC3D,sBAAiB,GAAW,SAAS,CAAC;IA0DhD,CAAC;IArDC;;;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,C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\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"]}
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createModel = exports.getModelToken = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const core_1 = require("@tsed/core");
6
- const mongoose_1 = (0, tslib_1.__importDefault)(require("mongoose"));
6
+ const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
7
7
  const constants_1 = require("../constants/constants");
8
8
  const MongooseModels_1 = require("../registries/MongooseModels");
9
9
  const createSchema_1 = require("./createSchema");
@@ -21,12 +21,12 @@ exports.getModelToken = getModelToken;
21
21
  * @param {"mongoose".Schema} schema Schema that will be attached to the model.
22
22
  * @param name model name
23
23
  * @param collection (optional, induced from model name)
24
- * @param skipInit whether to skip initialization (defaults to false)
24
+ * @param overwriteModels
25
25
  * @param connection
26
26
  * @param discriminatorValue
27
27
  * @returns {Model<T extends Document>}
28
28
  */
29
- function createModel(target, schema, name = (0, core_1.nameOf)(target), collection, skipInit, connection, discriminatorValue) {
29
+ function createModel(target, schema, name = (0, core_1.nameOf)(target), collection, overwriteModels, connection, discriminatorValue) {
30
30
  // if ancestor has a discriminatorKey we need to link model with anscestor schema
31
31
  const ancestor = (0, core_1.ancestorsOf)(target).find((ancestor) => {
32
32
  const options = core_1.Store.from(ancestor).get(constants_1.MONGOOSE_SCHEMA_OPTIONS) || {};
@@ -44,13 +44,9 @@ function createModel(target, schema, name = (0, core_1.nameOf)(target), collecti
44
44
  }
45
45
  }
46
46
  }
47
- /* istanbul ignore else */
48
- if (connection) {
49
- const model = connection.model(name, schema, collection);
50
- core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL, model);
51
- return model;
52
- }
53
- const model = mongoose_1.default.model(name, schema, collection, skipInit);
47
+ const opts = overwriteModels ? { overwriteModels } : undefined;
48
+ const c = connection || mongoose_1.default;
49
+ const model = c.model(name, schema, collection, opts);
54
50
  core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL, model);
55
51
  return model;
56
52
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,qEAAsE;AACtE,sDAAoG;AACpG,iEAA4D;AAC5D,iDAA8C;AAE9C,SAAgB,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,IAAA,6BAAc,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,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,CAAC,CAAC;QACzD,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,0BAAc,EAAE,KAAK,CAAC,CAAC;QAC9C,OAAO,KAAK,CAAC;KACd;IAED,MAAM,KAAK,GAAG,kBAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;IACjE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,0BAAc,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,KAAK,CAAC;AACf,CAAC;AArCD,kCAqCC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param skipInit whether to skip initialization (defaults to false)\n * @param connection\n * @param discriminatorValue\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n skipInit?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n /* istanbul ignore else */\n if (connection) {\n const model = connection.model(name, schema, collection);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n }\n\n const model = mongoose.model(name, schema, collection, skipInit);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n}\n"]}
1
+ {"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,gEAA2F;AAC3F,sDAAoG;AACpG,iEAA4D;AAC5D,iDAA8C;AAE9C,SAAgB,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,IAAA,6BAAc,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,+BAAmB,EAAE,cAAc,CAAC,CAAC;IAC5D,+BAAc,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;IAE3C,OAAO,EAAC,KAAK,EAAE,cAAc,EAAC,CAAC;AACjC,CAAC;AAPD,sCAOC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CACzB,MAAW,EACX,MAAuB,EACvB,OAAe,IAAA,aAAM,EAAC,MAAM,CAAC,EAC7B,UAAmB,EACnB,eAAyB,EACzB,UAAuB,EACvB,kBAA2B;IAE3B,iFAAiF;IACjF,MAAM,QAAQ,GAAG,IAAA,kBAAW,EAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACrD,MAAM,OAAO,GAAG,YAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,mCAAuB,CAAC,IAAI,EAAE,CAAC;QACxE,OAAO,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,IAAI,QAAQ,KAAK,MAAM,EAAE;QACnC,MAAM,aAAa,GAAG,YAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,0BAAc,CAA6C,CAAC;QAC3G,IAAI,aAAa,EAAE;YACjB,MAAM,iBAAiB,GAAG,kBAAkB,IAAI,IAAI,CAAC;YACrD,IAAI,aAAa,CAAC,cAAc,IAAI,aAAa,CAAC,cAAc,CAAC,iBAAiB,CAAC,EAAE;gBACnF,OAAO,aAAa,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;aACxD;iBAAM;gBACL,OAAO,aAAa,CAAC,aAAa,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;aAC/D;SACF;KACF;IAED,MAAM,IAAI,GAAG,eAAe,CAAC,CAAC,CAAC,EAAC,eAAe,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7D,MAAM,CAAC,GAAG,UAAU,IAAI,kBAAQ,CAAC;IAEjC,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;IACtD,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,0BAAc,EAAE,KAAK,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC;AACf,CAAC;AAlCD,kCAkCC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {CompileModelOptions, Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param overwriteModels\n * @param connection\n * @param discriminatorValue\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n overwriteModels?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n const opts = overwriteModels ? {overwriteModels} : undefined;\n const c = connection || mongoose;\n\n const model = c.model(name, schema, collection, opts);\n Store.from(target).set(MONGOOSE_MODEL, model);\n\n return model;\n}\n"]}
@@ -6,7 +6,7 @@ const core_1 = require("@tsed/core");
6
6
  const json_mapper_1 = require("@tsed/json-mapper");
7
7
  const schema_1 = require("@tsed/schema");
8
8
  const change_case_1 = require("change-case");
9
- const mongoose_1 = (0, tslib_1.__importDefault)(require("mongoose"));
9
+ const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
10
10
  const constants_1 = require("../constants/constants");
11
11
  const resolveRefType_1 = require("./resolveRefType");
12
12
  const schemaOptions_1 = require("./schemaOptions");
@@ -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,qEAA8F;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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AAE;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"]}
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,CA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IAAI;YACrB,KAAK,EAAE,KAAe;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"]}
@@ -56,7 +56,7 @@ export function Model(options = {}) {
56
56
  deps: [MONGOOSE_CONNECTIONS, token],
57
57
  useFactory(connections, schema) {
58
58
  applySchemaOptions(schema, schemaOptions(target));
59
- return createModel(target, schema, collectionName, options.collection, options.skipInit, connections.get(options.connection), options.discriminatorValue);
59
+ return createModel(target, schema, collectionName, options.collection, options.overwriteModels, connections.get(options.connection), options.discriminatorValue);
60
60
  }
61
61
  });
62
62
  };
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/decorators/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAG1C,OAAO,EAAC,oBAAoB,EAAC,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAC,WAAW,EAAE,aAAa,EAAC,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAC,SAAS,EAAC,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAC,kBAAkB,EAAE,aAAa,EAAC,MAAM,wBAAwB,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,KAAK,CAAC,UAAgC,EAAE;IACtD,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,MAAM,EAAC,KAAK,EAAE,cAAc,EAAC,GAAG,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE/D,gBAAgB,CAAC;YACf,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;YACR,UAAU;gBACR,OAAO,SAAS,CAAC,MAAM,EAAE,OAAc,CAAC,CAAC;YAC3C,CAAC;SACF,CAAC,CAAC;QAEH,gBAAgB,CAAC;YACf,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,CAAC,oBAAoB,EAAE,KAAK,CAAC;YACnC,UAAU,CAAC,WAAiC,EAAE,MAAc;gBAC1D,kBAAkB,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;gBAClD,OAAO,WAAW,CAChB,MAAM,EACN,MAAM,EACN,cAAc,EACd,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,QAAQ,EAChB,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,kBAAkB,CAC3B,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {registerProvider} from \"@tsed/di\";\nimport {Schema} from \"mongoose\";\nimport {MongooseModelOptions} from \"../interfaces/MongooseModelOptions\";\nimport {MONGOOSE_CONNECTIONS} from \"../services/MongooseConnections\";\nimport {createModel, getModelToken} from \"../utils/createModel\";\nimport {getSchema} from \"../utils/createSchema\";\nimport {applySchemaOptions, schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with\n * `@Inject` annotation.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * Then inject the model into a service:\n *\n * ```typescript\n * class MyService {\n * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {\n * eventModel.findById().exec();\n * }\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n * - `name` (String): model name.\n * - `collection` (String): collection (optional, induced from model name).\n * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).\n *\n * @param {MongooseModelOptions} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @class\n */\nexport function Model(options: MongooseModelOptions = {}) {\n return (target: any) => {\n const {token, collectionName} = getModelToken(target, options);\n\n registerProvider({\n provide: token,\n deps: [],\n useFactory() {\n return getSchema(target, options as any);\n }\n });\n\n registerProvider({\n provide: target,\n type: \"mongoose:model\",\n deps: [MONGOOSE_CONNECTIONS, token],\n useFactory(connections: MONGOOSE_CONNECTIONS, schema: Schema) {\n applySchemaOptions(schema, schemaOptions(target));\n return createModel(\n target,\n schema,\n collectionName,\n options.collection,\n options.skipInit,\n connections.get(options.connection),\n options.discriminatorValue\n );\n }\n });\n };\n}\n"]}
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/decorators/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAG1C,OAAO,EAAC,oBAAoB,EAAC,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAC,WAAW,EAAE,aAAa,EAAC,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAC,SAAS,EAAC,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAC,kBAAkB,EAAE,aAAa,EAAC,MAAM,wBAAwB,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,KAAK,CAAC,UAAgC,EAAE;IACtD,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,MAAM,EAAC,KAAK,EAAE,cAAc,EAAC,GAAG,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE/D,gBAAgB,CAAC;YACf,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;YACR,UAAU;gBACR,OAAO,SAAS,CAAC,MAAM,EAAE,OAAc,CAAC,CAAC;YAC3C,CAAC;SACF,CAAC,CAAC;QAEH,gBAAgB,CAAC;YACf,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,CAAC,oBAAoB,EAAE,KAAK,CAAC;YACnC,UAAU,CAAC,WAAiC,EAAE,MAAc;gBAC1D,kBAAkB,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;gBAClD,OAAO,WAAW,CAChB,MAAM,EACN,MAAM,EACN,cAAc,EACd,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,eAAe,EACvB,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,kBAAkB,CAC3B,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {registerProvider} from \"@tsed/di\";\nimport {Schema} from \"mongoose\";\nimport {MongooseModelOptions} from \"../interfaces/MongooseModelOptions\";\nimport {MONGOOSE_CONNECTIONS} from \"../services/MongooseConnections\";\nimport {createModel, getModelToken} from \"../utils/createModel\";\nimport {getSchema} from \"../utils/createSchema\";\nimport {applySchemaOptions, schemaOptions} from \"../utils/schemaOptions\";\n\n/**\n * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with\n * `@Inject` annotation.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Property()\n * field: string;\n * }\n * ```\n *\n * Then inject the model into a service:\n *\n * ```typescript\n * class MyService {\n * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {\n * eventModel.findById().exec();\n * }\n * }\n * ```\n *\n * ### Options\n *\n * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.\n * - `name` (String): model name.\n * - `collection` (String): collection (optional, induced from model name).\n * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).\n *\n * @param {MongooseModelOptions} options\n * @returns {(target: any) => void}\n * @decorator\n * @mongoose\n * @class\n */\nexport function Model(options: MongooseModelOptions = {}) {\n return (target: any) => {\n const {token, collectionName} = getModelToken(target, options);\n\n registerProvider({\n provide: token,\n deps: [],\n useFactory() {\n return getSchema(target, options as any);\n }\n });\n\n registerProvider({\n provide: target,\n type: \"mongoose:model\",\n deps: [MONGOOSE_CONNECTIONS, token],\n useFactory(connections: MONGOOSE_CONNECTIONS, schema: Schema) {\n applySchemaOptions(schema, schemaOptions(target));\n return createModel(\n target,\n schema,\n collectionName,\n options.collection,\n options.overwriteModels,\n connections.get(options.connection),\n options.discriminatorValue\n );\n }\n });\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {JsonDeserializerOptions} from \"@tsed/json-mapper\";\nimport {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport interface MongooseModel<T> extends Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>> {}\n"]}
1
+ {"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport type MongooseModel<T> = Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>>;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n skipInit?: boolean;\n discriminatorValue?: string;\n}\n"]}
1
+ {"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\nimport {CompileModelOptions} from \"mongoose\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n overwriteModels?: boolean;\n discriminatorValue?: string;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: object;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
1
+ {"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: Record<any, any>;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
@@ -35,11 +35,7 @@ registerProvider({
35
35
  const settings = mapOptions(configuration.get("mongoose"));
36
36
  let isDefault = true;
37
37
  for (const current of settings) {
38
- await mongooseService.connect(current.id, current.url, current.connectionOptions || {
39
- useCreateIndex: true,
40
- useNewUrlParser: true,
41
- useUnifiedTopology: true
42
- }, isDefault);
38
+ await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);
43
39
  isDefault = false;
44
40
  }
45
41
  return mongooseService;
@@ -1 +1 @@
1
- {"version":3,"file":"MongooseConnections.js","sourceRoot":"","sources":["../../../src/services/MongooseConnections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AACzD,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAE5D;;GAEG;AACH,yCAAyC;AACzC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAMvE,SAAS,UAAU,CAAC,OAA4E;IAC9F,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,CAAC;KACX;IAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACrB,MAAM,EAAC,GAAG,EAAE,iBAAiB,EAAC,GAAG,OAAO,IAAI,EAAE,CAAC;QAE/C,OAAO;YACL;gBACE,EAAE,EAAE,SAAS;gBACb,GAAG;gBACH,iBAAiB;aAClB;SACF,CAAC;KACH;IAED,OAAQ,OAAuC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC/D,OAAO;YACL,GAAG,QAAQ;YACX,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;SAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gBAAgB,CAAC;IACf,OAAO,EAAE,oBAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,aAAa,EAAE,eAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAC3B,OAAO,CAAC,EAAE,EACV,OAAO,CAAC,GAAG,EACX,OAAO,CAAC,iBAAiB,IAAI;gBAC3B,cAAc,EAAE,IAAI;gBACpB,eAAe,EAAE,IAAI;gBACrB,kBAAkB,EAAE,IAAI;aACzB,EACD,SAAS,CACV,CAAC;YAEF,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces/MongooseConnectionOptions\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(\n current.id,\n current.url,\n current.connectionOptions || {\n useCreateIndex: true,\n useNewUrlParser: true,\n useUnifiedTopology: true\n },\n isDefault\n );\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
1
+ {"version":3,"file":"MongooseConnections.js","sourceRoot":"","sources":["../../../src/services/MongooseConnections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AACzD,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAE5D;;GAEG;AACH,yCAAyC;AACzC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAMvE,SAAS,UAAU,CAAC,OAA4E;IAC9F,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,EAAE,CAAC;KACX;IAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACrB,MAAM,EAAC,GAAG,EAAE,iBAAiB,EAAC,GAAG,OAAO,IAAI,EAAE,CAAC;QAE/C,OAAO;YACL;gBACE,EAAE,EAAE,SAAS;gBACb,GAAG;gBACH,iBAAiB;aAClB;SACF,CAAC;KACH;IAED,OAAQ,OAAuC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC/D,OAAO;YACL,GAAG,QAAQ;YACX,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;SAC9C,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gBAAgB,CAAC;IACf,OAAO,EAAE,oBAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,aAAa,EAAE,eAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,IAAI,EAAE,EAAE,SAAS,CAAC,CAAC;YAEnG,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces/MongooseConnectionOptions\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
@@ -16,12 +16,12 @@ export function getModelToken(target, options) {
16
16
  * @param {"mongoose".Schema} schema Schema that will be attached to the model.
17
17
  * @param name model name
18
18
  * @param collection (optional, induced from model name)
19
- * @param skipInit whether to skip initialization (defaults to false)
19
+ * @param overwriteModels
20
20
  * @param connection
21
21
  * @param discriminatorValue
22
22
  * @returns {Model<T extends Document>}
23
23
  */
24
- export function createModel(target, schema, name = nameOf(target), collection, skipInit, connection, discriminatorValue) {
24
+ export function createModel(target, schema, name = nameOf(target), collection, overwriteModels, connection, discriminatorValue) {
25
25
  // if ancestor has a discriminatorKey we need to link model with anscestor schema
26
26
  const ancestor = ancestorsOf(target).find((ancestor) => {
27
27
  const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};
@@ -39,13 +39,9 @@ export function createModel(target, schema, name = nameOf(target), collection, s
39
39
  }
40
40
  }
41
41
  }
42
- /* istanbul ignore else */
43
- if (connection) {
44
- const model = connection.model(name, schema, collection);
45
- Store.from(target).set(MONGOOSE_MODEL, model);
46
- return model;
47
- }
48
- const model = mongoose.model(name, schema, collection, skipInit);
42
+ const opts = overwriteModels ? { overwriteModels } : undefined;
43
+ const c = connection || mongoose;
44
+ const model = c.model(name, schema, collection, opts);
49
45
  Store.from(target).set(MONGOOSE_MODEL, model);
50
46
  return model;
51
47
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAC5D,OAAO,QAA8C,MAAM,UAAU,CAAC;AACtE,OAAO,EAAC,cAAc,EAAE,mBAAmB,EAAE,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AACpG,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EA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ACL,OAAO,aAAa,CAAC,aAAa,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;aAC/D;SACF;KACF;IAED,0BAA0B;IAC1B,IAAI,UAAU,EAAE;QACd,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;QACzD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;QAC9C,OAAO,KAAK,CAAC;KACd;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;IACjE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param skipInit whether to skip initialization (defaults to false)\n * @param connection\n * @param discriminatorValue\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n skipInit?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n /* istanbul ignore else */\n if (connection) {\n const model = connection.model(name, schema, collection);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n }\n\n const model = mongoose.model(name, schema, collection, skipInit);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n}\n"]}
1
+ {"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAC5D,OAAO,QAAmE,MAAM,UAAU,CAAC;AAC3F,OAAO,EAAC,cAAc,EAAE,mBAAmB,EAAE,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AACpG,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAC,cAAc,EAAC,MAAM,gBAAgB,CAAC;AAE9C,MAAM,UAAU,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,mBAAmB,EAAE,cAAc,CAAC,CAAC;IAC5D,cAAc,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAC,CA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C;IACtD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {CompileModelOptions, Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param overwriteModels\n * @param connection\n * @param discriminatorValue\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n overwriteModels?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n const opts = overwriteModels ? {overwriteModels} : undefined;\n const c = connection || mongoose;\n\n const model = c.model(name, schema, collection, opts);\n Store.from(target).set(MONGOOSE_MODEL, model);\n\n return model;\n}\n"]}
@@ -5,5 +5,4 @@ export declare type MongooseMergedDocument<T> = {
5
5
  export interface MongooseDocumentMethods<T> {
6
6
  toClass(): T;
7
7
  }
8
- export interface MongooseModel<T> extends Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>> {
9
- }
8
+ export declare type MongooseModel<T> = Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>>;
@@ -3,6 +3,6 @@ export interface MongooseModelOptions extends MongooseSchemaOptions {
3
3
  name?: string;
4
4
  connection?: string;
5
5
  collection?: string;
6
- skipInit?: boolean;
6
+ overwriteModels?: boolean;
7
7
  discriminatorValue?: string;
8
8
  }
@@ -23,7 +23,7 @@ export interface MongoosePluginOptions {
23
23
  options?: Record<string, unknown>;
24
24
  }
25
25
  export interface MongooseIndexOptions {
26
- fields: object;
26
+ fields: Record<any, any>;
27
27
  options?: Record<string, unknown>;
28
28
  }
29
29
  export interface MongooseSchemaOptions {
@@ -11,9 +11,9 @@ export declare function getModelToken(target: Type<any>, options: any): {
11
11
  * @param {"mongoose".Schema} schema Schema that will be attached to the model.
12
12
  * @param name model name
13
13
  * @param collection (optional, induced from model name)
14
- * @param skipInit whether to skip initialization (defaults to false)
14
+ * @param overwriteModels
15
15
  * @param connection
16
16
  * @param discriminatorValue
17
17
  * @returns {Model<T extends Document>}
18
18
  */
19
- export declare function createModel<T>(target: any, schema: mongoose.Schema, name?: string, collection?: string, skipInit?: boolean, connection?: Connection, discriminatorValue?: string): mongoose.Model<mongoose.Document<any, {}>, {}> | mongoose.Model<any, {}>;
19
+ export declare function createModel<T>(target: any, schema: mongoose.Schema, name?: string, collection?: string, overwriteModels?: boolean, connection?: Connection, discriminatorValue?: string): mongoose.Model<any, any, any, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/mongoose",
3
- "version": "6.107.5",
3
+ "version": "6.108.0",
4
4
  "description": "Mongoose package for Ts.ED framework",
5
5
  "private": false,
6
6
  "keywords": [
@@ -29,25 +29,25 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "change-case": "4.1.2",
32
- "tslib": "2.2.0"
32
+ "tslib": "2.3.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@tsed/ajv": "6.107.5",
36
- "@tsed/core": "6.107.5",
37
- "@tsed/di": "6.107.5",
38
- "@tsed/json-mapper": "6.107.5",
39
- "@tsed/logger": ">=6.1.0",
40
- "@tsed/schema": "6.107.5",
41
- "mongoose": "^5.12.2"
35
+ "@tsed/ajv": "6.108.0",
36
+ "@tsed/core": "6.108.0",
37
+ "@tsed/di": "6.108.0",
38
+ "@tsed/json-mapper": "6.108.0",
39
+ "@tsed/logger": ">=6.1.1",
40
+ "@tsed/schema": "6.108.0",
41
+ "mongoose": "^6.1.7"
42
42
  },
43
43
  "peerDependencies": {
44
- "@tsed/ajv": "^6.107.5",
45
- "@tsed/core": "^6.107.5",
46
- "@tsed/di": "^6.107.5",
47
- "@tsed/json-mapper": "^6.107.5",
48
- "@tsed/logger": ">=6.1.0",
49
- "@tsed/schema": "^6.107.5",
50
- "mongoose": ">=5.12.2"
44
+ "@tsed/ajv": "^6.108.0",
45
+ "@tsed/core": "^6.108.0",
46
+ "@tsed/di": "^6.108.0",
47
+ "@tsed/json-mapper": "^6.108.0",
48
+ "@tsed/logger": ">=6.1.1",
49
+ "@tsed/schema": "^6.108.0",
50
+ "mongoose": ">=6.0.0"
51
51
  },
52
52
  "repository": "https://github.com/tsedio/tsed",
53
53
  "bugs": {