@bedrockio/model 0.2.19 → 0.2.21
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/dist/cjs/include.js +8 -2
- package/dist/cjs/schema.js +3 -3
- package/dist/cjs/validation.js +5 -12
- package/jest-mongodb-config.cjs +14 -0
- package/package.json +5 -5
- package/src/include.js +8 -2
- package/src/schema.js +3 -3
- package/src/validation.js +5 -14
- package/types/load.d.ts +67 -1
- package/types/load.d.ts.map +1 -1
- package/types/schema.d.ts +7 -3
- package/types/schema.d.ts.map +1 -1
- package/types/validation.d.ts +1 -0
- package/types/validation.d.ts.map +1 -1
- package/jest-mongodb-config.js +0 -12
package/dist/cjs/include.js
CHANGED
|
@@ -181,6 +181,12 @@ function nodeToPopulates(node) {
|
|
|
181
181
|
populate
|
|
182
182
|
};
|
|
183
183
|
}
|
|
184
|
+
|
|
185
|
+
// Null serves as a flag that the key terminates
|
|
186
|
+
// the branch and this is a leaf node. Using null
|
|
187
|
+
// here as it's simple and serializes to JSON for
|
|
188
|
+
// easy inspection.
|
|
189
|
+
const LEAF_NODE = null;
|
|
184
190
|
function pathsToNode(paths, modelName) {
|
|
185
191
|
const node = {};
|
|
186
192
|
for (let str of paths) {
|
|
@@ -228,7 +234,7 @@ function setNodePath(node, options) {
|
|
|
228
234
|
// -user.name - Implies population of "user" but exclude "user.name",
|
|
229
235
|
// so continue traversing into object when part is "user".
|
|
230
236
|
if (isExact && exclude) {
|
|
231
|
-
node['-' + key] =
|
|
237
|
+
node['-' + key] = LEAF_NODE;
|
|
232
238
|
} else if (field.ref) {
|
|
233
239
|
node[key] ||= {};
|
|
234
240
|
setNodePath(node[key], {
|
|
@@ -239,7 +245,7 @@ function setNodePath(node, options) {
|
|
|
239
245
|
});
|
|
240
246
|
halt = true;
|
|
241
247
|
} else if ((0, _utils.isSchemaTypedef)(field)) {
|
|
242
|
-
node[key] =
|
|
248
|
+
node[key] = LEAF_NODE;
|
|
243
249
|
}
|
|
244
250
|
} else if (type === 'virtual') {
|
|
245
251
|
const virtual = schema.virtual(key);
|
package/dist/cjs/schema.js
CHANGED
|
@@ -227,13 +227,13 @@ function applyScopeExtension(typedef, definition) {
|
|
|
227
227
|
...options
|
|
228
228
|
};
|
|
229
229
|
} else {
|
|
230
|
-
val =
|
|
230
|
+
val = {
|
|
231
231
|
type: 'Object',
|
|
232
232
|
attributes: val,
|
|
233
233
|
...options
|
|
234
|
-
}
|
|
234
|
+
};
|
|
235
235
|
}
|
|
236
|
-
definition[key] = val;
|
|
236
|
+
definition[key] = attributesToMongoose(val);
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
239
|
|
package/dist/cjs/validation.js
CHANGED
|
@@ -105,13 +105,13 @@ function applyValidation(schema, definition) {
|
|
|
105
105
|
model: this,
|
|
106
106
|
appendSchema,
|
|
107
107
|
allowInclude,
|
|
108
|
+
allowUnset: true,
|
|
108
109
|
skipRequired: true,
|
|
109
110
|
stripUnknown: true,
|
|
110
111
|
stripDeleted: true,
|
|
111
112
|
stripTimestamps: true,
|
|
112
113
|
allowExpandedRefs: true,
|
|
113
114
|
requireWriteAccess: true,
|
|
114
|
-
allowNullForPrimitives: true,
|
|
115
115
|
...(hasUnique && {
|
|
116
116
|
assertUniqueOptions: {
|
|
117
117
|
schema,
|
|
@@ -270,8 +270,8 @@ function getSchemaForTypedef(typedef, options = {}) {
|
|
|
270
270
|
}
|
|
271
271
|
if (isRequired(typedef, options)) {
|
|
272
272
|
schema = schema.required();
|
|
273
|
-
} else if (
|
|
274
|
-
schema = _yada.default.allow(null, schema);
|
|
273
|
+
} else if (allowUnset(typedef, options)) {
|
|
274
|
+
schema = _yada.default.allow(null, '', schema);
|
|
275
275
|
}
|
|
276
276
|
if (typedef.default && options.allowDefaultTags) {
|
|
277
277
|
// Tag the default value to allow OpenAPI description
|
|
@@ -379,15 +379,8 @@ function getSearchSchema(schema, type) {
|
|
|
379
379
|
function isRequired(typedef, options) {
|
|
380
380
|
return typedef.required && !typedef.default && !options.skipRequired;
|
|
381
381
|
}
|
|
382
|
-
function
|
|
383
|
-
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
return !typedef.required && isPrimitiveTypedef(typedef);
|
|
387
|
-
}
|
|
388
|
-
const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
|
|
389
|
-
function isPrimitiveTypedef(typedef) {
|
|
390
|
-
return PRIMITIVE_TYPES.includes(typedef.type);
|
|
382
|
+
function allowUnset(typedef, options) {
|
|
383
|
+
return options.allowUnset && !typedef.required;
|
|
391
384
|
}
|
|
392
385
|
function isExcludedField(field, options) {
|
|
393
386
|
if ((0, _utils.isSchemaTypedef)(field)) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Note this file MUST be a CJS module as it will be required
|
|
2
|
+
// by @shelf/jest-mongodb. Note also that the binary version
|
|
3
|
+
// does not match the mongodb driver version.
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
mongodbMemoryServerOptions: {
|
|
7
|
+
binary: {
|
|
8
|
+
version: '6.0.2',
|
|
9
|
+
skipMD5: true,
|
|
10
|
+
},
|
|
11
|
+
autoStart: false,
|
|
12
|
+
instance: {},
|
|
13
|
+
},
|
|
14
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/model",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.21",
|
|
4
4
|
"description": "Bedrock utilities for model creation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"lodash": "^4.17.21"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@bedrockio/yada": "^1.0.
|
|
33
|
+
"@bedrockio/yada": "^1.0.40",
|
|
34
34
|
"mongoose": "^7.6.4"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"@babel/core": "^7.20.12",
|
|
39
39
|
"@babel/preset-env": "^7.20.2",
|
|
40
40
|
"@bedrockio/prettier-config": "^1.0.2",
|
|
41
|
-
"@bedrockio/yada": "^1.0.
|
|
42
|
-
"@shelf/jest-mongodb": "^4.
|
|
41
|
+
"@bedrockio/yada": "^1.0.40",
|
|
42
|
+
"@shelf/jest-mongodb": "^4.2.0",
|
|
43
43
|
"eslint": "^8.33.0",
|
|
44
44
|
"eslint-plugin-bedrock": "^1.0.26",
|
|
45
45
|
"jest": "^29.4.1",
|
|
46
46
|
"jest-environment-node": "^29.4.1",
|
|
47
|
-
"mongodb": "^6.
|
|
47
|
+
"mongodb": "^6.5.0",
|
|
48
48
|
"mongoose": "^7.6.4",
|
|
49
49
|
"prettier-eslint": "^15.0.1",
|
|
50
50
|
"typescript": "^4.9.5"
|
package/src/include.js
CHANGED
|
@@ -170,6 +170,12 @@ function nodeToPopulates(node) {
|
|
|
170
170
|
};
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
+
// Null serves as a flag that the key terminates
|
|
174
|
+
// the branch and this is a leaf node. Using null
|
|
175
|
+
// here as it's simple and serializes to JSON for
|
|
176
|
+
// easy inspection.
|
|
177
|
+
const LEAF_NODE = null;
|
|
178
|
+
|
|
173
179
|
function pathsToNode(paths, modelName) {
|
|
174
180
|
const node = {};
|
|
175
181
|
for (let str of paths) {
|
|
@@ -214,7 +220,7 @@ function setNodePath(node, options) {
|
|
|
214
220
|
// -user.name - Implies population of "user" but exclude "user.name",
|
|
215
221
|
// so continue traversing into object when part is "user".
|
|
216
222
|
if (isExact && exclude) {
|
|
217
|
-
node['-' + key] =
|
|
223
|
+
node['-' + key] = LEAF_NODE;
|
|
218
224
|
} else if (field.ref) {
|
|
219
225
|
node[key] ||= {};
|
|
220
226
|
setNodePath(node[key], {
|
|
@@ -225,7 +231,7 @@ function setNodePath(node, options) {
|
|
|
225
231
|
});
|
|
226
232
|
halt = true;
|
|
227
233
|
} else if (isSchemaTypedef(field)) {
|
|
228
|
-
node[key] =
|
|
234
|
+
node[key] = LEAF_NODE;
|
|
229
235
|
}
|
|
230
236
|
} else if (type === 'virtual') {
|
|
231
237
|
const virtual = schema.virtual(key);
|
package/src/schema.js
CHANGED
|
@@ -236,13 +236,13 @@ function applyScopeExtension(typedef, definition) {
|
|
|
236
236
|
...options,
|
|
237
237
|
};
|
|
238
238
|
} else {
|
|
239
|
-
val =
|
|
239
|
+
val = {
|
|
240
240
|
type: 'Object',
|
|
241
241
|
attributes: val,
|
|
242
242
|
...options,
|
|
243
|
-
}
|
|
243
|
+
};
|
|
244
244
|
}
|
|
245
|
-
definition[key] = val;
|
|
245
|
+
definition[key] = attributesToMongoose(val);
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
|
package/src/validation.js
CHANGED
|
@@ -118,13 +118,13 @@ export function applyValidation(schema, definition) {
|
|
|
118
118
|
model: this,
|
|
119
119
|
appendSchema,
|
|
120
120
|
allowInclude,
|
|
121
|
+
allowUnset: true,
|
|
121
122
|
skipRequired: true,
|
|
122
123
|
stripUnknown: true,
|
|
123
124
|
stripDeleted: true,
|
|
124
125
|
stripTimestamps: true,
|
|
125
126
|
allowExpandedRefs: true,
|
|
126
127
|
requireWriteAccess: true,
|
|
127
|
-
allowNullForPrimitives: true,
|
|
128
128
|
...(hasUnique && {
|
|
129
129
|
assertUniqueOptions: {
|
|
130
130
|
schema,
|
|
@@ -283,8 +283,8 @@ function getSchemaForTypedef(typedef, options = {}) {
|
|
|
283
283
|
|
|
284
284
|
if (isRequired(typedef, options)) {
|
|
285
285
|
schema = schema.required();
|
|
286
|
-
} else if (
|
|
287
|
-
schema = yd.allow(null, schema);
|
|
286
|
+
} else if (allowUnset(typedef, options)) {
|
|
287
|
+
schema = yd.allow(null, '', schema);
|
|
288
288
|
}
|
|
289
289
|
|
|
290
290
|
if (typedef.default && options.allowDefaultTags) {
|
|
@@ -434,17 +434,8 @@ function isRequired(typedef, options) {
|
|
|
434
434
|
return typedef.required && !typedef.default && !options.skipRequired;
|
|
435
435
|
}
|
|
436
436
|
|
|
437
|
-
function
|
|
438
|
-
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
return !typedef.required && isPrimitiveTypedef(typedef);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
|
|
445
|
-
|
|
446
|
-
function isPrimitiveTypedef(typedef) {
|
|
447
|
-
return PRIMITIVE_TYPES.includes(typedef.type);
|
|
437
|
+
function allowUnset(typedef, options) {
|
|
438
|
+
return options.allowUnset && !typedef.required;
|
|
448
439
|
}
|
|
449
440
|
|
|
450
441
|
function isExcludedField(field, options) {
|
package/types/load.d.ts
CHANGED
|
@@ -4,7 +4,73 @@
|
|
|
4
4
|
* @param {string} name
|
|
5
5
|
* @returns mongoose.Model
|
|
6
6
|
*/
|
|
7
|
-
export function loadModel(definition: object, name: string): any
|
|
7
|
+
export function loadModel(definition: object, name: string): mongoose.Model<any, any, any, any, any, mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, {
|
|
8
|
+
[x: string]: any;
|
|
9
|
+
}, {
|
|
10
|
+
autoIndex?: boolean;
|
|
11
|
+
autoCreate?: boolean;
|
|
12
|
+
bufferCommands?: boolean;
|
|
13
|
+
bufferTimeoutMS?: number;
|
|
14
|
+
capped?: number | boolean | {
|
|
15
|
+
size?: number;
|
|
16
|
+
max?: number;
|
|
17
|
+
autoIndexId?: boolean;
|
|
18
|
+
};
|
|
19
|
+
collation?: mongoose.mongo.CollationOptions;
|
|
20
|
+
collectionOptions?: mongoose.mongo.CreateCollectionOptions;
|
|
21
|
+
timeseries?: mongoose.mongo.TimeSeriesCollectionOptions;
|
|
22
|
+
expireAfterSeconds?: number;
|
|
23
|
+
expires?: string | number;
|
|
24
|
+
collection?: string;
|
|
25
|
+
discriminatorKey?: string;
|
|
26
|
+
excludeIndexes?: boolean;
|
|
27
|
+
id?: boolean;
|
|
28
|
+
_id?: boolean;
|
|
29
|
+
minimize?: boolean;
|
|
30
|
+
optimisticConcurrency?: boolean;
|
|
31
|
+
pluginTags?: string[];
|
|
32
|
+
read?: string;
|
|
33
|
+
writeConcern?: mongoose.mongo.WriteConcern;
|
|
34
|
+
safe?: boolean | {
|
|
35
|
+
w?: string | number;
|
|
36
|
+
wtimeout?: number;
|
|
37
|
+
j?: boolean;
|
|
38
|
+
};
|
|
39
|
+
shardKey?: Record<string, unknown>;
|
|
40
|
+
strict?: boolean | "throw";
|
|
41
|
+
strictQuery?: boolean | "throw";
|
|
42
|
+
toJSON: {
|
|
43
|
+
getters: boolean;
|
|
44
|
+
versionKey: boolean;
|
|
45
|
+
transform: (doc: any, ret: any, options: any) => void;
|
|
46
|
+
} | mongoose.ToObjectOptions<any>;
|
|
47
|
+
toObject: {
|
|
48
|
+
getters: boolean;
|
|
49
|
+
versionKey: boolean;
|
|
50
|
+
transform: (doc: any, ret: any, options: any) => void;
|
|
51
|
+
} | mongoose.ToObjectOptions<any>;
|
|
52
|
+
typeKey?: string;
|
|
53
|
+
validateBeforeSave?: boolean;
|
|
54
|
+
validateModifiedOnly?: boolean;
|
|
55
|
+
versionKey?: string | boolean;
|
|
56
|
+
selectPopulatedPaths?: boolean;
|
|
57
|
+
skipVersioning?: {
|
|
58
|
+
[key: string]: boolean;
|
|
59
|
+
};
|
|
60
|
+
storeSubdocValidationError?: boolean;
|
|
61
|
+
timestamps: boolean | mongoose.SchemaTimestampsConfig;
|
|
62
|
+
suppressReservedKeysWarning?: boolean;
|
|
63
|
+
statics?: {
|
|
64
|
+
[x: string]: any;
|
|
65
|
+
};
|
|
66
|
+
methods?: any;
|
|
67
|
+
query?: any;
|
|
68
|
+
castNonArrays?: boolean;
|
|
69
|
+
virtuals?: mongoose.SchemaOptionsVirtualsPropertyType<any, any, any>;
|
|
70
|
+
overwriteModels?: boolean;
|
|
71
|
+
}, any, any>> & {
|
|
72
|
+
[x: string]: any;
|
|
73
|
+
};
|
|
8
74
|
/**
|
|
9
75
|
* Loads all model definitions in the given directory.
|
|
10
76
|
* Returns the full loaded model set.
|
package/types/load.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAahB;AAED;;;;GAIG;AACH,sCAFW,MAAM,mBAkBhB"}
|
package/types/schema.d.ts
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* @param {mongoose.SchemaOptions} options
|
|
7
7
|
* @returns mongoose.Schema
|
|
8
8
|
*/
|
|
9
|
-
export function createSchema(definition: object, options?: mongoose.SchemaOptions): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any,
|
|
9
|
+
export function createSchema(definition: object, options?: mongoose.SchemaOptions): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, {
|
|
10
|
+
[x: string]: any;
|
|
11
|
+
}, {
|
|
10
12
|
autoIndex?: boolean;
|
|
11
13
|
autoCreate?: boolean;
|
|
12
14
|
bufferCommands?: boolean;
|
|
@@ -60,8 +62,10 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
60
62
|
storeSubdocValidationError?: boolean;
|
|
61
63
|
timestamps: boolean | mongoose.SchemaTimestampsConfig;
|
|
62
64
|
suppressReservedKeysWarning?: boolean;
|
|
63
|
-
statics?:
|
|
64
|
-
|
|
65
|
+
statics?: {
|
|
66
|
+
[x: string]: any;
|
|
67
|
+
};
|
|
68
|
+
methods?: any;
|
|
65
69
|
query?: any;
|
|
66
70
|
castNonArrays?: boolean;
|
|
67
71
|
virtuals?: mongoose.SchemaOptionsVirtualsPropertyType<any, any, any>;
|
package/types/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAoBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAoBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAuChC;AAED,iEAsBC"}
|
package/types/validation.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEAqFC;AAsBD,wEAkBC;
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEAqFC;AAsBD,wEAkBC;AAgSD;;;EAEC;AAED;;;EAOC;AA5eD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
|
package/jest-mongodb-config.js
DELETED