@tsed/mongoose 6.94.0 → 6.95.3
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/lib/cjs/decorators/ref.d.ts +7 -2
- package/lib/cjs/decorators/ref.js +17 -7
- package/lib/cjs/decorators/ref.js.map +1 -1
- package/lib/cjs/decorators/virtualRef.js +1 -1
- package/lib/cjs/decorators/virtualRef.js.map +1 -1
- package/lib/esm/decorators/ref.js +19 -9
- package/lib/esm/decorators/ref.js.map +1 -1
- package/lib/esm/decorators/virtualRef.js +1 -1
- package/lib/esm/decorators/virtualRef.js.map +1 -1
- package/lib/types/decorators/ref.d.ts +7 -2
- package/package.json +11 -11
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Type } from "@tsed/core";
|
|
2
2
|
import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes";
|
|
3
|
+
interface RefOptions {
|
|
4
|
+
type?: MongooseSchemaTypes;
|
|
5
|
+
populatedGroups?: string[];
|
|
6
|
+
}
|
|
3
7
|
/**
|
|
4
8
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
5
9
|
*
|
|
@@ -23,11 +27,12 @@ import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes";
|
|
|
23
27
|
* ```
|
|
24
28
|
*
|
|
25
29
|
* @param model
|
|
26
|
-
* @param
|
|
30
|
+
* @param options
|
|
27
31
|
* @returns {Function}
|
|
28
32
|
* @decorator
|
|
29
33
|
* @mongoose
|
|
30
34
|
* @property
|
|
31
35
|
*/
|
|
32
|
-
export declare function Ref(model: string | (() => Type) | any,
|
|
36
|
+
export declare function Ref(model: string | (() => Type) | any, options?: RefOptions | MongooseSchemaTypes): PropertyDecorator;
|
|
33
37
|
export declare type Ref<T> = T | string;
|
|
38
|
+
export {};
|
|
@@ -11,6 +11,18 @@ const MongooseModels_1 = require("../registries/MongooseModels");
|
|
|
11
11
|
function isRef(value) {
|
|
12
12
|
return (value && value._bsontype) || (0, core_1.isString)(value);
|
|
13
13
|
}
|
|
14
|
+
function PopulateGroups(populatedGroups) {
|
|
15
|
+
return (0, core_1.useDecorators)((0, schema_1.ForwardGroups)(true), (0, schema_1.JsonEntityFn)((store) => {
|
|
16
|
+
store.schema.$hooks.on("oneOf", (obj, givenGroups) => {
|
|
17
|
+
if ((0, schema_1.matchGroups)(populatedGroups, givenGroups)) {
|
|
18
|
+
return obj.filter((x) => x.type === "string"); // keep the object id;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
return obj.filter((x) => x.type !== "string"); // keep the ref definition
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
14
26
|
/**
|
|
15
27
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
16
28
|
*
|
|
@@ -34,16 +46,17 @@ function isRef(value) {
|
|
|
34
46
|
* ```
|
|
35
47
|
*
|
|
36
48
|
* @param model
|
|
37
|
-
* @param
|
|
49
|
+
* @param options
|
|
38
50
|
* @returns {Function}
|
|
39
51
|
* @decorator
|
|
40
52
|
* @mongoose
|
|
41
53
|
* @property
|
|
42
54
|
*/
|
|
43
|
-
function Ref(model,
|
|
55
|
+
function Ref(model, options = MongooseSchemaTypes_1.MongooseSchemaTypes.OBJECT_ID) {
|
|
44
56
|
const getType = () => ((0, core_1.isString)(model) ? MongooseModels_1.MongooseModels.get(model) : (0, core_1.isArrowFn)(model) ? model() : model);
|
|
57
|
+
const populatedGroups = ((0, core_1.isObject)(options) && options.populatedGroups) || [];
|
|
45
58
|
return (0, core_1.useDecorators)((0, schema_1.Property)(Object), (0, core_1.StoreMerge)(constants_1.MONGOOSE_SCHEMA, {
|
|
46
|
-
type: mongoose_1.Schema.Types[type],
|
|
59
|
+
type: mongoose_1.Schema.Types[(0, core_1.isObject)(options) ? options.type || MongooseSchemaTypes_1.MongooseSchemaTypes.OBJECT_ID : options],
|
|
47
60
|
ref: model
|
|
48
61
|
}), (0, json_mapper_1.OnDeserialize)((value) => {
|
|
49
62
|
if (isRef(value)) {
|
|
@@ -56,10 +69,7 @@ function Ref(model, type = MongooseSchemaTypes_1.MongooseSchemaTypes.OBJECT_ID)
|
|
|
56
69
|
}
|
|
57
70
|
const type = getType();
|
|
58
71
|
return (0, json_mapper_1.serialize)(value, { ...ctx, type });
|
|
59
|
-
}), (0, schema_1.
|
|
60
|
-
store.itemSchema.oneOf([(0, schema_1.string)().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), (0, schema_1.lazyRef)(getType)]);
|
|
61
|
-
store.type = Object;
|
|
62
|
-
}));
|
|
72
|
+
}), (0, schema_1.OneOf)((0, schema_1.string)().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), (0, schema_1.lazyRef)(getType)), populatedGroups.length && PopulateGroups(populatedGroups));
|
|
63
73
|
}
|
|
64
74
|
exports.Ref = Ref;
|
|
65
75
|
//# sourceMappingURL=ref.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/decorators/ref.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/decorators/ref.ts"],"names":[],"mappings":";;;AAAA,qCAA0F;AAC1F,mDAAwE;AACxE,yCAAwG;AACxG,uCAAkD;AAClD,4CAA6C;AAC7C,2EAAsE;AACtE,iEAA4D;AAO5D,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,cAAc,CAAC,eAAyB;IAC/C,OAAO,IAAA,oBAAa,EAClB,IAAA,sBAAa,EAAC,IAAI,CAAC,EACnB,IAAA,qBAAY,EAAC,CAAC,KAAK,EAAE,EAAE;QACrB,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,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,KAAK,EAAE,EAAE;QACtB,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC,EACF,IAAA,yBAAW,EAAC,CAAC,KAAU,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QACD,MAAM,IAAI,GAAG,OAAO,EAAE,CAAC;QAEvB,OAAO,IAAA,uBAAS,EAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAC,CAAC,CAAC;IAC1C,CAAC,CAAC,EACF,IAAA,cAAK,EAAC,IAAA,eAAM,GAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,IAAA,gBAAO,EAAC,OAAO,CAAC,CAAC,EAC1G,eAAe,CAAC,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,CACrC,CAAC;AACzB,CAAC;AA/BD,kBA+BC","sourcesContent":["import {isArrowFn, isObject, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants\";\nimport {MongooseSchemaTypes} from \"../interfaces/MongooseSchemaTypes\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\ninterface RefOptions {\n type?: MongooseSchemaTypes;\n populatedGroups?: string[];\n}\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction PopulateGroups(populatedGroups: string[]) {\n return useDecorators(\n ForwardGroups(true),\n JsonEntityFn((store) => {\n store.schema.$hooks.on(\"oneOf\", (obj: any[], givenGroups: string[]) => {\n if (matchGroups(populatedGroups, givenGroups)) {\n return obj.filter((x) => x.type === \"string\"); // keep the object id;\n } else {\n return obj.filter((x) => x.type !== \"string\"); // keep the ref definition\n }\n });\n })\n );\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n *\n * @Model()\n * class FooModel {\n *\n * @Ref(Foo2Model)\n * field: Ref<Foo2Model>\n *\n * @Ref(Foo2Model)\n * list: Ref<Foo2Model>[]\n * }\n *\n * @Model()\n * class Foo2Model {\n * }\n * ```\n *\n * @param model\n * @param options\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Ref(\n model: string | (() => Type) | any,\n options: RefOptions | MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID\n): PropertyDecorator {\n const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);\n const populatedGroups = (isObject(options) && options.populatedGroups) || [];\n\n return useDecorators(\n Property(Object),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],\n ref: model\n }),\n OnDeserialize((value) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return value;\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n const type = getType();\n\n return serialize(value, {...ctx, type});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), lazyRef(getType)),\n populatedGroups.length && PopulateGroups(populatedGroups)\n ) as PropertyDecorator;\n}\n\nexport type Ref<T> = T | string;\n"]}
|
|
@@ -11,7 +11,7 @@ function getRef(opts) {
|
|
|
11
11
|
}
|
|
12
12
|
function getType(opts) {
|
|
13
13
|
const ref = opts.ref || opts.type;
|
|
14
|
-
return !(0, core_1.isString)(ref) ? ref : MongooseModels_1.MongooseModels.get(ref);
|
|
14
|
+
return !(0, core_1.isString)(ref) ? ref : MongooseModels_1.MongooseModels.get(ref) || (() => MongooseModels_1.MongooseModels.get(ref) || Object);
|
|
15
15
|
}
|
|
16
16
|
function getInitialOpts(options, foreignField) {
|
|
17
17
|
return (0, core_1.isPlainObject)(options) ? options : { type: options, foreignField };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../../src/decorators/virtualRef.ts"],"names":[],"mappings":";;;AAAA,qCAAqF;AACrF,yCAAoD;AACpD,4CAAkE;AAElE,iEAA4D;AAE5D,SAAS,MAAM,CAAC,IAAS;IACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC;IAElC,OAAO,IAAA,eAAQ,EAAC,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;
|
|
1
|
+
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../../src/decorators/virtualRef.ts"],"names":[],"mappings":";;;AAAA,qCAAqF;AACrF,yCAAoD;AACpD,4CAAkE;AAElE,iEAA4D;AAE5D,SAAS,MAAM,CAAC,IAAS;IACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC;IAElC,OAAO,IAAA,eAAQ,EAAC,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;IA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AAO,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\";\nimport {MongooseVirtualRefOptions} from \"../interfaces/MongooseVirtualRefOptions\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\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 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 : {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 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 * ### 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(\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"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isArrowFn, isString, StoreMerge, useDecorators } from "@tsed/core";
|
|
1
|
+
import { isArrowFn, isObject, isString, StoreMerge, useDecorators } from "@tsed/core";
|
|
2
2
|
import { OnDeserialize, OnSerialize, serialize } from "@tsed/json-mapper";
|
|
3
|
-
import { JsonEntityFn, lazyRef, Property, string } from "@tsed/schema";
|
|
3
|
+
import { ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string } from "@tsed/schema";
|
|
4
4
|
import { Schema as MongooseSchema } from "mongoose";
|
|
5
5
|
import { MONGOOSE_SCHEMA } from "../constants";
|
|
6
6
|
import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes";
|
|
@@ -8,6 +8,18 @@ import { MongooseModels } from "../registries/MongooseModels";
|
|
|
8
8
|
function isRef(value) {
|
|
9
9
|
return (value && value._bsontype) || isString(value);
|
|
10
10
|
}
|
|
11
|
+
function PopulateGroups(populatedGroups) {
|
|
12
|
+
return useDecorators(ForwardGroups(true), JsonEntityFn((store) => {
|
|
13
|
+
store.schema.$hooks.on("oneOf", (obj, givenGroups) => {
|
|
14
|
+
if (matchGroups(populatedGroups, givenGroups)) {
|
|
15
|
+
return obj.filter((x) => x.type === "string"); // keep the object id;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return obj.filter((x) => x.type !== "string"); // keep the ref definition
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
11
23
|
/**
|
|
12
24
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
13
25
|
*
|
|
@@ -31,16 +43,17 @@ function isRef(value) {
|
|
|
31
43
|
* ```
|
|
32
44
|
*
|
|
33
45
|
* @param model
|
|
34
|
-
* @param
|
|
46
|
+
* @param options
|
|
35
47
|
* @returns {Function}
|
|
36
48
|
* @decorator
|
|
37
49
|
* @mongoose
|
|
38
50
|
* @property
|
|
39
51
|
*/
|
|
40
|
-
export function Ref(model,
|
|
52
|
+
export function Ref(model, options = MongooseSchemaTypes.OBJECT_ID) {
|
|
41
53
|
const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);
|
|
54
|
+
const populatedGroups = (isObject(options) && options.populatedGroups) || [];
|
|
42
55
|
return useDecorators(Property(Object), StoreMerge(MONGOOSE_SCHEMA, {
|
|
43
|
-
type: MongooseSchema.Types[type],
|
|
56
|
+
type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],
|
|
44
57
|
ref: model
|
|
45
58
|
}), OnDeserialize((value) => {
|
|
46
59
|
if (isRef(value)) {
|
|
@@ -53,9 +66,6 @@ export function Ref(model, type = MongooseSchemaTypes.OBJECT_ID) {
|
|
|
53
66
|
}
|
|
54
67
|
const type = getType();
|
|
55
68
|
return serialize(value, { ...ctx, type });
|
|
56
|
-
}),
|
|
57
|
-
store.itemSchema.oneOf([string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), lazyRef(getType)]);
|
|
58
|
-
store.type = Object;
|
|
59
|
-
}));
|
|
69
|
+
}), OneOf(string().example("5ce7ad3028890bd71749d477").description("Mongoose Ref ObjectId"), lazyRef(getType)), populatedGroups.length && PopulateGroups(populatedGroups));
|
|
60
70
|
}
|
|
61
71
|
//# sourceMappingURL=ref.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/decorators/ref.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAQ,aAAa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AO,SAAS,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAC,CAAC,CAAC;IAC1C,CAAC,CAAC,EACF,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,EAC1G,eAAe,CAAC,MAAM,IAAI,cAAc,CAAC,eAAe,CAAC,CACrC,CAAC;AACzB,CAAC","sourcesContent":["import {isArrowFn, isObject, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string} from \"@tsed/schema\";\nimport {Schema as MongooseSchema} from \"mongoose\";\nimport {MONGOOSE_SCHEMA} from \"../constants\";\nimport {MongooseSchemaTypes} from \"../interfaces/MongooseSchemaTypes\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\n\ninterface RefOptions {\n type?: MongooseSchemaTypes;\n populatedGroups?: string[];\n}\n\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\n}\n\nfunction PopulateGroups(populatedGroups: string[]) {\n return useDecorators(\n ForwardGroups(true),\n JsonEntityFn((store) => {\n store.schema.$hooks.on(\"oneOf\", (obj: any[], givenGroups: string[]) => {\n if (matchGroups(populatedGroups, givenGroups)) {\n return obj.filter((x) => x.type === \"string\"); // keep the object id;\n } else {\n return obj.filter((x) => x.type !== \"string\"); // keep the ref definition\n }\n });\n })\n );\n}\n\n/**\n * Define a property as mongoose reference to other Model (decorated with @Model).\n *\n * ### Example\n *\n * ```typescript\n *\n * @Model()\n * class FooModel {\n *\n * @Ref(Foo2Model)\n * field: Ref<Foo2Model>\n *\n * @Ref(Foo2Model)\n * list: Ref<Foo2Model>[]\n * }\n *\n * @Model()\n * class Foo2Model {\n * }\n * ```\n *\n * @param model\n * @param options\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Ref(\n model: string | (() => Type) | any,\n options: RefOptions | MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID\n): PropertyDecorator {\n const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);\n const populatedGroups = (isObject(options) && options.populatedGroups) || [];\n\n return useDecorators(\n Property(Object),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],\n ref: model\n }),\n OnDeserialize((value) => {\n if (isRef(value)) {\n return value.toString();\n }\n\n return value;\n }),\n OnSerialize((value: any, ctx) => {\n if (isRef(value)) {\n return value.toString();\n }\n const type = getType();\n\n return serialize(value, {...ctx, type});\n }),\n OneOf(string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), lazyRef(getType)),\n populatedGroups.length && PopulateGroups(populatedGroups)\n ) as PropertyDecorator;\n}\n\nexport type Ref<T> = T | string;\n"]}
|
|
@@ -8,7 +8,7 @@ function getRef(opts) {
|
|
|
8
8
|
}
|
|
9
9
|
function getType(opts) {
|
|
10
10
|
const ref = opts.ref || opts.type;
|
|
11
|
-
return !isString(ref) ? ref : MongooseModels.get(ref);
|
|
11
|
+
return !isString(ref) ? ref : MongooseModels.get(ref) || (() => MongooseModels.get(ref) || Object);
|
|
12
12
|
}
|
|
13
13
|
function getInitialOpts(options, foreignField) {
|
|
14
14
|
return isPlainObject(options) ? options : { type: options, foreignField };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtualRef.js","sourceRoot":"","sources":["../../../src/decorators/virtualRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACrF,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AACpD,OAAO,EAAC,mBAAmB,EA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AAI,CAAC,CAAC,CACjG,CAAC;AACJ,CAAC","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\";\nimport {MongooseModels} from \"../registries/MongooseModels\";\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 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 : {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 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 * ### 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(\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"]}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Type } from "@tsed/core";
|
|
2
2
|
import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes";
|
|
3
|
+
interface RefOptions {
|
|
4
|
+
type?: MongooseSchemaTypes;
|
|
5
|
+
populatedGroups?: string[];
|
|
6
|
+
}
|
|
3
7
|
/**
|
|
4
8
|
* Define a property as mongoose reference to other Model (decorated with @Model).
|
|
5
9
|
*
|
|
@@ -23,11 +27,12 @@ import { MongooseSchemaTypes } from "../interfaces/MongooseSchemaTypes";
|
|
|
23
27
|
* ```
|
|
24
28
|
*
|
|
25
29
|
* @param model
|
|
26
|
-
* @param
|
|
30
|
+
* @param options
|
|
27
31
|
* @returns {Function}
|
|
28
32
|
* @decorator
|
|
29
33
|
* @mongoose
|
|
30
34
|
* @property
|
|
31
35
|
*/
|
|
32
|
-
export declare function Ref(model: string | (() => Type) | any,
|
|
36
|
+
export declare function Ref(model: string | (() => Type) | any, options?: RefOptions | MongooseSchemaTypes): PropertyDecorator;
|
|
33
37
|
export declare type Ref<T> = T | string;
|
|
38
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mongoose",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.95.3",
|
|
4
4
|
"description": "Mongoose package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -30,21 +30,21 @@
|
|
|
30
30
|
"tslib": "2.2.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tsed/ajv": "6.
|
|
34
|
-
"@tsed/core": "6.
|
|
35
|
-
"@tsed/di": "6.
|
|
36
|
-
"@tsed/json-mapper": "6.
|
|
33
|
+
"@tsed/ajv": "6.95.3",
|
|
34
|
+
"@tsed/core": "6.95.3",
|
|
35
|
+
"@tsed/di": "6.95.3",
|
|
36
|
+
"@tsed/json-mapper": "6.95.3",
|
|
37
37
|
"@tsed/logger": "^6.0.0",
|
|
38
|
-
"@tsed/schema": "6.
|
|
38
|
+
"@tsed/schema": "6.95.3",
|
|
39
39
|
"mongoose": "^5.12.2"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@tsed/ajv": "^6.
|
|
43
|
-
"@tsed/core": "^6.
|
|
44
|
-
"@tsed/di": "^6.
|
|
45
|
-
"@tsed/json-mapper": "^6.
|
|
42
|
+
"@tsed/ajv": "^6.95.3",
|
|
43
|
+
"@tsed/core": "^6.95.3",
|
|
44
|
+
"@tsed/di": "^6.95.3",
|
|
45
|
+
"@tsed/json-mapper": "^6.95.3",
|
|
46
46
|
"@tsed/logger": ">=6.0.0",
|
|
47
|
-
"@tsed/schema": "^6.
|
|
47
|
+
"@tsed/schema": "^6.95.3",
|
|
48
48
|
"mongoose": ">=5.12.2"
|
|
49
49
|
},
|
|
50
50
|
"repository": "https://github.com/tsedio/tsed",
|