@tsed/mongoose 8.11.1 → 8.12.0-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,361 +0,0 @@
1
- import {catchError, Store} from "@tsed/core";
2
- import {getJsonSchema, Property} from "@tsed/schema";
3
- import {Schema} from "mongoose";
4
-
5
- import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../constants/constants.js";
6
- import {MongooseModels} from "../registries/MongooseModels.js";
7
- import {Ref} from "./ref.js";
8
-
9
- describe("@Ref()", () => {
10
- describe("type is a class", () => {
11
- it("should set metadata and catch error", () => {
12
- const error = catchError(() => {
13
- class Model {
14
- @Ref(undefined)
15
- num: number[];
16
- }
17
- });
18
-
19
- expect(error?.message).toEqual(
20
- "A model is required on `@Ref(model)` decorator. Please give a model or wrap it inside an arrow function if you have a circular reference."
21
- );
22
- });
23
-
24
- it("should set metadata", () => {
25
- class RefTest {
26
- @Property()
27
- id: string;
28
- }
29
-
30
- Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");
31
-
32
- class Test {
33
- @Ref(RefTest)
34
- test: Ref<RefTest>;
35
- }
36
-
37
- const store = Store.from(Test, "test");
38
- const schema = getJsonSchema(Test);
39
-
40
- expect(schema).toEqual({
41
- definitions: {
42
- RefTest: {
43
- type: "object",
44
- properties: {
45
- id: {
46
- type: "string"
47
- }
48
- }
49
- }
50
- },
51
- properties: {
52
- test: {
53
- oneOf: [
54
- {
55
- description: "A reference ObjectID",
56
- examples: ["5ce7ad3028890bd71749d477"],
57
- type: "string"
58
- },
59
- {
60
- $ref: "#/definitions/RefTest"
61
- }
62
- ]
63
- }
64
- },
65
- type: "object"
66
- });
67
-
68
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
69
- type: Schema.Types.ObjectId,
70
- ref: RefTest
71
- });
72
- });
73
- });
74
- describe("type is a map of class", () => {
75
- it("should set metadata", () => {
76
- class RefTest {
77
- @Property()
78
- id: string;
79
- }
80
-
81
- Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");
82
- MongooseModels.set("RefTest", RefTest);
83
-
84
- class Test {
85
- @Ref(RefTest)
86
- test: Map<string, Ref<RefTest>>;
87
- }
88
-
89
- const store = Store.from(Test, "test");
90
- const schema = getJsonSchema(Test);
91
-
92
- expect(schema).toEqual({
93
- definitions: {
94
- RefTest: {
95
- properties: {
96
- id: {
97
- type: "string"
98
- }
99
- },
100
- type: "object"
101
- }
102
- },
103
- properties: {
104
- test: {
105
- additionalProperties: {
106
- oneOf: [
107
- {
108
- description: "A reference ObjectID",
109
- examples: ["5ce7ad3028890bd71749d477"],
110
- type: "string"
111
- },
112
- {
113
- $ref: "#/definitions/RefTest"
114
- }
115
- ]
116
- },
117
- type: "object"
118
- }
119
- },
120
- type: "object"
121
- });
122
-
123
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
124
- type: Schema.Types.ObjectId,
125
- ref: RefTest
126
- });
127
- });
128
- });
129
- describe("type is a Function", () => {
130
- it("should set metadata", () => {
131
- class RefTest {
132
- @Property()
133
- id: string;
134
- }
135
-
136
- Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");
137
- const arrow = () => RefTest;
138
-
139
- class Test {
140
- @Ref(arrow)
141
- test: Ref<RefTest>;
142
- }
143
-
144
- const store = Store.from(Test, "test");
145
- const schema = getJsonSchema(Test);
146
-
147
- expect(schema).toEqual({
148
- definitions: {
149
- RefTest: {
150
- type: "object",
151
- properties: {
152
- id: {
153
- type: "string"
154
- }
155
- }
156
- }
157
- },
158
- properties: {
159
- test: {
160
- oneOf: [
161
- {
162
- description: "A reference ObjectID",
163
- examples: ["5ce7ad3028890bd71749d477"],
164
- type: "string"
165
- },
166
- {
167
- $ref: "#/definitions/RefTest"
168
- }
169
- ]
170
- }
171
- },
172
- type: "object"
173
- });
174
-
175
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
176
- type: Schema.Types.ObjectId,
177
- ref: arrow
178
- });
179
- });
180
- });
181
- describe("type is a string (deprecated)", () => {
182
- it("should set metadata", () => {
183
- class RefTest {}
184
-
185
- class Test {
186
- @Ref("RefTest")
187
- test: Ref<RefTest>;
188
- }
189
-
190
- MongooseModels.set("RefTest", RefTest);
191
- const store = Store.from(Test, "test");
192
-
193
- expect(getJsonSchema(Test)).toEqual({
194
- definitions: {
195
- RefTest: {
196
- properties: {
197
- id: {
198
- type: "string"
199
- }
200
- },
201
- type: "object"
202
- }
203
- },
204
- properties: {
205
- test: {
206
- oneOf: [
207
- {
208
- description: "A reference ObjectID",
209
- examples: ["5ce7ad3028890bd71749d477"],
210
- type: "string"
211
- },
212
- {
213
- $ref: "#/definitions/RefTest"
214
- }
215
- ]
216
- }
217
- },
218
- type: "object"
219
- });
220
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
221
- type: Schema.Types.ObjectId,
222
- ref: "RefTest"
223
- });
224
- });
225
- });
226
- describe("JsonSchema gets generated based on populated groups", () => {
227
- class MyChildModel {
228
- @Property()
229
- test: string;
230
- }
231
-
232
- class MyParentModel {
233
- @Property()
234
- id: string;
235
-
236
- @Ref(MyChildModel, {populatedGroups: ["group1", "group2"]})
237
- child1: Ref<MyChildModel>;
238
-
239
- @Ref(MyChildModel, {populatedGroups: ["group2"]})
240
- child2: Ref<MyChildModel>;
241
-
242
- @Ref(MyChildModel)
243
- child3: Ref<MyChildModel>;
244
- }
245
-
246
- it("should reflect the populated groups options in the schema (with given groups)", () => {
247
- const spec = getJsonSchema(MyParentModel, {
248
- groups: ["group1", "group3"]
249
- });
250
-
251
- expect(spec).toEqual({
252
- definitions: {
253
- MyChildModel: {
254
- properties: {
255
- test: {
256
- type: "string"
257
- }
258
- },
259
- type: "object"
260
- },
261
- MyChildModelGroup1Group3: {
262
- properties: {
263
- test: {
264
- type: "string"
265
- }
266
- },
267
- type: "object"
268
- }
269
- },
270
- properties: {
271
- child1: {
272
- oneOf: [
273
- {
274
- $ref: "#/definitions/MyChildModelGroup1Group3"
275
- }
276
- ]
277
- },
278
- child2: {
279
- oneOf: [
280
- {
281
- description: "A reference ObjectID",
282
- examples: ["5ce7ad3028890bd71749d477"],
283
- type: "string"
284
- }
285
- ]
286
- },
287
- child3: {
288
- oneOf: [
289
- {
290
- description: "A reference ObjectID",
291
- examples: ["5ce7ad3028890bd71749d477"],
292
- type: "string"
293
- },
294
- {
295
- $ref: "#/definitions/MyChildModel"
296
- }
297
- ]
298
- },
299
- id: {
300
- type: "string"
301
- }
302
- },
303
- type: "object"
304
- });
305
- });
306
-
307
- it("should reflect the populated groups options in the schema (without given groups)", () => {
308
- const spec = getJsonSchema(MyParentModel, {
309
- groups: []
310
- });
311
- expect(spec).toEqual({
312
- definitions: {
313
- MyChildModel: {
314
- properties: {
315
- test: {
316
- type: "string"
317
- }
318
- },
319
- type: "object"
320
- }
321
- },
322
- properties: {
323
- child1: {
324
- oneOf: [
325
- {
326
- description: "A reference ObjectID",
327
- examples: ["5ce7ad3028890bd71749d477"],
328
- type: "string"
329
- }
330
- ]
331
- },
332
- child2: {
333
- oneOf: [
334
- {
335
- description: "A reference ObjectID",
336
- examples: ["5ce7ad3028890bd71749d477"],
337
- type: "string"
338
- }
339
- ]
340
- },
341
- child3: {
342
- oneOf: [
343
- {
344
- description: "A reference ObjectID",
345
- examples: ["5ce7ad3028890bd71749d477"],
346
- type: "string"
347
- },
348
- {
349
- $ref: "#/definitions/MyChildModel"
350
- }
351
- ]
352
- },
353
- id: {
354
- type: "string"
355
- }
356
- },
357
- type: "object"
358
- });
359
- });
360
- });
361
- });
@@ -1,111 +0,0 @@
1
- import {isArrowFn, isCollection, isObject, isObjectID, isString, StoreMerge, Type, useDecorators} from "@tsed/core";
2
- import {deserialize, OnDeserialize, OnSerialize, serialize} from "@tsed/json-mapper";
3
- import {ForwardGroups, JsonEntityFn, lazyRef, matchGroups, OneOf, Property, string} from "@tsed/schema";
4
- import {Schema as MongooseSchema} from "mongoose";
5
-
6
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
7
- import {MongooseSchemaTypes} from "../interfaces/MongooseSchemaTypes.js";
8
- import {MongooseModels} from "../registries/MongooseModels.js";
9
-
10
- interface RefOptions {
11
- type?: MongooseSchemaTypes;
12
- populatedGroups?: string[];
13
- }
14
-
15
- function isRef(value: undefined | string | any) {
16
- return isObjectID(value) || isString(value);
17
- }
18
-
19
- function PopulateGroups(populatedGroups: string[]) {
20
- return useDecorators(
21
- ForwardGroups(true),
22
- JsonEntityFn((store) => {
23
- store.schema.$hooks.on("oneOf", (obj: any[], givenGroups: string[]) => {
24
- if (matchGroups(populatedGroups, givenGroups)) {
25
- return obj.filter((x) => x.type === "string"); // keep the object id;
26
- } else {
27
- return obj.filter((x) => x.type !== "string"); // keep the ref definition
28
- }
29
- });
30
- })
31
- );
32
- }
33
-
34
- /**
35
- * Define a property as mongoose reference to other Model (decorated with @Model).
36
- *
37
- * ### Example
38
- *
39
- * ```typescript
40
- *
41
- * @Model()
42
- * class FooModel {
43
- *
44
- * @Ref(Foo2Model)
45
- * field: Ref<Foo2Model>
46
- *
47
- * @Ref(Foo2Model)
48
- * list: Ref<Foo2Model>[]
49
- * }
50
- *
51
- * @Model()
52
- * class Foo2Model {
53
- * }
54
- * ```
55
- *
56
- * @param model
57
- * @param options
58
- * @returns {Function}
59
- * @decorator
60
- * @mongoose
61
- * @property
62
- */
63
- export function Ref(
64
- model: string | (() => Type) | any,
65
- options: RefOptions | MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID
66
- ): PropertyDecorator {
67
- if (!model) {
68
- throw new Error(
69
- "A model is required on `@Ref(model)` decorator. Please give a model or wrap it inside an arrow function if you have a circular reference."
70
- );
71
- }
72
-
73
- const getType = () => (isString(model) ? MongooseModels.get(model) : isArrowFn(model) ? model() : model);
74
- const populatedGroups = (isObject(options) && options.populatedGroups) || [];
75
-
76
- return useDecorators(
77
- Property(Object),
78
- StoreMerge(MONGOOSE_SCHEMA, {
79
- type: MongooseSchema.Types[isObject(options) ? options.type || MongooseSchemaTypes.OBJECT_ID : options],
80
- ref: model
81
- }),
82
- OnDeserialize((value) => {
83
- if (isRef(value)) {
84
- return value.toString();
85
- }
86
-
87
- if (isCollection(value) && isRef(value[0])) {
88
- return value.map(String);
89
- }
90
-
91
- return deserialize(value, {type: getType(), useAlias: false});
92
- }),
93
- OnSerialize((value: any, ctx) => {
94
- if (isRef(value)) {
95
- return value.toString();
96
- }
97
-
98
- if (isCollection(value) && isRef(value[0])) {
99
- return value.map(String);
100
- }
101
-
102
- const type = getType();
103
-
104
- return serialize(value, {...ctx, type});
105
- }),
106
- OneOf(string().example("5ce7ad3028890bd71749d477").description("A reference ObjectID"), lazyRef(getType)),
107
- populatedGroups.length && PopulateGroups(populatedGroups)
108
- ) as PropertyDecorator;
109
- }
110
-
111
- export type Ref<T> = T | string;
@@ -1,79 +0,0 @@
1
- import {decoratorTypeOf, StoreMerge, useDecorators} from "@tsed/core";
2
- import {injectable} from "@tsed/di";
3
- import {Property} from "@tsed/schema";
4
- import {SchemaTypeOptions} from "mongoose";
5
-
6
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
7
- import {MongooseSchemaOptions} from "../interfaces/MongooseSchemaOptions.js";
8
- import {getSchema, getSchemaToken} from "../utils/createSchema.js";
9
-
10
- /**
11
- * Define a class as a Mongoose Schema ready to be used to compose other schemes and models.
12
- *
13
- * ### Example
14
- *
15
- * ```typescript
16
- * @MongooseSchema()
17
- * export class EventSchema {
18
- * @Property()
19
- * field: string;
20
- * }
21
- * ```
22
- *
23
- * ### Options
24
- *
25
- * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
26
- *
27
- * @param {MongooseSchemaOptions | undefined} options
28
- * @returns {(target: any) => void}
29
- * @decorator
30
- * @mongoose
31
- * @property
32
- * @class
33
- */
34
- export function Schema(options?: MongooseSchemaOptions): (target: any) => void;
35
- export function Schema(definition: SchemaTypeOptions<any>): Function;
36
- export function Schema(options: MongooseSchemaOptions | SchemaTypeOptions<any> = {}) {
37
- return (...parameters: any[]) => {
38
- switch (decoratorTypeOf(parameters)) {
39
- case "property":
40
- return useDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(parameters[0], parameters[1], parameters[2]);
41
-
42
- case "class":
43
- const {token} = getSchemaToken(parameters[0], options);
44
-
45
- injectable(token).factory(() => getSchema(parameters[0], options as any));
46
- break;
47
- }
48
- };
49
- }
50
-
51
- /**
52
- * Define a class as a Mongoose Schema ready to be used to compose other schemes and models.
53
- *
54
- * ### Example
55
- *
56
- * ```typescript
57
- * @MongooseSchema()
58
- * export class EventSchema {
59
- * @Property()
60
- * field: string;
61
- * }
62
- * ```
63
- *
64
- * ### Options
65
- *
66
- * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
67
- *
68
- * @param {MongooseSchemaOptions | undefined} options
69
- * @returns {(target: any) => void}
70
- * @decorator
71
- * @mongoose
72
- * @property
73
- * @class
74
- */
75
- export function MongooseSchema(options?: MongooseSchemaOptions): (target: any) => void;
76
- export function MongooseSchema(definition: SchemaTypeOptions<any>): Function;
77
- export function MongooseSchema(options: MongooseSchemaOptions | SchemaTypeOptions<any> = {}) {
78
- return Schema(options as any);
79
- }
@@ -1,18 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {SchemaIgnore} from "./schemaIgnore.js";
5
-
6
- describe("@SchemaIgnore()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @SchemaIgnore()
10
- test: any;
11
- }
12
-
13
- const store = Store.from(Test, "test");
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- schemaIgnore: true
16
- });
17
- });
18
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * Do not apply this property to schema (create virtual property)
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * @SchemaIgnore()
11
- * @Property()
12
- * kind: string;
13
- *
14
- * ```
15
- *
16
- * @returns {Function}
17
- * @decorator
18
- * @mongoose
19
- * @class
20
- */
21
-
22
- export function SchemaIgnore(): Function {
23
- return MongooseSchema({schemaIgnore: true} as any);
24
- }
@@ -1,18 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {Select} from "../../src/index.js";
4
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
5
-
6
- describe("@Select()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @Select()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- select: true
16
- });
17
- });
18
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * Tell Mongoose to set default select() behavior for this path.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Select()
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean | any} select
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Select(select: boolean | any = true) {
23
- return MongooseSchema({select});
24
- }
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Sparse} from "./sparse.js";
5
-
6
- describe("@Sparse()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Sparse()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- sparse: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Sparse(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- sparse: true
28
- });
29
- });
30
- });
@@ -1,26 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
5
- * disallow changes to this path once the document is saved to the database for the first time. Read more
6
- * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
7
- *
8
- * ### Example
9
- *
10
- * ```typescript
11
- * @Model()
12
- * export class EventModel {
13
- * @Sparse()
14
- * field: string;
15
- * }
16
- * ```
17
- *
18
- * @param {boolean | number} sparse
19
- * @returns {Function}
20
- * @decorator
21
- * @mongoose
22
- * @property
23
- */
24
- export function Sparse(sparse: boolean | number = true): PropertyDecorator {
25
- return MongooseSchema({sparse} as any);
26
- }