@tsed/mongoose 6.94.0 → 6.95.0

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.
@@ -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 type
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, type?: MongooseSchemaTypes): PropertyDecorator;
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 type
49
+ * @param options
38
50
  * @returns {Function}
39
51
  * @decorator
40
52
  * @mongoose
41
53
  * @property
42
54
  */
43
- function Ref(model, type = MongooseSchemaTypes_1.MongooseSchemaTypes.OBJECT_ID) {
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.JsonEntityFn)(async (store) => {
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,qCAAgF;AAChF,mDAAwE;AACxE,yCAAqE;AACrE,uCAAkD;AAClD,4CAA6C;AAC7C,2EAA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EAAE,KAAK,EAAE,EAAE;QAC3B,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,IAAA,eAAM,GAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,IAAA,gBAAO,EAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAE9H,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC;IACtB,CAAC,CAAC,CACkB,CAAC;AACzB,CAAC;AA9BD,kBA8BC","sourcesContent":["import {isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {JsonEntityFn, lazyRef, 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\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\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 type\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Ref(model: string | (() => Type) | any, type: MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID): PropertyDecorator {\n const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);\n\n return useDecorators(\n Property(Object),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types[type],\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 JsonEntityFn(async (store) => {\n store.itemSchema.oneOf([string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), lazyRef(getType)]);\n\n store.type = Object;\n })\n ) as PropertyDecorator;\n}\n\nexport type Ref<T> = T | string;\n"]}
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,GAAU,EAAE,WAAqB,EAAE,EAAE;YACpE,IAAI,IAAA,oBAAW,EAAC,eAAe,EAAE,WAAW,CAAC,EAAE;gBAC7C,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,sBAAsB;aACtE;iBAAM;gBACL,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,0BAA0B;aAC1E;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4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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"]}
@@ -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 type
46
+ * @param options
35
47
  * @returns {Function}
36
48
  * @decorator
37
49
  * @mongoose
38
50
  * @property
39
51
  */
40
- export function Ref(model, type = MongooseSchemaTypes.OBJECT_ID) {
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
- }), JsonEntityFn(async (store) => {
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,EAAC,MAAM,YAAY,CAAC;AAChF,OAAO,EAAC,aAAa,EAAE,WAA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AAI,KAAK,CAAC,KAAK,CAAC,EAAE;YAChB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;SACzB;QACD,MAAM,IAAI,GAAG,OAAO,EAAE,CAAC;QAEvB,OAAO,SAAS,CAAC,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,IAAI,EAAC,CAAC,CAAC;IAC1C,CAAC,CAAC,EACF,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QAC3B,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,WAAW,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAE9H,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC;IACtB,CAAC,CAAC,CACkB,CAAC;AACzB,CAAC","sourcesContent":["import {isArrowFn, isString, StoreMerge, Type, useDecorators} from \"@tsed/core\";\nimport {OnDeserialize, OnSerialize, serialize} from \"@tsed/json-mapper\";\nimport {JsonEntityFn, lazyRef, 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\nfunction isRef(value: undefined | string | any) {\n return (value && value._bsontype) || isString(value);\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 type\n * @returns {Function}\n * @decorator\n * @mongoose\n * @property\n */\nexport function Ref(model: string | (() => Type) | any, type: MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID): PropertyDecorator {\n const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);\n\n return useDecorators(\n Property(Object),\n StoreMerge(MONGOOSE_SCHEMA, {\n type: MongooseSchema.Types[type],\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 JsonEntityFn(async (store) => {\n store.itemSchema.oneOf([string().example(\"5ce7ad3028890bd71749d477\").description(\"Mongoose Ref ObjectId\"), lazyRef(getType)]);\n\n store.type = Object;\n })\n ) as PropertyDecorator;\n}\n\nexport type Ref<T> = T | string;\n"]}
1
+ {"version":3,"file":"ref.js","sourceRoot":"","sources":["../../../src/decorators/ref.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAQ,aAAa,EAAC,MAAM,YAAY,CAAC;AAC1F,OAAO,EAAC,aAAa,EAAE,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAC,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AACxG,OAAO,EAAC,MAAM,IAAI,cAAc,EAAC,MAAM,UAAU,CAAC;AAClD,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAC,mBAAmB,EAAC,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAO5D,SAAS,KAAK,CAAC,KAA+B;IAC5C,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,cAAc,CAAC,eAAyB;IAC/C,OAAO,aAAa,CAClB,aAAa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AAC,OAAO,CAAC;QACvG,GAAG,EAAE,KAAK;KACX,CAAC,EACF,aAAa,CAAC,CAAC,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,WAAW,CAAC,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,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"]}
@@ -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 type
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, type?: MongooseSchemaTypes): PropertyDecorator;
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.94.0",
3
+ "version": "6.95.0",
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.94.0",
34
- "@tsed/core": "6.94.0",
35
- "@tsed/di": "6.94.0",
36
- "@tsed/json-mapper": "6.94.0",
33
+ "@tsed/ajv": "6.95.0",
34
+ "@tsed/core": "6.95.0",
35
+ "@tsed/di": "6.95.0",
36
+ "@tsed/json-mapper": "6.95.0",
37
37
  "@tsed/logger": "^6.0.0",
38
- "@tsed/schema": "6.94.0",
38
+ "@tsed/schema": "6.95.0",
39
39
  "mongoose": "^5.12.2"
40
40
  },
41
41
  "peerDependencies": {
42
- "@tsed/ajv": "^6.94.0",
43
- "@tsed/core": "^6.94.0",
44
- "@tsed/di": "^6.94.0",
45
- "@tsed/json-mapper": "^6.94.0",
42
+ "@tsed/ajv": "^6.95.0",
43
+ "@tsed/core": "^6.95.0",
44
+ "@tsed/di": "^6.95.0",
45
+ "@tsed/json-mapper": "^6.95.0",
46
46
  "@tsed/logger": ">=6.0.0",
47
- "@tsed/schema": "^6.94.0",
47
+ "@tsed/schema": "^6.95.0",
48
48
  "mongoose": ">=5.12.2"
49
49
  },
50
50
  "repository": "https://github.com/tsedio/tsed",