@tsed/mongoose 6.69.0-rc.3 → 6.69.2
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/.mocharc.js +5 -0
- package/.nycrc +26 -0
- package/lib/index.js +1402 -10
- package/lib/index.js.map +1 -1
- package/lib/index.modern.js +1362 -0
- package/lib/index.modern.js.map +1 -0
- package/package.json +20 -19
- package/lib/MongooseModule.js +0 -36
- package/lib/MongooseModule.js.map +0 -1
- package/lib/constants/index.js +0 -24
- package/lib/constants/index.js.map +0 -1
- package/lib/decorators/auto.js +0 -28
- package/lib/decorators/auto.js.map +0 -1
- package/lib/decorators/discriminatorKey.js +0 -12
- package/lib/decorators/discriminatorKey.js.map +0 -1
- package/lib/decorators/dynamicRef.js +0 -46
- package/lib/decorators/dynamicRef.js.map +0 -1
- package/lib/decorators/excludeIndexes.js +0 -28
- package/lib/decorators/excludeIndexes.js.map +0 -1
- package/lib/decorators/expires.js +0 -28
- package/lib/decorators/expires.js.map +0 -1
- package/lib/decorators/immutable.js +0 -30
- package/lib/decorators/immutable.js.map +0 -1
- package/lib/decorators/index.js +0 -30
- package/lib/decorators/index.js.map +0 -1
- package/lib/decorators/indexed.js +0 -26
- package/lib/decorators/indexed.js.map +0 -1
- package/lib/decorators/lowercase.js +0 -28
- package/lib/decorators/lowercase.js.map +0 -1
- package/lib/decorators/model.js +0 -67
- package/lib/decorators/model.js.map +0 -1
- package/lib/decorators/mongooseIndex.js +0 -39
- package/lib/decorators/mongooseIndex.js.map +0 -1
- package/lib/decorators/mongooseIndexes.d.ts +0 -32
- package/lib/decorators/mongooseIndexes.js +0 -39
- package/lib/decorators/mongooseIndexes.js.map +0 -1
- package/lib/decorators/mongoosePlugin.js +0 -22
- package/lib/decorators/mongoosePlugin.js.map +0 -1
- package/lib/decorators/numberDecimal.js +0 -75
- package/lib/decorators/numberDecimal.js.map +0 -1
- package/lib/decorators/objectID.js +0 -26
- package/lib/decorators/objectID.js.map +0 -1
- package/lib/decorators/postHook.js +0 -26
- package/lib/decorators/postHook.js.map +0 -1
- package/lib/decorators/preHook.js +0 -71
- package/lib/decorators/preHook.js.map +0 -1
- package/lib/decorators/ref.js +0 -65
- package/lib/decorators/ref.js.map +0 -1
- package/lib/decorators/schema.js +0 -32
- package/lib/decorators/schema.js.map +0 -1
- package/lib/decorators/schemaIgnore.js +0 -27
- package/lib/decorators/schemaIgnore.js.map +0 -1
- package/lib/decorators/select.js +0 -28
- package/lib/decorators/select.js.map +0 -1
- package/lib/decorators/sparse.js +0 -30
- package/lib/decorators/sparse.js.map +0 -1
- package/lib/decorators/text.js +0 -29
- package/lib/decorators/text.js.map +0 -1
- package/lib/decorators/trim.js +0 -27
- package/lib/decorators/trim.js.map +0 -1
- package/lib/decorators/unique.js +0 -27
- package/lib/decorators/unique.js.map +0 -1
- package/lib/decorators/uppercase.js +0 -28
- package/lib/decorators/uppercase.js.map +0 -1
- package/lib/decorators/versionKey.js +0 -12
- package/lib/decorators/versionKey.js.map +0 -1
- package/lib/decorators/virtualRef.js +0 -36
- package/lib/decorators/virtualRef.js.map +0 -1
- package/lib/interfaces/MongooseConnectionOptions.js +0 -3
- package/lib/interfaces/MongooseConnectionOptions.js.map +0 -1
- package/lib/interfaces/MongooseDocument.js +0 -3
- package/lib/interfaces/MongooseDocument.js.map +0 -1
- package/lib/interfaces/MongooseModel.js +0 -3
- package/lib/interfaces/MongooseModel.js.map +0 -1
- package/lib/interfaces/MongooseModelOptions.js +0 -3
- package/lib/interfaces/MongooseModelOptions.js.map +0 -1
- package/lib/interfaces/MongooseSchemaOptions.js +0 -3
- package/lib/interfaces/MongooseSchemaOptions.js.map +0 -1
- package/lib/interfaces/MongooseSchemaTypes.js +0 -10
- package/lib/interfaces/MongooseSchemaTypes.js.map +0 -1
- package/lib/interfaces/MongooseVirtualRefOptions.js +0 -3
- package/lib/interfaces/MongooseVirtualRefOptions.js.map +0 -1
- package/lib/interfaces/index.js +0 -10
- package/lib/interfaces/index.js.map +0 -1
- package/lib/registries/MongooseModels.js +0 -5
- package/lib/registries/MongooseModels.js.map +0 -1
- package/lib/services/MongooseConnections.js +0 -51
- package/lib/services/MongooseConnections.js.map +0 -1
- package/lib/services/MongooseService.js +0 -73
- package/lib/services/MongooseService.js.map +0 -1
- package/lib/utils/createModel.js +0 -57
- package/lib/utils/createModel.js.map +0 -1
- package/lib/utils/createSchema.js +0 -160
- package/lib/utils/createSchema.js.map +0 -1
- package/lib/utils/index.js +0 -6
- package/lib/utils/index.js.map +0 -1
- package/lib/utils/resolveRefType.js +0 -19
- package/lib/utils/resolveRefType.js.map +0 -1
- package/lib/utils/schemaOptions.js +0 -60
- package/lib/utils/schemaOptions.js.map +0 -1
package/lib/decorators/sparse.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Sparse = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
/**
|
|
6
|
-
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
|
|
7
|
-
* disallow changes to this path once the document is saved to the database for the first time. Read more
|
|
8
|
-
* about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
|
|
9
|
-
*
|
|
10
|
-
* ### Example
|
|
11
|
-
*
|
|
12
|
-
* ```typescript
|
|
13
|
-
* @Model()
|
|
14
|
-
* export class EventModel {
|
|
15
|
-
* @Sparse()
|
|
16
|
-
* field: string;
|
|
17
|
-
* }
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @param {boolean | number} sparse
|
|
21
|
-
* @returns {Function}
|
|
22
|
-
* @decorator
|
|
23
|
-
* @mongoose
|
|
24
|
-
* @property
|
|
25
|
-
*/
|
|
26
|
-
function Sparse(sparse = true) {
|
|
27
|
-
return schema_1.MongooseSchema({ sparse });
|
|
28
|
-
}
|
|
29
|
-
exports.Sparse = Sparse;
|
|
30
|
-
//# sourceMappingURL=sparse.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sparse.js","sourceRoot":"","sources":["../../src/decorators/sparse.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,MAAM,CAAC,SAA2B,IAAI;IACpD,OAAO,uBAAc,CAAC,EAAC,MAAM,EAAQ,CAAC,CAAC;AACzC,CAAC;AAFD,wBAEC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will\n * disallow changes to this path once the document is saved to the database for the first time. Read more\n * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Sparse()\n * field: string;\n * }\n * ```\n *\n * @param {boolean | number} sparse\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Sparse(sparse: boolean | number = true): PropertyDecorator {\n return MongooseSchema({sparse} as any);\n}\n"]}
|
package/lib/decorators/text.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Text = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
/**
|
|
6
|
-
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
7
|
-
* will build a text index on this path.
|
|
8
|
-
*
|
|
9
|
-
* ### Example
|
|
10
|
-
*
|
|
11
|
-
* ```typescript
|
|
12
|
-
* @Model()
|
|
13
|
-
* export class EventModel {
|
|
14
|
-
* @Text()
|
|
15
|
-
* field: string;
|
|
16
|
-
* }
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @param {boolean | number | any} text
|
|
20
|
-
* @returns {Function}
|
|
21
|
-
* @decorator
|
|
22
|
-
* @mongoose
|
|
23
|
-
* @property
|
|
24
|
-
*/
|
|
25
|
-
function Text(text = true) {
|
|
26
|
-
return schema_1.MongooseSchema({ text });
|
|
27
|
-
}
|
|
28
|
-
exports.Text = Text;
|
|
29
|
-
//# sourceMappingURL=text.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/decorators/text.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,IAAI,CAAC,OAA+B,IAAI;IACtD,OAAO,uBAAc,CAAC,EAAC,IAAI,EAAQ,CAAC,CAAC;AACvC,CAAC;AAFD,oBAEC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose\n * will build a text index on this path.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Text()\n * field: string;\n * }\n * ```\n *\n * @param {boolean | number | any} text\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Text(text: boolean | number | any = true): PropertyDecorator {\n return MongooseSchema({text} as any);\n}\n"]}
|
package/lib/decorators/trim.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Trim = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
/**
|
|
6
|
-
* Tell Mongoose to make the property trimmable.
|
|
7
|
-
*
|
|
8
|
-
* ### Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* @Model()
|
|
12
|
-
* export class EventModel {
|
|
13
|
-
* @Trim()
|
|
14
|
-
* name: string;
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @returns {Function}
|
|
19
|
-
* @decorator
|
|
20
|
-
* @mongoose
|
|
21
|
-
* @property
|
|
22
|
-
*/
|
|
23
|
-
function Trim() {
|
|
24
|
-
return schema_1.MongooseSchema({ trim: true });
|
|
25
|
-
}
|
|
26
|
-
exports.Trim = Trim;
|
|
27
|
-
//# sourceMappingURL=trim.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"trim.js","sourceRoot":"","sources":["../../src/decorators/trim.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,IAAI;IAClB,OAAO,uBAAc,CAAC,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC;AACtC,CAAC;AAFD,oBAEC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Tell Mongoose to make the property trimmable.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Trim()\n * name: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Trim() {\n return MongooseSchema({trim: true});\n}\n"]}
|
package/lib/decorators/unique.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Unique = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
/**
|
|
6
|
-
* Tell Mongoose to ensure a unique index is created for this path.
|
|
7
|
-
*
|
|
8
|
-
* ### Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* @Model()
|
|
12
|
-
* export class EventModel {
|
|
13
|
-
* @Unique()
|
|
14
|
-
* index: string;
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @param {boolean | any} unique
|
|
19
|
-
* @returns {Function}
|
|
20
|
-
* @decorator
|
|
21
|
-
* @mongoose
|
|
22
|
-
*/
|
|
23
|
-
function Unique(unique = true) {
|
|
24
|
-
return schema_1.MongooseSchema({ unique });
|
|
25
|
-
}
|
|
26
|
-
exports.Unique = Unique;
|
|
27
|
-
//# sourceMappingURL=unique.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unique.js","sourceRoot":"","sources":["../../src/decorators/unique.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,MAAM,CAAC,SAAwB,IAAI;IACjD,OAAO,uBAAc,CAAC,EAAC,MAAM,EAAC,CAAC,CAAC;AAClC,CAAC;AAFD,wBAEC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * Tell Mongoose to ensure a unique index is created for this path.\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Unique()\n * index: string;\n * }\n * ```\n *\n * @param {boolean | any} unique\n * @returns {Function}\n * @decorator\n * @mongoose\n */\nexport function Unique(unique: boolean | any = true) {\n return MongooseSchema({unique});\n}\n"]}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Uppercase = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
/**
|
|
6
|
-
* If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().
|
|
7
|
-
*
|
|
8
|
-
* ### Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* @Model()
|
|
12
|
-
* export class EventModel {
|
|
13
|
-
* @Uppercase()
|
|
14
|
-
* field: string;
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @param {boolean} uppercase
|
|
19
|
-
* @returns {Function}
|
|
20
|
-
* @decorator
|
|
21
|
-
* @mongoose
|
|
22
|
-
* @property
|
|
23
|
-
*/
|
|
24
|
-
function Uppercase(uppercase = true) {
|
|
25
|
-
return schema_1.MongooseSchema({ uppercase });
|
|
26
|
-
}
|
|
27
|
-
exports.Uppercase = Uppercase;
|
|
28
|
-
//# sourceMappingURL=uppercase.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"uppercase.js","sourceRoot":"","sources":["../../src/decorators/uppercase.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,SAAS,CAAC,YAAqB,IAAI;IACjD,OAAO,uBAAc,CAAC,EAAC,SAAS,EAAQ,CAAC,CAAC;AAC5C,CAAC;AAFD,8BAEC","sourcesContent":["import {MongooseSchema} from \"./schema\";\n\n/**\n * If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * export class EventModel {\n * @Uppercase()\n * field: string;\n * }\n * ```\n *\n * @param {boolean} uppercase\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Uppercase(uppercase: boolean = true): PropertyDecorator {\n return MongooseSchema({uppercase} as any);\n}\n"]}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VersionKey = void 0;
|
|
4
|
-
const core_1 = require("@tsed/core");
|
|
5
|
-
const constants_1 = require("../constants");
|
|
6
|
-
function VersionKey() {
|
|
7
|
-
return (target, propertyKey) => {
|
|
8
|
-
core_1.Store.from(target).merge(constants_1.MONGOOSE_SCHEMA_OPTIONS, { versionKey: propertyKey });
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
exports.VersionKey = VersionKey;
|
|
12
|
-
//# sourceMappingURL=versionKey.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"versionKey.js","sourceRoot":"","sources":["../../src/decorators/versionKey.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AACjC,4CAAqD;AAErD,SAAgB,UAAU;IACxB,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC1C,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,mCAAuB,EAAE,EAAC,UAAU,EAAE,WAAW,EAAC,CAAC,CAAC;IAC/E,CAAC,CAAC;AACJ,CAAC;AAJD,gCAIC","sourcesContent":["import {Store} from \"@tsed/core\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants\";\n\nexport function VersionKey(): PropertyDecorator {\n return (target: any, propertyKey: string) => {\n Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {versionKey: propertyKey});\n };\n}\n"]}
|
|
@@ -1,36 +0,0 @@
|
|
|
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");
|
|
7
|
-
function getRef(opts) {
|
|
8
|
-
const ref = opts.ref || opts.type;
|
|
9
|
-
return core_1.isString(ref) ? ref : core_1.Store.from(ref).get(constants_1.MONGOOSE_MODEL_NAME);
|
|
10
|
-
}
|
|
11
|
-
function getType(opts) {
|
|
12
|
-
const ref = opts.ref || opts.type;
|
|
13
|
-
return !core_1.isString(ref) ? ref : Object;
|
|
14
|
-
}
|
|
15
|
-
function getInitialOpts(options, foreignField) {
|
|
16
|
-
return core_1.isPlainObject(options) ? options : { type: options, foreignField };
|
|
17
|
-
}
|
|
18
|
-
function mapToSchema(opts) {
|
|
19
|
-
const ref = getRef(opts);
|
|
20
|
-
const schema = {
|
|
21
|
-
ref,
|
|
22
|
-
localField: opts.localField || "_id",
|
|
23
|
-
foreignField: opts.foreignField,
|
|
24
|
-
justOne: opts.justOne || false,
|
|
25
|
-
options: opts.options
|
|
26
|
-
};
|
|
27
|
-
return schema;
|
|
28
|
-
}
|
|
29
|
-
function VirtualRef(options, foreignField) {
|
|
30
|
-
const opts = getInitialOpts(options, foreignField);
|
|
31
|
-
const schema = mapToSchema(opts);
|
|
32
|
-
const type = getType(opts);
|
|
33
|
-
return core_1.useDecorators(core_1.StoreMerge(constants_1.MONGOOSE_SCHEMA, schema), type && (schema.justOne ? schema_1.Property(type) : schema_1.CollectionOf(type)));
|
|
34
|
-
}
|
|
35
|
-
exports.VirtualRef = VirtualRef;
|
|
36
|
-
//# sourceMappingURL=virtualRef.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../src/decorators/virtualRef.ts"],"names":[],"mappings":";;;AAAA,qCAAqF;AACrF,yCAAoD;AACpD,4CAAkE;AAGlE,SAAS,MAAM,CAAC,IAAS;IACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC;IAElC,OAAO,eAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,YAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,+BAAmB,CAAC,CAAC;AACxE,CAAC;AAED,SAAS,OAAO,CAAC,IAAS;IACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC;IAElC,OAAO,CAAC,eAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACvC,CAAC;AAED,SAAS,cAAc,CAAC,OAA2C,EAAE,YAAqB;IACxF,OAAO,oBAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAC,IAAI,EAAE,OAAc,EAAE,YAAY,EAAC,CAAC;AACjF,CAAC;AAED,SAAS,WAAW,CAAC,IAAS;IAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAEzB,MAAM,MAAM,GAAQ;QAClB,GAAG;QACH,UAAU,EAAE,IAAI,CAAC,UAAU,IAAI,KAAK;QACpC,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,OAAO,EAAE,IAAI,CAAC,OAAO,IAAI,KAAK;QAC9B,OAAO,EAAE,IAAI,CAAC,OAAO;KACtB,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AA0BD,SAAgB,UAAU,CAAC,OAA2C,EAAE,YAAqB;IAC3F,MAAM,IAAI,GAAG,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3B,OAAO,oBAAa,CAAC,iBAAU,CAAC,2BAAe,EAAE,MAAM,CAAC,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,iBAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,qBAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC5H,CAAC;AAND,gCAMC","sourcesContent":["import {isPlainObject, isString, Store, StoreMerge, useDecorators} from \"@tsed/core\";\nimport {CollectionOf, Property} from \"@tsed/schema\";\nimport {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from \"../constants\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\n\nfunction getRef(opts: any) {\n const ref = opts.ref || opts.type;\n\n return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);\n}\n\nfunction getType(opts: any) {\n const ref = opts.ref || opts.type;\n\n return !isString(ref) ? ref : Object;\n}\n\nfunction getInitialOpts(options: string | MongooseVirtualRefOptions, foreignField?: string): any {\n return isPlainObject(options) ? options : {type: 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 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 * ### Example\n *\n * <<< @/tutorials/snippets/mongoose/virtual-references.ts\n *\n * @param type\n * @param foreignField\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function VirtualRef(type: 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(StoreMerge(MONGOOSE_SCHEMA, schema), type && (schema.justOne ? Property(type) : CollectionOf(type)));\n}\n\nexport type VirtualRef<T> = T | null;\nexport type VirtualRefs<T> = T[];\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseModel.js","sourceRoot":"","sources":["../../src/interfaces/MongooseModel.ts"],"names":[],"mappings":"","sourcesContent":["import {JsonDeserializerOptions} from \"@tsed/json-mapper\";\nimport {Document, Model} from \"mongoose\";\n\n// TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684\nexport type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};\n\nexport interface MongooseDocumentMethods<T> {\n toClass(): T;\n}\n\nexport interface MongooseModel<T> extends Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>> {}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseModelOptions.js","sourceRoot":"","sources":["../../src/interfaces/MongooseModelOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {MongooseSchemaOptions} from \"./MongooseSchemaOptions\";\n\nexport interface MongooseModelOptions extends MongooseSchemaOptions {\n name?: string;\n connection?: string;\n collection?: string;\n skipInit?: boolean;\n discriminatorValue?: string;\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {NativeError, Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: NativeError) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongoosePreHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: NativeError, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: NativeError) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>) => Promise<void> | void);\n\nexport interface MongoosePreHook<T = any> {\n method: string | RegExp;\n fn: MongoosePreHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePostHook<T = any> {\n method: string | RegExp;\n fn: MongoosePostHookCB<T>;\n options?: MongooseHookOptions;\n}\n\nexport interface MongoosePluginOptions {\n plugin: (schema: Schema, options?: any) => void;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseIndexOptions {\n fields: object;\n options?: Record<string, unknown>;\n}\n\nexport interface MongooseSchemaOptions {\n schemaOptions?: SchemaOptions;\n plugins?: MongoosePluginOptions[];\n indexes?: MongooseIndexOptions[];\n pre?: MongoosePreHook[];\n post?: MongoosePostHook[];\n}\n"]}
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
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 /**\n * @deprecated Since v6. Use ref instead.\n */\n type?: string | Type<any> | (() => Type<any>);\n ref?: string | Type<any> | (() => Type<any>);\n foreignField?: string;\n localField?: string;\n justOne?: boolean;\n options?: object;\n}\n"]}
|
package/lib/interfaces/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./MongooseConnectionOptions"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./MongooseDocument"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./MongooseModel"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./MongooseModelOptions"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./MongooseSchemaOptions"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./MongooseVirtualRefOptions"), exports);
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;AAUA,sEAA4C;AAC5C,6DAAmC;AACnC,0DAAgC;AAChC,iEAAuC;AACvC,kEAAwC;AACxC,sEAA4C","sourcesContent":["import {MongooseConnectionOptions} from \"./MongooseConnectionOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n mongoose: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[];\n }\n }\n}\n\nexport * from \"./MongooseConnectionOptions\";\nexport * from \"./MongooseDocument\";\nexport * from \"./MongooseModel\";\nexport * from \"./MongooseModelOptions\";\nexport * from \"./MongooseSchemaOptions\";\nexport * from \"./MongooseVirtualRefOptions\";\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1,51 +0,0 @@
|
|
|
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 (!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
|
-
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 || {
|
|
42
|
-
useCreateIndex: true,
|
|
43
|
-
useNewUrlParser: true,
|
|
44
|
-
useUnifiedTopology: true
|
|
45
|
-
}, isDefault);
|
|
46
|
-
isDefault = false;
|
|
47
|
-
}
|
|
48
|
-
return mongooseService;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
//# sourceMappingURL=MongooseConnections.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,cAAO,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,qBAAgB,CAAC;IACf,OAAO,EAAE,4BAAoB;IAC7B,UAAU,EAAE,KAAK;IACjB,IAAI,EAAE,CAAC,kBAAa,EAAE,iCAAe,CAAC;IACtC,KAAK,CAAC,eAAe,CAAC,aAA4B,EAAE,eAAgC;QAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAA0D,UAAU,CAAC,CAAC,CAAC;QACpH,IAAI,SAAS,GAAG,IAAI,CAAC;QAErB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,MAAM,eAAe,CAAC,OAAO,CAC3B,OAAO,CAAC,EAAE,EACV,OAAO,CAAC,GAAG,EACX,OAAO,CAAC,iBAAiB,IAAI;gBAC3B,cAAc,EAAE,IAAI;gBACpB,eAAe,EAAE,IAAI;gBACrB,kBAAkB,EAAE,IAAI;aACzB,EACD,SAAS,CACV,CAAC;YAEF,SAAS,GAAG,KAAK,CAAC;SACnB;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {Configuration, registerProvider} from \"@tsed/di\";\nimport {isArray} from \"@tsed/core\";\nimport {MongooseConnectionOptions} from \"../interfaces\";\nimport {MongooseService} from \"../services/MongooseService\";\n\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const MONGOOSE_CONNECTIONS = Symbol.for(\"MONGOOSE_CONNECTIONS\");\n/**\n * @ignore\n */\nexport type MONGOOSE_CONNECTIONS = MongooseService;\n\nfunction mapOptions(options: Omit<MongooseConnectionOptions, \"id\"> | MongooseConnectionOptions[]): MongooseConnectionOptions[] {\n if (!options) {\n return [];\n }\n\n if (!isArray(options)) {\n const {url, connectionOptions} = options || {};\n\n return [\n {\n id: \"default\",\n url,\n connectionOptions\n }\n ];\n }\n\n return (options as MongooseConnectionOptions[]).map((settings) => {\n return {\n ...settings,\n connectionOptions: settings.connectionOptions\n };\n });\n}\n\nregisterProvider({\n provide: MONGOOSE_CONNECTIONS,\n injectable: false,\n deps: [Configuration, MongooseService],\n async useAsyncFactory(configuration: Configuration, mongooseService: MongooseService) {\n const settings = mapOptions(configuration.get<MongooseConnectionOptions | MongooseConnectionOptions[]>(\"mongoose\"));\n let isDefault = true;\n\n for (const current of settings) {\n await mongooseService.connect(\n current.id,\n current.url,\n current.connectionOptions || {\n useCreateIndex: true,\n useNewUrlParser: true,\n useUnifiedTopology: true\n },\n isDefault\n );\n\n isDefault = false;\n }\n\n return mongooseService;\n }\n});\n"]}
|
|
@@ -1,73 +0,0 @@
|
|
|
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
|
-
di_1.Inject(),
|
|
67
|
-
tslib_1.__metadata("design:type", logger_1.Logger)
|
|
68
|
-
], MongooseService.prototype, "logger", void 0);
|
|
69
|
-
MongooseService = tslib_1.__decorate([
|
|
70
|
-
di_1.Injectable()
|
|
71
|
-
], MongooseService);
|
|
72
|
-
exports.MongooseService = MongooseService;
|
|
73
|
-
//# sourceMappingURL=MongooseService.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseService.js","sourceRoot":"","sources":["../../src/services/MongooseService.ts"],"names":[],"mappings":";;;;AAAA,iCAA4C;AAC5C,yCAAoC;AACpC,gEAAgC;AAGhC,uBAAuB;AACvB,SAAS,SAAS,CAAC,CAAM;IACvB,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAGD,IAAa,eAAe,GAA5B,MAAa,eAAe;IAA5B;QACW,gBAAW,GAAqC,IAAI,GAAG,EAAE,CAAC;QAC3D,sBAAiB,GAAW,SAAS,CAAC;IA0DhD,CAAC;IArDC;;;OAGG;IACH,KAAK,CAAC,OAAO,CAAC,EAAU,EAAE,GAAW,EAAE,iBAAiC,EAAE,SAAS,GAAG,KAAK;QACzF,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SAC5B;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QAEnE,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,kBAAQ,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;YACtF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;YAErC,IAAI,EAAE,KAAK,SAAS,IAAI,SAAS,EAAE;gBACjC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;aAC7B;YAED,OAAO,UAAU,CAAC;SACnB;QAAC,OAAO,EAAE,EAAE;YACX,0BAA0B;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACtB,0BAA0B;YAC1B,OAAO,CAAC,IAAI,EAAE,CAAC;SAChB;IACH,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,EAAW;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,KAAK,MAAM,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE;YACzD,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAvDC;IADC,WAAM,EAAE;sCACD,eAAM;+CAAC;AALJ,eAAe;IAD3B,eAAU,EAAE;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"]}
|
package/lib/utils/createModel.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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");
|
|
8
|
-
const MongooseModels_1 = require("../registries/MongooseModels");
|
|
9
|
-
const createSchema_1 = require("./createSchema");
|
|
10
|
-
function getModelToken(target, options) {
|
|
11
|
-
const { collectionName, token } = 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 skipInit whether to skip initialization (defaults to false)
|
|
25
|
-
* @param connection
|
|
26
|
-
* @returns {Model<T extends Document>}
|
|
27
|
-
*/
|
|
28
|
-
function createModel(target, schema, name = core_1.nameOf(target), collection, skipInit, connection, discriminatorValue) {
|
|
29
|
-
// if ancestor has a discriminatorKey we need to link model with anscestor schema
|
|
30
|
-
const ancestor = core_1.ancestorsOf(target).find((ancestor) => {
|
|
31
|
-
const options = core_1.Store.from(ancestor).get(constants_1.MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
32
|
-
return !!options.discriminatorKey;
|
|
33
|
-
});
|
|
34
|
-
if (ancestor && ancestor !== target) {
|
|
35
|
-
const ancestorModel = core_1.Store.from(ancestor).get(constants_1.MONGOOSE_MODEL);
|
|
36
|
-
if (ancestorModel) {
|
|
37
|
-
const discriminatorName = discriminatorValue || name;
|
|
38
|
-
if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {
|
|
39
|
-
return ancestorModel.discriminators[discriminatorName];
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
return ancestorModel.discriminator(discriminatorName, schema);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/* istanbul ignore else */
|
|
47
|
-
if (connection) {
|
|
48
|
-
const model = connection.model(name, schema, collection);
|
|
49
|
-
core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL, model);
|
|
50
|
-
return model;
|
|
51
|
-
}
|
|
52
|
-
const model = mongoose_1.default.model(name, schema, collection, skipInit);
|
|
53
|
-
core_1.Store.from(target).set(constants_1.MONGOOSE_MODEL, model);
|
|
54
|
-
return model;
|
|
55
|
-
}
|
|
56
|
-
exports.createModel = createModel;
|
|
57
|
-
//# sourceMappingURL=createModel.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA4D;AAC5D,gEAAsE;AACtE,4CAA0F;AAC1F,iEAA4D;AAC5D,iDAA8C;AAE9C,SAAgB,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,6BAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,+BAAmB,EAAE,cAAc,CAAC,CAAC;IAC5D,+BAAc,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;IAE3C,OAAO,EAAC,KAAK,EAAE,cAAc,EAAC,CAAC;AACjC,CAAC;AAPD,sCAOC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,WAAW,CACzB,MAAW,EACX,MAAuB,EACvB,OAAe,aAAM,CAAC,MAAM,CAAC,EAC7B,UAAmB,EACnB,QAAkB,EAClB,UAAuB,EACvB,kBAA2B;IAE3B,iFAAiF;IACjF,MAAM,QAAQ,GAAG,kBAAW,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACrD,MAAM,OAAO,GAAG,YAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,mCAAuB,CAAC,IAAI,EAAE,CAAC;QACxE,OAAO,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,IAAI,QAAQ,KAAK,MAAM,EAAE;QACnC,MAAM,aAAa,GAAG,YAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,0BAAc,CAA6C,CAAC;QAC3G,IAAI,aAAa,EAAE;YACjB,MAAM,iBAAiB,GAAG,kBAAkB,IAAI,IAAI,CAAC;YACrD,IAAI,aAAa,CAAC,cAAc,IAAI,aAAa,CAAC,cAAc,CAAC,iBAAiB,CAAC,EAAE;gBACnF,OAAO,aAAa,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;aACxD;iBAAM;gBACL,OAAO,aAAa,CAAC,aAAa,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;aAC/D;SACF;KACF;IAED,0BAA0B;IAC1B,IAAI,UAAU,EAAE;QACd,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;QACzD,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,0BAAc,EAAE,KAAK,CAAC,CAAC;QAC9C,OAAO,KAAK,CAAC;KACd;IAED,MAAM,KAAK,GAAG,kBAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;IACjE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,0BAAc,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,KAAK,CAAC;AACf,CAAC;AArCD,kCAqCC","sourcesContent":["import {ancestorsOf, nameOf, Store, Type} from \"@tsed/core\";\nimport mongoose, {Connection, Model as MongooseModel} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA_OPTIONS} from \"../constants\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\nimport {getSchemaToken} from \"./createSchema\";\n\nexport function getModelToken(target: Type<any>, options: any) {\n const {collectionName, token} = getSchemaToken(target, options);\n\n Store.from(target).set(MONGOOSE_MODEL_NAME, collectionName);\n MongooseModels.set(collectionName, target);\n\n return {token, collectionName};\n}\n\n/**\n * Create an instance of mongoose.model from a class.\n *\n * @param {Type<any>} target Class attached to the schema and model.\n * @param {\"mongoose\".Schema} schema Schema that will be attached to the model.\n * @param name model name\n * @param collection (optional, induced from model name)\n * @param skipInit whether to skip initialization (defaults to false)\n * @param connection\n * @returns {Model<T extends Document>}\n */\nexport function createModel<T>(\n target: any,\n schema: mongoose.Schema,\n name: string = nameOf(target),\n collection?: string,\n skipInit?: boolean,\n connection?: Connection,\n discriminatorValue?: string\n) {\n // if ancestor has a discriminatorKey we need to link model with anscestor schema\n const ancestor = ancestorsOf(target).find((ancestor) => {\n const options = Store.from(ancestor).get(MONGOOSE_SCHEMA_OPTIONS) || {};\n return !!options.discriminatorKey;\n });\n\n if (ancestor && ancestor !== target) {\n const ancestorModel = Store.from(ancestor).get(MONGOOSE_MODEL) as MongooseModel<typeof target> | undefined;\n if (ancestorModel) {\n const discriminatorName = discriminatorValue || name;\n if (ancestorModel.discriminators && ancestorModel.discriminators[discriminatorName]) {\n return ancestorModel.discriminators[discriminatorName];\n } else {\n return ancestorModel.discriminator(discriminatorName, schema);\n }\n }\n }\n\n /* istanbul ignore else */\n if (connection) {\n const model = connection.model(name, schema, collection);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n }\n\n const model = mongoose.model(name, schema, collection, skipInit);\n Store.from(target).set(MONGOOSE_MODEL, model);\n return model;\n}\n"]}
|