@bedrockio/model 0.19.1 → 0.19.3
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/delete-hooks.js +16 -10
- package/dist/cjs/search.js +11 -4
- package/package.json +1 -1
- package/src/delete-hooks.js +20 -12
- package/src/search.js +10 -4
- package/types/delete-hooks.d.ts.map +1 -1
- package/types/search.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/delete-hooks.js
CHANGED
|
@@ -11,7 +11,8 @@ var _errors = require("./errors");
|
|
|
11
11
|
var _utils = require("./utils");
|
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const {
|
|
14
|
-
ObjectId: SchemaObjectId
|
|
14
|
+
ObjectId: SchemaObjectId,
|
|
15
|
+
DocumentArray: SchemaDocumentArray
|
|
15
16
|
} = _mongoose.default.Schema.Types;
|
|
16
17
|
function addDeletedFields(definition) {
|
|
17
18
|
let {
|
|
@@ -223,7 +224,7 @@ function assertModelNames(arr = []) {
|
|
|
223
224
|
function getAllReferences(doc) {
|
|
224
225
|
const targetName = doc.constructor.modelName;
|
|
225
226
|
return Object.values(_mongoose.default.models).map(model => {
|
|
226
|
-
const paths = getModelReferences(model, targetName);
|
|
227
|
+
const paths = getModelReferences(model.schema, targetName);
|
|
227
228
|
return {
|
|
228
229
|
model,
|
|
229
230
|
paths
|
|
@@ -234,10 +235,13 @@ function getAllReferences(doc) {
|
|
|
234
235
|
return paths.length > 0;
|
|
235
236
|
});
|
|
236
237
|
}
|
|
237
|
-
function getModelReferences(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (
|
|
238
|
+
function getModelReferences(schema, targetName, path = []) {
|
|
239
|
+
let references = [];
|
|
240
|
+
schema.eachPath((name, schemaType) => {
|
|
241
|
+
if (name.startsWith('_') || name === 'deletedRefs') {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (schemaType instanceof SchemaObjectId) {
|
|
241
245
|
const {
|
|
242
246
|
ref,
|
|
243
247
|
refPath
|
|
@@ -246,16 +250,18 @@ function getModelReferences(model, targetName) {
|
|
|
246
250
|
if (ref) {
|
|
247
251
|
refs = [ref];
|
|
248
252
|
} else if (refPath) {
|
|
249
|
-
refs =
|
|
253
|
+
refs = schema.path(refPath).options.enum;
|
|
250
254
|
} else {
|
|
251
|
-
throw new Error(`Cannot derive refs for ${
|
|
255
|
+
throw new Error(`Cannot derive refs for ${targetName}#${name}.`);
|
|
252
256
|
}
|
|
253
257
|
if (refs.includes(targetName)) {
|
|
254
|
-
|
|
258
|
+
references.push([...path, name].join('.'));
|
|
255
259
|
}
|
|
260
|
+
} else if (schemaType instanceof SchemaDocumentArray) {
|
|
261
|
+
references = [...references, ...getModelReferences(schemaType.schema, targetName, [name])];
|
|
256
262
|
}
|
|
257
263
|
});
|
|
258
|
-
return
|
|
264
|
+
return references;
|
|
259
265
|
}
|
|
260
266
|
|
|
261
267
|
// Delete
|
package/dist/cjs/search.js
CHANGED
|
@@ -123,7 +123,9 @@ function searchPipeline(Model, pipeline, options) {
|
|
|
123
123
|
limit,
|
|
124
124
|
sort
|
|
125
125
|
} = options;
|
|
126
|
-
sort = resolveSort(sort, schema
|
|
126
|
+
sort = resolveSort(sort, schema, {
|
|
127
|
+
allowUnknown: true
|
|
128
|
+
});
|
|
127
129
|
if (_env.debug) {
|
|
128
130
|
_logger.default.info(`Search pipeline for ${Model.modelName}:\n`, JSON.stringify(pipeline, null, 2));
|
|
129
131
|
}
|
|
@@ -168,7 +170,10 @@ function validateDefinition(definition) {
|
|
|
168
170
|
throw new Error('Invalid model definition.');
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
|
-
function resolveSort(sort, schema) {
|
|
173
|
+
function resolveSort(sort, schema, options = {}) {
|
|
174
|
+
const {
|
|
175
|
+
allowUnknown = false
|
|
176
|
+
} = options;
|
|
172
177
|
if (!sort) {
|
|
173
178
|
return {
|
|
174
179
|
_id: 1
|
|
@@ -186,8 +191,10 @@ function resolveSort(sort, schema) {
|
|
|
186
191
|
if (name) {
|
|
187
192
|
throw new Error('Sort property "name" is not allowed. Use "field" instead.');
|
|
188
193
|
}
|
|
189
|
-
if (!
|
|
190
|
-
|
|
194
|
+
if (!schema.path(field)) {
|
|
195
|
+
if (!allowUnknown && !field.startsWith('$')) {
|
|
196
|
+
throw new Error(`Unknown sort field "${field}".`);
|
|
197
|
+
}
|
|
191
198
|
}
|
|
192
199
|
result[field] = order === 'desc' ? -1 : 1;
|
|
193
200
|
}
|
package/package.json
CHANGED
package/src/delete-hooks.js
CHANGED
|
@@ -4,7 +4,8 @@ import mongoose from 'mongoose';
|
|
|
4
4
|
import { ReferenceError } from './errors';
|
|
5
5
|
import { getInnerField } from './utils';
|
|
6
6
|
|
|
7
|
-
const { ObjectId: SchemaObjectId } =
|
|
7
|
+
const { ObjectId: SchemaObjectId, DocumentArray: SchemaDocumentArray } =
|
|
8
|
+
mongoose.Schema.Types;
|
|
8
9
|
|
|
9
10
|
export function addDeletedFields(definition) {
|
|
10
11
|
let { onDelete: deleteHooks } = definition;
|
|
@@ -215,7 +216,7 @@ function getAllReferences(doc) {
|
|
|
215
216
|
const targetName = doc.constructor.modelName;
|
|
216
217
|
return Object.values(mongoose.models)
|
|
217
218
|
.map((model) => {
|
|
218
|
-
const paths = getModelReferences(model, targetName);
|
|
219
|
+
const paths = getModelReferences(model.schema, targetName);
|
|
219
220
|
return { model, paths };
|
|
220
221
|
})
|
|
221
222
|
.filter(({ paths }) => {
|
|
@@ -223,27 +224,34 @@ function getAllReferences(doc) {
|
|
|
223
224
|
});
|
|
224
225
|
}
|
|
225
226
|
|
|
226
|
-
function getModelReferences(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (
|
|
227
|
+
function getModelReferences(schema, targetName, path = []) {
|
|
228
|
+
let references = [];
|
|
229
|
+
schema.eachPath((name, schemaType) => {
|
|
230
|
+
if (name.startsWith('_') || name === 'deletedRefs') {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (schemaType instanceof SchemaObjectId) {
|
|
230
235
|
const { ref, refPath } = schemaType.options;
|
|
231
236
|
let refs;
|
|
232
237
|
if (ref) {
|
|
233
238
|
refs = [ref];
|
|
234
239
|
} else if (refPath) {
|
|
235
|
-
refs =
|
|
240
|
+
refs = schema.path(refPath).options.enum;
|
|
236
241
|
} else {
|
|
237
|
-
throw new Error(
|
|
238
|
-
`Cannot derive refs for ${model.modelName}#${schemaPath}.`,
|
|
239
|
-
);
|
|
242
|
+
throw new Error(`Cannot derive refs for ${targetName}#${name}.`);
|
|
240
243
|
}
|
|
241
244
|
if (refs.includes(targetName)) {
|
|
242
|
-
|
|
245
|
+
references.push([...path, name].join('.'));
|
|
243
246
|
}
|
|
247
|
+
} else if (schemaType instanceof SchemaDocumentArray) {
|
|
248
|
+
references = [
|
|
249
|
+
...references,
|
|
250
|
+
...getModelReferences(schemaType.schema, targetName, [name]),
|
|
251
|
+
];
|
|
244
252
|
}
|
|
245
253
|
});
|
|
246
|
-
return
|
|
254
|
+
return references;
|
|
247
255
|
}
|
|
248
256
|
|
|
249
257
|
// Delete
|
package/src/search.js
CHANGED
|
@@ -129,7 +129,9 @@ function searchPipeline(Model, pipeline, options) {
|
|
|
129
129
|
options = mergeOptions(SEARCH_DEFAULTS, options);
|
|
130
130
|
|
|
131
131
|
let { skip, limit, sort } = options;
|
|
132
|
-
sort = resolveSort(sort, schema
|
|
132
|
+
sort = resolveSort(sort, schema, {
|
|
133
|
+
allowUnknown: true,
|
|
134
|
+
});
|
|
133
135
|
|
|
134
136
|
if (debug) {
|
|
135
137
|
logger.info(
|
|
@@ -199,7 +201,9 @@ function validateDefinition(definition) {
|
|
|
199
201
|
}
|
|
200
202
|
}
|
|
201
203
|
|
|
202
|
-
function resolveSort(sort, schema) {
|
|
204
|
+
function resolveSort(sort, schema, options = {}) {
|
|
205
|
+
const { allowUnknown = false } = options;
|
|
206
|
+
|
|
203
207
|
if (!sort) {
|
|
204
208
|
return { _id: 1 };
|
|
205
209
|
}
|
|
@@ -216,8 +220,10 @@ function resolveSort(sort, schema) {
|
|
|
216
220
|
'Sort property "name" is not allowed. Use "field" instead.',
|
|
217
221
|
);
|
|
218
222
|
}
|
|
219
|
-
if (!
|
|
220
|
-
|
|
223
|
+
if (!schema.path(field)) {
|
|
224
|
+
if (!allowUnknown && !field.startsWith('$')) {
|
|
225
|
+
throw new Error(`Unknown sort field "${field}".`);
|
|
226
|
+
}
|
|
221
227
|
}
|
|
222
228
|
|
|
223
229
|
result[field] = order === 'desc' ? -1 : 1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete-hooks.d.ts","sourceRoot":"","sources":["../src/delete-hooks.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"delete-hooks.d.ts","sourceRoot":"","sources":["../src/delete-hooks.js"],"names":[],"mappings":"AASA,wDAgBC;AAED,qEAqCC"}
|
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;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAuBA,gEAaC;AAED;;;;;;;;;;;;kBAoHY,CAAL;oBACI,CAAC;qBAGF,CAAL;sBACqB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eA5BQ,CAAA;;;;;;;;;;;;;;EApElC;AAED;;;;;;;;;;;;;;;;mBA1BmD,CAAC;;;;;;;;;;;;qBAuBlD,CAAC;sBAGmB,CAAC;sBACrB,CAAC;wBAA8B,CAAC;wBACrB,CAAC;;;4BAwBc,CAAC;kCAK3B,CAAD;wBACc,CAAC;wBAA+B,CAAC;wCACtB,CAAC;2BAGhB,CAAC;kCAES,CAAC;2BACd,CAAC;qBACL,CAAJ;;;uBAuBgC,CAAC;6BAEjB,CAAC;8BACF,CAAC;6BACD,CAAC;0BAIZ,CAAA;6BACmB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBANW,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA5FgB,CAAC;;;;;;;;;;;;qBAuBlD,CAAC;sBAGmB,CAAC;sBACrB,CAAC;wBAA8B,CAAC;wBACrB,CAAC;;;4BAwBc,CAAC;kCAK3B,CAAD;wBACc,CAAC;wBAA+B,CAAC;wCACtB,CAAC;2BAGhB,CAAC;kCAES,CAAC;2BACd,CAAC;qBACL,CAAJ;;;uBAuBgC,CAAC;6BAEjB,CAAC;8BACF,CAAC;6BACD,CAAC;0BAIZ,CAAA;6BACmB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBANW,CAAA;;;;;;;;;;;;;;;;;EArDlC"}
|