@tsed/mongoose 8.11.1 → 8.11.2-rc.1

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.
Files changed (168) hide show
  1. package/lib/esm/MongooseModule.js +0 -1
  2. package/lib/esm/constants/constants.js +0 -1
  3. package/lib/esm/decorators/auto.js +0 -1
  4. package/lib/esm/decorators/discriminatorKey.js +0 -1
  5. package/lib/esm/decorators/dynamicRef.js +0 -1
  6. package/lib/esm/decorators/excludeIndexes.js +0 -1
  7. package/lib/esm/decorators/expires.js +0 -1
  8. package/lib/esm/decorators/immutable.js +0 -1
  9. package/lib/esm/decorators/indexed.js +0 -1
  10. package/lib/esm/decorators/lowercase.js +0 -1
  11. package/lib/esm/decorators/model.js +0 -1
  12. package/lib/esm/decorators/mongooseIndex.js +0 -1
  13. package/lib/esm/decorators/mongooseIndexes.js +0 -1
  14. package/lib/esm/decorators/mongoosePlugin.js +0 -1
  15. package/lib/esm/decorators/numberDecimal.js +0 -1
  16. package/lib/esm/decorators/objectID.js +0 -1
  17. package/lib/esm/decorators/postHook.js +0 -1
  18. package/lib/esm/decorators/preHook.js +0 -1
  19. package/lib/esm/decorators/ref.js +0 -1
  20. package/lib/esm/decorators/schema.js +0 -1
  21. package/lib/esm/decorators/schemaIgnore.js +0 -1
  22. package/lib/esm/decorators/select.js +0 -1
  23. package/lib/esm/decorators/sparse.js +0 -1
  24. package/lib/esm/decorators/text.js +0 -1
  25. package/lib/esm/decorators/trim.js +0 -1
  26. package/lib/esm/decorators/unique.js +0 -1
  27. package/lib/esm/decorators/uppercase.js +0 -1
  28. package/lib/esm/decorators/versionKey.js +0 -1
  29. package/lib/esm/decorators/virtualRef.js +0 -1
  30. package/lib/esm/index.js +0 -1
  31. package/lib/esm/interfaces/MongooseConnectionOptions.js +0 -1
  32. package/lib/esm/interfaces/MongooseDocument.js +0 -1
  33. package/lib/esm/interfaces/MongooseModel.js +0 -1
  34. package/lib/esm/interfaces/MongooseModelOptions.js +0 -1
  35. package/lib/esm/interfaces/MongooseSchemaOptions.js +0 -1
  36. package/lib/esm/interfaces/MongooseSchemaTypes.js +0 -1
  37. package/lib/esm/interfaces/MongooseVirtualRefOptions.js +0 -1
  38. package/lib/esm/interfaces/interfaces.js +0 -1
  39. package/lib/esm/registries/MongooseModels.js +0 -1
  40. package/lib/esm/services/MongooseConnections.js +0 -1
  41. package/lib/esm/services/MongooseService.js +0 -1
  42. package/lib/esm/utils/createModel.js +0 -1
  43. package/lib/esm/utils/createSchema.js +0 -1
  44. package/lib/esm/utils/resolveRefType.js +0 -1
  45. package/lib/esm/utils/schemaOptions.js +0 -1
  46. package/package.json +13 -10
  47. package/lib/esm/MongooseModule.js.map +0 -1
  48. package/lib/esm/constants/constants.js.map +0 -1
  49. package/lib/esm/decorators/auto.js.map +0 -1
  50. package/lib/esm/decorators/discriminatorKey.js.map +0 -1
  51. package/lib/esm/decorators/dynamicRef.js.map +0 -1
  52. package/lib/esm/decorators/excludeIndexes.js.map +0 -1
  53. package/lib/esm/decorators/expires.js.map +0 -1
  54. package/lib/esm/decorators/immutable.js.map +0 -1
  55. package/lib/esm/decorators/indexed.js.map +0 -1
  56. package/lib/esm/decorators/lowercase.js.map +0 -1
  57. package/lib/esm/decorators/model.js.map +0 -1
  58. package/lib/esm/decorators/mongooseIndex.js.map +0 -1
  59. package/lib/esm/decorators/mongooseIndexes.js.map +0 -1
  60. package/lib/esm/decorators/mongoosePlugin.js.map +0 -1
  61. package/lib/esm/decorators/numberDecimal.js.map +0 -1
  62. package/lib/esm/decorators/objectID.js.map +0 -1
  63. package/lib/esm/decorators/postHook.js.map +0 -1
  64. package/lib/esm/decorators/preHook.js.map +0 -1
  65. package/lib/esm/decorators/ref.js.map +0 -1
  66. package/lib/esm/decorators/schema.js.map +0 -1
  67. package/lib/esm/decorators/schemaIgnore.js.map +0 -1
  68. package/lib/esm/decorators/select.js.map +0 -1
  69. package/lib/esm/decorators/sparse.js.map +0 -1
  70. package/lib/esm/decorators/text.js.map +0 -1
  71. package/lib/esm/decorators/trim.js.map +0 -1
  72. package/lib/esm/decorators/unique.js.map +0 -1
  73. package/lib/esm/decorators/uppercase.js.map +0 -1
  74. package/lib/esm/decorators/versionKey.js.map +0 -1
  75. package/lib/esm/decorators/virtualRef.js.map +0 -1
  76. package/lib/esm/index.js.map +0 -1
  77. package/lib/esm/interfaces/MongooseConnectionOptions.js.map +0 -1
  78. package/lib/esm/interfaces/MongooseDocument.js.map +0 -1
  79. package/lib/esm/interfaces/MongooseModel.js.map +0 -1
  80. package/lib/esm/interfaces/MongooseModelOptions.js.map +0 -1
  81. package/lib/esm/interfaces/MongooseSchemaOptions.js.map +0 -1
  82. package/lib/esm/interfaces/MongooseSchemaTypes.js.map +0 -1
  83. package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +0 -1
  84. package/lib/esm/interfaces/interfaces.js.map +0 -1
  85. package/lib/esm/registries/MongooseModels.js.map +0 -1
  86. package/lib/esm/services/MongooseConnections.js.map +0 -1
  87. package/lib/esm/services/MongooseService.js.map +0 -1
  88. package/lib/esm/utils/createModel.js.map +0 -1
  89. package/lib/esm/utils/createSchema.js.map +0 -1
  90. package/lib/esm/utils/resolveRefType.js.map +0 -1
  91. package/lib/esm/utils/schemaOptions.js.map +0 -1
  92. package/src/MongooseModule.ts +0 -32
  93. package/src/constants/constants.ts +0 -20
  94. package/src/decorators/auto.spec.ts +0 -30
  95. package/src/decorators/auto.ts +0 -24
  96. package/src/decorators/discriminatorKey.ts +0 -1
  97. package/src/decorators/dynamicRef.spec.ts +0 -46
  98. package/src/decorators/dynamicRef.ts +0 -76
  99. package/src/decorators/excludeIndexes.spec.ts +0 -30
  100. package/src/decorators/excludeIndexes.ts +0 -24
  101. package/src/decorators/expires.spec.ts +0 -18
  102. package/src/decorators/expires.ts +0 -24
  103. package/src/decorators/immutable.spec.ts +0 -30
  104. package/src/decorators/immutable.ts +0 -26
  105. package/src/decorators/indexed.spec.ts +0 -19
  106. package/src/decorators/indexed.ts +0 -24
  107. package/src/decorators/lowercase.spec.ts +0 -30
  108. package/src/decorators/lowercase.ts +0 -24
  109. package/src/decorators/model.ts +0 -69
  110. package/src/decorators/mongooseIndex.spec.ts +0 -26
  111. package/src/decorators/mongooseIndex.ts +0 -35
  112. package/src/decorators/mongooseIndexes.spec.ts +0 -35
  113. package/src/decorators/mongooseIndexes.ts +0 -35
  114. package/src/decorators/mongoosePlugin.spec.ts +0 -25
  115. package/src/decorators/mongoosePlugin.ts +0 -20
  116. package/src/decorators/numberDecimal.spec.ts +0 -266
  117. package/src/decorators/numberDecimal.ts +0 -84
  118. package/src/decorators/objectID.spec.ts +0 -24
  119. package/src/decorators/objectID.ts +0 -39
  120. package/src/decorators/postHook.spec.ts +0 -44
  121. package/src/decorators/postHook.ts +0 -72
  122. package/src/decorators/preHook.spec.ts +0 -75
  123. package/src/decorators/preHook.ts +0 -79
  124. package/src/decorators/ref.spec.ts +0 -361
  125. package/src/decorators/ref.ts +0 -111
  126. package/src/decorators/schema.ts +0 -79
  127. package/src/decorators/schemaIgnore.spec.ts +0 -18
  128. package/src/decorators/schemaIgnore.ts +0 -24
  129. package/src/decorators/select.spec.ts +0 -18
  130. package/src/decorators/select.ts +0 -24
  131. package/src/decorators/sparse.spec.ts +0 -30
  132. package/src/decorators/sparse.ts +0 -26
  133. package/src/decorators/text.spec.ts +0 -30
  134. package/src/decorators/text.ts +0 -25
  135. package/src/decorators/trim.spec.ts +0 -18
  136. package/src/decorators/trim.ts +0 -23
  137. package/src/decorators/unique.spec.ts +0 -18
  138. package/src/decorators/unique.ts +0 -23
  139. package/src/decorators/uppercase.spec.ts +0 -30
  140. package/src/decorators/uppercase.ts +0 -24
  141. package/src/decorators/versionKey.spec.ts +0 -19
  142. package/src/decorators/versionKey.ts +0 -9
  143. package/src/decorators/virtualRef.spec.ts +0 -383
  144. package/src/decorators/virtualRef.ts +0 -73
  145. package/src/index.ts +0 -47
  146. package/src/interfaces/MongooseConnectionOptions.ts +0 -10
  147. package/src/interfaces/MongooseDocument.ts +0 -3
  148. package/src/interfaces/MongooseModel.ts +0 -10
  149. package/src/interfaces/MongooseModelOptions.ts +0 -8
  150. package/src/interfaces/MongooseSchemaOptions.ts +0 -61
  151. package/src/interfaces/MongooseSchemaTypes.ts +0 -5
  152. package/src/interfaces/MongooseVirtualRefOptions.ts +0 -10
  153. package/src/interfaces/interfaces.ts +0 -9
  154. package/src/registries/MongooseModels.ts +0 -3
  155. package/src/services/MongooseConnection.spec.ts +0 -70
  156. package/src/services/MongooseConnections.ts +0 -58
  157. package/src/services/MongooseService.spec.ts +0 -73
  158. package/src/services/MongooseService.ts +0 -77
  159. package/src/utils/buildMongooseSchema.spec.ts +0 -67
  160. package/src/utils/createModel.spec.ts +0 -49
  161. package/src/utils/createModel.ts +0 -54
  162. package/src/utils/createSchema.spec.ts +0 -817
  163. package/src/utils/createSchema.ts +0 -207
  164. package/src/utils/resolveRefType.spec.ts +0 -30
  165. package/src/utils/resolveRefType.ts +0 -18
  166. package/src/utils/schemaOptions.spec.ts +0 -68
  167. package/src/utils/schemaOptions.ts +0 -74
  168. package/vitest.config.mts +0 -25
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Text} from "./text.js";
5
-
6
- describe("@Text()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Text()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- text: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Text(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- text: true
28
- });
29
- });
30
- });
@@ -1,25 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
5
- * will build a text index on this path.
6
- *
7
- * ### Example
8
- *
9
- * ```typescript
10
- * @Model()
11
- * export class EventModel {
12
- * @Text()
13
- * field: string;
14
- * }
15
- * ```
16
- *
17
- * @param {boolean | number | any} text
18
- * @returns {Function}
19
- * @decorator
20
- * @mongoose
21
- * @property
22
- */
23
- export function Text(text: boolean | number | any = true): PropertyDecorator {
24
- return MongooseSchema({text} as any);
25
- }
@@ -1,18 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Trim} from "./trim.js";
5
-
6
- describe("@Trim()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @Trim()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- trim: true
16
- });
17
- });
18
- });
@@ -1,23 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * Tell Mongoose to make the property trimmable.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Trim()
12
- * name: string;
13
- * }
14
- * ```
15
- *
16
- * @returns {Function}
17
- * @decorator
18
- * @mongoose
19
- * @property
20
- */
21
- export function Trim() {
22
- return MongooseSchema({trim: true});
23
- }
@@ -1,18 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {Unique} from "../../src/index.js";
4
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
5
-
6
- describe("@Unique()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @Unique()
10
- id: string;
11
- }
12
-
13
- const store = Store.from(Test, "id");
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- unique: true
16
- });
17
- });
18
- });
@@ -1,23 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * Tell Mongoose to ensure a unique index is created for this path.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Unique()
12
- * index: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean | any} unique
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- */
21
- export function Unique(unique: boolean | any = true) {
22
- return MongooseSchema({unique});
23
- }
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Uppercase} from "./uppercase.js";
5
-
6
- describe("@Uppercase()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Uppercase()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- uppercase: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Uppercase(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- uppercase: true
28
- });
29
- });
30
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in String#toUpperCase().
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Uppercase()
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean} uppercase
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Uppercase(uppercase: boolean = true): PropertyDecorator {
23
- return MongooseSchema({uppercase} as any);
24
- }
@@ -1,19 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
4
- import {Model} from "./model.js";
5
- import {VersionKey} from "./versionKey.js";
6
-
7
- describe("@VersionKey()", () => {
8
- it("should set metadata", () => {
9
- @Model()
10
- class TestVersionKey {
11
- @VersionKey()
12
- rev: number;
13
- }
14
-
15
- expect(Store.from(TestVersionKey).get(MONGOOSE_SCHEMA_OPTIONS)).toEqual({
16
- versionKey: "rev"
17
- });
18
- });
19
- });
@@ -1,9 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
4
-
5
- export function VersionKey(): PropertyDecorator {
6
- return (target: any, propertyKey: string) => {
7
- Store.from(target).merge(MONGOOSE_SCHEMA_OPTIONS, {versionKey: propertyKey});
8
- };
9
- }
@@ -1,383 +0,0 @@
1
- import {Store} from "@tsed/core";
2
- import {Controller} from "@tsed/di";
3
- import {BodyParams} from "@tsed/platform-params";
4
- import {Format, getJsonSchema, getSpec, Post, Property, ReadOnly, Returns, SpecTypes} from "@tsed/schema";
5
-
6
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
7
- import {Model} from "./model.js";
8
- import {VirtualRef, VirtualRefs} from "./virtualRef.js";
9
-
10
- describe("@VirtualRef()", () => {
11
- describe("when type and foreign value are given", () => {
12
- it("should set metadata", () => {
13
- // WHEN
14
- class Test {
15
- @VirtualRef("RefTest", "foreign")
16
- test: any;
17
- }
18
-
19
- // THEN
20
- const store = Store.from(Test, "test");
21
-
22
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
23
- ref: "RefTest",
24
- justOne: false,
25
- count: false,
26
- foreignField: "foreign",
27
- localField: "_id",
28
- options: undefined
29
- });
30
- });
31
- });
32
-
33
- describe("when options is given with minimal fields", () => {
34
- it("should set metadata", () => {
35
- // WHEN
36
- class Test {
37
- @VirtualRef({ref: "RefTest", foreignField: "foreign"})
38
- test: any;
39
- }
40
-
41
- // THEN
42
- const store = Store.from(Test, "test");
43
-
44
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
45
- ref: "RefTest",
46
- localField: "_id",
47
- foreignField: "foreign",
48
- justOne: false,
49
- count: false,
50
- options: undefined
51
- });
52
- });
53
- });
54
-
55
- describe("when options is given with all fields", () => {
56
- it("should set metadata", () => {
57
- // WHEN
58
- class Test {
59
- @VirtualRef({
60
- ref: "RefTest",
61
- foreignField: "foreign",
62
- localField: "test_2",
63
- justOne: true,
64
- count: true,
65
- options: {}
66
- })
67
- test: any;
68
- }
69
-
70
- // THEN
71
- const store = Store.from(Test, "test");
72
-
73
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
74
- ref: "RefTest",
75
- localField: "test_2",
76
- foreignField: "foreign",
77
- justOne: true,
78
- count: true,
79
- options: {}
80
- });
81
- });
82
- });
83
-
84
- describe("with a given model", () => {
85
- it("should set metadata", () => {
86
- @Model({name: "VirtualTestPerson"})
87
- class TestPerson {
88
- @Property()
89
- name: string;
90
-
91
- @Property()
92
- band: string;
93
- }
94
-
95
- // WHEN
96
- @Model({name: "VirtualTestBand"})
97
- class TestBand {
98
- @VirtualRef({
99
- ref: TestPerson,
100
- foreignField: "foreign",
101
- localField: "test_2",
102
- justOne: true,
103
- count: false,
104
- options: {}
105
- })
106
- members: VirtualRef<TestPerson>;
107
- }
108
-
109
- // THEN
110
- const store = Store.from(TestBand, "members");
111
-
112
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
113
- ref: "VirtualTestPerson",
114
- localField: "test_2",
115
- foreignField: "foreign",
116
- justOne: true,
117
- count: false,
118
- options: {}
119
- });
120
-
121
- expect(getJsonSchema(TestBand)).toEqual({
122
- definitions: {
123
- TestPerson: {
124
- properties: {
125
- band: {
126
- type: "string"
127
- },
128
- name: {
129
- type: "string"
130
- }
131
- },
132
- type: "object"
133
- }
134
- },
135
- properties: {
136
- members: {
137
- $ref: "#/definitions/TestPerson"
138
- }
139
- },
140
- type: "object"
141
- });
142
- });
143
- it("should generate spec", () => {
144
- @Model({name: "VirtualTestPerson"})
145
- class TestPerson {
146
- @Property()
147
- name: string;
148
-
149
- @Property()
150
- band: string;
151
- }
152
-
153
- // WHEN
154
- @Model({name: "VirtualTestBand"})
155
- class TestBand {
156
- @Format("date-time")
157
- @ReadOnly()
158
- createdAt: number = Date.now();
159
-
160
- @Format("date-time")
161
- @ReadOnly()
162
- updatedAt: number = Date.now();
163
-
164
- @ReadOnly()
165
- @VirtualRef({
166
- ref: TestPerson,
167
- foreignField: "foreign",
168
- localField: "test_2",
169
- justOne: true,
170
- options: {}
171
- })
172
- members: VirtualRef<TestPerson>;
173
- }
174
-
175
- @Controller("/")
176
- class MyCtrl {
177
- @Post("/")
178
- @Returns(200, TestBand)
179
- post(@BodyParams() model: TestBand) {}
180
- }
181
-
182
- // THEN
183
- const store = Store.from(TestBand, "members");
184
-
185
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
186
- ref: "VirtualTestPerson",
187
- localField: "test_2",
188
- foreignField: "foreign",
189
- justOne: true,
190
- count: false,
191
- options: {}
192
- });
193
-
194
- expect(getSpec(MyCtrl, {specType: SpecTypes.OPENAPI})).toEqual({
195
- components: {
196
- schemas: {
197
- TestBand: {
198
- properties: {
199
- createdAt: {
200
- format: "date-time",
201
- readOnly: true,
202
- type: "number"
203
- },
204
- members: {
205
- anyOf: [
206
- {
207
- $ref: "#/components/schemas/TestPerson"
208
- }
209
- ],
210
- readOnly: true
211
- },
212
- updatedAt: {
213
- format: "date-time",
214
- readOnly: true,
215
- type: "number"
216
- }
217
- },
218
- type: "object"
219
- },
220
- TestPerson: {
221
- properties: {
222
- band: {
223
- type: "string"
224
- },
225
- name: {
226
- type: "string"
227
- }
228
- },
229
- type: "object"
230
- }
231
- }
232
- },
233
- paths: {
234
- "/": {
235
- post: {
236
- operationId: "myCtrlPost",
237
- parameters: [],
238
- requestBody: {
239
- content: {
240
- "application/json": {
241
- schema: {
242
- $ref: "#/components/schemas/TestBand"
243
- }
244
- }
245
- },
246
- required: false
247
- },
248
- responses: {
249
- "200": {
250
- content: {
251
- "application/json": {
252
- schema: {
253
- $ref: "#/components/schemas/TestBand"
254
- }
255
- }
256
- },
257
- description: "Success"
258
- }
259
- },
260
- tags: ["MyCtrl"]
261
- }
262
- }
263
- },
264
- tags: [
265
- {
266
- name: "MyCtrl"
267
- }
268
- ]
269
- });
270
- });
271
- });
272
-
273
- describe("with a given model as string ref", () => {
274
- it("should set metadata and json schema", () => {
275
- // WHEN
276
- @Model()
277
- class VirtualRefStringTestBand {
278
- @VirtualRef({
279
- ref: "VirtualRefStringTestPerson",
280
- foreignField: "foreign",
281
- localField: "test_2",
282
- justOne: true,
283
- count: false,
284
- options: {}
285
- })
286
- member: VirtualRef<VirtualRefStringTestPerson>;
287
-
288
- @VirtualRef({
289
- ref: "VirtualRefStringTestPerson",
290
- foreignField: "foreign",
291
- localField: "test_2",
292
- justOne: false,
293
- count: false,
294
- options: {}
295
- })
296
- members: VirtualRefStringTestPerson[];
297
-
298
- @VirtualRef({
299
- ref: "VirtualRefStringTestPerson",
300
- foreignField: "foreign",
301
- localField: "test_2",
302
- count: true,
303
- options: {}
304
- })
305
- memberCount: number;
306
- }
307
-
308
- @Model()
309
- class VirtualRefStringTestPerson {
310
- @Property()
311
- name: string;
312
-
313
- @Property()
314
- band: string;
315
- }
316
-
317
- // THEN
318
- const membersStore = Store.from(VirtualRefStringTestBand, "members");
319
- const memberStore = Store.from(VirtualRefStringTestBand, "member");
320
- const memberCount = Store.from(VirtualRefStringTestBand, "memberCount");
321
-
322
- expect(memberStore.get(MONGOOSE_SCHEMA)).toEqual({
323
- ref: "VirtualRefStringTestPerson",
324
- localField: "test_2",
325
- foreignField: "foreign",
326
- justOne: true,
327
- count: false,
328
- options: {}
329
- });
330
-
331
- expect(membersStore.get(MONGOOSE_SCHEMA)).toEqual({
332
- ref: "VirtualRefStringTestPerson",
333
- localField: "test_2",
334
- foreignField: "foreign",
335
- justOne: false,
336
- count: false,
337
- options: {}
338
- });
339
-
340
- expect(memberCount.get(MONGOOSE_SCHEMA)).toEqual({
341
- ref: "VirtualRefStringTestPerson",
342
- localField: "test_2",
343
- foreignField: "foreign",
344
- justOne: false,
345
- count: true,
346
- options: {}
347
- });
348
-
349
- const schema = getJsonSchema(VirtualRefStringTestBand);
350
-
351
- expect(schema).toEqual({
352
- definitions: {
353
- VirtualRefStringTestPerson: {
354
- properties: {
355
- band: {
356
- type: "string"
357
- },
358
- name: {
359
- type: "string"
360
- }
361
- },
362
- type: "object"
363
- }
364
- },
365
- properties: {
366
- members: {
367
- type: "array",
368
- items: {
369
- $ref: "#/definitions/VirtualRefStringTestPerson"
370
- }
371
- },
372
- member: {
373
- $ref: "#/definitions/VirtualRefStringTestPerson"
374
- },
375
- memberCount: {
376
- type: "number"
377
- }
378
- },
379
- type: "object"
380
- });
381
- });
382
- });
383
- });
@@ -1,73 +0,0 @@
1
- import {isPlainObject, isString, Store, StoreMerge, useDecorators} from "@tsed/core";
2
- import {CollectionOf, Property} from "@tsed/schema";
3
-
4
- import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../constants/constants.js";
5
- import {MongooseVirtualRefOptions} from "../interfaces/MongooseVirtualRefOptions.js";
6
- import {MongooseModels} from "../registries/MongooseModels.js";
7
-
8
- function getRef(opts: any) {
9
- const ref = opts.ref;
10
-
11
- return isString(ref) ? ref : Store.from(ref).get(MONGOOSE_MODEL_NAME);
12
- }
13
-
14
- function getType(opts: any) {
15
- const ref = opts.ref;
16
- return !isString(ref) ? ref : MongooseModels.get(ref) || (() => MongooseModels.get(ref) || Object);
17
- }
18
-
19
- function getInitialOpts(options: string | MongooseVirtualRefOptions, foreignField?: string): any {
20
- return isPlainObject(options) ? options : {ref: options as any, foreignField};
21
- }
22
-
23
- function mapToSchema(opts: any) {
24
- const ref = getRef(opts);
25
-
26
- const schema: any = {
27
- ref,
28
- localField: opts.localField || "_id",
29
- foreignField: opts.foreignField,
30
- justOne: opts.justOne || false,
31
- count: opts.count || false,
32
- options: opts.options
33
- };
34
-
35
- return schema;
36
- }
37
-
38
- /**
39
- * Define a property as mongoose virtual reference to other Model (decorated with @Model).
40
- *
41
- * ::: warning
42
- * To avoid circular dependencies, do not use the virtual reference model in
43
- * anything except a type declaration. Using the virtual reference model will prevent
44
- * typescript transpiler from stripping away the import statement and cause a circular
45
- * import in node.
46
- * :::
47
- *
48
- * @param ref
49
- * @param foreignField
50
- * @returns {Function}
51
- * @decorator
52
- * @mongoose
53
- * @property
54
- */
55
- export function VirtualRef(ref: string, foreignField: string): Function;
56
- export function VirtualRef(options: MongooseVirtualRefOptions): Function;
57
- export function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function;
58
- export function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function {
59
- const opts = getInitialOpts(options, foreignField);
60
- const schema = mapToSchema(opts);
61
- const type = getType(opts);
62
-
63
- return useDecorators(
64
- StoreMerge(MONGOOSE_SCHEMA, schema),
65
- schema.count ? Property(Number) : type && (schema.justOne ? Property(type) : CollectionOf(type, Array))
66
- );
67
- }
68
-
69
- export type VirtualRef<T> = T | null;
70
- /**
71
- * @deprecated Use T[] instead
72
- */
73
- export type VirtualRefs<T> = T[];