@bedrockio/model 0.14.2 → 0.14.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.
- package/CHANGELOG.md +8 -0
- package/dist/cjs/search.js +23 -9
- package/dist/cjs/utils.js +4 -0
- package/dist/cjs/validation.js +7 -8
- package/package.json +1 -1
- package/src/search.js +23 -9
- package/src/utils.js +4 -0
- package/src/validation.js +7 -8
- package/types/schema.d.ts +5 -8
- package/types/schema.d.ts.map +1 -1
- package/types/search.d.ts.map +1 -1
- package/types/utils.d.ts.map +1 -1
- package/types/validation.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/search.js
CHANGED
|
@@ -85,7 +85,7 @@ function searchQuery(Model, options, config) {
|
|
|
85
85
|
...rest
|
|
86
86
|
} = options;
|
|
87
87
|
sort = resolveSort(sort, schema);
|
|
88
|
-
let query = normalizeQuery(rest, schema
|
|
88
|
+
let query = normalizeQuery(rest, schema);
|
|
89
89
|
if (ids?.length) {
|
|
90
90
|
query = (0, _query.mergeQuery)(query, {
|
|
91
91
|
_id: {
|
|
@@ -231,7 +231,7 @@ function buildKeywordQuery(schema, keyword, config) {
|
|
|
231
231
|
return getTextIndexQuery(keyword);
|
|
232
232
|
}
|
|
233
233
|
keyword = (0, _lodash.escapeRegExp)(keyword);
|
|
234
|
-
const queries = [...getDecomposedQueries(keyword, config), ...getFieldQueries(keyword, config)];
|
|
234
|
+
const queries = [...getDecomposedQueries(keyword, config), ...getFieldQueries(schema, keyword, config)];
|
|
235
235
|
|
|
236
236
|
// Note: Mongo will error on empty $or/$and array.
|
|
237
237
|
if (queries.length > 1) {
|
|
@@ -295,7 +295,7 @@ const compileDecomposer = (0, _lodash.memoize)(template => {
|
|
|
295
295
|
}
|
|
296
296
|
};
|
|
297
297
|
});
|
|
298
|
-
function getFieldQueries(keyword, config) {
|
|
298
|
+
function getFieldQueries(schema, keyword, config) {
|
|
299
299
|
const {
|
|
300
300
|
fields
|
|
301
301
|
} = config;
|
|
@@ -303,12 +303,26 @@ function getFieldQueries(keyword, config) {
|
|
|
303
303
|
return [];
|
|
304
304
|
}
|
|
305
305
|
const queries = fields.map(field => {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
$
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
306
|
+
if ((0, _utils.isNumberField)(schema, field)) {
|
|
307
|
+
return {
|
|
308
|
+
$expr: {
|
|
309
|
+
$regexMatch: {
|
|
310
|
+
input: {
|
|
311
|
+
$toString: `$${field}`
|
|
312
|
+
},
|
|
313
|
+
regex: keyword,
|
|
314
|
+
options: 'i'
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
} else {
|
|
319
|
+
return {
|
|
320
|
+
[field]: {
|
|
321
|
+
$regex: keyword,
|
|
322
|
+
$options: 'i'
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
312
326
|
});
|
|
313
327
|
if (ObjectId.isValid(keyword)) {
|
|
314
328
|
queries.push({
|
package/dist/cjs/utils.js
CHANGED
|
@@ -91,6 +91,10 @@ function isSchemaTypedef(arg) {
|
|
|
91
91
|
// "inventory" field. It must traverse into arrays and other mongoose
|
|
92
92
|
// schemas along the way except for the final field.
|
|
93
93
|
function getField(obj, path) {
|
|
94
|
+
if (obj instanceof _mongoose.default.Schema) {
|
|
95
|
+
// Unwrap mongoose schema if passed.
|
|
96
|
+
obj = obj.obj;
|
|
97
|
+
}
|
|
94
98
|
let field = obj;
|
|
95
99
|
if (typeof path === 'string') {
|
|
96
100
|
path = path.split('.');
|
package/dist/cjs/validation.js
CHANGED
|
@@ -10,7 +10,6 @@ exports.getTupleValidator = getTupleValidator;
|
|
|
10
10
|
exports.getValidationSchema = getValidationSchema;
|
|
11
11
|
var _yada = _interopRequireDefault(require("@bedrockio/yada"));
|
|
12
12
|
var _lodash = require("lodash");
|
|
13
|
-
var _mongoose = _interopRequireDefault(require("mongoose"));
|
|
14
13
|
var _access = require("./access");
|
|
15
14
|
var _errors = require("./errors");
|
|
16
15
|
var _include = require("./include");
|
|
@@ -59,7 +58,7 @@ function addValidators(schemas) {
|
|
|
59
58
|
}
|
|
60
59
|
function applyValidation(schema, definition) {
|
|
61
60
|
schema.static('getCreateValidation', function getCreateValidation(options) {
|
|
62
|
-
return
|
|
61
|
+
return getSchemaForMongoose(schema, {
|
|
63
62
|
model: this,
|
|
64
63
|
stripEmpty: true,
|
|
65
64
|
applyUnique: true,
|
|
@@ -73,7 +72,7 @@ function applyValidation(schema, definition) {
|
|
|
73
72
|
});
|
|
74
73
|
});
|
|
75
74
|
schema.static('getUpdateValidation', function getUpdateValidation(options) {
|
|
76
|
-
return
|
|
75
|
+
return getSchemaForMongoose(schema, {
|
|
77
76
|
model: this,
|
|
78
77
|
allowNull: true,
|
|
79
78
|
applyUnique: true,
|
|
@@ -96,7 +95,7 @@ function applyValidation(schema, definition) {
|
|
|
96
95
|
formats,
|
|
97
96
|
...rest
|
|
98
97
|
} = options;
|
|
99
|
-
let validation =
|
|
98
|
+
let validation = getSchemaForMongoose(schema, {
|
|
100
99
|
model: this,
|
|
101
100
|
allowNull: true,
|
|
102
101
|
stripEmpty: true,
|
|
@@ -132,7 +131,7 @@ function applyValidation(schema, definition) {
|
|
|
132
131
|
return _include.INCLUDE_FIELD_SCHEMA;
|
|
133
132
|
});
|
|
134
133
|
schema.static('getBaseSchema', function getBaseSchema() {
|
|
135
|
-
return
|
|
134
|
+
return getSchemaForMongoose(schema, {
|
|
136
135
|
model: this,
|
|
137
136
|
stripDeleted: true,
|
|
138
137
|
requireReadAccess: true
|
|
@@ -142,7 +141,7 @@ function applyValidation(schema, definition) {
|
|
|
142
141
|
|
|
143
142
|
// Yada schemas
|
|
144
143
|
|
|
145
|
-
function
|
|
144
|
+
function getSchemaForMongoose(schema, options = {}) {
|
|
146
145
|
const fields = getMongooseFields(schema, options);
|
|
147
146
|
return getValidationSchema(fields, options);
|
|
148
147
|
}
|
|
@@ -182,7 +181,7 @@ function getValidationSchema(attributes, options = {}) {
|
|
|
182
181
|
function getObjectSchema(arg, options) {
|
|
183
182
|
if ((0, _utils.isSchemaTypedef)(arg)) {
|
|
184
183
|
return getSchemaForTypedef(arg, options);
|
|
185
|
-
} else if (
|
|
184
|
+
} else if ((0, _utils.isMongooseSchema)(arg)) {
|
|
186
185
|
return getObjectSchema(arg.obj, options);
|
|
187
186
|
} else if (Array.isArray(arg)) {
|
|
188
187
|
return getArraySchema(arg, options);
|
|
@@ -244,7 +243,7 @@ function getSchemaForTypedef(typedef, options = {}) {
|
|
|
244
243
|
} = typedef;
|
|
245
244
|
let schema;
|
|
246
245
|
if ((0, _utils.isMongooseSchema)(type)) {
|
|
247
|
-
schema =
|
|
246
|
+
schema = getObjectSchema(type, options);
|
|
248
247
|
} else if (Array.isArray(type)) {
|
|
249
248
|
schema = getArraySchema(type, options);
|
|
250
249
|
} else if (typeof type === 'object') {
|
package/package.json
CHANGED
package/src/search.js
CHANGED
|
@@ -86,7 +86,7 @@ function searchQuery(Model, options, config) {
|
|
|
86
86
|
|
|
87
87
|
sort = resolveSort(sort, schema);
|
|
88
88
|
|
|
89
|
-
let query = normalizeQuery(rest, schema
|
|
89
|
+
let query = normalizeQuery(rest, schema);
|
|
90
90
|
|
|
91
91
|
if (ids?.length) {
|
|
92
92
|
query = mergeQuery(query, {
|
|
@@ -266,7 +266,7 @@ function buildKeywordQuery(schema, keyword, config) {
|
|
|
266
266
|
|
|
267
267
|
const queries = [
|
|
268
268
|
...getDecomposedQueries(keyword, config),
|
|
269
|
-
...getFieldQueries(keyword, config),
|
|
269
|
+
...getFieldQueries(schema, keyword, config),
|
|
270
270
|
];
|
|
271
271
|
|
|
272
272
|
// Note: Mongo will error on empty $or/$and array.
|
|
@@ -342,7 +342,7 @@ const compileDecomposer = memoize((template) => {
|
|
|
342
342
|
};
|
|
343
343
|
});
|
|
344
344
|
|
|
345
|
-
function getFieldQueries(keyword, config) {
|
|
345
|
+
function getFieldQueries(schema, keyword, config) {
|
|
346
346
|
const { fields } = config;
|
|
347
347
|
|
|
348
348
|
if (!fields) {
|
|
@@ -350,12 +350,26 @@ function getFieldQueries(keyword, config) {
|
|
|
350
350
|
}
|
|
351
351
|
|
|
352
352
|
const queries = fields.map((field) => {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
$
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
353
|
+
if (isNumberField(schema, field)) {
|
|
354
|
+
return {
|
|
355
|
+
$expr: {
|
|
356
|
+
$regexMatch: {
|
|
357
|
+
input: {
|
|
358
|
+
$toString: `$${field}`,
|
|
359
|
+
},
|
|
360
|
+
regex: keyword,
|
|
361
|
+
options: 'i',
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
} else {
|
|
366
|
+
return {
|
|
367
|
+
[field]: {
|
|
368
|
+
$regex: keyword,
|
|
369
|
+
$options: 'i',
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
359
373
|
});
|
|
360
374
|
|
|
361
375
|
if (ObjectId.isValid(keyword)) {
|
package/src/utils.js
CHANGED
|
@@ -79,6 +79,10 @@ export function isSchemaTypedef(arg) {
|
|
|
79
79
|
// "inventory" field. It must traverse into arrays and other mongoose
|
|
80
80
|
// schemas along the way except for the final field.
|
|
81
81
|
export function getField(obj, path) {
|
|
82
|
+
if (obj instanceof mongoose.Schema) {
|
|
83
|
+
// Unwrap mongoose schema if passed.
|
|
84
|
+
obj = obj.obj;
|
|
85
|
+
}
|
|
82
86
|
let field = obj;
|
|
83
87
|
if (typeof path === 'string') {
|
|
84
88
|
path = path.split('.');
|
package/src/validation.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import yd from '@bedrockio/yada';
|
|
2
2
|
import { get, lowerFirst, omit } from 'lodash';
|
|
3
|
-
import mongoose from 'mongoose';
|
|
4
3
|
|
|
5
4
|
import { hasAccess } from './access';
|
|
6
5
|
import { ImplementationError, PermissionsError } from './errors';
|
|
@@ -62,7 +61,7 @@ export function addValidators(schemas) {
|
|
|
62
61
|
|
|
63
62
|
export function applyValidation(schema, definition) {
|
|
64
63
|
schema.static('getCreateValidation', function getCreateValidation(options) {
|
|
65
|
-
return
|
|
64
|
+
return getSchemaForMongoose(schema, {
|
|
66
65
|
model: this,
|
|
67
66
|
stripEmpty: true,
|
|
68
67
|
applyUnique: true,
|
|
@@ -77,7 +76,7 @@ export function applyValidation(schema, definition) {
|
|
|
77
76
|
});
|
|
78
77
|
|
|
79
78
|
schema.static('getUpdateValidation', function getUpdateValidation(options) {
|
|
80
|
-
return
|
|
79
|
+
return getSchemaForMongoose(schema, {
|
|
81
80
|
model: this,
|
|
82
81
|
allowNull: true,
|
|
83
82
|
applyUnique: true,
|
|
@@ -99,7 +98,7 @@ export function applyValidation(schema, definition) {
|
|
|
99
98
|
function getSearchValidation(options = {}) {
|
|
100
99
|
const { allowExport, defaults, formats, ...rest } = options;
|
|
101
100
|
|
|
102
|
-
let validation =
|
|
101
|
+
let validation = getSchemaForMongoose(schema, {
|
|
103
102
|
model: this,
|
|
104
103
|
allowNull: true,
|
|
105
104
|
stripEmpty: true,
|
|
@@ -146,7 +145,7 @@ export function applyValidation(schema, definition) {
|
|
|
146
145
|
});
|
|
147
146
|
|
|
148
147
|
schema.static('getBaseSchema', function getBaseSchema() {
|
|
149
|
-
return
|
|
148
|
+
return getSchemaForMongoose(schema, {
|
|
150
149
|
model: this,
|
|
151
150
|
stripDeleted: true,
|
|
152
151
|
requireReadAccess: true,
|
|
@@ -156,7 +155,7 @@ export function applyValidation(schema, definition) {
|
|
|
156
155
|
|
|
157
156
|
// Yada schemas
|
|
158
157
|
|
|
159
|
-
function
|
|
158
|
+
function getSchemaForMongoose(schema, options = {}) {
|
|
160
159
|
const fields = getMongooseFields(schema, options);
|
|
161
160
|
return getValidationSchema(fields, options);
|
|
162
161
|
}
|
|
@@ -196,7 +195,7 @@ export function getValidationSchema(attributes, options = {}) {
|
|
|
196
195
|
function getObjectSchema(arg, options) {
|
|
197
196
|
if (isSchemaTypedef(arg)) {
|
|
198
197
|
return getSchemaForTypedef(arg, options);
|
|
199
|
-
} else if (arg
|
|
198
|
+
} else if (isMongooseSchema(arg)) {
|
|
200
199
|
return getObjectSchema(arg.obj, options);
|
|
201
200
|
} else if (Array.isArray(arg)) {
|
|
202
201
|
return getArraySchema(arg, options);
|
|
@@ -261,7 +260,7 @@ function getSchemaForTypedef(typedef, options = {}) {
|
|
|
261
260
|
let schema;
|
|
262
261
|
|
|
263
262
|
if (isMongooseSchema(type)) {
|
|
264
|
-
schema =
|
|
263
|
+
schema = getObjectSchema(type, options);
|
|
265
264
|
} else if (Array.isArray(type)) {
|
|
266
265
|
schema = getArraySchema(type, options);
|
|
267
266
|
} else if (typeof type === 'object') {
|
package/types/schema.d.ts
CHANGED
|
@@ -28,9 +28,7 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
28
28
|
id?: boolean;
|
|
29
29
|
_id?: boolean;
|
|
30
30
|
minimize?: boolean;
|
|
31
|
-
optimisticConcurrency?: boolean
|
|
32
|
-
exclude: string[];
|
|
33
|
-
};
|
|
31
|
+
optimisticConcurrency?: boolean;
|
|
34
32
|
pluginTags?: string[];
|
|
35
33
|
read?: string;
|
|
36
34
|
readConcern?: {
|
|
@@ -49,16 +47,16 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
49
47
|
getters: boolean;
|
|
50
48
|
versionKey: boolean;
|
|
51
49
|
transform: (doc: any, ret: any, options: any) => void;
|
|
52
|
-
} | mongoose.ToObjectOptions<any
|
|
50
|
+
} | mongoose.ToObjectOptions<any>;
|
|
53
51
|
toObject: {
|
|
54
52
|
getters: boolean;
|
|
55
53
|
versionKey: boolean;
|
|
56
54
|
transform: (doc: any, ret: any, options: any) => void;
|
|
57
|
-
} | mongoose.ToObjectOptions<any
|
|
55
|
+
} | mongoose.ToObjectOptions<any>;
|
|
58
56
|
typeKey?: string;
|
|
59
57
|
validateBeforeSave?: boolean;
|
|
60
58
|
validateModifiedOnly?: boolean;
|
|
61
|
-
versionKey?: string |
|
|
59
|
+
versionKey?: string | boolean;
|
|
62
60
|
selectPopulatedPaths?: boolean;
|
|
63
61
|
skipVersioning?: {
|
|
64
62
|
[key: string]: boolean;
|
|
@@ -66,13 +64,12 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
66
64
|
storeSubdocValidationError?: boolean;
|
|
67
65
|
timestamps: boolean | mongoose.SchemaTimestampsConfig;
|
|
68
66
|
suppressReservedKeysWarning?: boolean;
|
|
69
|
-
statics?: mongoose.AddThisParameter<any, mongoose.Model<any,
|
|
67
|
+
statics?: mongoose.AddThisParameter<any, mongoose.Model<any, {}, {}, {}, any, any>>;
|
|
70
68
|
methods?: mongoose.AddThisParameter<any, any> & mongoose.AnyObject;
|
|
71
69
|
query?: any;
|
|
72
70
|
castNonArrays?: boolean;
|
|
73
71
|
virtuals?: mongoose.SchemaOptionsVirtualsPropertyType<any, any, any>;
|
|
74
72
|
overwriteModels?: boolean;
|
|
75
|
-
encryptionType?: "csfle" | "queryableEncryption";
|
|
76
73
|
}, any, any>;
|
|
77
74
|
export function normalizeAttributes(arg: any, path?: any[]): any;
|
|
78
75
|
import mongoose from 'mongoose';
|
package/types/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAuBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAuBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;YA6CQ,CAAA;WACvC,CAAD;mBAAqB,CAAC;;;;;;;;;;;;;;;;;;;;;SAiHX,CAAC;gBAA4B,CAAA;SAAW,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aApHpD;AAED,iEAsBC;qBA9FoB,UAAU"}
|
package/types/search.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAuBA,gEAaC;AAED;;;;;;;;;;;;kBA8GU,
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAuBA,gEAaC;AAED;;;;;;;;;;;;kBA8GU,CAAH;oBAEG,CAAR;qBAEU,CAAF;sBAEF,CAAJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;eArDF,CAAC;;;;;;;;;;;;;;;;;EAtCF;AAED;;;;;;;;;;;;;;;;;mBAtBI,CAAA;;;;;;;;;;;;qBAuBC,CAAC;sBAA4B,CAAC;sBACvB,CAAC;wBAA8B,CAAC;wBAEnC,CAAC;;;4BA4BN,CAAD;kCAAyC,CAAC;wBACtC,CAAC;wBAA+B,CAAC;wCAKlC,CAAA;2BAAkC,CAAC;kCACP,CAAC;2BAIhB,CAAC;qBAA4B,CAAC;;;uBAuB/C,CAAA;6BAEA,CAAA;8BAEK,CAAC;6BAAoC,CAAC;0BAClB,CAAC;6BAIjB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAvCV,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA1DC,CAAA;;;;;;;;;;;;qBAuBC,CAAC;sBAA4B,CAAC;sBACvB,CAAC;wBAA8B,CAAC;wBAEnC,CAAC;;;4BA4BN,CAAD;kCAAyC,CAAC;wBACtC,CAAC;wBAA+B,CAAC;wCAKlC,CAAA;2BAAkC,CAAC;kCACP,CAAC;2BAIhB,CAAC;qBAA4B,CAAC;;;uBAuB/C,CAAA;6BAEA,CAAA;8BAEK,CAAC;6BAAoC,CAAC;0BAClB,CAAC;6BAIjB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAvCV,CAAC;;;;;;;;;;;;;;;;;;;;EAvBF"}
|
package/types/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,gHAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,gHAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,mDAgBC;AAKD;;;;EAoBC;AAKD,wDAEC;qBAhIoB,UAAU"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAyDA,kDAEC;AAED,oEA4FC;AAsBD,wEAiBC;AA8SD;;;EAEC;AAED;;;EAOC"}
|