@tsed/mongoose 7.0.0-alpha.2 → 7.0.0-beta.11
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.
- package/jest.config.js +14 -0
- package/lib/cjs/MongooseModule.js +36 -0
- package/lib/cjs/MongooseModule.js.map +1 -0
- package/lib/cjs/constants/constants.js +24 -0
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/auto.js +28 -0
- package/lib/cjs/decorators/auto.js.map +1 -0
- package/lib/cjs/decorators/discriminatorKey.js +14 -0
- package/lib/cjs/decorators/discriminatorKey.js.map +1 -0
- package/lib/cjs/decorators/dynamicRef.js +65 -0
- package/lib/cjs/decorators/dynamicRef.js.map +1 -0
- package/lib/cjs/decorators/excludeIndexes.js +28 -0
- package/lib/cjs/decorators/excludeIndexes.js.map +1 -0
- package/lib/cjs/decorators/expires.js +28 -0
- package/lib/cjs/decorators/expires.js.map +1 -0
- package/lib/cjs/decorators/immutable.js +30 -0
- package/lib/cjs/decorators/immutable.js.map +1 -0
- package/lib/cjs/decorators/indexed.js +26 -0
- package/lib/cjs/decorators/indexed.js.map +1 -0
- package/lib/cjs/decorators/lowercase.js +28 -0
- package/lib/cjs/decorators/lowercase.js.map +1 -0
- package/lib/cjs/decorators/model.js +68 -0
- package/lib/cjs/decorators/model.js.map +1 -0
- package/lib/cjs/decorators/mongooseIndex.js +39 -0
- package/lib/cjs/decorators/mongooseIndex.js.map +1 -0
- package/lib/cjs/decorators/mongooseIndexes.js +39 -0
- package/lib/cjs/decorators/mongooseIndexes.js.map +1 -0
- package/lib/cjs/decorators/mongoosePlugin.js +22 -0
- package/lib/cjs/decorators/mongoosePlugin.js.map +1 -0
- package/lib/cjs/decorators/numberDecimal.js +78 -0
- package/lib/cjs/decorators/numberDecimal.js.map +1 -0
- package/lib/cjs/decorators/objectID.js +26 -0
- package/lib/cjs/decorators/objectID.js.map +1 -0
- package/lib/cjs/decorators/postHook.js +26 -0
- package/lib/cjs/decorators/postHook.js.map +1 -0
- package/lib/cjs/decorators/preHook.js +71 -0
- package/lib/cjs/decorators/preHook.js.map +1 -0
- package/lib/cjs/decorators/ref.js +75 -0
- package/lib/cjs/decorators/ref.js.map +1 -0
- package/lib/cjs/decorators/schema.js +32 -0
- package/lib/cjs/decorators/schema.js.map +1 -0
- package/lib/cjs/decorators/schemaIgnore.js +27 -0
- package/lib/cjs/decorators/schemaIgnore.js.map +1 -0
- package/lib/cjs/decorators/select.js +28 -0
- package/lib/cjs/decorators/select.js.map +1 -0
- package/lib/cjs/decorators/sparse.js +30 -0
- package/lib/cjs/decorators/sparse.js.map +1 -0
- package/lib/cjs/decorators/text.js +29 -0
- package/lib/cjs/decorators/text.js.map +1 -0
- package/lib/cjs/decorators/trim.js +27 -0
- package/lib/cjs/decorators/trim.js.map +1 -0
- package/lib/cjs/decorators/unique.js +27 -0
- package/lib/cjs/decorators/unique.js.map +1 -0
- package/lib/cjs/decorators/uppercase.js +28 -0
- package/lib/cjs/decorators/uppercase.js.map +1 -0
- package/lib/cjs/decorators/versionKey.js +12 -0
- package/lib/cjs/decorators/versionKey.js.map +1 -0
- package/lib/cjs/decorators/virtualRef.js +38 -0
- package/lib/cjs/decorators/virtualRef.js.map +1 -0
- package/lib/cjs/index.js +51 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/MongooseConnectionOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseConnectionOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseDocument.js +3 -0
- package/lib/cjs/interfaces/MongooseDocument.js.map +1 -0
- package/lib/cjs/interfaces/MongooseModel.js +3 -0
- package/lib/cjs/interfaces/MongooseModel.js.map +1 -0
- package/lib/cjs/interfaces/MongooseModelOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseModelOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseSchemaOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -0
- package/lib/cjs/interfaces/MongooseSchemaTypes.js +10 -0
- package/lib/cjs/interfaces/MongooseSchemaTypes.js.map +1 -0
- package/lib/cjs/interfaces/MongooseVirtualRefOptions.js +3 -0
- package/lib/cjs/interfaces/MongooseVirtualRefOptions.js.map +1 -0
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/registries/MongooseModels.js +5 -0
- package/lib/cjs/registries/MongooseModels.js.map +1 -0
- package/lib/cjs/services/MongooseConnections.js +47 -0
- package/lib/cjs/services/MongooseConnections.js.map +1 -0
- package/lib/cjs/services/MongooseService.js +73 -0
- package/lib/cjs/services/MongooseService.js.map +1 -0
- package/lib/cjs/utils/createModel.js +54 -0
- package/lib/cjs/utils/createModel.js.map +1 -0
- package/lib/cjs/utils/createSchema.js +160 -0
- package/lib/cjs/utils/createSchema.js.map +1 -0
- package/lib/cjs/utils/resolveRefType.js +19 -0
- package/lib/cjs/utils/resolveRefType.js.map +1 -0
- package/lib/cjs/utils/schemaOptions.js +60 -0
- package/lib/cjs/utils/schemaOptions.js.map +1 -0
- package/lib/esm/MongooseModule.js +33 -0
- package/lib/esm/MongooseModule.js.map +1 -0
- package/lib/esm/constants/constants.js +21 -0
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/auto.js +24 -0
- package/lib/esm/decorators/auto.js.map +1 -0
- package/lib/esm/decorators/discriminatorKey.js +10 -0
- package/lib/esm/decorators/discriminatorKey.js.map +1 -0
- package/lib/esm/decorators/dynamicRef.js +61 -0
- package/lib/esm/decorators/dynamicRef.js.map +1 -0
- package/lib/esm/decorators/excludeIndexes.js +24 -0
- package/lib/esm/decorators/excludeIndexes.js.map +1 -0
- package/lib/esm/decorators/expires.js +24 -0
- package/lib/esm/decorators/expires.js.map +1 -0
- package/lib/esm/decorators/immutable.js +26 -0
- package/lib/esm/decorators/immutable.js.map +1 -0
- package/lib/esm/decorators/indexed.js +22 -0
- package/lib/esm/decorators/indexed.js.map +1 -0
- package/lib/esm/decorators/lowercase.js +24 -0
- package/lib/esm/decorators/lowercase.js.map +1 -0
- package/lib/esm/decorators/model.js +64 -0
- package/lib/esm/decorators/model.js.map +1 -0
- package/lib/esm/decorators/mongooseIndex.js +35 -0
- package/lib/esm/decorators/mongooseIndex.js.map +1 -0
- package/lib/esm/decorators/mongooseIndexes.js +35 -0
- package/lib/esm/decorators/mongooseIndexes.js.map +1 -0
- package/lib/esm/decorators/mongoosePlugin.js +18 -0
- package/lib/esm/decorators/mongoosePlugin.js.map +1 -0
- package/lib/esm/decorators/numberDecimal.js +74 -0
- package/lib/esm/decorators/numberDecimal.js.map +1 -0
- package/lib/esm/decorators/objectID.js +22 -0
- package/lib/esm/decorators/objectID.js.map +1 -0
- package/lib/esm/decorators/postHook.js +22 -0
- package/lib/esm/decorators/postHook.js.map +1 -0
- package/lib/esm/decorators/preHook.js +67 -0
- package/lib/esm/decorators/preHook.js.map +1 -0
- package/lib/esm/decorators/ref.js +71 -0
- package/lib/esm/decorators/ref.js.map +1 -0
- package/lib/esm/decorators/schema.js +27 -0
- package/lib/esm/decorators/schema.js.map +1 -0
- package/lib/esm/decorators/schemaIgnore.js +23 -0
- package/lib/esm/decorators/schemaIgnore.js.map +1 -0
- package/lib/esm/decorators/select.js +24 -0
- package/lib/esm/decorators/select.js.map +1 -0
- package/lib/esm/decorators/sparse.js +26 -0
- package/lib/esm/decorators/sparse.js.map +1 -0
- package/lib/esm/decorators/text.js +25 -0
- package/lib/esm/decorators/text.js.map +1 -0
- package/lib/esm/decorators/trim.js +23 -0
- package/lib/esm/decorators/trim.js.map +1 -0
- package/lib/esm/decorators/unique.js +23 -0
- package/lib/esm/decorators/unique.js.map +1 -0
- package/lib/esm/decorators/uppercase.js +24 -0
- package/lib/esm/decorators/uppercase.js.map +1 -0
- package/lib/esm/decorators/versionKey.js +8 -0
- package/lib/esm/decorators/versionKey.js.map +1 -0
- package/lib/esm/decorators/virtualRef.js +34 -0
- package/lib/esm/decorators/virtualRef.js.map +1 -0
- package/lib/esm/index.js +48 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/MongooseConnectionOptions.js +2 -0
- package/lib/esm/interfaces/MongooseConnectionOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseDocument.js +2 -0
- package/lib/esm/interfaces/MongooseDocument.js.map +1 -0
- package/lib/esm/interfaces/MongooseModel.js +2 -0
- package/lib/esm/interfaces/MongooseModel.js.map +1 -0
- package/lib/esm/interfaces/MongooseModelOptions.js +2 -0
- package/lib/esm/interfaces/MongooseModelOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseSchemaOptions.js +2 -0
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -0
- package/lib/esm/interfaces/MongooseSchemaTypes.js +7 -0
- package/lib/esm/interfaces/MongooseSchemaTypes.js.map +1 -0
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js +2 -0
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +1 -0
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/registries/MongooseModels.js +2 -0
- package/lib/esm/registries/MongooseModels.js.map +1 -0
- package/lib/esm/services/MongooseConnections.js +44 -0
- package/lib/esm/services/MongooseConnections.js.map +1 -0
- package/lib/esm/services/MongooseService.js +70 -0
- package/lib/esm/services/MongooseService.js.map +1 -0
- package/lib/esm/utils/createModel.js +48 -0
- package/lib/esm/utils/createModel.js.map +1 -0
- package/lib/esm/utils/createSchema.js +151 -0
- package/lib/esm/utils/createSchema.js.map +1 -0
- package/lib/esm/utils/resolveRefType.js +15 -0
- package/lib/esm/utils/resolveRefType.js.map +1 -0
- package/lib/esm/utils/schemaOptions.js +54 -0
- package/lib/esm/utils/schemaOptions.js.map +1 -0
- package/lib/{MongooseModule.d.ts → types/MongooseModule.d.ts} +0 -0
- package/lib/{constants/index.d.ts → types/constants/constants.d.ts} +0 -0
- package/lib/{decorators → types/decorators}/auto.d.ts +0 -0
- package/lib/{decorators → types/decorators}/discriminatorKey.d.ts +0 -0
- package/lib/{decorators → types/decorators}/dynamicRef.d.ts +4 -2
- package/lib/{decorators → types/decorators}/excludeIndexes.d.ts +0 -0
- package/lib/{decorators → types/decorators}/expires.d.ts +0 -0
- package/lib/{decorators → types/decorators}/immutable.d.ts +0 -0
- package/lib/{decorators → types/decorators}/indexed.d.ts +0 -0
- package/lib/{decorators → types/decorators}/lowercase.d.ts +0 -0
- package/lib/{decorators → types/decorators}/model.d.ts +0 -0
- package/lib/{decorators → types/decorators}/mongooseIndex.d.ts +0 -0
- package/lib/types/decorators/mongooseIndexes.d.ts +32 -0
- package/lib/{decorators → types/decorators}/mongoosePlugin.d.ts +0 -0
- package/lib/{decorators → types/decorators}/numberDecimal.d.ts +0 -0
- package/lib/{decorators → types/decorators}/objectID.d.ts +0 -0
- package/lib/{decorators → types/decorators}/postHook.d.ts +1 -1
- package/lib/{decorators → types/decorators}/preHook.d.ts +1 -1
- package/lib/{decorators → types/decorators}/ref.d.ts +7 -2
- package/lib/{decorators → types/decorators}/schema.d.ts +1 -1
- package/lib/{decorators → types/decorators}/schemaIgnore.d.ts +0 -0
- package/lib/{decorators → types/decorators}/select.d.ts +0 -0
- package/lib/{decorators → types/decorators}/sparse.d.ts +0 -0
- package/lib/{decorators → types/decorators}/text.d.ts +0 -0
- package/lib/{decorators → types/decorators}/trim.d.ts +0 -0
- package/lib/{decorators → types/decorators}/unique.d.ts +0 -0
- package/lib/{decorators → types/decorators}/uppercase.d.ts +0 -0
- package/lib/{decorators → types/decorators}/versionKey.d.ts +0 -0
- package/lib/{decorators → types/decorators}/virtualRef.d.ts +0 -4
- package/lib/types/index.d.ts +47 -0
- package/lib/{interfaces → types/interfaces}/MongooseConnectionOptions.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseDocument.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseModel.d.ts +1 -2
- package/lib/{interfaces → types/interfaces}/MongooseModelOptions.d.ts +1 -1
- package/lib/{interfaces → types/interfaces}/MongooseSchemaOptions.d.ts +1 -1
- package/lib/{interfaces → types/interfaces}/MongooseSchemaTypes.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/MongooseVirtualRefOptions.d.ts +1 -0
- package/lib/{interfaces/index.d.ts → types/interfaces/interfaces.d.ts} +0 -6
- package/lib/{registries → types/registries}/MongooseModels.d.ts +0 -0
- package/lib/{services → types/services}/MongooseConnections.d.ts +0 -0
- package/lib/{services → types/services}/MongooseService.d.ts +0 -0
- package/lib/{utils → types/utils}/createModel.d.ts +3 -2
- package/lib/{utils → types/utils}/createSchema.d.ts +1 -1
- package/lib/{utils → types/utils}/resolveRefType.d.ts +0 -0
- package/lib/{utils → types/utils}/schemaOptions.d.ts +1 -1
- package/package.json +30 -27
- package/readme.md +124 -134
- package/tsconfig.compile.esm.json +13 -0
- package/.mocharc.js +0 -5
- package/.nycrc +0 -27
- package/lib/decorators/index.d.ts +0 -26
- package/lib/index.d.ts +0 -6
- package/lib/index.js +0 -1402
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -1362
- package/lib/index.modern.js.map +0 -1
- package/lib/utils/index.d.ts +0 -2
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./MongooseModule.js";
|
|
5
|
+
export * from "./constants/constants.js";
|
|
6
|
+
export * from "./decorators/auto.js";
|
|
7
|
+
export * from "./decorators/discriminatorKey.js";
|
|
8
|
+
export * from "./decorators/dynamicRef.js";
|
|
9
|
+
export * from "./decorators/excludeIndexes.js";
|
|
10
|
+
export * from "./decorators/expires.js";
|
|
11
|
+
export * from "./decorators/immutable.js";
|
|
12
|
+
export * from "./decorators/indexed.js";
|
|
13
|
+
export * from "./decorators/lowercase.js";
|
|
14
|
+
export * from "./decorators/model.js";
|
|
15
|
+
export * from "./decorators/mongooseIndex.js";
|
|
16
|
+
export * from "./decorators/mongooseIndexes.js";
|
|
17
|
+
export * from "./decorators/mongoosePlugin.js";
|
|
18
|
+
export * from "./decorators/numberDecimal.js";
|
|
19
|
+
export * from "./decorators/objectID.js";
|
|
20
|
+
export * from "./decorators/postHook.js";
|
|
21
|
+
export * from "./decorators/preHook.js";
|
|
22
|
+
export * from "./decorators/ref.js";
|
|
23
|
+
export * from "./decorators/schema.js";
|
|
24
|
+
export * from "./decorators/schemaIgnore.js";
|
|
25
|
+
export * from "./decorators/select.js";
|
|
26
|
+
export * from "./decorators/sparse.js";
|
|
27
|
+
export * from "./decorators/text.js";
|
|
28
|
+
export * from "./decorators/trim.js";
|
|
29
|
+
export * from "./decorators/unique.js";
|
|
30
|
+
export * from "./decorators/uppercase.js";
|
|
31
|
+
export * from "./decorators/versionKey.js";
|
|
32
|
+
export * from "./decorators/virtualRef.js";
|
|
33
|
+
export * from "./interfaces/MongooseConnectionOptions.js";
|
|
34
|
+
export * from "./interfaces/MongooseDocument.js";
|
|
35
|
+
export * from "./interfaces/MongooseModel.js";
|
|
36
|
+
export * from "./interfaces/MongooseModelOptions.js";
|
|
37
|
+
export * from "./interfaces/MongooseSchemaOptions.js";
|
|
38
|
+
export * from "./interfaces/MongooseSchemaTypes.js";
|
|
39
|
+
export * from "./interfaces/MongooseVirtualRefOptions.js";
|
|
40
|
+
export * from "./interfaces/interfaces.js";
|
|
41
|
+
export * from "./registries/MongooseModels.js";
|
|
42
|
+
export * from "./services/MongooseConnections.js";
|
|
43
|
+
export * from "./services/MongooseService.js";
|
|
44
|
+
export * from "./utils/createModel.js";
|
|
45
|
+
export * from "./utils/createSchema.js";
|
|
46
|
+
export * from "./utils/resolveRefType.js";
|
|
47
|
+
export * from "./utils/schemaOptions.js";
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","source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cesContent":["/**\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseConnectionOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseConnectionOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {ConnectOptions} from \"mongoose\";\n\nexport interface MongooseConnectionOptions {\n url: string;\n /**\n * The connection ID\n */\n id: string;\n connectionOptions?: ConnectOptions;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseDocument.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseDocument.ts"],"names":[],"mappings":"","sourcesContent":["import {Document} from \"mongoose\";\n\nexport type MongooseDocument<T> = T & Document;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
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"]}
|
|
@@ -0,0 +1 @@
|
|
|
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"]}
|
|
@@ -0,0 +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: 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"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export var MongooseSchemaTypes;
|
|
2
|
+
(function (MongooseSchemaTypes) {
|
|
3
|
+
MongooseSchemaTypes["OBJECT_ID"] = "ObjectId";
|
|
4
|
+
MongooseSchemaTypes["STRING"] = "String";
|
|
5
|
+
MongooseSchemaTypes["NUMBER"] = "Number";
|
|
6
|
+
})(MongooseSchemaTypes || (MongooseSchemaTypes = {}));
|
|
7
|
+
//# sourceMappingURL=MongooseSchemaTypes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseSchemaTypes.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaTypes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,6CAAsB,CAAA;IACtB,wCAAiB,CAAA;IACjB,wCAAiB,CAAA;AACnB,CAAC,EAJW,mBAAmB,KAAnB,mBAAmB,QAI9B","sourcesContent":["export enum MongooseSchemaTypes {\n OBJECT_ID = \"ObjectId\",\n STRING = \"String\",\n NUMBER = \"Number\"\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseVirtualRefOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseVirtualRefOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\n\nexport interface MongooseVirtualRefOptions {\n ref?: string | Type<any> | (() => Type<any>);\n foreignField?: string;\n localField?: string;\n justOne?: boolean;\n count?: boolean;\n options?: object;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseConnectionOptions} from \"./MongooseConnectionOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n mongoose: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[];\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseModels.js","sourceRoot":"","sources":["../../../src/registries/MongooseModels.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAsB,IAAI,GAAG,EAAE,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\n\nexport const MongooseModels: Map<string, Type> = new Map();\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Configuration, registerProvider } from "@tsed/di";
|
|
2
|
+
import { isArray } from "@tsed/core";
|
|
3
|
+
import { MongooseService } from "../services/MongooseService.js";
|
|
4
|
+
/**
|
|
5
|
+
* @ignore
|
|
6
|
+
*/
|
|
7
|
+
// tslint:disable-next-line:variable-name
|
|
8
|
+
export const MONGOOSE_CONNECTIONS = Symbol.for("MONGOOSE_CONNECTIONS");
|
|
9
|
+
function mapOptions(options) {
|
|
10
|
+
if (!options) {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
if (!isArray(options)) {
|
|
14
|
+
const { url, connectionOptions } = options || {};
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
id: "default",
|
|
18
|
+
url,
|
|
19
|
+
connectionOptions
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
return options.map((settings) => {
|
|
24
|
+
return {
|
|
25
|
+
...settings,
|
|
26
|
+
connectionOptions: settings.connectionOptions
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
registerProvider({
|
|
31
|
+
provide: MONGOOSE_CONNECTIONS,
|
|
32
|
+
injectable: false,
|
|
33
|
+
deps: [Configuration, MongooseService],
|
|
34
|
+
async useAsyncFactory(configuration, mongooseService) {
|
|
35
|
+
const settings = mapOptions(configuration.get("mongoose"));
|
|
36
|
+
let isDefault = true;
|
|
37
|
+
for (const current of settings) {
|
|
38
|
+
await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);
|
|
39
|
+
isDefault = false;
|
|
40
|
+
}
|
|
41
|
+
return mongooseService;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=MongooseConnections.js.map
|
|
@@ -0,0 +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,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"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Inject, Injectable } from "@tsed/di";
|
|
3
|
+
import { Logger } from "@tsed/logger";
|
|
4
|
+
import Mongoose from "mongoose";
|
|
5
|
+
// istanbul ignore next
|
|
6
|
+
function asPromise(c) {
|
|
7
|
+
return c && c.asPromise ? c.asPromise() : c;
|
|
8
|
+
}
|
|
9
|
+
let MongooseService = class MongooseService {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.connections = new Map();
|
|
12
|
+
this.defaultConnection = "default";
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<"mongoose".Connection>}
|
|
17
|
+
*/
|
|
18
|
+
async connect(id, url, connectionOptions, isDefault = false) {
|
|
19
|
+
if (this.has(id)) {
|
|
20
|
+
return await this.get(id);
|
|
21
|
+
}
|
|
22
|
+
this.logger.info(`Connect to mongo database: ${id}`);
|
|
23
|
+
this.logger.debug(`Url: ${url}`);
|
|
24
|
+
this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);
|
|
25
|
+
try {
|
|
26
|
+
const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));
|
|
27
|
+
this.connections.set(id, connection);
|
|
28
|
+
if (id === "default" || isDefault) {
|
|
29
|
+
this.defaultConnection = id;
|
|
30
|
+
}
|
|
31
|
+
return connection;
|
|
32
|
+
}
|
|
33
|
+
catch (er) {
|
|
34
|
+
/* istanbul ignore next */
|
|
35
|
+
this.logger.error(er);
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
process.exit();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @returns {"mongoose".Connection}
|
|
43
|
+
*/
|
|
44
|
+
get(id) {
|
|
45
|
+
return this.connections.get(id || this.defaultConnection);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param {string} id
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
has(id) {
|
|
53
|
+
return this.connections.has(id || this.defaultConnection);
|
|
54
|
+
}
|
|
55
|
+
async closeConnections() {
|
|
56
|
+
for (const [id, connection] of this.connections.entries()) {
|
|
57
|
+
await connection.close();
|
|
58
|
+
this.connections.delete(id);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
__decorate([
|
|
63
|
+
Inject(),
|
|
64
|
+
__metadata("design:type", Logger)
|
|
65
|
+
], MongooseService.prototype, "logger", void 0);
|
|
66
|
+
MongooseService = __decorate([
|
|
67
|
+
Injectable()
|
|
68
|
+
], MongooseService);
|
|
69
|
+
export { MongooseService };
|
|
70
|
+
//# sourceMappingURL=MongooseService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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ACjC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;aAC7B;YAED,OAAO,UAAU,CAAC;SACnB;QAAC,OAAO,EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,MAAM,EAAE;8BACD,MAAM;+CAAC;AALJ,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CA4D3B;SA5DY,eAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ancestorsOf, nameOf, Store } from "@tsed/core";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
import { MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS } from "../constants/constants.js";
|
|
4
|
+
import { MongooseModels } from "../registries/MongooseModels.js";
|
|
5
|
+
import { getSchemaToken } from "./createSchema.js";
|
|
6
|
+
export function getModelToken(target, options) {
|
|
7
|
+
const { collectionName, token } = getSchemaToken(target, options);
|
|
8
|
+
Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);
|
|
9
|
+
MongooseModels.set(collectionName, target);
|
|
10
|
+
return { token, collectionName };
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Create an instance of mongoose.model from a class.
|
|
14
|
+
*
|
|
15
|
+
* @param {Type<any>} target Class attached to the schema and model.
|
|
16
|
+
* @param {"mongoose".Schema} schema Schema that will be attached to the model.
|
|
17
|
+
* @param name model name
|
|
18
|
+
* @param collection (optional, induced from model name)
|
|
19
|
+
* @param overwriteModels
|
|
20
|
+
* @param connection
|
|
21
|
+
* @param discriminatorValue
|
|
22
|
+
* @returns {Model<T extends Document>}
|
|
23
|
+
*/
|
|
24
|
+
export function createModel(target, schema, name = nameOf(target), collection, overwriteModels, connection, discriminatorValue) {
|
|
25
|
+
// if ancestor has a discriminatorKey we need to link model with anscestor schema
|
|
26
|
+
const ancestor = ancestorsOf(target).find((ancestor) => {
|
|
27
|
+
const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
28
|
+
return !!options.discriminatorKey;
|
|
29
|
+
});
|
|
30
|
+
if (ancestor && ancestor !== target) {
|
|
31
|
+
const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL);
|
|
32
|
+
if (ancestorModel) {
|
|
33
|
+
const discriminatorName = discriminatorValue || name;
|
|
34
|
+
if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
|
|
35
|
+
return ancestorModel.discriminators[discriminatorName];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return ancestorModel.discriminator(discriminatorName, schema);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const opts = overwriteModels ? { overwriteModels } : undefined;
|
|
43
|
+
const c = connection || mongoose;
|
|
44
|
+
const model = c.model(name, schema, collection, opts);
|
|
45
|
+
Store.from(target).set(MONGOOSE_MODEL, model);
|
|
46
|
+
return model;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=createModel.js.map
|
|
@@ -0,0 +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,QAAmE,MAAM,UAAU,CAAC;AAC3F,OAAO,EAAC,cAAc,EAAE,mBAAmB,EAAE,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,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,QAAQ,CAAC;IAEjC,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;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"]}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { cleanObject, nameOf, Store } from "@tsed/core";
|
|
2
|
+
import { deserialize, serialize } from "@tsed/json-mapper";
|
|
3
|
+
import { getProperties } from "@tsed/schema";
|
|
4
|
+
import { pascalCase } from "change-case";
|
|
5
|
+
import mongoose from "mongoose";
|
|
6
|
+
import { MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS } from "../constants/constants.js";
|
|
7
|
+
import { resolveRefType } from "./resolveRefType.js";
|
|
8
|
+
import { schemaOptions } from "./schemaOptions.js";
|
|
9
|
+
const MONGOOSE_RESERVED_KEYS = ["_id"];
|
|
10
|
+
/**
|
|
11
|
+
* @ignore
|
|
12
|
+
*/
|
|
13
|
+
function setUpSchema({ schema, virtuals }, options) {
|
|
14
|
+
const mongooseSchema = new mongoose.Schema(schema, options);
|
|
15
|
+
for (const [key, options] of virtuals.entries()) {
|
|
16
|
+
mongooseSchema.virtual(key, options);
|
|
17
|
+
}
|
|
18
|
+
return mongooseSchema;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @ignore
|
|
22
|
+
*/
|
|
23
|
+
function isVirtualRef(target) {
|
|
24
|
+
return !!(target.ref && target.localField && target.foreignField);
|
|
25
|
+
}
|
|
26
|
+
function hasVersionField(schema, versionKey) {
|
|
27
|
+
// Check if versioning was disabled explicitly
|
|
28
|
+
if (!versionKey)
|
|
29
|
+
return false;
|
|
30
|
+
// Check for alternative version field in schema
|
|
31
|
+
return versionKey in schema.paths;
|
|
32
|
+
}
|
|
33
|
+
export function createSchema(target, options = {}) {
|
|
34
|
+
var _a;
|
|
35
|
+
const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
36
|
+
options.schemaOptions = { ...options.schemaOptions, ...schemaOptionsFromStore };
|
|
37
|
+
const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
|
|
38
|
+
schemaOptions(target, options);
|
|
39
|
+
const outputVersionKey = hasVersionField(schema, (_a = options.schemaOptions) === null || _a === void 0 ? void 0 : _a.versionKey);
|
|
40
|
+
schema.methods.toClass = function toClass() {
|
|
41
|
+
return deserialize(this.toObject({
|
|
42
|
+
versionKey: outputVersionKey,
|
|
43
|
+
flattenMaps: true
|
|
44
|
+
}), {
|
|
45
|
+
type: target,
|
|
46
|
+
useAlias: false,
|
|
47
|
+
additionalProperties: true,
|
|
48
|
+
groups: false
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
schema.methods.toJSON = function toJSON(options) {
|
|
52
|
+
return serialize(this.toClass(), options);
|
|
53
|
+
};
|
|
54
|
+
schema.loadClass(target);
|
|
55
|
+
Store.from(target).set(MONGOOSE_SCHEMA, schema);
|
|
56
|
+
return schema;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get a schema already created. If the schema doesn't exists in registry, it'll be created.
|
|
60
|
+
* @param target
|
|
61
|
+
* @param options
|
|
62
|
+
*/
|
|
63
|
+
export function getSchema(target, options = {}) {
|
|
64
|
+
const store = Store.from(target);
|
|
65
|
+
if (!store.has(MONGOOSE_SCHEMA)) {
|
|
66
|
+
createSchema(target, options);
|
|
67
|
+
}
|
|
68
|
+
return store.get(MONGOOSE_SCHEMA);
|
|
69
|
+
}
|
|
70
|
+
export function getSchemaToken(target, options) {
|
|
71
|
+
const collectionName = options.name || nameOf(target);
|
|
72
|
+
const token = Symbol.for(pascalCase(`${collectionName}Schema`));
|
|
73
|
+
return { token, collectionName };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @ignore
|
|
77
|
+
*/
|
|
78
|
+
export function buildMongooseSchema(target) {
|
|
79
|
+
const properties = getProperties(target, { withIgnoredProps: true, mongoose: true, groups: false });
|
|
80
|
+
const schema = { schema: {}, virtuals: new Map() };
|
|
81
|
+
properties.forEach((propertyMetadata, key) => {
|
|
82
|
+
if (MONGOOSE_RESERVED_KEYS.includes(key)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
|
|
86
|
+
const schemaTypeOptions = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};
|
|
87
|
+
if (schemaTypeOptions.schemaIgnore) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (schemaTypeOptions.ref) {
|
|
91
|
+
schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref);
|
|
92
|
+
}
|
|
93
|
+
if (isVirtualRef(schemaTypeOptions)) {
|
|
94
|
+
schema.virtuals.set(key, schemaTypeOptions);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
schema.schema[key] = createSchemaTypeOptions(propertyMetadata);
|
|
98
|
+
});
|
|
99
|
+
return schema;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @ignore
|
|
103
|
+
*/
|
|
104
|
+
export function createSchemaTypeOptions(propEntity) {
|
|
105
|
+
const key = propEntity.propertyKey;
|
|
106
|
+
const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};
|
|
107
|
+
let schemaTypeOptions = {
|
|
108
|
+
required: propEntity.required
|
|
109
|
+
? function () {
|
|
110
|
+
return propEntity.isRequired(this[key]);
|
|
111
|
+
}
|
|
112
|
+
: false
|
|
113
|
+
};
|
|
114
|
+
if (!propEntity.isClass) {
|
|
115
|
+
const jsonSchema = propEntity.itemSchema.toJSON();
|
|
116
|
+
const { minimum: min, maximum: max, minLength: minlength, maxLength: maxlength } = jsonSchema;
|
|
117
|
+
let match = jsonSchema.pattern;
|
|
118
|
+
if (match) {
|
|
119
|
+
match = new RegExp(match);
|
|
120
|
+
}
|
|
121
|
+
schemaTypeOptions = {
|
|
122
|
+
...schemaTypeOptions,
|
|
123
|
+
type: propEntity.type,
|
|
124
|
+
match: match,
|
|
125
|
+
min,
|
|
126
|
+
max,
|
|
127
|
+
minlength,
|
|
128
|
+
maxlength,
|
|
129
|
+
enum: jsonSchema["enum"],
|
|
130
|
+
default: jsonSchema["default"]
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
else if (!rawMongooseSchema.ref) {
|
|
134
|
+
// References are handled by the final merge
|
|
135
|
+
schemaTypeOptions = { ...schemaTypeOptions, type: getSchema(propEntity.type) };
|
|
136
|
+
}
|
|
137
|
+
schemaTypeOptions = cleanObject({ ...schemaTypeOptions, ...rawMongooseSchema });
|
|
138
|
+
if (propEntity.isCollection) {
|
|
139
|
+
if (propEntity.isArray) {
|
|
140
|
+
return [schemaTypeOptions];
|
|
141
|
+
}
|
|
142
|
+
// Can be a Map or a Set;
|
|
143
|
+
// Mongoose implements only Map;
|
|
144
|
+
if (propEntity.collectionType !== Map) {
|
|
145
|
+
throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
|
|
146
|
+
}
|
|
147
|
+
return { type: Map, of: schemaTypeOptions };
|
|
148
|
+
}
|
|
149
|
+
return schemaTypeOptions;
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=createSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../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UAAU,CAAC,MAAM,CAAC;YACxB,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SAC/B,CAAC;KACH;SAAM,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE;QACjC,4CAA4C;QAC5C,iBAAiB,GAAG,EAAC,GAAG,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAQ,EAAC,CAAC;KACrF;IAED,iBAAiB,GAAG,WAAW,CAAC,EAAC,GAAG,iBAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,IAAI,UAAU,CAAC,OAAO,EAAE;YACtB,OAAO,CAAC,iBAAiB,CAAC,CAAC;SAC5B;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,4BAA4B,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;SACpG;QAED,OAAO,EAAC,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,iBAAiB,EAAoC,CAAC;KAC9E;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC","sourcesContent":["import {cleanObject, nameOf, Store, Type} from \"@tsed/core\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {getProperties, JsonEntityStore} from \"@tsed/schema\";\nimport {pascalCase} from \"change-case\";\nimport mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from \"mongoose\";\nimport {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {resolveRefType} from \"./resolveRefType\";\nimport {schemaOptions} from \"./schemaOptions\";\n\nconst MONGOOSE_RESERVED_KEYS = [\"_id\"];\n\nexport interface MongooseSchemaMetadata {\n schema: SchemaDefinition;\n virtuals: Map<string, MongooseVirtualRefOptions>;\n}\n\n/**\n * @ignore\n */\nfunction setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {\n const mongooseSchema = new mongoose.Schema(schema, options);\n\n for (const [key, options] of virtuals.entries()) {\n mongooseSchema.virtual(key, options);\n }\n\n return mongooseSchema;\n}\n\n/**\n * @ignore\n */\nfunction isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {\n return !!(target.ref && target.localField && target.foreignField);\n}\n\nfunction hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {\n // Check if versioning was disabled explicitly\n if (!versionKey) return false;\n\n // Check for alternative version field in schema\n return (versionKey as string) in schema.paths;\n}\n\nexport function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {\n const schemaOptionsFromStore = Store.from(target).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};\n\n const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);\n\n schemaOptions(target, options);\n\n const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);\n\n schema.methods.toClass = function toClass() {\n return deserialize(\n this.toObject({\n versionKey: outputVersionKey,\n flattenMaps: true\n }),\n {\n type: target,\n useAlias: false,\n additionalProperties: true,\n groups: false\n }\n );\n };\n\n schema.methods.toJSON = function toJSON(options?: any) {\n return serialize((this as any).toClass(), options);\n };\n\n schema.loadClass(target);\n\n Store.from(target).set(MONGOOSE_SCHEMA, schema);\n\n return schema;\n}\n\n/**\n * Get a schema already created. If the schema doesn't exists in registry, it'll be created.\n * @param target\n * @param options\n */\nexport function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA)) {\n createSchema(target, options);\n }\n\n return store.get(MONGOOSE_SCHEMA);\n}\n\nexport function getSchemaToken(target: Type<any>, options?: any) {\n const collectionName = options.name || nameOf(target);\n const token = Symbol.for(pascalCase(`${collectionName}Schema`));\n\n return {token, collectionName};\n}\n\n/**\n * @ignore\n */\nexport function buildMongooseSchema(target: any): MongooseSchemaMetadata {\n const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});\n const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};\n\n properties.forEach((propertyMetadata, key) => {\n if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {\n return;\n }\n\n // Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.\n const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};\n\n if (schemaTypeOptions.schemaIgnore) {\n return;\n }\n\n if (schemaTypeOptions.ref) {\n schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);\n }\n\n if (isVirtualRef(schemaTypeOptions)) {\n schema.virtuals.set(key as string, schemaTypeOptions);\n\n return;\n }\n\n schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);\n });\n\n return schema;\n}\n\n/**\n * @ignore\n */\nexport function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): SchemaTypeOptions<T> | SchemaTypeOptions<T>[] {\n const key = propEntity.propertyKey;\n const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};\n\n let schemaTypeOptions: SchemaTypeOptions<T> = {\n required: propEntity.required\n ? function () {\n return propEntity.isRequired(this[key]);\n }\n : false\n };\n\n if (!propEntity.isClass) {\n const jsonSchema = propEntity.itemSchema.toJSON();\n const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;\n\n let match: string | RegExp = jsonSchema.pattern;\n if (match) {\n match = new RegExp(match);\n }\n\n schemaTypeOptions = {\n ...schemaTypeOptions,\n type: propEntity.type,\n match: match as RegExp,\n min,\n max,\n minlength,\n maxlength,\n enum: jsonSchema[\"enum\"],\n default: jsonSchema[\"default\"]\n };\n } else if (!rawMongooseSchema.ref) {\n // References are handled by the final merge\n schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};\n }\n\n schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});\n\n if (propEntity.isCollection) {\n if (propEntity.isArray) {\n return [schemaTypeOptions];\n }\n // Can be a Map or a Set;\n // Mongoose implements only Map;\n if (propEntity.collectionType !== Map) {\n throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);\n }\n\n return {type: Map, of: schemaTypeOptions} as unknown as SchemaTypeOptions<T>;\n }\n\n return schemaTypeOptions;\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isClass, isString, nameOf, Store } from "@tsed/core";
|
|
2
|
+
import { MONGOOSE_MODEL_NAME } from "../constants/constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
export function resolveRefType(type) {
|
|
7
|
+
if (isString(type)) {
|
|
8
|
+
return type;
|
|
9
|
+
}
|
|
10
|
+
if (!isClass(type)) {
|
|
11
|
+
type = type();
|
|
12
|
+
}
|
|
13
|
+
return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=resolveRefType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveRefType.js","sourceRoot":"","sources":["../../../src/utils/resolveRefType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAClE,OAAO,EAAC,mBAAmB,EAAC,MAAM,wBAAwB,CAAC;AAE3D;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAA4C;IACzE,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE;QAClB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QAClB,IAAI,GAAI,IAAY,EAAE,CAAC;KACxB;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import {isClass, isString, nameOf, Store, Type} from \"@tsed/core\";\nimport {MONGOOSE_MODEL_NAME} from \"../constants/constants\";\n\n/**\n * @ignore\n */\nexport function resolveRefType(type: string | (() => Type<any>) | Type<any>): string {\n if (isString(type)) {\n return type;\n }\n\n if (!isClass(type)) {\n type = (type as any)();\n }\n\n return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { deepMerge, Store } from "@tsed/core";
|
|
2
|
+
import { MONGOOSE_SCHEMA_OPTIONS } from "../constants/constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
export function schemaOptions(target, options) {
|
|
7
|
+
const store = Store.from(target);
|
|
8
|
+
if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {
|
|
9
|
+
store.set(MONGOOSE_SCHEMA_OPTIONS, {});
|
|
10
|
+
}
|
|
11
|
+
if (options) {
|
|
12
|
+
store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));
|
|
13
|
+
}
|
|
14
|
+
return store.get(MONGOOSE_SCHEMA_OPTIONS);
|
|
15
|
+
}
|
|
16
|
+
function mapHookArgs(hook) {
|
|
17
|
+
return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @ignore
|
|
21
|
+
*/
|
|
22
|
+
export function buildPreHook(fn) {
|
|
23
|
+
return fn.length === 2
|
|
24
|
+
? function (next) {
|
|
25
|
+
return fn(this, next);
|
|
26
|
+
}
|
|
27
|
+
: function (next, options) {
|
|
28
|
+
return fn(this, next, options);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @ignore
|
|
33
|
+
*/
|
|
34
|
+
export function applySchemaOptions(schema, options) {
|
|
35
|
+
if (options.plugins) {
|
|
36
|
+
options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));
|
|
37
|
+
}
|
|
38
|
+
if (options.indexes) {
|
|
39
|
+
options.indexes.forEach((item) => schema.index(item.fields, item.options));
|
|
40
|
+
}
|
|
41
|
+
if (options.pre) {
|
|
42
|
+
options.pre.forEach((item) => {
|
|
43
|
+
item = {
|
|
44
|
+
...item,
|
|
45
|
+
fn: buildPreHook(item.fn)
|
|
46
|
+
};
|
|
47
|
+
schema.pre(...mapHookArgs(item));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (options.post) {
|
|
51
|
+
options.post.forEach((item) => schema.post(...mapHookArgs(item)));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=schemaOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,OAAO,EAAC,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AAG/D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,MAAW,EAAE,OAA+B;IACxE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE;QACvC,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,EAAE,CAAC,CAAC;KACxC;IAED,IAAI,OAAO,EAAE;QACX,KAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAA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I,EAAE,EAAE;YAC3B,IAAI,GAAG;gBACL,GAAG,IAAI;gBACP,EAAE,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;aAC1B,CAAC;YAED,MAAM,CAAC,GAAW,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,EAAE;QAChB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAE,MAAM,CAAC,IAAY,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KAC5E;AACH,CAAC","sourcesContent":["import {deepMerge, Store} from \"@tsed/core\";\nimport {Schema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {MongooseNextCB, MongoosePostHook, MongoosePreHook, MongooseSchemaOptions} from \"../interfaces/MongooseSchemaOptions\";\n\n/**\n * @ignore\n */\nexport function schemaOptions(target: any, options?: MongooseSchemaOptions) {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, {});\n }\n\n if (options) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));\n }\n\n return store.get(MONGOOSE_SCHEMA_OPTIONS);\n}\n\nfunction mapHookArgs(hook: MongoosePreHook | MongoosePostHook): [string | RegExp, Function] {\n return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean) as any;\n}\n\n/**\n * @ignore\n */\nexport function buildPreHook(fn: Function) {\n return fn.length === 2\n ? function (next: MongooseNextCB) {\n return fn(this, next);\n }\n : function (next: MongooseNextCB, options: any) {\n return fn(this, next, options);\n };\n}\n\n/**\n * @ignore\n */\nexport function applySchemaOptions(schema: Schema, options: MongooseSchemaOptions) {\n if (options.plugins) {\n options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));\n }\n\n if (options.indexes) {\n options.indexes.forEach((item) => schema.index(item.fields, item.options));\n }\n\n if (options.pre) {\n options.pre.forEach((item) => {\n item = {\n ...item,\n fn: buildPreHook(item.fn)\n };\n\n (schema.pre as any)(...mapHookArgs(item));\n });\n }\n\n if (options.post) {\n options.post.forEach((item) => (schema.post as any)(...mapHookArgs(item)));\n }\n}\n"]}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|