@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 CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.21.0
2
+
3
+ - Allow comments in schemas with .jsonc.
4
+
1
5
  ## 0.20.2
2
6
 
3
7
  - Fixed `_id: false` on validation schemas.
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} dirPath
37
+ * @param {string} dir
36
38
  */
37
- function loadModelDir(dirPath) {
38
- const files = _fs.default.readdirSync(dirPath);
39
+ function loadModelDir(dir) {
40
+ const files = _fs.default.readdirSync(dir);
39
41
  for (const file of files) {
40
- const basename = _path.default.basename(file, '.json');
41
- if (file.match(/\.json$/)) {
42
- const filePath = _path.default.join(dirPath, file);
43
- const data = _fs.default.readFileSync(filePath, 'utf-8');
44
- const definition = JSON.parse(data);
45
- const modelName = definition.modelName || (0, _lodash.startCase)(basename).replace(/\s/g, '');
46
- if (!_mongoose.default.models[modelName]) {
47
- loadModel(definition, modelName);
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.20.2",
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} dirPath
31
+ * @param {string} dir
31
32
  */
32
- export function loadModelDir(dirPath) {
33
- const files = fs.readdirSync(dirPath);
33
+ export function loadModelDir(dir) {
34
+ const files = fs.readdirSync(dir);
34
35
  for (const file of files) {
35
- const basename = path.basename(file, '.json');
36
- if (file.match(/\.json$/)) {
37
- const filePath = path.join(dirPath, file);
38
- const data = fs.readFileSync(filePath, 'utf-8');
39
- const definition = JSON.parse(data);
40
- const modelName =
41
- definition.modelName || startCase(basename).replace(/\s/g, '');
42
- if (!mongoose.models[modelName]) {
43
- loadModel(definition, modelName);
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} dirPath
11
+ * @param {string} dir
12
12
  */
13
- export function loadModelDir(dirPath: string): mongoose.Models;
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":"AAQA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;GAIG;AACH,sCAFW,MAAM,mBAkBhB;qBA3CoB,UAAU"}
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"}