@bedrockio/model 0.1.23 → 0.1.25

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.
@@ -27,7 +27,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
27
27
  * @returns mongoose.Schema
28
28
  */
29
29
  function createSchema(definition, options = {}) {
30
- const schema = new _mongoose.default.Schema(attributesToMongoose(normalizeAttributes({
30
+ const schema = new _mongoose.default.Schema(attributesToMongoose({
31
31
  ...definition.attributes,
32
32
  // Although timestamps are being set below, we still need to add
33
33
  // them to the schema so that validation can be generated for them,
@@ -39,7 +39,7 @@ function createSchema(definition, options = {}) {
39
39
  type: 'Boolean',
40
40
  default: false
41
41
  }
42
- })), {
42
+ }), {
43
43
  timestamps: true,
44
44
  toJSON: _serialization.serializeOptions,
45
45
  toObject: _serialization.serializeOptions,
@@ -65,9 +65,8 @@ function normalizeAttributes(arg, path = []) {
65
65
  type: arg
66
66
  }, path);
67
67
  } else if (Array.isArray(arg)) {
68
- const type = normalizeArrayAttributes(arg, path);
69
68
  return normalizeSchemaTypedef({
70
- type
69
+ type: arg
71
70
  }, path);
72
71
  } else if (typeof arg === 'object') {
73
72
  assertRefs(arg, path);
@@ -105,6 +104,7 @@ function attributesToMongoose(attributes) {
105
104
  } else if (Array.isArray(attributes)) {
106
105
  return attributes.map(attributesToMongoose);
107
106
  }
107
+ attributes = normalizeAttributes(attributes);
108
108
  let definition = {};
109
109
  const isTypedef = (0, _utils.isSchemaTypedef)(attributes);
110
110
  for (let [key, val] of Object.entries(attributes)) {
@@ -211,20 +211,29 @@ function isExtendedSyntax(typedef) {
211
211
  } = typedef;
212
212
  return attributes && (type === 'Object' || type === 'Array');
213
213
  }
214
- function isScopeExtension(obj) {
215
- return (0, _utils.isSchemaTypedef)(obj) && obj.type === 'Scope';
214
+ function isScopeExtension(arg) {
215
+ return (0, _utils.isSchemaTypedef)(arg) && arg.type === 'Scope';
216
216
  }
217
- function applyScopeExtension(scope, definition) {
217
+ function applyScopeExtension(typedef, definition) {
218
218
  const {
219
219
  type,
220
220
  attributes,
221
221
  ...options
222
- } = scope;
222
+ } = typedef;
223
223
  for (let [key, val] of Object.entries(normalizeAttributes(attributes))) {
224
- definition[key] = {
225
- ...val,
226
- ...options
227
- };
224
+ if ((0, _utils.isSchemaTypedef)(val)) {
225
+ val = {
226
+ ...val,
227
+ ...options
228
+ };
229
+ } else {
230
+ val = attributesToMongoose({
231
+ type: 'Object',
232
+ attributes: val,
233
+ ...options
234
+ });
235
+ }
236
+ definition[key] = val;
228
237
  }
229
238
  }
230
239
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/schema.js CHANGED
@@ -28,19 +28,17 @@ import {
28
28
  */
29
29
  export function createSchema(definition, options = {}) {
30
30
  const schema = new mongoose.Schema(
31
- attributesToMongoose(
32
- normalizeAttributes({
33
- ...definition.attributes,
34
-
35
- // Although timestamps are being set below, we still need to add
36
- // them to the schema so that validation can be generated for them,
37
- // namely in getSearchValidation.
38
- createdAt: 'Date',
39
- updatedAt: 'Date',
40
- deletedAt: 'Date',
41
- deleted: { type: 'Boolean', default: false },
42
- })
43
- ),
31
+ attributesToMongoose({
32
+ ...definition.attributes,
33
+
34
+ // Although timestamps are being set below, we still need to add
35
+ // them to the schema so that validation can be generated for them,
36
+ // namely in getSearchValidation.
37
+ createdAt: 'Date',
38
+ updatedAt: 'Date',
39
+ deletedAt: 'Date',
40
+ deleted: { type: 'Boolean', default: false },
41
+ }),
44
42
  {
45
43
  timestamps: true,
46
44
  toJSON: serializeOptions,
@@ -69,8 +67,7 @@ export function normalizeAttributes(arg, path = []) {
69
67
  } else if (typeof arg === 'string') {
70
68
  return normalizeSchemaTypedef({ type: arg }, path);
71
69
  } else if (Array.isArray(arg)) {
72
- const type = normalizeArrayAttributes(arg, path);
73
- return normalizeSchemaTypedef({ type }, path);
70
+ return normalizeSchemaTypedef({ type: arg }, path);
74
71
  } else if (typeof arg === 'object') {
75
72
  assertRefs(arg, path);
76
73
 
@@ -113,6 +110,8 @@ function attributesToMongoose(attributes) {
113
110
  return attributes.map(attributesToMongoose);
114
111
  }
115
112
 
113
+ attributes = normalizeAttributes(attributes);
114
+
116
115
  let definition = {};
117
116
 
118
117
  const isTypedef = isSchemaTypedef(attributes);
@@ -223,17 +222,26 @@ function isExtendedSyntax(typedef) {
223
222
  return attributes && (type === 'Object' || type === 'Array');
224
223
  }
225
224
 
226
- function isScopeExtension(obj) {
227
- return isSchemaTypedef(obj) && obj.type === 'Scope';
225
+ function isScopeExtension(arg) {
226
+ return isSchemaTypedef(arg) && arg.type === 'Scope';
228
227
  }
229
228
 
230
- function applyScopeExtension(scope, definition) {
231
- const { type, attributes, ...options } = scope;
229
+ function applyScopeExtension(typedef, definition) {
230
+ const { type, attributes, ...options } = typedef;
232
231
  for (let [key, val] of Object.entries(normalizeAttributes(attributes))) {
233
- definition[key] = {
234
- ...val,
235
- ...options,
236
- };
232
+ if (isSchemaTypedef(val)) {
233
+ val = {
234
+ ...val,
235
+ ...options,
236
+ };
237
+ } else {
238
+ val = attributesToMongoose({
239
+ type: 'Object',
240
+ attributes: val,
241
+ ...options,
242
+ });
243
+ }
244
+ definition[key] = val;
237
245
  }
238
246
  }
239
247
 
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAoBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAoChC;AAED,iEAuBC"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAoBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAkChC;AAED,iEAsBC"}