@tsed/mongoose 8.0.0 → 8.0.2

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 (77) hide show
  1. package/package.json +15 -14
  2. package/src/MongooseModule.ts +32 -0
  3. package/src/constants/constants.ts +20 -0
  4. package/src/decorators/auto.spec.ts +30 -0
  5. package/src/decorators/auto.ts +24 -0
  6. package/src/decorators/discriminatorKey.ts +1 -0
  7. package/src/decorators/dynamicRef.spec.ts +46 -0
  8. package/src/decorators/dynamicRef.ts +76 -0
  9. package/src/decorators/excludeIndexes.spec.ts +30 -0
  10. package/src/decorators/excludeIndexes.ts +24 -0
  11. package/src/decorators/expires.spec.ts +18 -0
  12. package/src/decorators/expires.ts +24 -0
  13. package/src/decorators/immutable.spec.ts +30 -0
  14. package/src/decorators/immutable.ts +26 -0
  15. package/src/decorators/indexed.spec.ts +19 -0
  16. package/src/decorators/indexed.ts +24 -0
  17. package/src/decorators/lowercase.spec.ts +30 -0
  18. package/src/decorators/lowercase.ts +24 -0
  19. package/src/decorators/model.ts +69 -0
  20. package/src/decorators/mongooseIndex.spec.ts +26 -0
  21. package/src/decorators/mongooseIndex.ts +35 -0
  22. package/src/decorators/mongooseIndexes.spec.ts +35 -0
  23. package/src/decorators/mongooseIndexes.ts +35 -0
  24. package/src/decorators/mongoosePlugin.spec.ts +25 -0
  25. package/src/decorators/mongoosePlugin.ts +20 -0
  26. package/src/decorators/numberDecimal.spec.ts +266 -0
  27. package/src/decorators/numberDecimal.ts +84 -0
  28. package/src/decorators/objectID.spec.ts +24 -0
  29. package/src/decorators/objectID.ts +39 -0
  30. package/src/decorators/postHook.spec.ts +44 -0
  31. package/src/decorators/postHook.ts +72 -0
  32. package/src/decorators/preHook.spec.ts +75 -0
  33. package/src/decorators/preHook.ts +70 -0
  34. package/src/decorators/ref.spec.ts +361 -0
  35. package/src/decorators/ref.ts +111 -0
  36. package/src/decorators/schema.ts +79 -0
  37. package/src/decorators/schemaIgnore.spec.ts +18 -0
  38. package/src/decorators/schemaIgnore.ts +24 -0
  39. package/src/decorators/select.spec.ts +18 -0
  40. package/src/decorators/select.ts +24 -0
  41. package/src/decorators/sparse.spec.ts +30 -0
  42. package/src/decorators/sparse.ts +26 -0
  43. package/src/decorators/text.spec.ts +30 -0
  44. package/src/decorators/text.ts +25 -0
  45. package/src/decorators/trim.spec.ts +18 -0
  46. package/src/decorators/trim.ts +23 -0
  47. package/src/decorators/unique.spec.ts +18 -0
  48. package/src/decorators/unique.ts +23 -0
  49. package/src/decorators/uppercase.spec.ts +30 -0
  50. package/src/decorators/uppercase.ts +24 -0
  51. package/src/decorators/versionKey.spec.ts +19 -0
  52. package/src/decorators/versionKey.ts +9 -0
  53. package/src/decorators/virtualRef.spec.ts +383 -0
  54. package/src/decorators/virtualRef.ts +73 -0
  55. package/src/index.ts +47 -0
  56. package/src/interfaces/MongooseConnectionOptions.ts +10 -0
  57. package/src/interfaces/MongooseDocument.ts +3 -0
  58. package/src/interfaces/MongooseModel.ts +10 -0
  59. package/src/interfaces/MongooseModelOptions.ts +8 -0
  60. package/src/interfaces/MongooseSchemaOptions.ts +51 -0
  61. package/src/interfaces/MongooseSchemaTypes.ts +5 -0
  62. package/src/interfaces/MongooseVirtualRefOptions.ts +10 -0
  63. package/src/interfaces/interfaces.ts +9 -0
  64. package/src/registries/MongooseModels.ts +3 -0
  65. package/src/services/MongooseConnection.spec.ts +70 -0
  66. package/src/services/MongooseConnections.ts +58 -0
  67. package/src/services/MongooseService.spec.ts +73 -0
  68. package/src/services/MongooseService.ts +77 -0
  69. package/src/utils/buildMongooseSchema.spec.ts +67 -0
  70. package/src/utils/createModel.spec.ts +49 -0
  71. package/src/utils/createModel.ts +54 -0
  72. package/src/utils/createSchema.spec.ts +771 -0
  73. package/src/utils/createSchema.ts +198 -0
  74. package/src/utils/resolveRefType.spec.ts +30 -0
  75. package/src/utils/resolveRefType.ts +18 -0
  76. package/src/utils/schemaOptions.spec.ts +68 -0
  77. package/src/utils/schemaOptions.ts +74 -0
@@ -0,0 +1,383 @@
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
+ });
@@ -0,0 +1,73 @@
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[];
package/src/index.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./constants/constants.js";
5
+ export * from "./decorators/auto.js";
6
+ export * from "./decorators/discriminatorKey.js";
7
+ export * from "./decorators/dynamicRef.js";
8
+ export * from "./decorators/excludeIndexes.js";
9
+ export * from "./decorators/expires.js";
10
+ export * from "./decorators/immutable.js";
11
+ export * from "./decorators/indexed.js";
12
+ export * from "./decorators/lowercase.js";
13
+ export * from "./decorators/model.js";
14
+ export * from "./decorators/mongooseIndex.js";
15
+ export * from "./decorators/mongooseIndexes.js";
16
+ export * from "./decorators/mongoosePlugin.js";
17
+ export * from "./decorators/numberDecimal.js";
18
+ export * from "./decorators/objectID.js";
19
+ export * from "./decorators/postHook.js";
20
+ export * from "./decorators/preHook.js";
21
+ export * from "./decorators/ref.js";
22
+ export * from "./decorators/schema.js";
23
+ export * from "./decorators/schemaIgnore.js";
24
+ export * from "./decorators/select.js";
25
+ export * from "./decorators/sparse.js";
26
+ export * from "./decorators/text.js";
27
+ export * from "./decorators/trim.js";
28
+ export * from "./decorators/unique.js";
29
+ export * from "./decorators/uppercase.js";
30
+ export * from "./decorators/versionKey.js";
31
+ export * from "./decorators/virtualRef.js";
32
+ export * from "./interfaces/interfaces.js";
33
+ export * from "./interfaces/MongooseConnectionOptions.js";
34
+ export * from "./interfaces/MongooseDocument.js";
35
+ export * from "./interfaces/MongooseModel.js";
36
+ export * from "./interfaces/MongooseModelOptions.js";
37
+ export * from "./interfaces/MongooseSchemaOptions.js";
38
+ export * from "./interfaces/MongooseSchemaTypes.js";
39
+ export * from "./interfaces/MongooseVirtualRefOptions.js";
40
+ export * from "./MongooseModule.js";
41
+ export * from "./registries/MongooseModels.js";
42
+ export * from "./services/MongooseConnections.js";
43
+ export * from "./services/MongooseService.js";
44
+ export * from "./utils/createModel.js";
45
+ export * from "./utils/createSchema.js";
46
+ export * from "./utils/resolveRefType.js";
47
+ export * from "./utils/schemaOptions.js";
@@ -0,0 +1,10 @@
1
+ import {ConnectOptions} from "mongoose";
2
+
3
+ export interface MongooseConnectionOptions {
4
+ url: string;
5
+ /**
6
+ * The connection ID
7
+ */
8
+ id: string;
9
+ connectionOptions?: ConnectOptions;
10
+ }
@@ -0,0 +1,3 @@
1
+ import {Document} from "mongoose";
2
+
3
+ export type MongooseDocument<T> = T & Document;
@@ -0,0 +1,10 @@
1
+ import {Document, Model} from "mongoose";
2
+
3
+ // TODO since v5.11.5 Model require Document with id, See issue https://github.com/Automattic/mongoose/issues/9684
4
+ export type MongooseMergedDocument<T> = {[K in keyof T]: T[K]};
5
+
6
+ export interface MongooseDocumentMethods<T> {
7
+ toClass(): T;
8
+ }
9
+
10
+ export type MongooseModel<T> = Model<MongooseMergedDocument<Document & T & MongooseDocumentMethods<T>>>;
@@ -0,0 +1,8 @@
1
+ import {MongooseSchemaOptions} from "./MongooseSchemaOptions.js";
2
+
3
+ export interface MongooseModelOptions extends MongooseSchemaOptions {
4
+ name?: string;
5
+ connection?: string;
6
+ collection?: string;
7
+ overwriteModels?: boolean;
8
+ }
@@ -0,0 +1,51 @@
1
+ import {type IndexOptions, Schema, SchemaOptions} from "mongoose";
2
+
3
+ import {MongooseDocument} from "./MongooseDocument.js";
4
+
5
+ export type MongooseNextCB = (err?: Error) => void;
6
+
7
+ export interface MongooseHookOptions {
8
+ document?: boolean;
9
+ query?: boolean;
10
+ parallel?: boolean;
11
+ }
12
+
13
+ export type MongooseHookPromised<T = any> = (doc: T | MongooseDocument<T>) => Promise<void> | void;
14
+
15
+ export type MongoosePreHookCB<T = any> = ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void) | MongooseHookPromised;
16
+
17
+ export type MongoosePostHookCB<T = any> =
18
+ | ((doc: T | MongooseDocument<T>, error: Error, next: MongooseNextCB) => void)
19
+ | ((doc: T | MongooseDocument<T>, error: Error) => Promise<void> | void)
20
+ | ((doc: T | MongooseDocument<T>, next: MongooseNextCB) => void)
21
+ | MongooseHookPromised;
22
+
23
+ export interface MongoosePreHook<T = any> {
24
+ method: string | RegExp;
25
+ fn: MongoosePreHookCB<T>;
26
+ options?: MongooseHookOptions;
27
+ }
28
+
29
+ export interface MongoosePostHook<T = any> {
30
+ method: string | RegExp;
31
+ fn: MongoosePostHookCB<T>;
32
+ options?: MongooseHookOptions;
33
+ }
34
+
35
+ export interface MongoosePluginOptions {
36
+ plugin: (schema: Schema, options?: any) => void;
37
+ options?: Record<string, unknown>;
38
+ }
39
+
40
+ export interface MongooseIndexOptions {
41
+ fields: Record<any, any>;
42
+ options?: IndexOptions;
43
+ }
44
+
45
+ export interface MongooseSchemaOptions {
46
+ schemaOptions?: SchemaOptions;
47
+ plugins?: MongoosePluginOptions[];
48
+ indexes?: MongooseIndexOptions[];
49
+ pre?: MongoosePreHook[];
50
+ post?: MongoosePostHook[];
51
+ }
@@ -0,0 +1,5 @@
1
+ export enum MongooseSchemaTypes {
2
+ OBJECT_ID = "ObjectId",
3
+ STRING = "String",
4
+ NUMBER = "Number"
5
+ }
@@ -0,0 +1,10 @@
1
+ import {Type} from "@tsed/core";
2
+
3
+ export interface MongooseVirtualRefOptions {
4
+ ref?: string | Type<any> | (() => Type<any>);
5
+ foreignField?: string;
6
+ localField?: string;
7
+ justOne?: boolean;
8
+ count?: boolean;
9
+ options?: object;
10
+ }
@@ -0,0 +1,9 @@
1
+ import {MongooseConnectionOptions} from "./MongooseConnectionOptions.js";
2
+
3
+ declare global {
4
+ namespace TsED {
5
+ interface Configuration {
6
+ mongoose: Omit<MongooseConnectionOptions, "id"> | MongooseConnectionOptions[];
7
+ }
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ import {Type} from "@tsed/core";
2
+
3
+ export const MongooseModels: Map<string, Type> = new Map();
@@ -0,0 +1,70 @@
1
+ import {Configuration} from "@tsed/di";
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
3
+
4
+ import {MongooseService} from "../../src/index.js";
5
+ import {MONGOOSE_CONNECTIONS} from "../../src/services/MongooseConnections.js";
6
+
7
+ describe("MongooseConnections", () => {
8
+ beforeEach(PlatformTest.create);
9
+ afterEach(PlatformTest.reset);
10
+
11
+ it("should init connection with url", async () => {
12
+ // GIVEN
13
+ const connectStub = vi.fn().mockResolvedValue("test");
14
+
15
+ // WHEN
16
+ await PlatformTest.invoke(MONGOOSE_CONNECTIONS, [
17
+ {
18
+ token: Configuration,
19
+ use: {
20
+ get() {
21
+ return {
22
+ url: "mongodb://test",
23
+ connectionOptions: {options: "options"}
24
+ };
25
+ }
26
+ }
27
+ },
28
+ {
29
+ token: MongooseService,
30
+ use: {
31
+ connect: connectStub
32
+ }
33
+ }
34
+ ]);
35
+
36
+ // THEN
37
+ expect(connectStub).toHaveBeenCalledWith("default", "mongodb://test", {options: "options"}, true);
38
+ });
39
+ it("should init with a list of connection", async () => {
40
+ // GIVEN
41
+ const connectStub = vi.fn().mockResolvedValue("test");
42
+
43
+ // WHEN
44
+ await PlatformTest.invoke(MONGOOSE_CONNECTIONS, [
45
+ {
46
+ token: Configuration,
47
+ use: {
48
+ get() {
49
+ return [
50
+ {
51
+ id: "id",
52
+ url: "mongodb://test",
53
+ connectionOptions: {options: "options"}
54
+ }
55
+ ];
56
+ }
57
+ }
58
+ },
59
+ {
60
+ token: MongooseService,
61
+ use: {
62
+ connect: connectStub
63
+ }
64
+ }
65
+ ]);
66
+
67
+ // THEN
68
+ expect(connectStub).toHaveBeenCalledWith("id", "mongodb://test", {options: "options"}, true);
69
+ });
70
+ });