@bedrockio/model 0.21.0 → 0.21.2
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/README.md +20 -5
- package/dist/cjs/index.js +7 -0
- package/dist/cjs/load.js +36 -22
- package/package.json +1 -1
- package/src/index.js +1 -1
- package/src/load.js +36 -20
- package/types/generated/index.d.ts +1 -1
- package/types/generated/load.d.ts +7 -1
- package/types/generated/load.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
|
package/dist/cjs/index.js
CHANGED
|
@@ -7,6 +7,7 @@ var _exportNames = {
|
|
|
7
7
|
createSchema: true,
|
|
8
8
|
loadModel: true,
|
|
9
9
|
loadModelDir: true,
|
|
10
|
+
loadSchema: true,
|
|
10
11
|
addValidators: true,
|
|
11
12
|
isEqual: true
|
|
12
13
|
};
|
|
@@ -40,6 +41,12 @@ Object.defineProperty(exports, "loadModelDir", {
|
|
|
40
41
|
return _load.loadModelDir;
|
|
41
42
|
}
|
|
42
43
|
});
|
|
44
|
+
Object.defineProperty(exports, "loadSchema", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function () {
|
|
47
|
+
return _load.loadSchema;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
43
50
|
var _schema = require("./schema");
|
|
44
51
|
var _load = require("./load");
|
|
45
52
|
var _validation = require("./validation");
|
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;
|
|
@@ -43,55 +44,68 @@ function loadModelDir(dir) {
|
|
|
43
44
|
if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
44
45
|
continue;
|
|
45
46
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const {
|
|
48
|
+
name: basename
|
|
49
|
+
} = _path.default.parse(file);
|
|
50
|
+
const definition = loadDefinition(basename, dir);
|
|
51
|
+
const modelName = getModelName(definition, basename);
|
|
49
52
|
if (!_mongoose.default.models[modelName]) {
|
|
50
53
|
loadModel(definition, modelName);
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
56
|
return _mongoose.default.models;
|
|
54
57
|
}
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Loads the schema from a .json or .jsonc file.
|
|
61
|
+
* @param {string} name - The model or schema name.
|
|
62
|
+
* @param {string} [dir] - The schema directory (defaults to `src/models/definitions`)
|
|
63
|
+
*/
|
|
64
|
+
function loadSchema(name, dir) {
|
|
65
|
+
const definition = loadDefinition(name, dir);
|
|
57
66
|
return (0, _schema.createSchema)(definition);
|
|
58
67
|
}
|
|
68
|
+
const DEFINITION_DIR = 'src/models/definitions';
|
|
59
69
|
const SCHEMA_EXTENSIONS = ['.json', '.jsonc'];
|
|
60
|
-
function loadDefinition(
|
|
70
|
+
function loadDefinition(name, dir) {
|
|
61
71
|
const {
|
|
62
72
|
filepath,
|
|
63
73
|
ext
|
|
64
|
-
} = resolvePath(
|
|
74
|
+
} = resolvePath(name, dir);
|
|
65
75
|
const content = _fs.default.readFileSync(filepath, 'utf-8');
|
|
66
76
|
return ext === '.jsonc' ? (0, _jsoncParser.parse)(content) : JSON.parse(content);
|
|
67
77
|
}
|
|
68
|
-
function resolvePath(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
ext = resolveSchemaExtension(filepath);
|
|
72
|
-
filepath += ext;
|
|
78
|
+
function resolvePath(name, dir = DEFINITION_DIR) {
|
|
79
|
+
if (name.includes('.')) {
|
|
80
|
+
throw new Error('Name should not include extension');
|
|
73
81
|
}
|
|
82
|
+
let filename = (0, _lodash.kebabCase)(name);
|
|
83
|
+
let ext = _path.default.extname(name);
|
|
74
84
|
if (!ext) {
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
ext = resolveExtension(filename, dir);
|
|
86
|
+
if (ext) {
|
|
87
|
+
filename += ext;
|
|
88
|
+
} else {
|
|
89
|
+
throw new Error(`No .json or .jsonc file found for: ${name}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
77
93
|
throw new Error(`Schema files must be .json or .jsonc`);
|
|
78
94
|
}
|
|
95
|
+
const filepath = _path.default.resolve(dir, filename);
|
|
79
96
|
return {
|
|
80
97
|
ext,
|
|
81
|
-
filepath
|
|
98
|
+
filepath
|
|
82
99
|
};
|
|
83
100
|
}
|
|
84
|
-
function
|
|
101
|
+
function resolveExtension(filename, dir) {
|
|
85
102
|
for (const ext of SCHEMA_EXTENSIONS) {
|
|
86
|
-
const filepath =
|
|
103
|
+
const filepath = _path.default.resolve(dir, filename + ext);
|
|
87
104
|
if (_fs.default.existsSync(filepath)) {
|
|
88
105
|
return ext;
|
|
89
106
|
}
|
|
90
107
|
}
|
|
91
108
|
}
|
|
92
|
-
function getModelName(definition,
|
|
93
|
-
|
|
94
|
-
name: filename
|
|
95
|
-
} = _path.default.parse(filepath);
|
|
96
|
-
return definition.modelName || (0, _lodash.startCase)(filename).replace(/\s/g, '');
|
|
109
|
+
function getModelName(definition, basename) {
|
|
110
|
+
return definition.modelName || (0, _lodash.startCase)(basename).replace(/\s/g, '');
|
|
97
111
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createSchema } from './schema';
|
|
2
|
-
export { loadModel, loadModelDir } from './load';
|
|
2
|
+
export { loadModel, loadModelDir, loadSchema } from './load';
|
|
3
3
|
export { addValidators } from './validation';
|
|
4
4
|
export { isEqual } from './utils';
|
|
5
5
|
export * from './testing';
|
package/src/load.js
CHANGED
|
@@ -2,6 +2,7 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
|
|
4
4
|
import { parse as parseWithComments } from 'jsonc-parser';
|
|
5
|
+
import { kebabCase } from 'lodash';
|
|
5
6
|
import { startCase } from 'lodash';
|
|
6
7
|
import mongoose from 'mongoose';
|
|
7
8
|
|
|
@@ -39,9 +40,9 @@ export function loadModelDir(dir) {
|
|
|
39
40
|
continue;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
const
|
|
43
|
-
const definition = loadDefinition(
|
|
44
|
-
const modelName = getModelName(definition,
|
|
43
|
+
const { name: basename } = path.parse(file);
|
|
44
|
+
const definition = loadDefinition(basename, dir);
|
|
45
|
+
const modelName = getModelName(definition, basename);
|
|
45
46
|
|
|
46
47
|
if (!mongoose.models[modelName]) {
|
|
47
48
|
loadModel(definition, modelName);
|
|
@@ -50,49 +51,64 @@ export function loadModelDir(dir) {
|
|
|
50
51
|
return mongoose.models;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Loads the schema from a .json or .jsonc file.
|
|
56
|
+
* @param {string} name - The model or schema name.
|
|
57
|
+
* @param {string} [dir] - The schema directory (defaults to `src/models/definitions`)
|
|
58
|
+
*/
|
|
59
|
+
export function loadSchema(name, dir) {
|
|
60
|
+
const definition = loadDefinition(name, dir);
|
|
55
61
|
return createSchema(definition);
|
|
56
62
|
}
|
|
57
63
|
|
|
64
|
+
const DEFINITION_DIR = 'src/models/definitions';
|
|
58
65
|
const SCHEMA_EXTENSIONS = ['.json', '.jsonc'];
|
|
59
66
|
|
|
60
|
-
function loadDefinition(
|
|
61
|
-
const { filepath, ext } = resolvePath(
|
|
67
|
+
export function loadDefinition(name, dir) {
|
|
68
|
+
const { filepath, ext } = resolvePath(name, dir);
|
|
62
69
|
const content = fs.readFileSync(filepath, 'utf-8');
|
|
63
70
|
return ext === '.jsonc' ? parseWithComments(content) : JSON.parse(content);
|
|
64
71
|
}
|
|
65
72
|
|
|
66
|
-
function resolvePath(
|
|
67
|
-
|
|
73
|
+
function resolvePath(name, dir = DEFINITION_DIR) {
|
|
74
|
+
if (name.includes('.')) {
|
|
75
|
+
throw new Error('Name should not include extension');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let filename = kebabCase(name);
|
|
79
|
+
let ext = path.extname(name);
|
|
68
80
|
|
|
69
81
|
if (!ext) {
|
|
70
|
-
ext =
|
|
71
|
-
|
|
82
|
+
ext = resolveExtension(filename, dir);
|
|
83
|
+
|
|
84
|
+
if (ext) {
|
|
85
|
+
filename += ext;
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error(`No .json or .jsonc file found for: ${name}`);
|
|
88
|
+
}
|
|
72
89
|
}
|
|
73
90
|
|
|
74
|
-
if (!ext) {
|
|
75
|
-
throw new Error(`No .json or .jsonc file found for: ${filepath}`);
|
|
76
|
-
} else if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
91
|
+
if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
77
92
|
throw new Error(`Schema files must be .json or .jsonc`);
|
|
78
93
|
}
|
|
79
94
|
|
|
95
|
+
const filepath = path.resolve(dir, filename);
|
|
96
|
+
|
|
80
97
|
return {
|
|
81
98
|
ext,
|
|
82
|
-
filepath
|
|
99
|
+
filepath,
|
|
83
100
|
};
|
|
84
101
|
}
|
|
85
102
|
|
|
86
|
-
function
|
|
103
|
+
function resolveExtension(filename, dir) {
|
|
87
104
|
for (const ext of SCHEMA_EXTENSIONS) {
|
|
88
|
-
const filepath =
|
|
105
|
+
const filepath = path.resolve(dir, filename + ext);
|
|
89
106
|
if (fs.existsSync(filepath)) {
|
|
90
107
|
return ext;
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
111
|
|
|
95
|
-
function getModelName(definition,
|
|
96
|
-
|
|
97
|
-
return definition.modelName || startCase(filename).replace(/\s/g, '');
|
|
112
|
+
function getModelName(definition, basename) {
|
|
113
|
+
return definition.modelName || startCase(basename).replace(/\s/g, '');
|
|
98
114
|
}
|
|
@@ -3,5 +3,5 @@ export { addValidators } from "./validation";
|
|
|
3
3
|
export { isEqual } from "./utils";
|
|
4
4
|
export * from "./testing";
|
|
5
5
|
export * from "./errors";
|
|
6
|
-
export { loadModel, loadModelDir } from "./load";
|
|
6
|
+
export { loadModel, loadModelDir, loadSchema } from "./load";
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -11,7 +11,12 @@ export function loadModel(definition: object, name: string): any;
|
|
|
11
11
|
* @param {string} dir
|
|
12
12
|
*/
|
|
13
13
|
export function loadModelDir(dir: string): mongoose.Models;
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Loads the schema from a .json or .jsonc file.
|
|
16
|
+
* @param {string} name - The model or schema name.
|
|
17
|
+
* @param {string} [dir] - The schema directory (defaults to `src/models/definitions`)
|
|
18
|
+
*/
|
|
19
|
+
export function loadSchema(name: string, dir?: string): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, any, mongoose.DefaultSchemaOptions, {
|
|
15
20
|
[x: number]: unknown;
|
|
16
21
|
[x: symbol]: unknown;
|
|
17
22
|
[x: string]: unknown;
|
|
@@ -28,5 +33,6 @@ export function loadSchema(input: any): mongoose.Schema<any, mongoose.Model<any,
|
|
|
28
33
|
}> & {
|
|
29
34
|
__v: number;
|
|
30
35
|
}>;
|
|
36
|
+
export function loadDefinition(name: any, dir: any): any;
|
|
31
37
|
import mongoose from 'mongoose';
|
|
32
38
|
//# sourceMappingURL=load.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;GAIG;AACH,kCAFW,MAAM,mBAoBhB;AAED;;;;GAIG;AACH,iCAHW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;GAKhB;AAKD,yDAIC;qBAhEoB,UAAU"}
|