@bedrockio/model 0.21.1 → 0.21.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 +9 -0
- package/README.md +43 -5
- package/dist/cjs/load.js +2 -0
- package/dist/cjs/soft-delete.js +10 -1
- package/package.json +1 -1
- package/src/load.js +2 -1
- package/src/soft-delete.js +10 -1
- package/types/generated/load.d.ts +5 -1
- package/types/generated/load.d.ts.map +1 -1
- package/types/generated/soft-delete.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Bedrock utilities for model creation.
|
|
|
6
6
|
- [Dependencies](#dependencies)
|
|
7
7
|
- [Usage](#usage)
|
|
8
8
|
- [Schemas](#schemas)
|
|
9
|
+
- [Schema Comments](#schema-comments)
|
|
9
10
|
- [Schema Extensions](#schema-extensions)
|
|
10
11
|
- [Attributes](#attributes)
|
|
11
12
|
- [Scopes](#scopes)
|
|
@@ -58,14 +59,13 @@ const { loadModelDir } = require('@bedrockio/model');
|
|
|
58
59
|
model.exports = loadModelDir('path/to/definitions/');
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
Models that need to be extended can use the `
|
|
62
|
-
definition and add to the schema as needed:
|
|
62
|
+
Models that need to be extended can use the `loadSchema` method.
|
|
63
63
|
|
|
64
64
|
```js
|
|
65
65
|
const mongoose = require('mongoose');
|
|
66
|
-
const
|
|
66
|
+
const { loadSchema } = require('@bedrockio/model')
|
|
67
67
|
|
|
68
|
-
const schema =
|
|
68
|
+
const schema = loadSchema('user');
|
|
69
69
|
|
|
70
70
|
schema.virtual('name').get(function () {
|
|
71
71
|
return [this.firstName, this.lastName].join(' ');
|
|
@@ -89,7 +89,7 @@ model.exports = {
|
|
|
89
89
|
The `attributes` field of model definitions can be considered equivalent to
|
|
90
90
|
Mongoose, but defined in JSON with extended features:
|
|
91
91
|
|
|
92
|
-
```
|
|
92
|
+
```jsonc
|
|
93
93
|
{
|
|
94
94
|
"attributes": {
|
|
95
95
|
// Shortcut for the syntax below.
|
|
@@ -129,6 +129,21 @@ Links:
|
|
|
129
129
|
- [Validation](#validation)
|
|
130
130
|
- [Access Control](#access-control)
|
|
131
131
|
|
|
132
|
+
|
|
133
|
+
#### Schema Comments
|
|
134
|
+
|
|
135
|
+
Schemas may be `.jsonc` which will allow comments in definitions:
|
|
136
|
+
|
|
137
|
+
```jsonc
|
|
138
|
+
// user.jsonc
|
|
139
|
+
{
|
|
140
|
+
"attributes": {
|
|
141
|
+
// User name
|
|
142
|
+
"name": "String",
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
132
147
|
### Schema Extensions
|
|
133
148
|
|
|
134
149
|
This module provides a number of extensions to assist schema creation outside
|
|
@@ -1168,6 +1183,17 @@ const user = await User.find({
|
|
|
1168
1183
|
});
|
|
1169
1184
|
```
|
|
1170
1185
|
|
|
1186
|
+
#### Include with Create
|
|
1187
|
+
|
|
1188
|
+
Includes are not allowed in create operations by default. Instead use the `createWithInclude` method:
|
|
1189
|
+
|
|
1190
|
+
```js
|
|
1191
|
+
const user = await User.createWithInclude({
|
|
1192
|
+
firstName: 'Frank',
|
|
1193
|
+
include: 'profile',
|
|
1194
|
+
});
|
|
1195
|
+
```
|
|
1196
|
+
|
|
1171
1197
|
#### Validation with includes
|
|
1172
1198
|
|
|
1173
1199
|
The [validation](#validation) methods additionally allow `include` as a special
|
|
@@ -1190,6 +1216,18 @@ The `getSearchValidation` will allow the `include` property to be passed,
|
|
|
1190
1216
|
letting the client populate documents as they require. Note that the fields a
|
|
1191
1217
|
client is able to include is subject to [access control](#access-control).
|
|
1192
1218
|
|
|
1219
|
+
Additionally the `getIncludeValidation` exports the includes as a standalone schema so that it can be used ad-hoc in routes and [composed with other custom validations](https://github.com/bedrockio/yada#append).
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|Method|Includes|
|
|
1223
|
+
|---|---|
|
|
1224
|
+
|getSearchValiation|default|
|
|
1225
|
+
|getIncludeValidation|default|
|
|
1226
|
+
|getCreateValidation|optional|
|
|
1227
|
+
|getUpdateValidation|optional|
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1193
1231
|
#### Other Differences with Populate
|
|
1194
1232
|
|
|
1195
1233
|
Calling `populate` on a Mongoose document will always load the current data. In
|
package/dist/cjs/load.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.loadDefinition = loadDefinition;
|
|
6
7
|
exports.loadModel = loadModel;
|
|
7
8
|
exports.loadModelDir = loadModelDir;
|
|
8
9
|
exports.loadSchema = loadSchema;
|
|
@@ -35,6 +36,7 @@ function loadModel(definition, name) {
|
|
|
35
36
|
* Loads all model definitions in the given directory.
|
|
36
37
|
* Returns the full loaded model set.
|
|
37
38
|
* @param {string} dir
|
|
39
|
+
* @returns {{ [name: string]: mongoose.Model<any> }}
|
|
38
40
|
*/
|
|
39
41
|
function loadModelDir(dir) {
|
|
40
42
|
const files = _fs.default.readdirSync(dir);
|
package/dist/cjs/soft-delete.js
CHANGED
|
@@ -35,8 +35,11 @@ async function assertUnique(options) {
|
|
|
35
35
|
const exists = await model.exists(query);
|
|
36
36
|
if (exists) {
|
|
37
37
|
const message = getUniqueErrorMessage(field, options);
|
|
38
|
+
const {
|
|
39
|
+
modelName
|
|
40
|
+
} = model;
|
|
38
41
|
throw new _errors.UniqueConstraintError(message, {
|
|
39
|
-
|
|
42
|
+
modelName,
|
|
40
43
|
field,
|
|
41
44
|
value
|
|
42
45
|
});
|
|
@@ -407,6 +410,12 @@ function applyHookPatch(schema) {
|
|
|
407
410
|
const schemaPre = schema.pre;
|
|
408
411
|
const schemaPost = schema.post;
|
|
409
412
|
schema.pre = function (name, fn) {
|
|
413
|
+
// Newer Mongoose versions appear to use internal plugins
|
|
414
|
+
// that pass objects instead of functions here (not documented)
|
|
415
|
+
// so abort and pass to internal handler.
|
|
416
|
+
if (typeof fn !== 'function') {
|
|
417
|
+
return schemaPre.apply(this, arguments);
|
|
418
|
+
}
|
|
410
419
|
if (name === 'restore') {
|
|
411
420
|
// Document hooks
|
|
412
421
|
schemaPre.call(this, 'save', getPreDocRestore(fn));
|
package/package.json
CHANGED
package/src/load.js
CHANGED
|
@@ -30,6 +30,7 @@ export function loadModel(definition, name) {
|
|
|
30
30
|
* Loads all model definitions in the given directory.
|
|
31
31
|
* Returns the full loaded model set.
|
|
32
32
|
* @param {string} dir
|
|
33
|
+
* @returns {{ [name: string]: mongoose.Model<any> }}
|
|
33
34
|
*/
|
|
34
35
|
export function loadModelDir(dir) {
|
|
35
36
|
const files = fs.readdirSync(dir);
|
|
@@ -64,7 +65,7 @@ export function loadSchema(name, dir) {
|
|
|
64
65
|
const DEFINITION_DIR = 'src/models/definitions';
|
|
65
66
|
const SCHEMA_EXTENSIONS = ['.json', '.jsonc'];
|
|
66
67
|
|
|
67
|
-
function loadDefinition(name, dir) {
|
|
68
|
+
export function loadDefinition(name, dir) {
|
|
68
69
|
const { filepath, ext } = resolvePath(name, dir);
|
|
69
70
|
const content = fs.readFileSync(filepath, 'utf-8');
|
|
70
71
|
return ext === '.jsonc' ? parseWithComments(content) : JSON.parse(content);
|
package/src/soft-delete.js
CHANGED
|
@@ -28,8 +28,10 @@ export async function assertUnique(options) {
|
|
|
28
28
|
|
|
29
29
|
if (exists) {
|
|
30
30
|
const message = getUniqueErrorMessage(field, options);
|
|
31
|
+
const { modelName } = model;
|
|
32
|
+
|
|
31
33
|
throw new UniqueConstraintError(message, {
|
|
32
|
-
|
|
34
|
+
modelName,
|
|
33
35
|
field,
|
|
34
36
|
value,
|
|
35
37
|
});
|
|
@@ -454,6 +456,13 @@ function applyHookPatch(schema) {
|
|
|
454
456
|
const schemaPost = schema.post;
|
|
455
457
|
|
|
456
458
|
schema.pre = function (name, fn) {
|
|
459
|
+
// Newer Mongoose versions appear to use internal plugins
|
|
460
|
+
// that pass objects instead of functions here (not documented)
|
|
461
|
+
// so abort and pass to internal handler.
|
|
462
|
+
if (typeof fn !== 'function') {
|
|
463
|
+
return schemaPre.apply(this, arguments);
|
|
464
|
+
}
|
|
465
|
+
|
|
457
466
|
if (name === 'restore') {
|
|
458
467
|
// Document hooks
|
|
459
468
|
schemaPre.call(this, 'save', getPreDocRestore(fn));
|
|
@@ -9,8 +9,11 @@ export function loadModel(definition: object, name: string): any;
|
|
|
9
9
|
* Loads all model definitions in the given directory.
|
|
10
10
|
* Returns the full loaded model set.
|
|
11
11
|
* @param {string} dir
|
|
12
|
+
* @returns {{ [name: string]: mongoose.Model<any> }}
|
|
12
13
|
*/
|
|
13
|
-
export function loadModelDir(dir: string):
|
|
14
|
+
export function loadModelDir(dir: string): {
|
|
15
|
+
[name: string]: mongoose.Model<any>;
|
|
16
|
+
};
|
|
14
17
|
/**
|
|
15
18
|
* Loads the schema from a .json or .jsonc file.
|
|
16
19
|
* @param {string} name - The model or schema name.
|
|
@@ -33,5 +36,6 @@ export function loadSchema(name: string, dir?: string): mongoose.Schema<any, mon
|
|
|
33
36
|
}> & {
|
|
34
37
|
__v: number;
|
|
35
38
|
}>;
|
|
39
|
+
export function loadDefinition(name: any, dir: any): any;
|
|
36
40
|
import mongoose from 'mongoose';
|
|
37
41
|
//# sourceMappingURL=load.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;;GAKG;AACH,kCAHW,MAAM,GACJ;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;CAAE,CAoBnD;AAED;;;;GAIG;AACH,iCAHW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;GAKhB;AAKD,yDAIC;qBAjEoB,UAAU"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../../src/soft-delete.js"],"names":[],"mappings":"AAMA,mDAIC;AAED,
|
|
1
|
+
{"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../../src/soft-delete.js"],"names":[],"mappings":"AAMA,mDAIC;AAED,0DA0BC"}
|