@tsed/mongoose 8.4.2 → 8.4.4
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cleanObject, nameOf, Store } from "@tsed/core";
|
|
1
|
+
import { classOf, cleanObject, isClassObject, nameOf, Store } from "@tsed/core";
|
|
2
2
|
import { deserialize, serialize } from "@tsed/json-mapper";
|
|
3
3
|
import { getProperties, JsonEntityStore } from "@tsed/schema";
|
|
4
4
|
import { pascalCase } from "change-case";
|
|
@@ -121,13 +121,13 @@ export function createSchemaTypeOptions(propEntity) {
|
|
|
121
121
|
}
|
|
122
122
|
schemaTypeOptions = {
|
|
123
123
|
...schemaTypeOptions,
|
|
124
|
-
type: propEntity.type,
|
|
124
|
+
type: jsonSchema["enum"] && isClassObject(propEntity.type) ? classOf(jsonSchema["enum"][0]) : propEntity.type,
|
|
125
125
|
match: match,
|
|
126
126
|
min,
|
|
127
127
|
max,
|
|
128
128
|
minlength,
|
|
129
129
|
maxlength,
|
|
130
|
-
enum:
|
|
130
|
+
enum: jsonSchema["enum"],
|
|
131
131
|
default: jsonSchema["default"]
|
|
132
132
|
};
|
|
133
133
|
}
|
|
@@ -138,14 +138,23 @@ export function createSchemaTypeOptions(propEntity) {
|
|
|
138
138
|
schemaTypeOptions = cleanObject({ ...schemaTypeOptions, ...rawMongooseSchema });
|
|
139
139
|
if (propEntity.isCollection) {
|
|
140
140
|
if (propEntity.isArray) {
|
|
141
|
-
return
|
|
141
|
+
return {
|
|
142
|
+
...schemaTypeOptions,
|
|
143
|
+
type: [schemaTypeOptions.type]
|
|
144
|
+
};
|
|
142
145
|
}
|
|
143
146
|
// Can be a Map or a Set;
|
|
144
147
|
// Mongoose implements only Map;
|
|
145
148
|
if (propEntity.collectionType !== Map) {
|
|
146
149
|
throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
|
|
147
150
|
}
|
|
148
|
-
|
|
151
|
+
const { default: defaultValue, required, ...otherOpts } = schemaTypeOptions;
|
|
152
|
+
return {
|
|
153
|
+
type: Map,
|
|
154
|
+
of: otherOpts,
|
|
155
|
+
default: defaultValue,
|
|
156
|
+
required
|
|
157
|
+
};
|
|
149
158
|
}
|
|
150
159
|
return schemaTypeOptions;
|
|
151
160
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"createSchema.js","sourceRoot":"","sources":["../../../src/utils/createSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,EAAO,MAAM,YAAY,CAAC;AACpF,OAAO,EAAC,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAC,aAAa,EAAE,eAAe,EAAC,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AACvC,OAAO,QAAgG,MAAM,UAAU,CAAC;AAExH,OAAO,EAAC,eAAe,EAAE,uBAAuB,EAAC,MAAM,2BAA2B,CAAC;AAGnF,OAAO,EAAC,cAAc,EAAC,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAOjD;;GAEG;AACH,SAAS,WAAW,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAyB,EAAE,OAAuB;IACtF,MAAM,cAAc,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAgB,CAAC,CAAC;IAErE,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QAChD,cAAc,CAAC,OAAO,CAA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BAAiB,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IAE9E,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;QAC5B,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACvB,OAAO;gBACL,GAAG,iBAAiB;gBACpB,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC;aAC/B,CAAC;QACJ,CAAC;QACD,yBAAyB;QACzB,gCAAgC;QAChC,IAAI,UAAU,CAAC,cAAc,KAAK,GAAG,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,4BAA4B,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;QACrG,CAAC;QAED,MAAM,EAAC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,SAAS,EAAC,GAAG,iBAAiB,CAAC;QAC1E,OAAO;YACL,IAAI,EAAE,GAAG;YACT,EAAE,EAAE,SAAS;YACb,OAAO,EAAE,YAAY;YACrB,QAAQ;SACT,CAAC;IACJ,CAAC;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
/// <reference types="mongoose/types/inferrawdoctype.js" />
|
|
26
26
|
import { Type } from "@tsed/core";
|
|
27
27
|
import { JsonEntityStore } from "@tsed/schema";
|
|
28
|
-
import mongoose, { Schema, SchemaDefinition,
|
|
28
|
+
import mongoose, { Schema, SchemaDefinition, SchemaDefinitionProperty } from "mongoose";
|
|
29
29
|
import { MongooseSchemaOptions } from "../interfaces/MongooseSchemaOptions.js";
|
|
30
30
|
import { MongooseVirtualRefOptions } from "../interfaces/MongooseVirtualRefOptions.js";
|
|
31
31
|
export interface MongooseSchemaMetadata {
|
|
@@ -50,4 +50,4 @@ export declare function buildMongooseSchema(target: any): MongooseSchemaMetadata
|
|
|
50
50
|
/**
|
|
51
51
|
* @ignore
|
|
52
52
|
*/
|
|
53
|
-
export declare function createSchemaTypeOptions
|
|
53
|
+
export declare function createSchemaTypeOptions(propEntity: JsonEntityStore): SchemaDefinitionProperty;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/mongoose",
|
|
3
3
|
"description": "Mongoose package for Ts.ED framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.4.
|
|
5
|
+
"version": "8.4.4",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tsed",
|
|
8
8
|
"mongoose",
|
|
@@ -34,27 +34,27 @@
|
|
|
34
34
|
"tslib": "2.7.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@tsed/ajv": "8.4.
|
|
38
|
-
"@tsed/barrels": "8.4.
|
|
39
|
-
"@tsed/core": "8.4.
|
|
40
|
-
"@tsed/di": "8.4.
|
|
41
|
-
"@tsed/json-mapper": "8.4.
|
|
37
|
+
"@tsed/ajv": "8.4.4",
|
|
38
|
+
"@tsed/barrels": "8.4.4",
|
|
39
|
+
"@tsed/core": "8.4.4",
|
|
40
|
+
"@tsed/di": "8.4.4",
|
|
41
|
+
"@tsed/json-mapper": "8.4.4",
|
|
42
42
|
"@tsed/logger": "^7.0.1",
|
|
43
|
-
"@tsed/schema": "8.4.
|
|
44
|
-
"@tsed/testcontainers-mongo": "8.4.
|
|
45
|
-
"@tsed/typescript": "8.4.
|
|
43
|
+
"@tsed/schema": "8.4.4",
|
|
44
|
+
"@tsed/testcontainers-mongo": "8.4.4",
|
|
45
|
+
"@tsed/typescript": "8.4.4",
|
|
46
46
|
"eslint": "9.12.0",
|
|
47
47
|
"mongoose": "8.9.4",
|
|
48
48
|
"typescript": "5.4.5",
|
|
49
49
|
"vitest": "2.1.2"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@tsed/ajv": ">=8.4.
|
|
53
|
-
"@tsed/core": ">=8.4.
|
|
54
|
-
"@tsed/di": ">=8.4.
|
|
55
|
-
"@tsed/json-mapper": ">=8.4.
|
|
52
|
+
"@tsed/ajv": ">=8.4.4",
|
|
53
|
+
"@tsed/core": ">=8.4.4",
|
|
54
|
+
"@tsed/di": ">=8.4.4",
|
|
55
|
+
"@tsed/json-mapper": ">=8.4.4",
|
|
56
56
|
"@tsed/logger": ">=7.0.1",
|
|
57
|
-
"@tsed/schema": ">=8.4.
|
|
57
|
+
"@tsed/schema": ">=8.4.4",
|
|
58
58
|
"mongoose": ">=6.0.0"
|
|
59
59
|
},
|
|
60
60
|
"repository": "https://github.com/tsedio/tsed",
|
|
@@ -465,12 +465,10 @@ describe("createSchema", () => {
|
|
|
465
465
|
|
|
466
466
|
// THEN
|
|
467
467
|
expect(testSchema.obj).toEqual({
|
|
468
|
-
tests:
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}
|
|
473
|
-
]
|
|
468
|
+
tests: {
|
|
469
|
+
type: [childrenSchema],
|
|
470
|
+
required: false
|
|
471
|
+
}
|
|
474
472
|
});
|
|
475
473
|
|
|
476
474
|
expect(childrenSchema.obj).toEqual({
|
|
@@ -499,6 +497,54 @@ describe("createSchema", () => {
|
|
|
499
497
|
}
|
|
500
498
|
});
|
|
501
499
|
});
|
|
500
|
+
it("should create schema with collection (Array of subdocument and default value undefined)", () => {
|
|
501
|
+
// GIVEN
|
|
502
|
+
enum MyEnum {
|
|
503
|
+
V1 = "v1",
|
|
504
|
+
V2 = "v2"
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
@Schema()
|
|
508
|
+
class Children {
|
|
509
|
+
@Name("id")
|
|
510
|
+
_id: string;
|
|
511
|
+
|
|
512
|
+
@Enum(MyEnum)
|
|
513
|
+
@Default(undefined)
|
|
514
|
+
enum?: MyEnum[];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
@Model()
|
|
518
|
+
class Test6 {
|
|
519
|
+
@CollectionOf(Children)
|
|
520
|
+
@Default(undefined)
|
|
521
|
+
tests?: Children[];
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// WHEN
|
|
525
|
+
const testSchema = getSchema(Test6);
|
|
526
|
+
const childrenSchema = getSchema(Children);
|
|
527
|
+
|
|
528
|
+
// THEN
|
|
529
|
+
expect(testSchema.obj).toEqual({
|
|
530
|
+
tests: {
|
|
531
|
+
type: [childrenSchema],
|
|
532
|
+
required: false
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
expect(childrenSchema.obj).toEqual({
|
|
537
|
+
_id: {
|
|
538
|
+
required: false,
|
|
539
|
+
type: String
|
|
540
|
+
},
|
|
541
|
+
enum: {
|
|
542
|
+
enum: ["v1", "v2"],
|
|
543
|
+
required: false,
|
|
544
|
+
type: [String]
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
});
|
|
502
548
|
it("should create schema with collection (Array of ref)", () => {
|
|
503
549
|
// GIVEN
|
|
504
550
|
enum MyEnum {
|
|
@@ -536,13 +582,11 @@ describe("createSchema", () => {
|
|
|
536
582
|
|
|
537
583
|
// THEN
|
|
538
584
|
expect(testSchema.obj).toEqual({
|
|
539
|
-
tests:
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
}
|
|
545
|
-
]
|
|
585
|
+
tests: {
|
|
586
|
+
type: [SchemaMongoose.Types.ObjectId],
|
|
587
|
+
ref: "Children3",
|
|
588
|
+
required: false
|
|
589
|
+
}
|
|
546
590
|
});
|
|
547
591
|
});
|
|
548
592
|
it("should create schema with collection (Array of virtual ref", () => {
|
|
@@ -632,9 +676,9 @@ describe("createSchema", () => {
|
|
|
632
676
|
tests: {
|
|
633
677
|
type: Map,
|
|
634
678
|
of: {
|
|
635
|
-
type: childrenSchema
|
|
636
|
-
|
|
637
|
-
|
|
679
|
+
type: childrenSchema
|
|
680
|
+
},
|
|
681
|
+
required: false
|
|
638
682
|
}
|
|
639
683
|
});
|
|
640
684
|
|
|
@@ -729,6 +773,7 @@ describe("createSchema", () => {
|
|
|
729
773
|
@DiscriminatorKey()
|
|
730
774
|
kind: string;
|
|
731
775
|
}
|
|
776
|
+
|
|
732
777
|
const testSchema = getSchema(Test11);
|
|
733
778
|
// @ts-ignore
|
|
734
779
|
const options = testSchema.options;
|
|
@@ -741,6 +786,7 @@ describe("createSchema", () => {
|
|
|
741
786
|
@VersionKey()
|
|
742
787
|
version: number;
|
|
743
788
|
}
|
|
789
|
+
|
|
744
790
|
const testSchema = getSchema(Test12);
|
|
745
791
|
// @ts-ignore
|
|
746
792
|
const options = testSchema.options;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {cleanObject, nameOf, Store, Type} from "@tsed/core";
|
|
1
|
+
import {classOf, cleanObject, isClassObject, nameOf, Store, Type} from "@tsed/core";
|
|
2
2
|
import {deserialize, serialize} from "@tsed/json-mapper";
|
|
3
3
|
import {getProperties, JsonEntityStore} from "@tsed/schema";
|
|
4
4
|
import {pascalCase} from "change-case";
|
|
5
|
-
import mongoose, {Schema, SchemaDefinition, SchemaOptions, SchemaTypeOptions} from "mongoose";
|
|
5
|
+
import mongoose, {Schema, SchemaDefinition, SchemaDefinitionProperty, SchemaOptions, SchemaTypeOptions} from "mongoose";
|
|
6
6
|
|
|
7
7
|
import {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
|
|
8
8
|
import {MongooseSchemaOptions} from "../interfaces/MongooseSchemaOptions.js";
|
|
@@ -142,11 +142,11 @@ export function buildMongooseSchema(target: any): MongooseSchemaMetadata {
|
|
|
142
142
|
/**
|
|
143
143
|
* @ignore
|
|
144
144
|
*/
|
|
145
|
-
export function createSchemaTypeOptions
|
|
145
|
+
export function createSchemaTypeOptions(propEntity: JsonEntityStore): SchemaDefinitionProperty {
|
|
146
146
|
const key = propEntity.propertyKey;
|
|
147
147
|
const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};
|
|
148
148
|
|
|
149
|
-
let schemaTypeOptions: SchemaTypeOptions<
|
|
149
|
+
let schemaTypeOptions: SchemaTypeOptions<any> = {
|
|
150
150
|
required: propEntity.required
|
|
151
151
|
? function () {
|
|
152
152
|
return propEntity.isRequired(this[key]);
|
|
@@ -165,13 +165,13 @@ export function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): S
|
|
|
165
165
|
|
|
166
166
|
schemaTypeOptions = {
|
|
167
167
|
...schemaTypeOptions,
|
|
168
|
-
type: propEntity.type,
|
|
168
|
+
type: jsonSchema["enum"] && isClassObject(propEntity.type) ? classOf(jsonSchema["enum"][0]) : propEntity.type,
|
|
169
169
|
match: match as RegExp,
|
|
170
170
|
min,
|
|
171
171
|
max,
|
|
172
172
|
minlength,
|
|
173
173
|
maxlength,
|
|
174
|
-
enum:
|
|
174
|
+
enum: jsonSchema["enum"],
|
|
175
175
|
default: jsonSchema["default"]
|
|
176
176
|
};
|
|
177
177
|
} else if (!rawMongooseSchema.ref) {
|
|
@@ -183,7 +183,10 @@ export function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): S
|
|
|
183
183
|
|
|
184
184
|
if (propEntity.isCollection) {
|
|
185
185
|
if (propEntity.isArray) {
|
|
186
|
-
return
|
|
186
|
+
return {
|
|
187
|
+
...schemaTypeOptions,
|
|
188
|
+
type: [schemaTypeOptions.type]
|
|
189
|
+
};
|
|
187
190
|
}
|
|
188
191
|
// Can be a Map or a Set;
|
|
189
192
|
// Mongoose implements only Map;
|
|
@@ -191,7 +194,13 @@ export function createSchemaTypeOptions<T = any>(propEntity: JsonEntityStore): S
|
|
|
191
194
|
throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
|
|
192
195
|
}
|
|
193
196
|
|
|
194
|
-
|
|
197
|
+
const {default: defaultValue, required, ...otherOpts} = schemaTypeOptions;
|
|
198
|
+
return {
|
|
199
|
+
type: Map,
|
|
200
|
+
of: otherOpts,
|
|
201
|
+
default: defaultValue,
|
|
202
|
+
required
|
|
203
|
+
};
|
|
195
204
|
}
|
|
196
205
|
|
|
197
206
|
return schemaTypeOptions;
|