@bedrockio/model 0.20.2 → 0.21.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/CHANGELOG.md +4 -0
- package/dist/cjs/load.js +57 -12
- package/package.json +2 -1
- package/src/load.js +63 -13
- package/types/generated/load.d.ts +19 -2
- package/types/generated/load.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/load.js
CHANGED
|
@@ -5,8 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.loadModel = loadModel;
|
|
7
7
|
exports.loadModelDir = loadModelDir;
|
|
8
|
+
exports.loadSchema = loadSchema;
|
|
8
9
|
var _fs = _interopRequireDefault(require("fs"));
|
|
9
10
|
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _jsoncParser = require("jsonc-parser");
|
|
10
12
|
var _lodash = require("lodash");
|
|
11
13
|
var _mongoose = _interopRequireDefault(require("mongoose"));
|
|
12
14
|
var _schema = require("./schema");
|
|
@@ -32,21 +34,64 @@ function loadModel(definition, name) {
|
|
|
32
34
|
/**
|
|
33
35
|
* Loads all model definitions in the given directory.
|
|
34
36
|
* Returns the full loaded model set.
|
|
35
|
-
* @param {string}
|
|
37
|
+
* @param {string} dir
|
|
36
38
|
*/
|
|
37
|
-
function loadModelDir(
|
|
38
|
-
const files = _fs.default.readdirSync(
|
|
39
|
+
function loadModelDir(dir) {
|
|
40
|
+
const files = _fs.default.readdirSync(dir);
|
|
39
41
|
for (const file of files) {
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
const ext = _path.default.extname(file);
|
|
43
|
+
if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const filepath = _path.default.join(dir, file);
|
|
47
|
+
const definition = loadDefinition(filepath);
|
|
48
|
+
const modelName = getModelName(definition, filepath);
|
|
49
|
+
if (!_mongoose.default.models[modelName]) {
|
|
50
|
+
loadModel(definition, modelName);
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
return _mongoose.default.models;
|
|
54
|
+
}
|
|
55
|
+
function loadSchema(input) {
|
|
56
|
+
const definition = loadDefinition(input);
|
|
57
|
+
return (0, _schema.createSchema)(definition);
|
|
58
|
+
}
|
|
59
|
+
const SCHEMA_EXTENSIONS = ['.json', '.jsonc'];
|
|
60
|
+
function loadDefinition(input) {
|
|
61
|
+
const {
|
|
62
|
+
filepath,
|
|
63
|
+
ext
|
|
64
|
+
} = resolvePath(input);
|
|
65
|
+
const content = _fs.default.readFileSync(filepath, 'utf-8');
|
|
66
|
+
return ext === '.jsonc' ? (0, _jsoncParser.parse)(content) : JSON.parse(content);
|
|
67
|
+
}
|
|
68
|
+
function resolvePath(filepath) {
|
|
69
|
+
let ext = _path.default.extname(filepath);
|
|
70
|
+
if (!ext) {
|
|
71
|
+
ext = resolveSchemaExtension(filepath);
|
|
72
|
+
filepath += ext;
|
|
73
|
+
}
|
|
74
|
+
if (!ext) {
|
|
75
|
+
throw new Error(`No .json or .jsonc file found for: ${filepath}`);
|
|
76
|
+
} else if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
77
|
+
throw new Error(`Schema files must be .json or .jsonc`);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
ext,
|
|
81
|
+
filepath: filepath
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function resolveSchemaExtension(input) {
|
|
85
|
+
for (const ext of SCHEMA_EXTENSIONS) {
|
|
86
|
+
const filepath = input + ext;
|
|
87
|
+
if (_fs.default.existsSync(filepath)) {
|
|
88
|
+
return ext;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function getModelName(definition, filepath) {
|
|
93
|
+
const {
|
|
94
|
+
name: filename
|
|
95
|
+
} = _path.default.parse(filepath);
|
|
96
|
+
return definition.modelName || (0, _lodash.startCase)(filename).replace(/\s/g, '');
|
|
52
97
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/model",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Bedrock utilities for model creation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@bedrockio/logger": "^1.1.1",
|
|
30
|
+
"jsonc-parser": "^3.3.1",
|
|
30
31
|
"lodash": "^4.17.21"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
package/src/load.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
|
|
4
|
+
import { parse as parseWithComments } from 'jsonc-parser';
|
|
4
5
|
import { startCase } from 'lodash';
|
|
5
6
|
import mongoose from 'mongoose';
|
|
6
7
|
|
|
@@ -27,22 +28,71 @@ export function loadModel(definition, name) {
|
|
|
27
28
|
/**
|
|
28
29
|
* Loads all model definitions in the given directory.
|
|
29
30
|
* Returns the full loaded model set.
|
|
30
|
-
* @param {string}
|
|
31
|
+
* @param {string} dir
|
|
31
32
|
*/
|
|
32
|
-
export function loadModelDir(
|
|
33
|
-
const files = fs.readdirSync(
|
|
33
|
+
export function loadModelDir(dir) {
|
|
34
|
+
const files = fs.readdirSync(dir);
|
|
34
35
|
for (const file of files) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
const ext = path.extname(file);
|
|
37
|
+
|
|
38
|
+
if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const filepath = path.join(dir, file);
|
|
43
|
+
const definition = loadDefinition(filepath);
|
|
44
|
+
const modelName = getModelName(definition, filepath);
|
|
45
|
+
|
|
46
|
+
if (!mongoose.models[modelName]) {
|
|
47
|
+
loadModel(definition, modelName);
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
return mongoose.models;
|
|
48
51
|
}
|
|
52
|
+
|
|
53
|
+
export function loadSchema(input) {
|
|
54
|
+
const definition = loadDefinition(input);
|
|
55
|
+
return createSchema(definition);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const SCHEMA_EXTENSIONS = ['.json', '.jsonc'];
|
|
59
|
+
|
|
60
|
+
function loadDefinition(input) {
|
|
61
|
+
const { filepath, ext } = resolvePath(input);
|
|
62
|
+
const content = fs.readFileSync(filepath, 'utf-8');
|
|
63
|
+
return ext === '.jsonc' ? parseWithComments(content) : JSON.parse(content);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resolvePath(filepath) {
|
|
67
|
+
let ext = path.extname(filepath);
|
|
68
|
+
|
|
69
|
+
if (!ext) {
|
|
70
|
+
ext = resolveSchemaExtension(filepath);
|
|
71
|
+
filepath += ext;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!ext) {
|
|
75
|
+
throw new Error(`No .json or .jsonc file found for: ${filepath}`);
|
|
76
|
+
} else if (!SCHEMA_EXTENSIONS.includes(ext)) {
|
|
77
|
+
throw new Error(`Schema files must be .json or .jsonc`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
ext,
|
|
82
|
+
filepath: filepath,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function resolveSchemaExtension(input) {
|
|
87
|
+
for (const ext of SCHEMA_EXTENSIONS) {
|
|
88
|
+
const filepath = input + ext;
|
|
89
|
+
if (fs.existsSync(filepath)) {
|
|
90
|
+
return ext;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function getModelName(definition, filepath) {
|
|
96
|
+
const { name: filename } = path.parse(filepath);
|
|
97
|
+
return definition.modelName || startCase(filename).replace(/\s/g, '');
|
|
98
|
+
}
|
|
@@ -8,8 +8,25 @@ export function loadModel(definition: object, name: string): any;
|
|
|
8
8
|
/**
|
|
9
9
|
* Loads all model definitions in the given directory.
|
|
10
10
|
* Returns the full loaded model set.
|
|
11
|
-
* @param {string}
|
|
11
|
+
* @param {string} dir
|
|
12
12
|
*/
|
|
13
|
-
export function loadModelDir(
|
|
13
|
+
export function loadModelDir(dir: string): mongoose.Models;
|
|
14
|
+
export function loadSchema(input: any): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, any, mongoose.DefaultSchemaOptions, {
|
|
15
|
+
[x: number]: unknown;
|
|
16
|
+
[x: symbol]: unknown;
|
|
17
|
+
[x: string]: unknown;
|
|
18
|
+
}, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
|
|
19
|
+
[x: number]: unknown;
|
|
20
|
+
[x: symbol]: unknown;
|
|
21
|
+
[x: string]: unknown;
|
|
22
|
+
}>, any, mongoose.ResolveSchemaOptions<mongoose.DefaultSchemaOptions>> & mongoose.FlatRecord<{
|
|
23
|
+
[x: number]: unknown;
|
|
24
|
+
[x: symbol]: unknown;
|
|
25
|
+
[x: string]: unknown;
|
|
26
|
+
}> & Required<{
|
|
27
|
+
_id: unknown;
|
|
28
|
+
}> & {
|
|
29
|
+
__v: number;
|
|
30
|
+
}>;
|
|
14
31
|
import mongoose from 'mongoose';
|
|
15
32
|
//# 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":"AASA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;GAIG;AACH,kCAFW,MAAM,mBAoBhB;AAED;;;;;;;;;;;;;;;;GAGC;qBAlDoB,UAAU"}
|