@bedrockio/model 0.1.24 → 0.1.26
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 → UPDATE.md} +2 -1
- package/dist/cjs/schema.js +12 -11
- package/dist/cjs/search.js +1 -1
- package/package.json +1 -1
- package/src/schema.js +22 -22
- package/src/search.js +2 -2
- package/types/schema.d.ts.map +1 -1
|
@@ -16,7 +16,8 @@ yarn add @bedrockio/logger
|
|
|
16
16
|
yarn add @bedrockio/model
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
Replace all references to `src/utils/schema.js` and `src/utils/validation.js` to
|
|
19
|
+
Replace all references to `src/utils/schema.js` and `src/utils/validation.js` to
|
|
20
|
+
`@bedrockio/model`.
|
|
20
21
|
|
|
21
22
|
In the model definitions:
|
|
22
23
|
|
package/dist/cjs/schema.js
CHANGED
|
@@ -27,7 +27,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
27
27
|
* @returns mongoose.Schema
|
|
28
28
|
*/
|
|
29
29
|
function createSchema(definition, options = {}) {
|
|
30
|
-
const schema = new _mongoose.default.Schema(attributesToMongoose(
|
|
30
|
+
const schema = new _mongoose.default.Schema(attributesToMongoose({
|
|
31
31
|
...definition.attributes,
|
|
32
32
|
// Although timestamps are being set below, we still need to add
|
|
33
33
|
// them to the schema so that validation can be generated for them,
|
|
@@ -39,7 +39,7 @@ function createSchema(definition, options = {}) {
|
|
|
39
39
|
type: 'Boolean',
|
|
40
40
|
default: false
|
|
41
41
|
}
|
|
42
|
-
})
|
|
42
|
+
}), {
|
|
43
43
|
timestamps: true,
|
|
44
44
|
toJSON: _serialization.serializeOptions,
|
|
45
45
|
toObject: _serialization.serializeOptions,
|
|
@@ -65,9 +65,8 @@ function normalizeAttributes(arg, path = []) {
|
|
|
65
65
|
type: arg
|
|
66
66
|
}, path);
|
|
67
67
|
} else if (Array.isArray(arg)) {
|
|
68
|
-
const type = normalizeArrayAttributes(arg, path);
|
|
69
68
|
return normalizeSchemaTypedef({
|
|
70
|
-
type
|
|
69
|
+
type: arg
|
|
71
70
|
}, path);
|
|
72
71
|
} else if (typeof arg === 'object') {
|
|
73
72
|
assertRefs(arg, path);
|
|
@@ -105,6 +104,7 @@ function attributesToMongoose(attributes) {
|
|
|
105
104
|
} else if (Array.isArray(attributes)) {
|
|
106
105
|
return attributes.map(attributesToMongoose);
|
|
107
106
|
}
|
|
107
|
+
attributes = normalizeAttributes(attributes);
|
|
108
108
|
let definition = {};
|
|
109
109
|
const isTypedef = (0, _utils.isSchemaTypedef)(attributes);
|
|
110
110
|
for (let [key, val] of Object.entries(attributes)) {
|
|
@@ -211,28 +211,29 @@ function isExtendedSyntax(typedef) {
|
|
|
211
211
|
} = typedef;
|
|
212
212
|
return attributes && (type === 'Object' || type === 'Array');
|
|
213
213
|
}
|
|
214
|
-
function isScopeExtension(
|
|
215
|
-
return (0, _utils.isSchemaTypedef)(
|
|
214
|
+
function isScopeExtension(arg) {
|
|
215
|
+
return (0, _utils.isSchemaTypedef)(arg) && arg.type === 'Scope';
|
|
216
216
|
}
|
|
217
|
-
function applyScopeExtension(
|
|
217
|
+
function applyScopeExtension(typedef, definition) {
|
|
218
218
|
const {
|
|
219
219
|
type,
|
|
220
220
|
attributes,
|
|
221
221
|
...options
|
|
222
|
-
} =
|
|
222
|
+
} = typedef;
|
|
223
223
|
for (let [key, val] of Object.entries(normalizeAttributes(attributes))) {
|
|
224
224
|
if ((0, _utils.isSchemaTypedef)(val)) {
|
|
225
|
-
|
|
225
|
+
val = {
|
|
226
226
|
...val,
|
|
227
227
|
...options
|
|
228
228
|
};
|
|
229
229
|
} else {
|
|
230
|
-
|
|
230
|
+
val = attributesToMongoose({
|
|
231
231
|
type: 'Object',
|
|
232
232
|
attributes: val,
|
|
233
233
|
...options
|
|
234
|
-
};
|
|
234
|
+
});
|
|
235
235
|
}
|
|
236
|
+
definition[key] = val;
|
|
236
237
|
}
|
|
237
238
|
}
|
|
238
239
|
|
package/dist/cjs/search.js
CHANGED
|
@@ -186,7 +186,7 @@ function buildKeywordQuery(schema, keyword, fields) {
|
|
|
186
186
|
}
|
|
187
187
|
function buildRegexQuery(keyword, fields) {
|
|
188
188
|
return fields.map(field => {
|
|
189
|
-
const regexKeyword =
|
|
189
|
+
const regexKeyword = (0, _lodash.escapeRegExp)(keyword);
|
|
190
190
|
return {
|
|
191
191
|
[field]: {
|
|
192
192
|
$regex: `${regexKeyword}`,
|
package/package.json
CHANGED
package/src/schema.js
CHANGED
|
@@ -28,19 +28,17 @@ import {
|
|
|
28
28
|
*/
|
|
29
29
|
export function createSchema(definition, options = {}) {
|
|
30
30
|
const schema = new mongoose.Schema(
|
|
31
|
-
attributesToMongoose(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
})
|
|
43
|
-
),
|
|
31
|
+
attributesToMongoose({
|
|
32
|
+
...definition.attributes,
|
|
33
|
+
|
|
34
|
+
// Although timestamps are being set below, we still need to add
|
|
35
|
+
// them to the schema so that validation can be generated for them,
|
|
36
|
+
// namely in getSearchValidation.
|
|
37
|
+
createdAt: 'Date',
|
|
38
|
+
updatedAt: 'Date',
|
|
39
|
+
deletedAt: 'Date',
|
|
40
|
+
deleted: { type: 'Boolean', default: false },
|
|
41
|
+
}),
|
|
44
42
|
{
|
|
45
43
|
timestamps: true,
|
|
46
44
|
toJSON: serializeOptions,
|
|
@@ -69,8 +67,7 @@ export function normalizeAttributes(arg, path = []) {
|
|
|
69
67
|
} else if (typeof arg === 'string') {
|
|
70
68
|
return normalizeSchemaTypedef({ type: arg }, path);
|
|
71
69
|
} else if (Array.isArray(arg)) {
|
|
72
|
-
|
|
73
|
-
return normalizeSchemaTypedef({ type }, path);
|
|
70
|
+
return normalizeSchemaTypedef({ type: arg }, path);
|
|
74
71
|
} else if (typeof arg === 'object') {
|
|
75
72
|
assertRefs(arg, path);
|
|
76
73
|
|
|
@@ -113,6 +110,8 @@ function attributesToMongoose(attributes) {
|
|
|
113
110
|
return attributes.map(attributesToMongoose);
|
|
114
111
|
}
|
|
115
112
|
|
|
113
|
+
attributes = normalizeAttributes(attributes);
|
|
114
|
+
|
|
116
115
|
let definition = {};
|
|
117
116
|
|
|
118
117
|
const isTypedef = isSchemaTypedef(attributes);
|
|
@@ -223,25 +222,26 @@ function isExtendedSyntax(typedef) {
|
|
|
223
222
|
return attributes && (type === 'Object' || type === 'Array');
|
|
224
223
|
}
|
|
225
224
|
|
|
226
|
-
function isScopeExtension(
|
|
227
|
-
return isSchemaTypedef(
|
|
225
|
+
function isScopeExtension(arg) {
|
|
226
|
+
return isSchemaTypedef(arg) && arg.type === 'Scope';
|
|
228
227
|
}
|
|
229
228
|
|
|
230
|
-
function applyScopeExtension(
|
|
231
|
-
const { type, attributes, ...options } =
|
|
229
|
+
function applyScopeExtension(typedef, definition) {
|
|
230
|
+
const { type, attributes, ...options } = typedef;
|
|
232
231
|
for (let [key, val] of Object.entries(normalizeAttributes(attributes))) {
|
|
233
232
|
if (isSchemaTypedef(val)) {
|
|
234
|
-
|
|
233
|
+
val = {
|
|
235
234
|
...val,
|
|
236
235
|
...options,
|
|
237
236
|
};
|
|
238
237
|
} else {
|
|
239
|
-
|
|
238
|
+
val = attributesToMongoose({
|
|
240
239
|
type: 'Object',
|
|
241
240
|
attributes: val,
|
|
242
241
|
...options,
|
|
243
|
-
};
|
|
242
|
+
});
|
|
244
243
|
}
|
|
244
|
+
definition[key] = val;
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
247
|
|
package/src/search.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import yd from '@bedrockio/yada';
|
|
2
2
|
import logger from '@bedrockio/logger';
|
|
3
3
|
import mongoose from 'mongoose';
|
|
4
|
-
import { pick, isEmpty, isPlainObject } from 'lodash';
|
|
4
|
+
import { pick, isEmpty, escapeRegExp, isPlainObject } from 'lodash';
|
|
5
5
|
|
|
6
6
|
import { isDateField, isNumberField, getField } from './utils';
|
|
7
7
|
import { SEARCH_DEFAULTS } from './const';
|
|
@@ -196,7 +196,7 @@ function buildKeywordQuery(schema, keyword, fields) {
|
|
|
196
196
|
|
|
197
197
|
function buildRegexQuery(keyword, fields) {
|
|
198
198
|
return fields.map((field) => {
|
|
199
|
-
const regexKeyword = keyword
|
|
199
|
+
const regexKeyword = escapeRegExp(keyword);
|
|
200
200
|
return {
|
|
201
201
|
[field]: {
|
|
202
202
|
$regex: `${regexKeyword}`,
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAkChC;AAED,iEAsBC"}
|