@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 {Auto} from "./auto.js";
5
-
6
- describe("@Auto()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Auto()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- auto: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Auto(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- auto: true
28
- });
29
- });
30
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * If true, uses Mongoose's default _id settings. Only allowed for ObjectIds
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @ExcludeIndexes()
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean} auto
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Auto(auto: boolean = true): PropertyDecorator {
23
- return MongooseSchema({auto} as any);
24
- }
@@ -1 +0,0 @@
1
- export {DiscriminatorKey} from "@tsed/schema";
@@ -1,46 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
- import {getJsonSchema} from "@tsed/schema";
3
- import {Schema} from "mongoose";
4
-
5
- import {DynamicRef} from "../../src/index.js";
6
- import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../constants/constants.js";
7
-
8
- describe("@DynamicRef()", () => {
9
- it("should set metadata", () => {
10
- // GIVEN
11
- class RefTest {}
12
-
13
- Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");
14
-
15
- // WHEN
16
- class Test {
17
- @DynamicRef("RefTest")
18
- test: DynamicRef<RefTest>;
19
- }
20
-
21
- // THEN
22
- expect(getJsonSchema(Test)).toEqual({
23
- properties: {
24
- test: {
25
- description: "A reference ObjectID",
26
- examples: ["5ce7ad3028890bd71749d477"],
27
- oneOf: [
28
- {
29
- description: "A reference ObjectID",
30
- examples: ["5ce7ad3028890bd71749d477"],
31
- type: "string"
32
- }
33
- ]
34
- }
35
- },
36
- type: "object"
37
- });
38
-
39
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
40
-
41
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
42
- type: Schema.Types.ObjectId,
43
- refPath: "RefTest"
44
- });
45
- });
46
- });
@@ -1,76 +0,0 @@
1
- import {classOf, isArrowFn, isString, StoreMerge, Type, useDecorators} from "@tsed/core";
2
- import {deserialize, OnDeserialize, OnSerialize, serialize} from "@tsed/json-mapper";
3
- import {Description, Example, JsonHookContext, OneOf, Property, string} from "@tsed/schema";
4
- import {Schema as MongooseSchema} from "mongoose";
5
-
6
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
7
- import {MongooseModels} from "../registries/MongooseModels.js";
8
-
9
- function isRef(value: undefined | string | any) {
10
- return (value && value._bsontype) || isString(value);
11
- }
12
-
13
- function getType(refPath: string, ctx: JsonHookContext) {
14
- return (ctx?.self[refPath] && MongooseModels.get(ctx.self[refPath] as string)) || Object;
15
- }
16
-
17
- /**
18
- * Define a property as mongoose reference to other Model (decorated with @Model).
19
- *
20
- * ### Example
21
- *
22
- * ```typescript
23
- * @Model()
24
- * class FooModel {
25
- *
26
- * @DynamicRef('type')
27
- * field: DynamicRef<OtherFooModel | OtherModel>
28
- *
29
- * @Enum(['OtherFooModel', 'OtherModel'])
30
- * type: string
31
- * }
32
- *
33
- * @Model()
34
- * class OtherFooModel {
35
- * }
36
- *
37
- * @Model()
38
- * class OtherModel {
39
- * }
40
- * ```
41
- *
42
- * @param refPath {String} the path to apply the correct model
43
- * @param types {Type} the classes to generate the correct json schema
44
- * @returns {Function}
45
- * @decorator
46
- * @mongoose
47
- * @property
48
- */
49
- export function DynamicRef(refPath: string, ...types: Type<any>[]): PropertyDecorator {
50
- return useDecorators(
51
- Property(Object),
52
- Example("5ce7ad3028890bd71749d477"),
53
- Description("A reference ObjectID"),
54
- StoreMerge(MONGOOSE_SCHEMA, {
55
- type: MongooseSchema.Types.ObjectId,
56
- refPath
57
- }),
58
- OnDeserialize((value, ctx) => {
59
- if (isRef(value)) {
60
- return value.toString();
61
- }
62
-
63
- return deserialize(value, {...ctx, type: getType(refPath, ctx)});
64
- }),
65
- OnSerialize((value: any, ctx) => {
66
- if (isRef(value)) {
67
- return value.toString();
68
- }
69
-
70
- return serialize(value, {...ctx, type: getType(refPath, ctx)});
71
- }),
72
- OneOf(string().example("5ce7ad3028890bd71749d477").description("A reference ObjectID"), ...types)
73
- ) as PropertyDecorator;
74
- }
75
-
76
- export type DynamicRef<T> = T | string;
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {ExcludeIndexes} from "./excludeIndexes.js";
5
-
6
- describe("@ExcludeIndexes()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @ExcludeIndexes()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- excludeIndexes: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @ExcludeIndexes(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- excludeIndexes: true
28
- });
29
- });
30
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * If true, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @ExcludeIndexes()
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean} excludeIndexes
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function ExcludeIndexes(excludeIndexes: boolean = true): PropertyDecorator {
23
- return MongooseSchema({excludeIndexes} as any);
24
- }
@@ -1,18 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Expires} from "./expires.js";
5
-
6
- describe("@Expires()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @Expires("5d")
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- expires: "5d"
16
- });
17
- });
18
- });
@@ -1,24 +0,0 @@
1
- import {MongooseSchema} from "./schema.js";
2
-
3
- /**
4
- * Defines a TTL index on this path. Only allowed for dates.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Expires('5d')
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean | any} expires
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Expires(expires: number | string | Date): PropertyDecorator {
23
- return MongooseSchema({expires} as any);
24
- }
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Immutable} from "./immutable.js";
5
-
6
- describe("@Immutable()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Immutable()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- immutable: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Immutable(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- immutable: 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
- * @Immutable()
14
- * field: string;
15
- * }
16
- * ```
17
- *
18
- * @param {boolean | any} immutable
19
- * @returns {Function}
20
- * @decorator
21
- * @mongoose
22
- * @property
23
- */
24
- export function Immutable(immutable: boolean | ((this: any, doc: any) => boolean) = true): PropertyDecorator {
25
- return MongooseSchema({immutable} as any);
26
- }
@@ -1,19 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Indexed} from "./indexed.js";
5
-
6
- describe("@Indexed()", () => {
7
- it("should set metadata", () => {
8
- class Test {
9
- @Indexed()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test");
14
-
15
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
16
- index: true
17
- });
18
- });
19
- });
@@ -1,24 +0,0 @@
1
- import {IndexOptions} from "mongoose";
2
-
3
- import {MongooseSchema} from "./schema.js";
4
-
5
- /**
6
- * Tell Mongoose whether to define an index for the property.
7
- * ### Example
8
- *
9
- * ```typescript
10
- * @Model()
11
- * export class EventModel {
12
- * @Indexed()
13
- * field: string;
14
- * }
15
- * ```
16
- *
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Indexed(index: IndexOptions | boolean | string = true): PropertyDecorator {
23
- return MongooseSchema({index} as any);
24
- }
@@ -1,30 +0,0 @@
1
- import {descriptorOf, Store} from "@tsed/core";
2
-
3
- import {MONGOOSE_SCHEMA} from "../constants/constants.js";
4
- import {Lowercase} from "./lowercase.js";
5
-
6
- describe("@Lowercase()", () => {
7
- it("should set metadata (default)", () => {
8
- class Test {
9
- @Lowercase()
10
- test: string;
11
- }
12
-
13
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
14
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
15
- lowercase: true
16
- });
17
- });
18
-
19
- it("should set metadata (true)", () => {
20
- class Test {
21
- @Lowercase(true)
22
- test: string;
23
- }
24
-
25
- const store = Store.from(Test, "test", descriptorOf(Test, "test"));
26
- expect(store.get(MONGOOSE_SCHEMA)).toEqual({
27
- lowercase: 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 lowercases this string using JavaScript's built-in String#toLowerCase().
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * export class EventModel {
11
- * @Lowercase()
12
- * field: string;
13
- * }
14
- * ```
15
- *
16
- * @param {boolean} lowercase
17
- * @returns {Function}
18
- * @decorator
19
- * @mongoose
20
- * @property
21
- */
22
- export function Lowercase(lowercase: boolean = true): PropertyDecorator {
23
- return MongooseSchema({lowercase} as any);
24
- }
@@ -1,69 +0,0 @@
1
- import {useDecorators} from "@tsed/core";
2
- import {injectable} from "@tsed/di";
3
- import {Schema} from "mongoose";
4
-
5
- import {MongooseModelOptions} from "../interfaces/MongooseModelOptions.js";
6
- import {MONGOOSE_CONNECTIONS} from "../services/MongooseConnections.js";
7
- import {createModel, getModelToken} from "../utils/createModel.js";
8
- import {getSchema} from "../utils/createSchema.js";
9
- import {applySchemaOptions, schemaOptions} from "../utils/schemaOptions.js";
10
-
11
- /**
12
- * Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with
13
- * `@Inject` annotation.
14
- *
15
- * ### Example
16
- *
17
- * ```typescript
18
- * @Model()
19
- * export class EventModel {
20
- * @Property()
21
- * field: string;
22
- * }
23
- * ```
24
- *
25
- * Then inject the model into a service:
26
- *
27
- * ```typescript
28
- * class MyService {
29
- * constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {
30
- * eventModel.findById().exec();
31
- * }
32
- * }
33
- * ```
34
- *
35
- * ### Options
36
- *
37
- * - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
38
- * - `name` (String): model name.
39
- * - `collection` (String): collection (optional, induced from model name).
40
- * - `skipInit` (Boolean): skipInit whether to skip initialization (defaults to false).
41
- *
42
- * @param {MongooseModelOptions} options
43
- * @returns {(target: any) => void}
44
- * @decorator
45
- * @mongoose
46
- * @class
47
- */
48
- export function Model(options: MongooseModelOptions = {}) {
49
- return useDecorators((target: any) => {
50
- const {token, collectionName} = getModelToken(target, options);
51
-
52
- injectable(token).factory(() => getSchema(target, options as any));
53
-
54
- injectable(target)
55
- .type("mongoose:model")
56
- .deps([MONGOOSE_CONNECTIONS, token])
57
- .factory((connections: MONGOOSE_CONNECTIONS, schema: Schema) => {
58
- applySchemaOptions(schema, schemaOptions(target));
59
- return createModel(
60
- target,
61
- schema,
62
- collectionName,
63
- options.collection,
64
- options.overwriteModels,
65
- connections.get(options.connection)
66
- );
67
- });
68
- });
69
- }
@@ -1,26 +0,0 @@
1
- import {MongooseIndex} from "../../src/decorators/mongooseIndex.js";
2
- import {schemaOptions} from "../../src/utils/schemaOptions.js";
3
-
4
- describe("@MongooseIndex()", () => {
5
- class Test {}
6
-
7
- it("should store options", () => {
8
- // WHEN
9
- @MongooseIndex({field: "1"}, {})
10
- class Test {}
11
-
12
- // THEN
13
- const options = schemaOptions(Test);
14
-
15
- expect(options).toEqual({
16
- indexes: [
17
- {
18
- fields: {
19
- field: "1"
20
- },
21
- options: {}
22
- }
23
- ]
24
- });
25
- });
26
- });
@@ -1,35 +0,0 @@
1
- import {schemaOptions} from "../utils/schemaOptions.js";
2
-
3
- /**
4
- * Calls schema.index() to define an index (most likely compound) for the schema.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * @MongooseIndex({first: 1, second: 1}, {unique: 1})
11
- * export class EventModel {
12
- *
13
- * @Property()
14
- * first: string;
15
- *
16
- * @Property()
17
- * second: string;
18
- *
19
- * }
20
- * ```
21
- *
22
- * @param fields
23
- * @param options
24
- * @returns {Function}
25
- * @decorator
26
- * @mongoose
27
- * @class
28
- */
29
- export function MongooseIndex(fields: object, options?: any): Function {
30
- return (target: any) => {
31
- schemaOptions(target, {
32
- indexes: [{fields, options}]
33
- });
34
- };
35
- }
@@ -1,35 +0,0 @@
1
- import {schemaOptions} from "../utils/schemaOptions.js";
2
- import {MongooseIndexes} from "./mongooseIndexes.js";
3
-
4
- describe("@MongooseIndexes()", () => {
5
- class Test {}
6
-
7
- it("should store options", () => {
8
- // WHEN
9
- @MongooseIndexes([
10
- {fields: {field: "1"}, options: {}},
11
- {fields: {field2: "1"}, options: {}}
12
- ])
13
- class Test {}
14
-
15
- // THEN
16
- const options = schemaOptions(Test);
17
-
18
- expect(options).toEqual({
19
- indexes: [
20
- {
21
- fields: {
22
- field: "1"
23
- },
24
- options: {}
25
- },
26
- {
27
- fields: {
28
- field2: "1"
29
- },
30
- options: {}
31
- }
32
- ]
33
- });
34
- });
35
- });
@@ -1,35 +0,0 @@
1
- import {schemaOptions} from "../utils/schemaOptions.js";
2
-
3
- /**
4
- * Calls schema.index() to define multiple indexes (most likely compound) for the schema.
5
- *
6
- * ### Example
7
- *
8
- * ```typescript
9
- * @Model()
10
- * @MongooseIndexes([{fields: {first: 1, second: 1}, options:{unique: 1}}, {fields: {first: 1, third: 1}, options:{unique: 1}}])
11
- * export class EventModel {
12
- *
13
- * @Property()
14
- * first: string;
15
- *
16
- * @Property()
17
- * second: string;
18
- *
19
- * @Property()
20
- * third: string;
21
- *
22
- * }
23
- * ```
24
- *
25
- * @param indexes - define multiple mongoose indexes
26
- * @returns {Function}
27
- * @decorator
28
- * @mongoose
29
- * @class
30
- */
31
- export function MongooseIndexes(indexes: Array<{fields: object; options?: any}>): Function {
32
- return (target: any) => {
33
- schemaOptions(target, {indexes});
34
- };
35
- }
@@ -1,25 +0,0 @@
1
- import {MongoosePlugin} from "../../src/decorators/mongoosePlugin.js";
2
- import {schemaOptions} from "../../src/utils/schemaOptions.js";
3
-
4
- describe("@MongoosePlugin()", () => {
5
- it("should store options", () => {
6
- // GIVEN
7
- const fn = vi.fn();
8
-
9
- // WHEN
10
- @MongoosePlugin(fn, {})
11
- class Test {}
12
-
13
- // THEN
14
- const options = schemaOptions(Test);
15
-
16
- expect(options).toEqual({
17
- plugins: [
18
- {
19
- plugin: fn,
20
- options: {}
21
- }
22
- ]
23
- });
24
- });
25
- });