@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.
- package/lib/esm/MongooseModule.js +0 -1
- package/lib/esm/constants/constants.js +0 -1
- package/lib/esm/decorators/auto.js +0 -1
- package/lib/esm/decorators/discriminatorKey.js +0 -1
- package/lib/esm/decorators/dynamicRef.js +0 -1
- package/lib/esm/decorators/excludeIndexes.js +0 -1
- package/lib/esm/decorators/expires.js +0 -1
- package/lib/esm/decorators/immutable.js +0 -1
- package/lib/esm/decorators/indexed.js +0 -1
- package/lib/esm/decorators/lowercase.js +0 -1
- package/lib/esm/decorators/model.js +0 -1
- package/lib/esm/decorators/mongooseIndex.js +0 -1
- package/lib/esm/decorators/mongooseIndexes.js +0 -1
- package/lib/esm/decorators/mongoosePlugin.js +0 -1
- package/lib/esm/decorators/numberDecimal.js +0 -1
- package/lib/esm/decorators/objectID.js +0 -1
- package/lib/esm/decorators/postHook.js +0 -1
- package/lib/esm/decorators/preHook.js +0 -1
- package/lib/esm/decorators/ref.js +0 -1
- package/lib/esm/decorators/schema.js +0 -1
- package/lib/esm/decorators/schemaIgnore.js +0 -1
- package/lib/esm/decorators/select.js +0 -1
- package/lib/esm/decorators/sparse.js +0 -1
- package/lib/esm/decorators/text.js +0 -1
- package/lib/esm/decorators/trim.js +0 -1
- package/lib/esm/decorators/unique.js +0 -1
- package/lib/esm/decorators/uppercase.js +0 -1
- package/lib/esm/decorators/versionKey.js +0 -1
- package/lib/esm/decorators/virtualRef.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/interfaces/MongooseConnectionOptions.js +0 -1
- package/lib/esm/interfaces/MongooseDocument.js +0 -1
- package/lib/esm/interfaces/MongooseModel.js +0 -1
- package/lib/esm/interfaces/MongooseModelOptions.js +0 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js +0 -1
- package/lib/esm/interfaces/MongooseSchemaTypes.js +0 -1
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js +0 -1
- package/lib/esm/interfaces/interfaces.js +0 -1
- package/lib/esm/registries/MongooseModels.js +0 -1
- package/lib/esm/services/MongooseConnections.js +0 -1
- package/lib/esm/services/MongooseService.js +0 -1
- package/lib/esm/utils/createModel.js +0 -1
- package/lib/esm/utils/createSchema.js +0 -1
- package/lib/esm/utils/resolveRefType.js +0 -1
- package/lib/esm/utils/schemaOptions.js +0 -1
- package/package.json +13 -10
- package/lib/esm/MongooseModule.js.map +0 -1
- package/lib/esm/constants/constants.js.map +0 -1
- package/lib/esm/decorators/auto.js.map +0 -1
- package/lib/esm/decorators/discriminatorKey.js.map +0 -1
- package/lib/esm/decorators/dynamicRef.js.map +0 -1
- package/lib/esm/decorators/excludeIndexes.js.map +0 -1
- package/lib/esm/decorators/expires.js.map +0 -1
- package/lib/esm/decorators/immutable.js.map +0 -1
- package/lib/esm/decorators/indexed.js.map +0 -1
- package/lib/esm/decorators/lowercase.js.map +0 -1
- package/lib/esm/decorators/model.js.map +0 -1
- package/lib/esm/decorators/mongooseIndex.js.map +0 -1
- package/lib/esm/decorators/mongooseIndexes.js.map +0 -1
- package/lib/esm/decorators/mongoosePlugin.js.map +0 -1
- package/lib/esm/decorators/numberDecimal.js.map +0 -1
- package/lib/esm/decorators/objectID.js.map +0 -1
- package/lib/esm/decorators/postHook.js.map +0 -1
- package/lib/esm/decorators/preHook.js.map +0 -1
- package/lib/esm/decorators/ref.js.map +0 -1
- package/lib/esm/decorators/schema.js.map +0 -1
- package/lib/esm/decorators/schemaIgnore.js.map +0 -1
- package/lib/esm/decorators/select.js.map +0 -1
- package/lib/esm/decorators/sparse.js.map +0 -1
- package/lib/esm/decorators/text.js.map +0 -1
- package/lib/esm/decorators/trim.js.map +0 -1
- package/lib/esm/decorators/unique.js.map +0 -1
- package/lib/esm/decorators/uppercase.js.map +0 -1
- package/lib/esm/decorators/versionKey.js.map +0 -1
- package/lib/esm/decorators/virtualRef.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/MongooseConnectionOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseDocument.js.map +0 -1
- package/lib/esm/interfaces/MongooseModel.js.map +0 -1
- package/lib/esm/interfaces/MongooseModelOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseSchemaTypes.js.map +0 -1
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +0 -1
- package/lib/esm/interfaces/interfaces.js.map +0 -1
- package/lib/esm/registries/MongooseModels.js.map +0 -1
- package/lib/esm/services/MongooseConnections.js.map +0 -1
- package/lib/esm/services/MongooseService.js.map +0 -1
- package/lib/esm/utils/createModel.js.map +0 -1
- package/lib/esm/utils/createSchema.js.map +0 -1
- package/lib/esm/utils/resolveRefType.js.map +0 -1
- package/lib/esm/utils/schemaOptions.js.map +0 -1
- package/src/MongooseModule.ts +0 -32
- package/src/constants/constants.ts +0 -20
- package/src/decorators/auto.spec.ts +0 -30
- package/src/decorators/auto.ts +0 -24
- package/src/decorators/discriminatorKey.ts +0 -1
- package/src/decorators/dynamicRef.spec.ts +0 -46
- package/src/decorators/dynamicRef.ts +0 -76
- package/src/decorators/excludeIndexes.spec.ts +0 -30
- package/src/decorators/excludeIndexes.ts +0 -24
- package/src/decorators/expires.spec.ts +0 -18
- package/src/decorators/expires.ts +0 -24
- package/src/decorators/immutable.spec.ts +0 -30
- package/src/decorators/immutable.ts +0 -26
- package/src/decorators/indexed.spec.ts +0 -19
- package/src/decorators/indexed.ts +0 -24
- package/src/decorators/lowercase.spec.ts +0 -30
- package/src/decorators/lowercase.ts +0 -24
- package/src/decorators/model.ts +0 -69
- package/src/decorators/mongooseIndex.spec.ts +0 -26
- package/src/decorators/mongooseIndex.ts +0 -35
- package/src/decorators/mongooseIndexes.spec.ts +0 -35
- package/src/decorators/mongooseIndexes.ts +0 -35
- package/src/decorators/mongoosePlugin.spec.ts +0 -25
- package/src/decorators/mongoosePlugin.ts +0 -20
- package/src/decorators/numberDecimal.spec.ts +0 -266
- package/src/decorators/numberDecimal.ts +0 -84
- package/src/decorators/objectID.spec.ts +0 -24
- package/src/decorators/objectID.ts +0 -39
- package/src/decorators/postHook.spec.ts +0 -44
- package/src/decorators/postHook.ts +0 -72
- package/src/decorators/preHook.spec.ts +0 -75
- package/src/decorators/preHook.ts +0 -79
- package/src/decorators/ref.spec.ts +0 -361
- package/src/decorators/ref.ts +0 -111
- package/src/decorators/schema.ts +0 -79
- package/src/decorators/schemaIgnore.spec.ts +0 -18
- package/src/decorators/schemaIgnore.ts +0 -24
- package/src/decorators/select.spec.ts +0 -18
- package/src/decorators/select.ts +0 -24
- package/src/decorators/sparse.spec.ts +0 -30
- package/src/decorators/sparse.ts +0 -26
- package/src/decorators/text.spec.ts +0 -30
- package/src/decorators/text.ts +0 -25
- package/src/decorators/trim.spec.ts +0 -18
- package/src/decorators/trim.ts +0 -23
- package/src/decorators/unique.spec.ts +0 -18
- package/src/decorators/unique.ts +0 -23
- package/src/decorators/uppercase.spec.ts +0 -30
- package/src/decorators/uppercase.ts +0 -24
- package/src/decorators/versionKey.spec.ts +0 -19
- package/src/decorators/versionKey.ts +0 -9
- package/src/decorators/virtualRef.spec.ts +0 -383
- package/src/decorators/virtualRef.ts +0 -73
- package/src/index.ts +0 -47
- package/src/interfaces/MongooseConnectionOptions.ts +0 -10
- package/src/interfaces/MongooseDocument.ts +0 -3
- package/src/interfaces/MongooseModel.ts +0 -10
- package/src/interfaces/MongooseModelOptions.ts +0 -8
- package/src/interfaces/MongooseSchemaOptions.ts +0 -61
- package/src/interfaces/MongooseSchemaTypes.ts +0 -5
- package/src/interfaces/MongooseVirtualRefOptions.ts +0 -10
- package/src/interfaces/interfaces.ts +0 -9
- package/src/registries/MongooseModels.ts +0 -3
- package/src/services/MongooseConnection.spec.ts +0 -70
- package/src/services/MongooseConnections.ts +0 -58
- package/src/services/MongooseService.spec.ts +0 -73
- package/src/services/MongooseService.ts +0 -77
- package/src/utils/buildMongooseSchema.spec.ts +0 -67
- package/src/utils/createModel.spec.ts +0 -49
- package/src/utils/createModel.ts +0 -54
- package/src/utils/createSchema.spec.ts +0 -817
- package/src/utils/createSchema.ts +0 -207
- package/src/utils/resolveRefType.spec.ts +0 -30
- package/src/utils/resolveRefType.ts +0 -18
- package/src/utils/schemaOptions.spec.ts +0 -68
- package/src/utils/schemaOptions.ts +0 -74
- package/vitest.config.mts +0 -25
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import {classOf, cleanObject, isClassObject, nameOf, Store, Type} from "@tsed/core";
|
|
2
|
-
import {deserialize, serialize} from "@tsed/json-mapper";
|
|
3
|
-
import {getProperties, JsonEntityStore} from "@tsed/schema";
|
|
4
|
-
import {pascalCase} from "change-case";
|
|
5
|
-
import mongoose, {Schema, SchemaDefinition, SchemaDefinitionProperty, SchemaOptions, SchemaTypeOptions} from "mongoose";
|
|
6
|
-
|
|
7
|
-
import {MONGOOSE_SCHEMA, MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
|
|
8
|
-
import {MongooseSchemaOptions} from "../interfaces/MongooseSchemaOptions.js";
|
|
9
|
-
import {MongooseVirtualRefOptions} from "../interfaces/MongooseVirtualRefOptions.js";
|
|
10
|
-
import {resolveRefType} from "./resolveRefType.js";
|
|
11
|
-
import {schemaOptions} from "./schemaOptions.js";
|
|
12
|
-
|
|
13
|
-
export interface MongooseSchemaMetadata {
|
|
14
|
-
schema: SchemaDefinition;
|
|
15
|
-
virtuals: Map<string, MongooseVirtualRefOptions>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @ignore
|
|
20
|
-
*/
|
|
21
|
-
function setUpSchema({schema, virtuals}: MongooseSchemaMetadata, options?: SchemaOptions) {
|
|
22
|
-
const mongooseSchema = new mongoose.Schema(schema, options as never);
|
|
23
|
-
|
|
24
|
-
for (const [key, options] of virtuals.entries()) {
|
|
25
|
-
mongooseSchema.virtual(key, options);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return mongooseSchema;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @ignore
|
|
33
|
-
*/
|
|
34
|
-
function isVirtualRef(target: Partial<MongooseVirtualRefOptions>): target is MongooseVirtualRefOptions {
|
|
35
|
-
return !!(target.ref && target.localField && target.foreignField);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function hasVersionField(schema: mongoose.Schema, versionKey?: string | boolean) {
|
|
39
|
-
// Check if versioning was disabled explicitly
|
|
40
|
-
if (!versionKey) return false;
|
|
41
|
-
|
|
42
|
-
// Check for alternative version field in schema
|
|
43
|
-
return (versionKey as string) in schema.paths;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function createSchema(target: Type<any>, options: MongooseSchemaOptions = {}): mongoose.Schema {
|
|
47
|
-
const entity = JsonEntityStore.from(target);
|
|
48
|
-
const schemaOptionsFromStore = entity.store.get(MONGOOSE_SCHEMA_OPTIONS) || {};
|
|
49
|
-
options.schemaOptions = {...options.schemaOptions, ...schemaOptionsFromStore};
|
|
50
|
-
|
|
51
|
-
if (entity.schema.isDiscriminator) {
|
|
52
|
-
options.schemaOptions!.discriminatorKey = entity.schema.discriminator().propertyName;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const schema = setUpSchema(buildMongooseSchema(target), options.schemaOptions);
|
|
56
|
-
|
|
57
|
-
schemaOptions(target, options);
|
|
58
|
-
|
|
59
|
-
const outputVersionKey = hasVersionField(schema, options.schemaOptions?.versionKey);
|
|
60
|
-
|
|
61
|
-
schema.methods.toClass = function toClass() {
|
|
62
|
-
return deserialize(
|
|
63
|
-
this.toObject({
|
|
64
|
-
virtuals: true,
|
|
65
|
-
versionKey: outputVersionKey,
|
|
66
|
-
flattenMaps: true
|
|
67
|
-
}),
|
|
68
|
-
{
|
|
69
|
-
type: target,
|
|
70
|
-
useAlias: false,
|
|
71
|
-
additionalProperties: true,
|
|
72
|
-
disabledUnsecureConstructor: false,
|
|
73
|
-
groups: false
|
|
74
|
-
}
|
|
75
|
-
);
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
schema.methods.toJSON = function toJSON(options?: any) {
|
|
79
|
-
return serialize((this as any).toClass(), options);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
schema.loadClass(target);
|
|
83
|
-
|
|
84
|
-
Store.from(target).set(MONGOOSE_SCHEMA, schema);
|
|
85
|
-
|
|
86
|
-
return schema;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get a schema already created. If the schema doesn't exists in registry, it'll be created.
|
|
91
|
-
* @param target
|
|
92
|
-
* @param options
|
|
93
|
-
*/
|
|
94
|
-
export function getSchema(target: Type<any>, options: MongooseSchemaOptions = {}): Schema {
|
|
95
|
-
const store = Store.from(target);
|
|
96
|
-
|
|
97
|
-
if (!store.has(MONGOOSE_SCHEMA)) {
|
|
98
|
-
createSchema(target, options);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return store.get(MONGOOSE_SCHEMA);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function getSchemaToken(target: Type<any>, options?: any) {
|
|
105
|
-
const collectionName = options.name || nameOf(target);
|
|
106
|
-
const token = Symbol.for(pascalCase(`${collectionName}Schema`));
|
|
107
|
-
|
|
108
|
-
return {token, collectionName};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @ignore
|
|
113
|
-
*/
|
|
114
|
-
export function buildMongooseSchema(target: any): MongooseSchemaMetadata {
|
|
115
|
-
const properties = getProperties(target, {withIgnoredProps: true, mongoose: true, groups: false});
|
|
116
|
-
const schema: MongooseSchemaMetadata = {schema: {}, virtuals: new Map()};
|
|
117
|
-
|
|
118
|
-
properties.forEach((propertyMetadata, key) => {
|
|
119
|
-
// Keeping the Mongoose Schema separate, so it can overwrite everything once schema has been built.
|
|
120
|
-
const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};
|
|
121
|
-
|
|
122
|
-
if (schemaTypeOptions.schemaIgnore || propertyMetadata.isDiscriminatorKey() || propertyMetadata.isGetterOnly()) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (schemaTypeOptions.ref) {
|
|
127
|
-
schemaTypeOptions.ref = resolveRefType(schemaTypeOptions.ref as any);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (isVirtualRef(schemaTypeOptions)) {
|
|
131
|
-
schema.virtuals.set(key as string, schemaTypeOptions);
|
|
132
|
-
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
schema.schema[key as string] = createSchemaTypeOptions(propertyMetadata);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
return schema;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @ignore
|
|
144
|
-
*/
|
|
145
|
-
export function createSchemaTypeOptions(propEntity: JsonEntityStore): SchemaDefinitionProperty {
|
|
146
|
-
const key = propEntity.propertyKey;
|
|
147
|
-
const rawMongooseSchema = propEntity.store.get(MONGOOSE_SCHEMA) || {};
|
|
148
|
-
|
|
149
|
-
let schemaTypeOptions: SchemaTypeOptions<any> = {
|
|
150
|
-
required: propEntity.required
|
|
151
|
-
? function () {
|
|
152
|
-
return propEntity.isRequired(this[key]);
|
|
153
|
-
}
|
|
154
|
-
: false
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
if (!propEntity.isClass) {
|
|
158
|
-
const jsonSchema = propEntity.itemSchema.toJSON();
|
|
159
|
-
const {minimum: min, maximum: max, minLength: minlength, maxLength: maxlength} = jsonSchema;
|
|
160
|
-
|
|
161
|
-
let match: string | RegExp = jsonSchema.pattern;
|
|
162
|
-
if (match) {
|
|
163
|
-
match = new RegExp(match);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
schemaTypeOptions = {
|
|
167
|
-
...schemaTypeOptions,
|
|
168
|
-
type: jsonSchema["enum"] && isClassObject(propEntity.type) ? classOf(jsonSchema["enum"][0]) : propEntity.type,
|
|
169
|
-
match: match as RegExp,
|
|
170
|
-
min,
|
|
171
|
-
max,
|
|
172
|
-
minlength,
|
|
173
|
-
maxlength,
|
|
174
|
-
enum: jsonSchema["enum"],
|
|
175
|
-
default: jsonSchema["default"]
|
|
176
|
-
};
|
|
177
|
-
} else if (!rawMongooseSchema.ref) {
|
|
178
|
-
// References are handled by the final merge
|
|
179
|
-
schemaTypeOptions = {...schemaTypeOptions, type: getSchema(propEntity.type) as any};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
schemaTypeOptions = cleanObject({...schemaTypeOptions, ...rawMongooseSchema});
|
|
183
|
-
|
|
184
|
-
if (propEntity.isCollection) {
|
|
185
|
-
if (propEntity.isArray) {
|
|
186
|
-
return {
|
|
187
|
-
...schemaTypeOptions,
|
|
188
|
-
type: [schemaTypeOptions.type]
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
// Can be a Map or a Set;
|
|
192
|
-
// Mongoose implements only Map;
|
|
193
|
-
if (propEntity.collectionType !== Map) {
|
|
194
|
-
throw new Error(`Invalid collection type. ${nameOf(propEntity.collectionType)} is not supported.`);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const {default: defaultValue, required, ...otherOpts} = schemaTypeOptions;
|
|
198
|
-
return {
|
|
199
|
-
type: Map,
|
|
200
|
-
of: otherOpts,
|
|
201
|
-
default: defaultValue,
|
|
202
|
-
required
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return schemaTypeOptions;
|
|
207
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {Store} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {MONGOOSE_MODEL_NAME} from "../constants/constants.js";
|
|
4
|
-
import {resolveRefType} from "./resolveRefType.js";
|
|
5
|
-
|
|
6
|
-
describe("resolveRefType", () => {
|
|
7
|
-
it("should return type as string (from string)", () => {
|
|
8
|
-
expect(resolveRefType("Test")).toBe("Test");
|
|
9
|
-
});
|
|
10
|
-
it("should return type as string (from class)", () => {
|
|
11
|
-
class Test {}
|
|
12
|
-
|
|
13
|
-
Store.from(Test).set(MONGOOSE_MODEL_NAME, "Test");
|
|
14
|
-
|
|
15
|
-
expect(resolveRefType(Test)).toBe("Test");
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("should return type as string (from class without store)", () => {
|
|
19
|
-
class Test {}
|
|
20
|
-
|
|
21
|
-
expect(resolveRefType(Test)).toBe("Test");
|
|
22
|
-
});
|
|
23
|
-
it("should return type as string (from arrow resolver)", () => {
|
|
24
|
-
class Test {}
|
|
25
|
-
|
|
26
|
-
Store.from(Test).set(MONGOOSE_MODEL_NAME, "Test");
|
|
27
|
-
|
|
28
|
-
expect(resolveRefType(() => Test)).toBe("Test");
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import {isClass, isString, nameOf, Store, Type} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {MONGOOSE_MODEL_NAME} from "../constants/constants.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @ignore
|
|
7
|
-
*/
|
|
8
|
-
export function resolveRefType(type: string | (() => Type<any>) | Type<any>): string {
|
|
9
|
-
if (isString(type)) {
|
|
10
|
-
return type;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (!isClass(type)) {
|
|
14
|
-
type = (type as any)();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return Store.from(type).get(MONGOOSE_MODEL_NAME) || nameOf(type);
|
|
18
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import {applySchemaOptions, schemaOptions} from "../../src/utils/schemaOptions.js";
|
|
2
|
-
|
|
3
|
-
describe("schemaOptions", () => {
|
|
4
|
-
describe("schemaOptions()", () => {
|
|
5
|
-
class Test {}
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {});
|
|
8
|
-
|
|
9
|
-
it("should return schema options (1)", () => {
|
|
10
|
-
expect(schemaOptions(Test)).toEqual({});
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("should return schema options (2)", () => {
|
|
14
|
-
expect(schemaOptions(Test, {options: "options"} as any)).toEqual({options: "options"});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("should return schema options (3)", () => {
|
|
18
|
-
expect(schemaOptions(Test, {options2: "options2"} as any)).toEqual({
|
|
19
|
-
options: "options",
|
|
20
|
-
options2: "options2"
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
describe("applySchemaOptions()", () => {
|
|
25
|
-
const schema: any = {
|
|
26
|
-
pre: vi.fn(),
|
|
27
|
-
post: vi.fn(),
|
|
28
|
-
plugin: vi.fn(),
|
|
29
|
-
index: vi.fn()
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
applySchemaOptions(schema, {
|
|
34
|
-
pre: [
|
|
35
|
-
{
|
|
36
|
-
method: "method",
|
|
37
|
-
fn: (doc: any, pre: any) => {},
|
|
38
|
-
options: {query: true}
|
|
39
|
-
}
|
|
40
|
-
],
|
|
41
|
-
post: [
|
|
42
|
-
{
|
|
43
|
-
method: "method",
|
|
44
|
-
fn: "fn"
|
|
45
|
-
}
|
|
46
|
-
],
|
|
47
|
-
plugins: [{plugin: "plugin", options: "options"}],
|
|
48
|
-
indexes: [{fields: "fields", options: "options"}]
|
|
49
|
-
} as any);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("should call schema.pre", () => {
|
|
53
|
-
expect(schema.pre).toHaveBeenCalledWith("method", {query: true}, expect.any(Function));
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("should call schema.post", () => {
|
|
57
|
-
expect(schema.post).toHaveBeenCalledWith("method", "fn");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("should call schema.plugin", () => {
|
|
61
|
-
expect(schema.plugin).toHaveBeenCalledWith("plugin", "options");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("should call schema.index", () => {
|
|
65
|
-
expect(schema.index).toHaveBeenCalledWith("fields", "options");
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import {deepMerge, Store} from "@tsed/core";
|
|
2
|
-
import {Schema} from "mongoose";
|
|
3
|
-
|
|
4
|
-
import {MONGOOSE_SCHEMA_OPTIONS} from "../constants/constants.js";
|
|
5
|
-
import {
|
|
6
|
-
MongooseHookPromised,
|
|
7
|
-
MongoosePostHook,
|
|
8
|
-
MongoosePreHook,
|
|
9
|
-
MongoosePreHookCB,
|
|
10
|
-
MongooseSchemaOptions
|
|
11
|
-
} from "../interfaces/MongooseSchemaOptions.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @ignore
|
|
15
|
-
*/
|
|
16
|
-
export function schemaOptions(target: any, options?: MongooseSchemaOptions) {
|
|
17
|
-
const store = Store.from(target);
|
|
18
|
-
|
|
19
|
-
if (!store.has(MONGOOSE_SCHEMA_OPTIONS)) {
|
|
20
|
-
store.set(MONGOOSE_SCHEMA_OPTIONS, {});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (options) {
|
|
24
|
-
store.set(MONGOOSE_SCHEMA_OPTIONS, deepMerge(store.get(MONGOOSE_SCHEMA_OPTIONS), options));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return store.get(MONGOOSE_SCHEMA_OPTIONS);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function mapHookArgs(hook: MongoosePreHook | MongoosePostHook): [string | RegExp, Function] {
|
|
31
|
-
return [hook.method, hook.options || hook.fn, hook.options ? hook.fn : undefined].filter(Boolean) as any;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @ignore
|
|
36
|
-
*/
|
|
37
|
-
export function buildPreHook(fn: MongoosePreHookCB) {
|
|
38
|
-
return fn.length === 1
|
|
39
|
-
? function () {
|
|
40
|
-
return (fn as MongooseHookPromised)(this);
|
|
41
|
-
}
|
|
42
|
-
: // we need to explicitly gives args to avoid a bug with mongoose
|
|
43
|
-
function (next: Function, arg1: unknown, arg2: unknown) {
|
|
44
|
-
return (fn as any)(this, next, arg1, arg2);
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @ignore
|
|
50
|
-
*/
|
|
51
|
-
export function applySchemaOptions(schema: Schema, options: MongooseSchemaOptions) {
|
|
52
|
-
if (options.plugins) {
|
|
53
|
-
options.plugins.forEach((item) => schema.plugin(item.plugin, item.options));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (options.indexes) {
|
|
57
|
-
options.indexes.forEach((item) => schema.index(item.fields, item.options));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (options.pre) {
|
|
61
|
-
options.pre.forEach((item) => {
|
|
62
|
-
item = {
|
|
63
|
-
...item,
|
|
64
|
-
fn: buildPreHook(item.fn)
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
(schema.pre as any)(...mapHookArgs(item));
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (options.post) {
|
|
72
|
-
options.post.forEach((item) => (schema.post as any)(...mapHookArgs(item)));
|
|
73
|
-
}
|
|
74
|
-
}
|
package/vitest.config.mts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import {presets} from "@tsed/vitest/presets";
|
|
3
|
-
import {defineConfig} from "vitest/config";
|
|
4
|
-
|
|
5
|
-
export default defineConfig(
|
|
6
|
-
{
|
|
7
|
-
...presets,
|
|
8
|
-
test: {
|
|
9
|
-
...presets.test,
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
globalSetup: [
|
|
12
|
-
"../testcontainers-mongo/src/setup/vi.setup.ts"
|
|
13
|
-
],
|
|
14
|
-
coverage: {
|
|
15
|
-
...presets.test.coverage,
|
|
16
|
-
thresholds: {
|
|
17
|
-
statements: 0,
|
|
18
|
-
branches: 0,
|
|
19
|
-
functions: 0,
|
|
20
|
-
lines: 0
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
);
|