@tsed/mongoose 6.105.1 → 6.105.4
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 +1 -1
- package/lib/cjs/decorators/dynamicRef.js +23 -4
- package/lib/cjs/decorators/dynamicRef.js.map +1 -1
- package/lib/esm/decorators/dynamicRef.js +25 -6
- package/lib/esm/decorators/dynamicRef.js.map +1 -1
- package/lib/types/decorators/dynamicRef.d.ts +4 -2
- package/package.json +11 -11
package/jest.config.js
CHANGED
|
@@ -5,6 +5,14 @@ const core_1 = require("@tsed/core");
|
|
|
5
5
|
const schema_1 = require("@tsed/schema");
|
|
6
6
|
const mongoose_1 = require("mongoose");
|
|
7
7
|
const constants_1 = require("../constants/constants");
|
|
8
|
+
const json_mapper_1 = require("@tsed/json-mapper");
|
|
9
|
+
const MongooseModels_1 = require("../registries/MongooseModels");
|
|
10
|
+
function isRef(value) {
|
|
11
|
+
return (value && value._bsontype) || (0, core_1.isString)(value);
|
|
12
|
+
}
|
|
13
|
+
function getType(refPath, ctx) {
|
|
14
|
+
return ((ctx === null || ctx === void 0 ? void 0 : ctx.self[refPath]) && MongooseModels_1.MongooseModels.get(ctx.self[refPath])) || Object;
|
|
15
|
+
}
|
|
8
16
|
/**
|
|
9
17
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
10
18
|
*
|
|
@@ -30,17 +38,28 @@ const constants_1 = require("../constants/constants");
|
|
|
30
38
|
* }
|
|
31
39
|
* ```
|
|
32
40
|
*
|
|
33
|
-
* @param refPath
|
|
41
|
+
* @param refPath {String} the path to apply the correct model
|
|
42
|
+
* @param types {Type} the classes to generate the correct json schema
|
|
34
43
|
* @returns {Function}
|
|
35
44
|
* @decorator
|
|
36
45
|
* @mongoose
|
|
37
46
|
* @property
|
|
38
47
|
*/
|
|
39
|
-
function DynamicRef(refPath) {
|
|
40
|
-
return (0, core_1.useDecorators)((0, schema_1.Property)(
|
|
48
|
+
function DynamicRef(refPath, ...types) {
|
|
49
|
+
return (0, core_1.useDecorators)((0, schema_1.Property)(Object), (0, schema_1.Example)("5ce7ad3028890bd71749d477"), (0, schema_1.Description)("Mongoose Ref ObjectId"), (0, core_1.StoreMerge)(constants_1.MONGOOSE_SCHEMA, {
|
|
41
50
|
type: mongoose_1.Schema.Types.ObjectId,
|
|
42
51
|
refPath
|
|
43
|
-
}))
|
|
52
|
+
}), (0, json_mapper_1.OnDeserialize)((value, ctx) => {
|
|
53
|
+
if (isRef(value)) {
|
|
54
|
+
return value.toString();
|
|
55
|
+
}
|
|
56
|
+
return (0, json_mapper_1.deserialize)(value, { ...ctx, type: getType(refPath, ctx) });
|
|
57
|
+
}), (0, json_mapper_1.OnSerialize)((value, ctx) => {
|
|
58
|
+
if (isRef(value)) {
|
|
59
|
+
return value.toString();
|
|
60
|
+
}
|
|
61
|
+
return (0, json_mapper_1.serialize)(value, { ...ctx, type: getType(refPath, ctx) });
|
|
62
|
+
}), (0, schema_1.OneOf)((0, schema_1.string)().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), ...types));
|
|
44
63
|
}
|
|
45
64
|
exports.DynamicRef = DynamicRef;
|
|
46
65
|
//# sourceMappingURL=dynamicRef.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":";;;AAAA,qCAAyF;AACzF,yCAA4F;AAC5F,uCAAkD;AAClD,sDAAuD;AACvD,mDAAqF;AACrF,iEAA4D;AAE5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,IAAA,eAAQ,EAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,OAAO,CAAC,OAAe,EA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0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC;AAzBD,gCAyBC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { StoreMerge, useDecorators } from "@tsed/core";
|
|
2
|
-
import { Description, Example, Property } from "@tsed/schema";
|
|
1
|
+
import { isString, StoreMerge, useDecorators } from "@tsed/core";
|
|
2
|
+
import { Description, Example, OneOf, Property, string } from "@tsed/schema";
|
|
3
3
|
import { Schema as MongooseSchema } from "mongoose";
|
|
4
4
|
import { MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
5
|
+
import { deserialize, OnDeserialize, OnSerialize, serialize } from "@tsed/json-mapper";
|
|
6
|
+
import { MongooseModels } from "../registries/MongooseModels.js";
|
|
7
|
+
function isRef(value) {
|
|
8
|
+
return (value && value._bsontype) || isString(value);
|
|
9
|
+
}
|
|
10
|
+
function getType(refPath, ctx) {
|
|
11
|
+
return ((ctx === null || ctx === void 0 ? void 0 : ctx.self[refPath]) && MongooseModels.get(ctx.self[refPath])) || Object;
|
|
12
|
+
}
|
|
5
13
|
/**
|
|
6
14
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
7
15
|
*
|
|
@@ -27,16 +35,27 @@ import { MONGOOSE_SCHEMA } from "../constants/constants.js";
|
|
|
27
35
|
* }
|
|
28
36
|
* ```
|
|
29
37
|
*
|
|
30
|
-
* @param refPath
|
|
38
|
+
* @param refPath {String} the path to apply the correct model
|
|
39
|
+
* @param types {Type} the classes to generate the correct json schema
|
|
31
40
|
* @returns {Function}
|
|
32
41
|
* @decorator
|
|
33
42
|
* @mongoose
|
|
34
43
|
* @property
|
|
35
44
|
*/
|
|
36
|
-
export function DynamicRef(refPath) {
|
|
37
|
-
return useDecorators(Property(
|
|
45
|
+
export function DynamicRef(refPath, ...types) {
|
|
46
|
+
return useDecorators(Property(Object), Example("5ce7ad3028890bd71749d477"), Description("Mongoose Ref ObjectId"), StoreMerge(MONGOOSE_SCHEMA, {
|
|
38
47
|
type: MongooseSchema.Types.ObjectId,
|
|
39
48
|
refPath
|
|
40
|
-
}))
|
|
49
|
+
}), OnDeserialize((value, ctx) => {
|
|
50
|
+
if (isRef(value)) {
|
|
51
|
+
return value.toString();
|
|
52
|
+
}
|
|
53
|
+
return deserialize(value, { ...ctx, type: getType(refPath, ctx) });
|
|
54
|
+
}), OnSerialize((value, ctx) => {
|
|
55
|
+
if (isRef(value)) {
|
|
56
|
+
return value.toString();
|
|
57
|
+
}
|
|
58
|
+
return serialize(value, { ...ctx, type: getType(refPath, ctx) });
|
|
59
|
+
}), OneOf(string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), ...types));
|
|
41
60
|
}
|
|
42
61
|
//# sourceMappingURL=dynamicRef.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"dynamicRef.js","sourceRoot":"","sources":["../../../src/decorators/dynamicRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,QAAQ,EAAE,UAAU,EAAQ,aAAa,EAAC,MAAM,YAAY,CAAC;AACzF,OAAO,EAAC,WAAW,EAAE,OAAO,EAAmB,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAC,MAAM,IAAI,cAAc,EAAC,MAAM,UAAU,CAAC;AAClD,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAC,WAAW,EAAE,aAAa,EAAE,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAE5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAA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C,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,GAAG,KAAK,CAAC,CAC9E,CAAC;AACzB,CAAC","sourcesContent":["import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {Description, Example, JsonHookContext, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants/constants\";\nimport {deserialize, OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction getType(refPath: string, ctx: JsonHookContext) {\n return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n * @Model()\n * class FooModel {\n *\n * @DynamicRef('type')\n * field: DynamicRef<OtherFooModel | OtherModel>\n *\n * @Enum(['OtherFooModel', 'OtherModel'])\n * type: string\n * }\n *\n * @Model()\n * class OtherFooModel {\n * }\n *\n * @Model()\n * class OtherModel {\n * }\n * ```\n *\n * @param refPath {String} the path to apply the correct model\n * @param types {Type} the classes to generate the correct json schema\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {\n return useDecorators(\n Property(Object),\n Example(\"5ce7ad3028890bd71749d477\"),\n Description(\"Mongoose Ref ObjectId\"),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types.ObjectId,\n refPath\n }),\n OnDeserialize((value, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return deserialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return serialize(value, {...ctx, type: getType(refPath, ctx)});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), ...types)\n ) as PropertyDecorator;\n}\n\nexport type DynamicRef<T> = T | string;\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Type } from "@tsed/core";
|
|
1
2
|
/**
|
|
2
3
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
3
4
|
*
|
|
@@ -23,11 +24,12 @@
|
|
|
23
24
|
* }
|
|
24
25
|
* ```
|
|
25
26
|
*
|
|
26
|
-
* @param refPath
|
|
27
|
+
* @param refPath {String} the path to apply the correct model
|
|
28
|
+
* @param types {Type} the classes to generate the correct json schema
|
|
27
29
|
* @returns {Function}
|
|
28
30
|
* @decorator
|
|
29
31
|
* @mongoose
|
|
30
32
|
* @property
|
|
31
33
|
*/
|
|
32
|
-
export declare function DynamicRef(refPath: string): PropertyDecorator;
|
|
34
|
+
export declare function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator;
|
|
33
35
|
export declare type DynamicRef<T> = T | string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mongoose",
|
|
3
|
-
"version": "6.105.
|
|
3
|
+
"version": "6.105.4",
|
|
4
4
|
"description": "Mongoose package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -32,21 +32,21 @@
|
|
|
32
32
|
"tslib": "2.2.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@tsed/ajv": "6.105.
|
|
36
|
-
"@tsed/core": "6.105.
|
|
37
|
-
"@tsed/di": "6.105.
|
|
38
|
-
"@tsed/json-mapper": "6.105.
|
|
35
|
+
"@tsed/ajv": "6.105.4",
|
|
36
|
+
"@tsed/core": "6.105.4",
|
|
37
|
+
"@tsed/di": "6.105.4",
|
|
38
|
+
"@tsed/json-mapper": "6.105.4",
|
|
39
39
|
"@tsed/logger": ">=6.1.0",
|
|
40
|
-
"@tsed/schema": "6.105.
|
|
40
|
+
"@tsed/schema": "6.105.4",
|
|
41
41
|
"mongoose": "^5.12.2"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@tsed/ajv": "^6.105.
|
|
45
|
-
"@tsed/core": "^6.105.
|
|
46
|
-
"@tsed/di": "^6.105.
|
|
47
|
-
"@tsed/json-mapper": "^6.105.
|
|
44
|
+
"@tsed/ajv": "^6.105.4",
|
|
45
|
+
"@tsed/core": "^6.105.4",
|
|
46
|
+
"@tsed/di": "^6.105.4",
|
|
47
|
+
"@tsed/json-mapper": "^6.105.4",
|
|
48
48
|
"@tsed/logger": ">=6.1.0",
|
|
49
|
-
"@tsed/schema": "^6.105.
|
|
49
|
+
"@tsed/schema": "^6.105.4",
|
|
50
50
|
"mongoose": ">=5.12.2"
|
|
51
51
|
},
|
|
52
52
|
"repository": "https://github.com/tsedio/tsed",
|