@tsed/mongoose 7.8.0 → 7.8.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/jest.config.js +4 -4
- package/lib/cjs/interfaces/MongooseSchemaOptions.js.map +1 -1
- package/lib/cjs/utils/createModel.js +4 -7
- package/lib/cjs/utils/createModel.js.map +1 -1
- package/lib/cjs/utils/schemaOptions.js +5 -5
- package/lib/cjs/utils/schemaOptions.js.map +1 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +1 -1
- package/lib/esm/utils/createModel.js +4 -7
- package/lib/esm/utils/createModel.js.map +1 -1
- package/lib/esm/utils/schemaOptions.js +5 -5
- package/lib/esm/utils/schemaOptions.js.map +1 -1
- package/lib/types/interfaces/MongooseSchemaOptions.d.ts +3 -2
- package/lib/types/utils/createModel.d.ts +0 -1
- package/lib/types/utils/schemaOptions.d.ts +2 -2
- package/package.json +11 -11
package/jest.config.js
CHANGED
|
@@ -5,10 +5,10 @@ module.exports = {
|
|
|
5
5
|
...require("@tsed/jest-config")(__dirname, "mongoose"),
|
|
6
6
|
coverageThreshold: {
|
|
7
7
|
global: {
|
|
8
|
-
statements: 99.
|
|
9
|
-
branches:
|
|
10
|
-
functions:
|
|
11
|
-
lines: 99.
|
|
8
|
+
statements: 99.5,
|
|
9
|
+
branches: 95.48,
|
|
10
|
+
functions: 100,
|
|
11
|
+
lines: 99.47
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: Error) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type
|
|
1
|
+
{"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: Error) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;\n\nexport type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | MongooseHookPromised;\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"]}
|
|
@@ -24,17 +24,14 @@ exports.getModelToken = getModelToken;
|
|
|
24
24
|
* @param collection (optional, induced from model name)
|
|
25
25
|
* @param overwriteModels
|
|
26
26
|
* @param connection
|
|
27
|
-
* @param discriminatorValue
|
|
28
27
|
* @returns {Model<T extends Document>}
|
|
29
28
|
*/
|
|
30
29
|
function createModel(target, schema, name = (0, core_1.nameOf)(target), collection, overwriteModels, connection) {
|
|
31
30
|
const entity = schema_1.JsonEntityStore.from(target);
|
|
32
|
-
if (entity.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return ancestorModel.discriminator(discriminatorName, schema);
|
|
37
|
-
}
|
|
31
|
+
if (entity.isDiscriminatorChild) {
|
|
32
|
+
const discriminatorName = entity.discriminatorAncestor.schema.discriminator().getDefaultValue(target);
|
|
33
|
+
const ancestorModel = entity.discriminatorAncestor.get(constants_1.MONGOOSE_MODEL);
|
|
34
|
+
return ancestorModel.discriminator(discriminatorName, schema);
|
|
38
35
|
}
|
|
39
36
|
const opts = overwriteModels ? { overwriteModels } : undefined;
|
|
40
37
|
const c = connection || mongoose_1.default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA+C;AAC/C,yCAA6C;AAC7C,gEAA8C;AAC9C,sDAA2E;AAC3E,iEAA4D;AAC5D,iDAA8C;AAE9C,SAAgB,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,IAAA,6BAAc,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,+BAAmB,EAAE,cAAc,CAAC,CAAC;IAC5D,+BAAc,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;IAE3C,OAAO,EAAC,KAAK,EAAE,cAAc,EAAC,CAAC;AACjC,CAAC;AAPD,sCAOC;AAED
|
|
1
|
+
{"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":";;;;AAAA,qCAA+C;AAC/C,yCAA6C;AAC7C,gEAA8C;AAC9C,sDAA2E;AAC3E,iEAA4D;AAC5D,iDAA8C;AAE9C,SAAgB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AE9C,OAAO,KAAK,CAAC;AACf,CAAC;AAxBD,kCAwBC","sourcesContent":["import {nameOf, Store, Type} from \"@tsed/core\";\nimport {JsonEntityStore} from \"@tsed/schema\";\nimport mongoose, {Connection} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME} 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 * @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) {\n const entity = JsonEntityStore.from(target);\n\n if (entity.isDiscriminatorChild) {\n const discriminatorName = entity.discriminatorAncestor!.schema.discriminator().getDefaultValue(target);\n const ancestorModel = entity.discriminatorAncestor!.get(MONGOOSE_MODEL);\n\n return ancestorModel.discriminator(discriminatorName, schema);\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"]}
|
|
@@ -24,12 +24,12 @@ function mapHookArgs(hook) {
|
|
|
24
24
|
* @ignore
|
|
25
25
|
*/
|
|
26
26
|
function buildPreHook(fn) {
|
|
27
|
-
return fn.length ===
|
|
28
|
-
? function (
|
|
29
|
-
return fn(this
|
|
27
|
+
return fn.length === 1
|
|
28
|
+
? function () {
|
|
29
|
+
return fn(this);
|
|
30
30
|
}
|
|
31
|
-
: function (next
|
|
32
|
-
return fn(this, next
|
|
31
|
+
: function (next) {
|
|
32
|
+
return fn(this, next);
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
exports.buildPreHook = buildPreHook;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":";;;AAAA,qCAA4C;AAE5C,sDAA+D;
|
|
1
|
+
{"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":";;;AAAA,qCAA4C;AAE5C,sDAA+D;AAU/D;;GAEG;AACH,SAAgB,aAAa,CAAC,MAAW,EAAE,OAA+B;IACxE,MAAM,KAAK,GAAG,YAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,mCAAuB,CAAC,EAAE;QACvC,KAAK,CAAC,GAAG,CAAC,mCAAuB,EAAE,EAAE,CAAC,CAAC;KACxC;IAED,IAAI,OAAO,EAAE;QACX,KAAK,CAAC,GAAG,CAAC,mCAAuB,EAAE,IAAA,gBAAS,EAAC,KAAK,CAAC,GAAG,CAAC,mCAAuB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;KAC5F;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,mCAAuB,CAAC,CAAC;AAC5C,CAAC;AAZD,sCAYC;AAED,SAAS,WAAW,CAAC,IAAwC;IAC3D,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,OAAO,CAAQ,CAAC;AAC3G,CAAC;AAED;;GAEG;AACH,SAAgB,YAAY,CAAC,EAAqB;IAChD,OAAO,EAAE,CAAC,MAAM,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Schema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {\n MongooseHookPromised,\n MongooseNextCB,\n MongoosePostHook,\n MongoosePreHook,\n MongoosePreHookCB,\n MongooseSchemaOptions\n} from \"../interfaces/MongooseSchemaOptions\";\n\n/**\n * @ignore\n */\nexport function schemaOptions(target: any, options?: MongooseSchemaOptions) {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, {});\n }\n\n if (options) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));\n }\n\n return store.get(MONGOOSE_SCHEMA_OPTIONS);\n}\n\nfunction mapHookArgs(hook: MongoosePreHook | MongoosePostHook): [string | RegExp, Function] {\n return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean) as any;\n}\n\n/**\n * @ignore\n */\nexport function buildPreHook(fn: MongoosePreHookCB) {\n return fn.length === 1\n ? function () {\n return (fn as MongooseHookPromised)(this);\n }\n : function (next: MongooseNextCB) {\n return fn(this, next);\n };\n}\n\n/**\n * @ignore\n */\nexport function applySchemaOptions(schema: Schema, options: MongooseSchemaOptions) {\n if (options.plugins) {\n options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));\n }\n\n if (options.indexes) {\n options.indexes.forEach((item) => schema.index(item.fields, item.options));\n }\n\n if (options.pre) {\n options.pre.forEach((item) => {\n item = {\n ...item,\n fn: buildPreHook(item.fn)\n };\n\n (schema.pre as any)(...mapHookArgs(item));\n });\n }\n\n if (options.post) {\n options.post.forEach((item) => (schema.post as any)(...mapHookArgs(item)));\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: Error) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type
|
|
1
|
+
{"version":3,"file":"MongooseSchemaOptions.js","sourceRoot":"","sources":["../../../src/interfaces/MongooseSchemaOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Schema, SchemaOptions} from \"mongoose\";\nimport {MongooseDocument} from \"./MongooseDocument\";\n\nexport type MongooseNextCB = (err?: Error) => void;\n\nexport interface MongooseHookOptions {\n document?: boolean;\n query?: boolean;\n parallel?: boolean;\n}\n\nexport type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;\n\nexport type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;\n\nexport type MongoosePostHookCB<T = any> =\n | ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void)\n | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void)\n | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)\n | MongooseHookPromised;\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"]}
|
|
@@ -19,17 +19,14 @@ export function getModelToken(target, options) {
|
|
|
19
19
|
* @param collection (optional, induced from model name)
|
|
20
20
|
* @param overwriteModels
|
|
21
21
|
* @param connection
|
|
22
|
-
* @param discriminatorValue
|
|
23
22
|
* @returns {Model<T extends Document>}
|
|
24
23
|
*/
|
|
25
24
|
export function createModel(target, schema, name = nameOf(target), collection, overwriteModels, connection) {
|
|
26
25
|
const entity = JsonEntityStore.from(target);
|
|
27
|
-
if (entity.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return ancestorModel.discriminator(discriminatorName, schema);
|
|
32
|
-
}
|
|
26
|
+
if (entity.isDiscriminatorChild) {
|
|
27
|
+
const discriminatorName = entity.discriminatorAncestor.schema.discriminator().getDefaultValue(target);
|
|
28
|
+
const ancestorModel = entity.discriminatorAncestor.get(MONGOOSE_MODEL);
|
|
29
|
+
return ancestorModel.discriminator(discriminatorName, schema);
|
|
33
30
|
}
|
|
34
31
|
const opts = overwriteModels ? { overwriteModels } : undefined;
|
|
35
32
|
const c = connection || mongoose;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createModel.js","sourceRoot":"","sources":["../../../src/utils/createModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAC;AAC7C,OAAO,QAAsB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAC,cAAc,EAAE,mBAAmB,EAAC,MAAM,wBAAwB,CAAC;AAC3E,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAC,cAAc,EAAC,MAAM,gBAAgB,CAAC;AAE9C,MAAM,UAAU,aAAa,CAAC,MAAiB,EAAE,OAAY;IAC3D,MAAM,EAAC,cAAc,EAAE,KAAK,EAAC,GAAG,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEhE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,mBAAmB,EAAE,cAAc,CAAC,C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AAC,MAAM,CAAC,CAAC;QACvG,MAAM,aAAa,GAAG,MAAM,CAAC,qBAAsB,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QAExE,OAAO,aAAa,CAAC,aAAa,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;KAC/D;IAED,MAAM,IAAI,GAAG,eAAe,CAAC,CAAC,CAAC,EAAC,eAAe,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7D,MAAM,CAAC,GAAG,UAAU,IAAI,QAAQ,CAAC;IAEjC,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;IACtD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {nameOf, Store, Type} from \"@tsed/core\";\nimport {JsonEntityStore} from \"@tsed/schema\";\nimport mongoose, {Connection} from \"mongoose\";\nimport {MONGOOSE_MODEL, MONGOOSE_MODEL_NAME} 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 * @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) {\n const entity = JsonEntityStore.from(target);\n\n if (entity.isDiscriminatorChild) {\n const discriminatorName = entity.discriminatorAncestor!.schema.discriminator().getDefaultValue(target);\n const ancestorModel = entity.discriminatorAncestor!.get(MONGOOSE_MODEL);\n\n return ancestorModel.discriminator(discriminatorName, schema);\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"]}
|
|
@@ -20,12 +20,12 @@ function mapHookArgs(hook) {
|
|
|
20
20
|
* @ignore
|
|
21
21
|
*/
|
|
22
22
|
export function buildPreHook(fn) {
|
|
23
|
-
return fn.length ===
|
|
24
|
-
? function (
|
|
25
|
-
return fn(this
|
|
23
|
+
return fn.length === 1
|
|
24
|
+
? function () {
|
|
25
|
+
return fn(this);
|
|
26
26
|
}
|
|
27
|
-
: function (next
|
|
28
|
-
return fn(this, next
|
|
27
|
+
: function (next) {
|
|
28
|
+
return fn(this, next);
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,OAAO,EAAC,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"schemaOptions.js","sourceRoot":"","sources":["../../../src/utils/schemaOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,OAAO,EAAC,uBAAuB,EAAC,MAAM,wBAAwB,CAAC;AAU/D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,MAAW,EAAE,OAA+B;IACxE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAu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AC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;KAC5E;IAED,IAAI,OAAO,CAAC,GAAG,EAAE;QACf,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAI,GAAG;gBACL,GAAG,IAAI;gBACP,EAAE,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;aAC1B,CAAC;YAED,MAAM,CAAC,GAAW,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,EAAE;QAChB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAE,MAAM,CAAC,IAAY,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KAC5E;AACH,CAAC","sourcesContent":["import {deepMerge, Store} from \"@tsed/core\";\nimport {Schema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA_OPTIONS} from \"../constants/constants\";\nimport {\n MongooseHookPromised,\n MongooseNextCB,\n MongoosePostHook,\n MongoosePreHook,\n MongoosePreHookCB,\n MongooseSchemaOptions\n} from \"../interfaces/MongooseSchemaOptions\";\n\n/**\n * @ignore\n */\nexport function schemaOptions(target: any, options?: MongooseSchemaOptions) {\n const store = Store.from(target);\n\n if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, {});\n }\n\n if (options) {\n store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));\n }\n\n return store.get(MONGOOSE_SCHEMA_OPTIONS);\n}\n\nfunction mapHookArgs(hook: MongoosePreHook | MongoosePostHook): [string | RegExp, Function] {\n return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean) as any;\n}\n\n/**\n * @ignore\n */\nexport function buildPreHook(fn: MongoosePreHookCB) {\n return fn.length === 1\n ? function () {\n return (fn as MongooseHookPromised)(this);\n }\n : function (next: MongooseNextCB) {\n return fn(this, next);\n };\n}\n\n/**\n * @ignore\n */\nexport function applySchemaOptions(schema: Schema, options: MongooseSchemaOptions) {\n if (options.plugins) {\n options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));\n }\n\n if (options.indexes) {\n options.indexes.forEach((item) => schema.index(item.fields, item.options));\n }\n\n if (options.pre) {\n options.pre.forEach((item) => {\n item = {\n ...item,\n fn: buildPreHook(item.fn)\n };\n\n (schema.pre as any)(...mapHookArgs(item));\n });\n }\n\n if (options.post) {\n options.post.forEach((item) => (schema.post as any)(...mapHookArgs(item)));\n }\n}\n"]}
|
|
@@ -6,8 +6,9 @@ export interface MongooseHookOptions {
|
|
|
6
6
|
query?: boolean;
|
|
7
7
|
parallel?: boolean;
|
|
8
8
|
}
|
|
9
|
-
export declare type
|
|
10
|
-
export declare type
|
|
9
|
+
export declare type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;
|
|
10
|
+
export declare type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
|
|
11
|
+
export declare type MongoosePostHookCB<T = any> = ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void) | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void) | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
|
|
11
12
|
export interface MongoosePreHook<T = any> {
|
|
12
13
|
method: string | RegExp;
|
|
13
14
|
fn: MongoosePreHookCB<T>;
|
|
@@ -13,7 +13,6 @@ export declare function getModelToken(target: Type<any>, options: any): {
|
|
|
13
13
|
* @param collection (optional, induced from model name)
|
|
14
14
|
* @param overwriteModels
|
|
15
15
|
* @param connection
|
|
16
|
-
* @param discriminatorValue
|
|
17
16
|
* @returns {Model<T extends Document>}
|
|
18
17
|
*/
|
|
19
18
|
export declare function createModel<T>(target: any, schema: mongoose.Schema, name?: string, collection?: string, overwriteModels?: boolean, connection?: Connection): any;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Schema } from "mongoose";
|
|
2
|
-
import { MongooseNextCB, MongooseSchemaOptions } from "../interfaces/MongooseSchemaOptions";
|
|
2
|
+
import { MongooseNextCB, MongoosePreHookCB, MongooseSchemaOptions } from "../interfaces/MongooseSchemaOptions";
|
|
3
3
|
/**
|
|
4
4
|
* @ignore
|
|
5
5
|
*/
|
|
@@ -7,7 +7,7 @@ export declare function schemaOptions(target: any, options?: MongooseSchemaOptio
|
|
|
7
7
|
/**
|
|
8
8
|
* @ignore
|
|
9
9
|
*/
|
|
10
|
-
export declare function buildPreHook(fn:
|
|
10
|
+
export declare function buildPreHook(fn: MongoosePreHookCB): (next: MongooseNextCB) => void | Promise<void>;
|
|
11
11
|
/**
|
|
12
12
|
* @ignore
|
|
13
13
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mongoose",
|
|
3
|
-
"version": "7.8.
|
|
3
|
+
"version": "7.8.2",
|
|
4
4
|
"description": "Mongoose package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -32,21 +32,21 @@
|
|
|
32
32
|
"tslib": "2.4.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@tsed/ajv": "7.8.
|
|
36
|
-
"@tsed/core": "7.8.
|
|
37
|
-
"@tsed/di": "7.8.
|
|
38
|
-
"@tsed/json-mapper": "7.8.
|
|
35
|
+
"@tsed/ajv": "7.8.2",
|
|
36
|
+
"@tsed/core": "7.8.2",
|
|
37
|
+
"@tsed/di": "7.8.2",
|
|
38
|
+
"@tsed/json-mapper": "7.8.2",
|
|
39
39
|
"@tsed/logger": ">=6.2.2",
|
|
40
|
-
"@tsed/schema": "7.8.
|
|
40
|
+
"@tsed/schema": "7.8.2",
|
|
41
41
|
"mongoose": "6.5.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@tsed/ajv": "^7.8.
|
|
45
|
-
"@tsed/core": "^7.8.
|
|
46
|
-
"@tsed/di": "^7.8.
|
|
47
|
-
"@tsed/json-mapper": "^7.8.
|
|
44
|
+
"@tsed/ajv": "^7.8.2",
|
|
45
|
+
"@tsed/core": "^7.8.2",
|
|
46
|
+
"@tsed/di": "^7.8.2",
|
|
47
|
+
"@tsed/json-mapper": "^7.8.2",
|
|
48
48
|
"@tsed/logger": ">=6.2.2",
|
|
49
|
-
"@tsed/schema": "^7.8.
|
|
49
|
+
"@tsed/schema": "^7.8.2",
|
|
50
50
|
"mongoose": ">=6.0.0"
|
|
51
51
|
},
|
|
52
52
|
"repository": "https://github.com/tsedio/tsed",
|