@tsed/mongoose 7.0.0-alpha.1 → 7.0.0-beta.10
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 +29 -23
- 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
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VirtualRef = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
const constants_1 = require("../constants/constants");
|
|
7
|
+
const MongooseModels_1 = require("../registries/MongooseModels");
|
|
8
|
+
function getRef(opts) {
|
|
9
|
+
const ref = opts.ref;
|
|
10
|
+
return (0, core_1.isString)(ref) ? ref : core_1.Store.from(ref).get(constants_1.MONGOOSE_MODEL_NAME);
|
|
11
|
+
}
|
|
12
|
+
function getType(opts) {
|
|
13
|
+
const ref = opts.ref;
|
|
14
|
+
return !(0, core_1.isString)(ref) ? ref : MongooseModels_1.MongooseModels.get(ref) || (() => MongooseModels_1.MongooseModels.get(ref) || Object);
|
|
15
|
+
}
|
|
16
|
+
function getInitialOpts(options, foreignField) {
|
|
17
|
+
return (0, core_1.isPlainObject)(options) ? options : { ref: options, foreignField };
|
|
18
|
+
}
|
|
19
|
+
function mapToSchema(opts) {
|
|
20
|
+
const ref = getRef(opts);
|
|
21
|
+
const schema = {
|
|
22
|
+
ref,
|
|
23
|
+
localField: opts.localField || "_id",
|
|
24
|
+
foreignField: opts.foreignField,
|
|
25
|
+
justOne: opts.justOne || false,
|
|
26
|
+
count: opts.count || false,
|
|
27
|
+
options: opts.options
|
|
28
|
+
};
|
|
29
|
+
return schema;
|
|
30
|
+
}
|
|
31
|
+
function VirtualRef(options, foreignField) {
|
|
32
|
+
const opts = getInitialOpts(options, foreignField);
|
|
33
|
+
const schema = mapToSchema(opts);
|
|
34
|
+
const type = getType(opts);
|
|
35
|
+
return (0, core_1.useDecorators)((0, core_1.StoreMerge)(constants_1.MONGOOSE_SCHEMA, schema), schema.count ? (0, schema_1.Property)(Number) : type && (schema.justOne ? (0, schema_1.Property)(type) : (0, schema_1.CollectionOf)(type)));
|
|
36
|
+
}
|
|
37
|
+
exports.VirtualRef = VirtualRef;
|
|
38
|
+
//# sourceMappingURL=virtualRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../../src/decorators/virtualRef.ts"],"names":[],"mappings":";;;AAAA,qCAAqF;AACrF,yCAAoD;AACpD,sDAA4E;AAE5E,iEAA4D;AAE5D,SAAS,MAAM,CAAC,IAAS;IACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;IAErB,OAAO,IAAA,eAAQ,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,YAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG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,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAA,iBAAQ,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAA,iBAAQ,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAY,EAAC,IAAI,CAAC,CAAC,CACjG,CAAC;AACJ,CAAC;AATD,gCASC","sourcesContent":["import {isPlainObject, isString, Store, StoreMerge, useDecorators} from \"@tsed/core\";\nimport {CollectionOf, Property} from \"@tsed/schema\";\nimport {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction getRef(opts: any) {\n const ref = opts.ref;\n\n return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);\n}\n\nfunction getType(opts: any) {\n const ref = opts.ref;\n return !isString(ref) ? ref : MongooseModels.get(ref) || (() => MongooseModels.get(ref) || Object);\n}\n\nfunction getInitialOpts(options: string | MongooseVirtualRefOptions, foreignField?: string): any {\n return isPlainObject(options) ? options : {ref: options as any, foreignField};\n}\n\nfunction mapToSchema(opts: any) {\n const ref = getRef(opts);\n\n const schema: any = {\n ref,\n localField: opts.localField || \"_id\",\n foreignField: opts.foreignField,\n justOne: opts.justOne || false,\n count: opts.count || false,\n options: opts.options\n };\n\n return schema;\n}\n\n/**\n * Define a property as mongoose virtual reference to other Model (decorated with @Model).\n *\n * ::: warning\n * To avoid circular dependencies, do not use the virtual reference model in\n * anything except a type declaration. Using the virtual reference model will prevent\n * typescript transpiler from stripping away the import statement and cause a circular\n * import in node.\n * :::\n *\n * @param ref\n * @param foreignField\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function VirtualRef(ref: string, foreignField: string): Function;\nexport function VirtualRef(options: MongooseVirtualRefOptions): Function;\nexport function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function;\nexport function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function {\n const opts = getInitialOpts(options, foreignField);\n const schema = mapToSchema(opts);\n const type = getType(opts);\n\n return useDecorators(\n StoreMerge(MONGOOSE_SCHEMA, schema),\n schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type))\n );\n}\n\nexport type VirtualRef<T> = T | null;\nexport type VirtualRefs<T> = T[];\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Automatically generated by barrelsby.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
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
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"]}
|
|
@@ -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,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongooseSchemaTypes = void 0;
|
|
4
|
+
var MongooseSchemaTypes;
|
|
5
|
+
(function (MongooseSchemaTypes) {
|
|
6
|
+
MongooseSchemaTypes["OBJECT_ID"] = "ObjectId";
|
|
7
|
+
MongooseSchemaTypes["STRING"] = "String";
|
|
8
|
+
MongooseSchemaTypes["NUMBER"] = "Number";
|
|
9
|
+
})(MongooseSchemaTypes = exports.MongooseSchemaTypes || (exports.MongooseSchemaTypes = {}));
|
|
10
|
+
//# sourceMappingURL=MongooseSchemaTypes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongooseSchemaTypes.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaTypes.ts"],"names":[],"mappings":";;;AAAA,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,6CAAsB,CAAA;IACtB,wCAAiB,CAAA;IACjB,wCAAiB,CAAA;AACnB,CAAC,EAJW,mBAAmB,GAAnB,2BAAmB,KAAnB,2BAAmB,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,QAAA,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,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MONGOOSE_CONNECTIONS = void 0;
|
|
4
|
+
const di_1 = require("@tsed/di");
|
|
5
|
+
const core_1 = require("@tsed/core");
|
|
6
|
+
const MongooseService_1 = require("../services/MongooseService");
|
|
7
|
+
/**
|
|
8
|
+
* @ignore
|
|
9
|
+
*/
|
|
10
|
+
// tslint:disable-next-line:variable-name
|
|
11
|
+
exports.MONGOOSE_CONNECTIONS = Symbol.for("MONGOOSE_CONNECTIONS");
|
|
12
|
+
function mapOptions(options) {
|
|
13
|
+
if (!options) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
if (!(0, core_1.isArray)(options)) {
|
|
17
|
+
const { url, connectionOptions } = options || {};
|
|
18
|
+
return [
|
|
19
|
+
{
|
|
20
|
+
id: "default",
|
|
21
|
+
url,
|
|
22
|
+
connectionOptions
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
return options.map((settings) => {
|
|
27
|
+
return {
|
|
28
|
+
...settings,
|
|
29
|
+
connectionOptions: settings.connectionOptions
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
(0, di_1.registerProvider)({
|
|
34
|
+
provide: exports.MONGOOSE_CONNECTIONS,
|
|
35
|
+
injectable: false,
|
|
36
|
+
deps: [di_1.Configuration, MongooseService_1.MongooseService],
|
|
37
|
+
async useAsyncFactory(configuration, mongooseService) {
|
|
38
|
+
const settings = mapOptions(configuration.get("mongoose"));
|
|
39
|
+
let isDefault = true;
|
|
40
|
+
for (const current of settings) {
|
|
41
|
+
await mongooseService.connect(current.id, current.url, current.connectionOptions || {}, isDefault);
|
|
42
|
+
isDefault = false;
|
|
43
|
+
}
|
|
44
|
+
return mongooseService;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
//# sourceMappingURL=MongooseConnections.js.map
|
|
@@ -0,0 +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,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,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongooseService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const logger_1 = require("@tsed/logger");
|
|
7
|
+
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
8
|
+
// istanbul ignore next
|
|
9
|
+
function asPromise(c) {
|
|
10
|
+
return c && c.asPromise ? c.asPromise() : c;
|
|
11
|
+
}
|
|
12
|
+
let MongooseService = class MongooseService {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.connections = new Map();
|
|
15
|
+
this.defaultConnection = "default";
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @returns {Promise<"mongoose".Connection>}
|
|
20
|
+
*/
|
|
21
|
+
async connect(id, url, connectionOptions, isDefault = false) {
|
|
22
|
+
if (this.has(id)) {
|
|
23
|
+
return await this.get(id);
|
|
24
|
+
}
|
|
25
|
+
this.logger.info(`Connect to mongo database: ${id}`);
|
|
26
|
+
this.logger.debug(`Url: ${url}`);
|
|
27
|
+
this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);
|
|
28
|
+
try {
|
|
29
|
+
const connection = await asPromise(mongoose_1.default.createConnection(url, connectionOptions));
|
|
30
|
+
this.connections.set(id, connection);
|
|
31
|
+
if (id === "default" || isDefault) {
|
|
32
|
+
this.defaultConnection = id;
|
|
33
|
+
}
|
|
34
|
+
return connection;
|
|
35
|
+
}
|
|
36
|
+
catch (er) {
|
|
37
|
+
/* istanbul ignore next */
|
|
38
|
+
this.logger.error(er);
|
|
39
|
+
/* istanbul ignore next */
|
|
40
|
+
process.exit();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @returns {"mongoose".Connection}
|
|
46
|
+
*/
|
|
47
|
+
get(id) {
|
|
48
|
+
return this.connections.get(id || this.defaultConnection);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @param {string} id
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
has(id) {
|
|
56
|
+
return this.connections.has(id || this.defaultConnection);
|
|
57
|
+
}
|
|
58
|
+
async closeConnections() {
|
|
59
|
+
for (const [id, connection] of this.connections.entries()) {
|
|
60
|
+
await connection.close();
|
|
61
|
+
this.connections.delete(id);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
tslib_1.__decorate([
|
|
66
|
+
(0, di_1.Inject)(),
|
|
67
|
+
tslib_1.__metadata("design:type", logger_1.Logger)
|
|
68
|
+
], MongooseService.prototype, "logger", void 0);
|
|
69
|
+
MongooseService = tslib_1.__decorate([
|
|
70
|
+
(0, di_1.Injectable)()
|
|
71
|
+
], MongooseService);
|
|
72
|
+
exports.MongooseService = MongooseService;
|
|
73
|
+
//# sourceMappingURL=MongooseService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,IAAA,WAAM,GAAE;sCACD,eAAM;+CAAC;AALJ,eAAe;IAD3B,IAAA,eAAU,GAAE;GACA,eAAe,CA4D3B;AA5DY,0CAAe","sourcesContent":["import {Inject, Injectable} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport Mongoose from \"mongoose\";\nimport {ConnectOptions} from \"mongoose\";\n\n// istanbul ignore next\nfunction asPromise(c: any) {\n return c && c.asPromise ? c.asPromise() : c;\n}\n\n@Injectable()\nexport class MongooseService {\n readonly connections: Map<string, Mongoose.Connection> = new Map();\n private defaultConnection: string = \"default\";\n\n @Inject()\n logger: Logger;\n\n /**\n *\n * @returns {Promise<\"mongoose\".Connection>}\n */\n async connect(id: string, url: string, connectionOptions: ConnectOptions, isDefault = false): Promise<any> {\n if (this.has(id)) {\n return await this.get(id)!;\n }\n\n this.logger.info(`Connect to mongo database: ${id}`);\n this.logger.debug(`Url: ${url}`);\n this.logger.debug(`options: ${JSON.stringify(connectionOptions)}`);\n\n try {\n const connection = await asPromise(Mongoose.createConnection(url, connectionOptions));\n this.connections.set(id, connection);\n\n if (id === \"default\" || isDefault) {\n this.defaultConnection = id;\n }\n\n return connection;\n } catch (er) {\n /* istanbul ignore next */\n this.logger.error(er);\n /* istanbul ignore next */\n process.exit();\n }\n }\n\n /**\n *\n * @returns {\"mongoose\".Connection}\n */\n get(id?: string): Mongoose.Connection | undefined {\n return this.connections.get(id || this.defaultConnection);\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id?: string): boolean {\n return this.connections.has(id || this.defaultConnection);\n }\n\n async closeConnections() {\n for (const [id, connection] of this.connections.entries()) {\n await connection.close();\n this.connections.delete(id);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createModel = exports.getModelToken = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@tsed/core");
|
|
6
|
+
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
7
|
+
const constants_1 = require("../constants/constants");
|
|
8
|
+
const MongooseModels_1 = require("../registries/MongooseModels");
|
|
9
|
+
const createSchema_1 = require("./createSchema");
|
|
10
|
+
function getModelToken(target, options) {
|
|
11
|
+
const { collectionName, token } = (0, createSchema_1.getSchemaToken)(target, options);
|
|
12
|
+
core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL_NAME, collectionName);
|
|
13
|
+
MongooseModels_1.MongooseModels.set(collectionName, target);
|
|
14
|
+
return { token, collectionName };
|
|
15
|
+
}
|
|
16
|
+
exports.getModelToken = getModelToken;
|
|
17
|
+
/**
|
|
18
|
+
* Create an instance of mongoose.model from a class.
|
|
19
|
+
*
|
|
20
|
+
* @param {Type<any>} target Class attached to the schema and model.
|
|
21
|
+
* @param {"mongoose".Schema} schema Schema that will be attached to the model.
|
|
22
|
+
* @param name model name
|
|
23
|
+
* @param collection (optional, induced from model name)
|
|
24
|
+
* @param overwriteModels
|
|
25
|
+
* @param connection
|
|
26
|
+
* @param discriminatorValue
|
|
27
|
+
* @returns {Model<T extends Document>}
|
|
28
|
+
*/
|
|
29
|
+
function createModel(target, schema, name = (0, core_1.nameOf)(target), collection, overwriteModels, connection, discriminatorValue) {
|
|
30
|
+
// if ancestor has a discriminatorKey we need to link model with anscestor schema
|
|
31
|
+
const ancestor = (0, core_1.ancestorsOf)(target).find((ancestor) => {
|
|
32
|
+
const options = core_1.Store.from(ancestor).get(constants_1.MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
33
|
+
return !!options.discriminatorKey;
|
|
34
|
+
});
|
|
35
|
+
if (ancestor && ancestor !== target) {
|
|
36
|
+
const ancestorModel = core_1.Store.from(ancestor).get(constants_1.MONGOOSE_MODEL);
|
|
37
|
+
if (ancestorModel) {
|
|
38
|
+
const discriminatorName = discriminatorValue || name;
|
|
39
|
+
if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
|
|
40
|
+
return ancestorModel.discriminators[discriminatorName];
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return ancestorModel.discriminator(discriminatorName, schema);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const opts = overwriteModels ? { overwriteModels } : undefined;
|
|
48
|
+
const c = connection || mongoose_1.default;
|
|
49
|
+
const model = c.model(name, schema, collection, opts);
|
|
50
|
+
core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL, model);
|
|
51
|
+
return model;
|
|
52
|
+
}
|
|
53
|
+
exports.createModel = createModel;
|
|
54
|
+
//# sourceMappingURL=createModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D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AAC,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"]}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSchemaTypeOptions = exports.buildMongooseSchema = exports.getSchemaToken = exports.getSchema = exports.createSchema = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@tsed/core");
|
|
6
|
+
const json_mapper_1 = require("@tsed/json-mapper");
|
|
7
|
+
const schema_1 = require("@tsed/schema");
|
|
8
|
+
const change_case_1 = require("change-case");
|
|
9
|
+
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
10
|
+
const constants_1 = require("../constants/constants");
|
|
11
|
+
const resolveRefType_1 = require("./resolveRefType");
|
|
12
|
+
const schemaOptions_1 = require("./schemaOptions");
|
|
13
|
+
const MONGOOSE_RESERVED_KEYS = ["_id"];
|
|
14
|
+
/**
|
|
15
|
+
* @ignore
|
|
16
|
+
*/
|
|
17
|
+
function setUpSchema({ schema, virtuals }, options) {
|
|
18
|
+
const mongooseSchema = new mongoose_1.default.Schema(schema, options);
|
|
19
|
+
for (const [key, options] of virtuals.entries()) {
|
|
20
|
+
mongooseSchema.virtual(key, options);
|
|
21
|
+
}
|
|
22
|
+
return mongooseSchema;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @ignore
|
|
26
|
+
*/
|
|
27
|
+
function isVirtualRef(target) {
|
|
28
|
+
return !!(target.ref && target.localField && target.foreignField);
|
|
29
|
+
}
|
|
30
|
+
function hasVersionField(schema, versionKey) {
|
|
31
|
+
// Check if versioning was disabled explicitly
|
|
32
|
+
if (!versionKey)
|
|
33
|
+
return false;
|
|
34
|
+
// Check for alternative version field in schema
|
|
35
|
+
return versionKey in schema.paths;
|
|
36
|
+
}
|
|
37
|
+
function createSchema(target, options = {}) {
|
|
38
|
+
var _a;
|
|
39
|
+
const schemaOptionsFromStore = core_1.Store.from(target).get(constants_1.MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
40
|
+
options.schemaOptions = { ...options.schemaOptions, ...schemaOptionsFromStore };
|
|
41
|
+
const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
|
|
42
|
+
(0, schemaOptions_1.schemaOptions)(target, options);
|
|
43
|
+
const outputVersionKey = hasVersionField(schema, (_a = options.schemaOptions) === null || _a === void 0 ? void 0 : _a.versionKey);
|
|
44
|
+
schema.methods.toClass = function toClass() {
|
|
45
|
+
return (0, json_mapper_1.deserialize)(this.toObject({
|
|
46
|
+
versionKey: outputVersionKey,
|
|
47
|
+
flattenMaps: true
|
|
48
|
+
}), {
|
|
49
|
+
type: target,
|
|
50
|
+
useAlias: false,
|
|
51
|
+
additionalProperties: true,
|
|
52
|
+
groups: false
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
schema.methods.toJSON = function toJSON(options) {
|
|
56
|
+
return (0, json_mapper_1.serialize)(this.toClass(), options);
|
|
57
|
+
};
|
|
58
|
+
schema.loadClass(target);
|
|
59
|
+
core_1.Store.from(target).set(constants_1.MONGOOSE_SCHEMA, schema);
|
|
60
|
+
return schema;
|
|
61
|
+
}
|
|
62
|
+
exports.createSchema = createSchema;
|
|
63
|
+
/**
|
|
64
|
+
* Get a schema already created. If the schema doesn't exists in registry, it'll be created.
|
|
65
|
+
* @param target
|
|
66
|
+
* @param options
|
|
67
|
+
*/
|
|
68
|
+
function getSchema(target, options = {}) {
|
|
69
|
+
const store = core_1.Store.from(target);
|
|
70
|
+
if (!store.has(constants_1.MONGOOSE_SCHEMA)) {
|
|
71
|
+
createSchema(target, options);
|
|
72
|
+
}
|
|
73
|
+
return store.get(constants_1.MONGOOSE_SCHEMA);
|
|
74
|
+
}
|
|
75
|
+
exports.getSchema = getSchema;
|
|
76
|
+
function getSchemaToken(target, options) {
|
|
77
|
+
const collectionName = options.name || (0, core_1.nameOf)(target);
|
|
78
|
+
const token = Symbol.for((0, change_case_1.pascalCase)(`${collectionName}Schema`));
|
|
79
|
+
return { token, collectionName };
|
|
80
|
+
}
|
|
81
|
+
exports.getSchemaToken = getSchemaToken;
|
|
82
|
+
/**
|
|
83
|
+
* @ignore
|
|
84
|
+
*/
|
|
85
|
+
function buildMongooseSchema(target) {
|
|
86
|
+
const properties = (0, schema_1.getProperties)(target, { withIgnoredProps: true, mongoose: true, groups: false });
|
|
87
|
+
const schema = { schema: {}, virtuals: new Map() };
|
|
88
|
+
properties.forEach((propertyMetadata, key) => {
|
|
89
|
+
if (MONGOOSE_RESERVED_KEYS.includes(key)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
|
|
93
|
+
const schemaTypeOptions = propertyMetadata.store.get(constants_1.MONGOOSE_SCHEMA) || {};
|
|
94
|
+
if (schemaTypeOptions.schemaIgnore) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (schemaTypeOptions.ref) {
|
|
98
|
+
schemaTypeOptions.ref = (0, resolveRefType_1.resolveRefType)(schemaTypeOptions.ref);
|
|
99
|
+
}
|
|
100
|
+
if (isVirtualRef(schemaTypeOptions)) {
|
|
101
|
+
schema.virtuals.set(key, schemaTypeOptions);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
schema.schema[key] = createSchemaTypeOptions(propertyMetadata);
|
|
105
|
+
});
|
|
106
|
+
return schema;
|
|
107
|
+
}
|
|
108
|
+
exports.buildMongooseSchema = buildMongooseSchema;
|
|
109
|
+
/**
|
|
110
|
+
* @ignore
|
|
111
|
+
*/
|
|
112
|
+
function createSchemaTypeOptions(propEntity) {
|
|
113
|
+
const key = propEntity.propertyKey;
|
|
114
|
+
const rawMongooseSchema = propEntity.store.get(constants_1.MONGOOSE_SCHEMA) || {};
|
|
115
|
+
let schemaTypeOptions = {
|
|
116
|
+
required: propEntity.required
|
|
117
|
+
? function () {
|
|
118
|
+
return propEntity.isRequired(this[key]);
|
|
119
|
+
}
|
|
120
|
+
: false
|
|
121
|
+
};
|
|
122
|
+
if (!propEntity.isClass) {
|
|
123
|
+
const jsonSchema = propEntity.itemSchema.toJSON();
|
|
124
|
+
const { minimum: min, maximum: max, minLength: minlength, maxLength: maxlength } = jsonSchema;
|
|
125
|
+
let match = jsonSchema.pattern;
|
|
126
|
+
if (match) {
|
|
127
|
+
match = new RegExp(match);
|
|
128
|
+
}
|
|
129
|
+
schemaTypeOptions = {
|
|
130
|
+
...schemaTypeOptions,
|
|
131
|
+
type: propEntity.type,
|
|
132
|
+
match: match,
|
|
133
|
+
min,
|
|
134
|
+
max,
|
|
135
|
+
minlength,
|
|
136
|
+
maxlength,
|
|
137
|
+
enum: jsonSchema["enum"],
|
|
138
|
+
default: jsonSchema["default"]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
else if (!rawMongooseSchema.ref) {
|
|
142
|
+
// References are handled by the final merge
|
|
143
|
+
schemaTypeOptions = { ...schemaTypeOptions, type: getSchema(propEntity.type) };
|
|
144
|
+
}
|
|
145
|
+
schemaTypeOptions = (0, core_1.cleanObject)({ ...schemaTypeOptions, ...rawMongooseSchema });
|
|
146
|
+
if (propEntity.isCollection) {
|
|
147
|
+
if (propEntity.isArray) {
|
|
148
|
+
return [schemaTypeOptions];
|
|
149
|
+
}
|
|
150
|
+
// Can be a Map or a Set;
|
|
151
|
+
// Mongoose implements only Map;
|
|
152
|
+
if (propEntity.collectionType !== Map) {
|
|
153
|
+
throw new Error(`Invalid collection type. ${(0, core_1.nameOf)(propEntity.collectionType)} is not supported.`);
|
|
154
|
+
}
|
|
155
|
+
return { type: Map, of: schemaTypeOptions };
|
|
156
|
+
}
|
|
157
|
+
return schemaTypeOptions;
|
|
158
|
+
}
|
|
159
|
+
exports.createSchemaTypeOptions = createSchemaTypeOptions;
|
|
160
|
+
//# sourceMappingURL=createSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,mDAAyD;AACzD,yCAA4D;AAC5D,6CAAuC;AACvC,gEAA8F;AAC9F,sDAAgF;AAGhF,qDAAgD;AAChD,mDAA8C;AAE9C,MAAM,sBAAsB,GAAG,CAAC,KAAK,CAAC,CAAC;AAOvC;;GAEG;AACH,SAAS,WAAW,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAyB,EAAE,OAAuB;IACtF,MAAM,cAAc,GAAG,IAAI,kBAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5D,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;QAC/C,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACtC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,MAA0C;IAC9D,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,eAAe,CAAC,MAAuB,EAAE,UAA6B;IAC7E,8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;YACT,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;SAC3B;QAED,iBAAiB,GAAG;YAClB,GAAG,iBAAiB;YACpB,IAAI,EAAE,UAAU,CAAC,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"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveRefType = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const constants_1 = require("../constants/constants");
|
|
6
|
+
/**
|
|
7
|
+
* @ignore
|
|
8
|
+
*/
|
|
9
|
+
function resolveRefType(type) {
|
|
10
|
+
if ((0, core_1.isString)(type)) {
|
|
11
|
+
return type;
|
|
12
|
+
}
|
|
13
|
+
if (!(0, core_1.isClass)(type)) {
|
|
14
|
+
type = type();
|
|
15
|
+
}
|
|
16
|
+
return core_1.Store.from(type).get(constants_1.MONGOOSE_MODEL_NAME) || (0, core_1.nameOf)(type);
|
|
17
|
+
}
|
|
18
|
+
exports.resolveRefType = resolveRefType;
|
|
19
|
+
//# sourceMappingURL=resolveRefType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveRefType.js","sourceRoot":"","sources":["../../../src/utils/resolveRefType.ts"],"names":[],"mappings":";;;AAAA,qCAAkE;AAClE,sDAA2D;AAE3D;;GAEG;AACH,SAAgB,cAAc,CAAC,IAA4C;IACzE,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC,EAAE;QAClB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,IAAA,cAAO,EAAC,IAAI,CAAC,EAAE;QAClB,IAAI,GAAI,IAAY,EAAE,CAAC;KACxB;IAED,OAAO,YAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,+BAAmB,CAAC,IAAI,IAAA,aAAM,EAAC,IAAI,CAAC,CAAC;AACnE,CAAC;AAVD,wCAUC","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"]}
|