@bedrockio/model 0.1.32 → 0.2.0
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/README.md +323 -109
- package/dist/cjs/const.js +2 -4
- package/dist/cjs/delete-hooks.js +351 -0
- package/dist/cjs/disallowed.js +0 -14
- package/dist/cjs/env.js +1 -2
- package/dist/cjs/include.js +11 -3
- package/dist/cjs/query.js +21 -0
- package/dist/cjs/schema.js +6 -3
- package/dist/cjs/search.js +12 -23
- package/dist/cjs/serialization.js +5 -3
- package/dist/cjs/soft-delete.js +226 -51
- package/dist/cjs/validation.js +1 -2
- package/package.json +5 -5
- package/src/delete-hooks.js +343 -0
- package/src/disallowed.js +0 -29
- package/src/include.js +10 -1
- package/src/query.js +15 -0
- package/src/schema.js +9 -4
- package/src/search.js +15 -27
- package/src/serialization.js +5 -1
- package/src/soft-delete.js +248 -55
- package/types/delete-hooks.d.ts +2 -0
- package/types/delete-hooks.d.ts.map +1 -0
- package/types/disallowed.d.ts.map +1 -1
- package/types/load.d.ts +67 -1
- package/types/load.d.ts.map +1 -1
- package/types/query.d.ts +2 -0
- package/types/query.d.ts.map +1 -0
- package/types/schema.d.ts +15 -9
- package/types/schema.d.ts.map +1 -1
- package/types/search.d.ts.map +1 -1
- package/types/serialization.d.ts.map +1 -1
- package/types/soft-delete.d.ts.map +1 -1
- package/types/testing.d.ts +1 -1
- package/types/testing.d.ts.map +1 -1
- package/dist/cjs/references.js +0 -104
- package/src/references.js +0 -101
package/dist/cjs/references.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.applyReferences = applyReferences;
|
|
7
|
-
var _mongoose = _interopRequireDefault(require("mongoose"));
|
|
8
|
-
var _errors = require("./errors");
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
const {
|
|
11
|
-
ObjectId: SchemaObjectId
|
|
12
|
-
} = _mongoose.default.Schema.Types;
|
|
13
|
-
function applyReferences(schema) {
|
|
14
|
-
schema.method('assertNoReferences', async function assertNoReferences(options = {}) {
|
|
15
|
-
const {
|
|
16
|
-
except = []
|
|
17
|
-
} = options;
|
|
18
|
-
const {
|
|
19
|
-
modelName
|
|
20
|
-
} = this.constructor;
|
|
21
|
-
assertExceptions(except);
|
|
22
|
-
const references = getAllReferences(modelName);
|
|
23
|
-
const results = [];
|
|
24
|
-
for (let {
|
|
25
|
-
model,
|
|
26
|
-
paths
|
|
27
|
-
} of references) {
|
|
28
|
-
const isAllowed = except.some(e => {
|
|
29
|
-
return e === model || e === model.modelName;
|
|
30
|
-
});
|
|
31
|
-
if (isAllowed) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
const query = {
|
|
35
|
-
$or: paths.map(path => {
|
|
36
|
-
return {
|
|
37
|
-
[path]: this.id
|
|
38
|
-
};
|
|
39
|
-
})
|
|
40
|
-
};
|
|
41
|
-
const docs = await model.find(query, {
|
|
42
|
-
_id: 1
|
|
43
|
-
}, {
|
|
44
|
-
lean: true
|
|
45
|
-
});
|
|
46
|
-
if (docs.length > 0) {
|
|
47
|
-
const ids = docs.map(doc => {
|
|
48
|
-
return String(doc._id);
|
|
49
|
-
});
|
|
50
|
-
results.push({
|
|
51
|
-
ids,
|
|
52
|
-
model,
|
|
53
|
-
count: ids.length
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (results.length) {
|
|
58
|
-
throw new _errors.ReferenceError('Refusing to delete.', results);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
function assertExceptions(except) {
|
|
63
|
-
for (let val of except) {
|
|
64
|
-
if (typeof val === 'string' && !_mongoose.default.models[val]) {
|
|
65
|
-
throw new Error(`Unknown model "${val}".`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function getAllReferences(targetName) {
|
|
70
|
-
return Object.values(_mongoose.default.models).map(model => {
|
|
71
|
-
const paths = getModelReferences(model, targetName);
|
|
72
|
-
return {
|
|
73
|
-
model,
|
|
74
|
-
paths
|
|
75
|
-
};
|
|
76
|
-
}).filter(({
|
|
77
|
-
paths
|
|
78
|
-
}) => {
|
|
79
|
-
return paths.length > 0;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function getModelReferences(model, targetName) {
|
|
83
|
-
const paths = [];
|
|
84
|
-
model.schema.eachPath((schemaPath, schemaType) => {
|
|
85
|
-
if (schemaType instanceof SchemaObjectId && schemaPath[0] !== '_') {
|
|
86
|
-
const {
|
|
87
|
-
ref,
|
|
88
|
-
refPath
|
|
89
|
-
} = schemaType.options;
|
|
90
|
-
let refs;
|
|
91
|
-
if (ref) {
|
|
92
|
-
refs = [ref];
|
|
93
|
-
} else if (refPath) {
|
|
94
|
-
refs = model.schema.path(refPath).options.enum;
|
|
95
|
-
} else {
|
|
96
|
-
throw new Error(`Cannot derive refs for ${model.modelName}#${schemaPath}.`);
|
|
97
|
-
}
|
|
98
|
-
if (refs.includes(targetName)) {
|
|
99
|
-
paths.push(schemaPath);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
return paths;
|
|
104
|
-
}
|
package/src/references.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
|
-
|
|
3
|
-
import { ReferenceError } from './errors';
|
|
4
|
-
|
|
5
|
-
const { ObjectId: SchemaObjectId } = mongoose.Schema.Types;
|
|
6
|
-
|
|
7
|
-
export function applyReferences(schema) {
|
|
8
|
-
schema.method(
|
|
9
|
-
'assertNoReferences',
|
|
10
|
-
async function assertNoReferences(options = {}) {
|
|
11
|
-
const { except = [] } = options;
|
|
12
|
-
const { modelName } = this.constructor;
|
|
13
|
-
|
|
14
|
-
assertExceptions(except);
|
|
15
|
-
|
|
16
|
-
const references = getAllReferences(modelName);
|
|
17
|
-
const results = [];
|
|
18
|
-
|
|
19
|
-
for (let { model, paths } of references) {
|
|
20
|
-
const isAllowed = except.some((e) => {
|
|
21
|
-
return e === model || e === model.modelName;
|
|
22
|
-
});
|
|
23
|
-
if (isAllowed) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const query = {
|
|
28
|
-
$or: paths.map((path) => {
|
|
29
|
-
return { [path]: this.id };
|
|
30
|
-
}),
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const docs = await model.find(
|
|
34
|
-
query,
|
|
35
|
-
{
|
|
36
|
-
_id: 1,
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
lean: true,
|
|
40
|
-
}
|
|
41
|
-
);
|
|
42
|
-
if (docs.length > 0) {
|
|
43
|
-
const ids = docs.map((doc) => {
|
|
44
|
-
return String(doc._id);
|
|
45
|
-
});
|
|
46
|
-
results.push({
|
|
47
|
-
ids,
|
|
48
|
-
model,
|
|
49
|
-
count: ids.length,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (results.length) {
|
|
55
|
-
throw new ReferenceError('Refusing to delete.', results);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function assertExceptions(except) {
|
|
62
|
-
for (let val of except) {
|
|
63
|
-
if (typeof val === 'string' && !mongoose.models[val]) {
|
|
64
|
-
throw new Error(`Unknown model "${val}".`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getAllReferences(targetName) {
|
|
70
|
-
return Object.values(mongoose.models)
|
|
71
|
-
.map((model) => {
|
|
72
|
-
const paths = getModelReferences(model, targetName);
|
|
73
|
-
return { model, paths };
|
|
74
|
-
})
|
|
75
|
-
.filter(({ paths }) => {
|
|
76
|
-
return paths.length > 0;
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function getModelReferences(model, targetName) {
|
|
81
|
-
const paths = [];
|
|
82
|
-
model.schema.eachPath((schemaPath, schemaType) => {
|
|
83
|
-
if (schemaType instanceof SchemaObjectId && schemaPath[0] !== '_') {
|
|
84
|
-
const { ref, refPath } = schemaType.options;
|
|
85
|
-
let refs;
|
|
86
|
-
if (ref) {
|
|
87
|
-
refs = [ref];
|
|
88
|
-
} else if (refPath) {
|
|
89
|
-
refs = model.schema.path(refPath).options.enum;
|
|
90
|
-
} else {
|
|
91
|
-
throw new Error(
|
|
92
|
-
`Cannot derive refs for ${model.modelName}#${schemaPath}.`
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
if (refs.includes(targetName)) {
|
|
96
|
-
paths.push(schemaPath);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
return paths;
|
|
101
|
-
}
|