@tsed/mongoose 8.0.1 → 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,24 @@
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
+ }
@@ -0,0 +1,18 @@
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
+ });
@@ -0,0 +1,24 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ });
@@ -0,0 +1,26 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ });
@@ -0,0 +1,25 @@
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
+ }
@@ -0,0 +1,18 @@
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
+ });
@@ -0,0 +1,23 @@
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
+ }
@@ -0,0 +1,18 @@
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
+ });
@@ -0,0 +1,23 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ });
@@ -0,0 +1,24 @@
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
+ }
@@ -0,0 +1,19 @@
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
+ });
@@ -0,0 +1,9 @@
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
+ }