@bedrockio/model 0.18.0 → 0.18.1

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.
@@ -0,0 +1,15 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(yarn test:*)",
5
+ "Bash(yarn list:*)",
6
+ "WebSearch",
7
+ "WebFetch(domain:www.npmjs.com)",
8
+ "WebFetch(domain:gist.github.com)",
9
+ "WebFetch(domain:mongoosejs.com)",
10
+ "WebFetch(domain:github.com)"
11
+ ],
12
+ "deny": [],
13
+ "ask": []
14
+ }
15
+ }
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.18.1
2
+
3
+ - Added `export` for dumping documents and to support reload.
4
+ - Fix for `include` not working on nested virtuals.
5
+ - Fixes for `reload`.
6
+
1
7
  ## 0.18.0
2
8
 
3
9
  - Added `reload`.
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.applyExport = applyExport;
7
+ var _lodash = require("lodash");
8
+ function applyExport(schema) {
9
+ schema.method('export', function () {
10
+ const result = {};
11
+ this.constructor.schema.eachPath(schemaPath => {
12
+ const value = this.get(schemaPath);
13
+ if (value !== undefined) {
14
+ (0, _lodash.set)(result, schemaPath, value);
15
+ }
16
+ });
17
+ return result;
18
+ });
19
+ }
@@ -162,16 +162,21 @@ function getDocumentParams(doc, arg, options = {}) {
162
162
  const params = getParams(doc.constructor.modelName, arg);
163
163
  if (!options.force) {
164
164
  params.populate = params.populate.filter(p => {
165
- return !isDocumentPopulated(doc, p);
165
+ return !isPopulated(doc, p);
166
166
  });
167
167
  }
168
168
  return params;
169
169
  }
170
- function isDocumentPopulated(doc, params) {
171
- if (doc.populated(params.path)) {
172
- const sub = doc.get(params.path);
170
+ function isPopulated(arg, params) {
171
+ if (Array.isArray(arg)) {
172
+ return arg.every(el => {
173
+ return isPopulated(el, params);
174
+ });
175
+ }
176
+ if (arg.populated(params.path)) {
177
+ const sub = arg.get(params.path);
173
178
  return params.populate.every(p => {
174
- return isDocumentPopulated(sub, p);
179
+ return isPopulated(sub, p);
175
180
  });
176
181
  } else {
177
182
  return false;
@@ -12,19 +12,53 @@ function applyReload(schema) {
12
12
  const paths = getPopulatedPaths(this);
13
13
  const doc = await this.constructor.findById(this.id).include(paths);
14
14
  if (!doc) {
15
- throw new Error('Document deleted');
15
+ throw new Error('Document does not exist');
16
+ }
17
+ this.overwrite(doc.export());
18
+
19
+ // Include on the query above will not work
20
+ // for virtuals so handle separately here.
21
+ for (const path of getVirtualReferencePaths(doc)) {
22
+ await doc.include(path);
23
+ this.set(path, doc[path]);
24
+ }
25
+
26
+ // All data reloaded so mark as unmodified.
27
+ for (const path of this.modifiedPaths()) {
28
+ this.unmarkModified(path);
16
29
  }
17
- this.overwrite(doc);
18
30
  });
19
31
  }
20
- function getPopulatedPaths(doc) {
21
- return getReferencePaths(doc.constructor.schema).filter(path => {
22
- const value = doc.get(path);
23
- return value && !_mongoose.default.isObjectIdOrHexString(value);
32
+ function getPopulatedPaths(doc, base = []) {
33
+ const schema = doc.constructor.schema;
34
+ return getReferencePaths(schema).filter(name => {
35
+ return doc.populated(name);
36
+ }).flatMap(name => {
37
+ const path = [...base, name];
38
+ const value = doc.get(name);
39
+ const inner = Array.isArray(value) ? value[0] : value;
40
+ return [path.join('.'), ...getPopulatedPaths(inner, path)];
24
41
  });
25
42
  }
26
43
  function getReferencePaths(schema) {
44
+ return [...getRealReferencePaths(schema), ...getVirtualReferencePaths(schema)];
45
+ }
46
+ function getRealReferencePaths(schema) {
27
47
  return (0, _utils.getSchemaPaths)(schema).filter(path => {
28
48
  return (0, _utils.isReferenceField)(schema, path);
29
49
  });
50
+ }
51
+ function getVirtualReferencePaths(arg) {
52
+ const schema = resolveSchema(arg);
53
+ return Object.keys(schema.virtuals).filter(key => {
54
+ return schema.virtuals[key].options?.ref;
55
+ });
56
+ }
57
+ function resolveSchema(arg) {
58
+ if (arg instanceof _mongoose.default.Document) {
59
+ // @ts-ignore
60
+ return arg.constructor.schema;
61
+ } else if (arg instanceof _mongoose.default.Schema) {
62
+ return arg;
63
+ }
30
64
  }
@@ -12,6 +12,7 @@ var _cache = require("./cache");
12
12
  var _clone = require("./clone");
13
13
  var _deleteHooks = require("./delete-hooks");
14
14
  var _disallowed = require("./disallowed");
15
+ var _export = require("./export");
15
16
  var _hydrate = require("./hydrate");
16
17
  var _include = require("./include");
17
18
  var _reload = require("./reload");
@@ -60,6 +61,7 @@ function createSchema(definition, options = {}) {
60
61
  (0, _cache.applyCache)(schema, definition);
61
62
  (0, _clone.applyClone)(schema);
62
63
  (0, _reload.applyReload)(schema);
64
+ (0, _export.applyExport)(schema);
63
65
  (0, _disallowed.applyDisallowed)(schema);
64
66
  (0, _include.applyInclude)(schema);
65
67
  (0, _hydrate.applyHydrate)(schema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/export.js ADDED
@@ -0,0 +1,14 @@
1
+ import { set } from 'lodash';
2
+
3
+ export function applyExport(schema) {
4
+ schema.method('export', function () {
5
+ const result = {};
6
+ this.constructor.schema.eachPath((schemaPath) => {
7
+ const value = this.get(schemaPath);
8
+ if (value !== undefined) {
9
+ set(result, schemaPath, value);
10
+ }
11
+ });
12
+ return result;
13
+ });
14
+ }
package/src/include.js CHANGED
@@ -146,18 +146,23 @@ export function getDocumentParams(doc, arg, options = {}) {
146
146
 
147
147
  if (!options.force) {
148
148
  params.populate = params.populate.filter((p) => {
149
- return !isDocumentPopulated(doc, p);
149
+ return !isPopulated(doc, p);
150
150
  });
151
151
  }
152
152
 
153
153
  return params;
154
154
  }
155
155
 
156
- function isDocumentPopulated(doc, params) {
157
- if (doc.populated(params.path)) {
158
- const sub = doc.get(params.path);
156
+ function isPopulated(arg, params) {
157
+ if (Array.isArray(arg)) {
158
+ return arg.every((el) => {
159
+ return isPopulated(el, params);
160
+ });
161
+ }
162
+ if (arg.populated(params.path)) {
163
+ const sub = arg.get(params.path);
159
164
  return params.populate.every((p) => {
160
- return isDocumentPopulated(sub, p);
165
+ return isPopulated(sub, p);
161
166
  });
162
167
  } else {
163
168
  return false;
package/src/reload.js CHANGED
@@ -6,25 +6,70 @@ import { isReferenceField } from './utils';
6
6
  export function applyReload(schema) {
7
7
  schema.method('reload', async function reload() {
8
8
  const paths = getPopulatedPaths(this);
9
+
9
10
  const doc = await this.constructor.findById(this.id).include(paths);
10
11
 
11
12
  if (!doc) {
12
- throw new Error('Document deleted');
13
+ throw new Error('Document does not exist');
14
+ }
15
+
16
+ this.overwrite(doc.export());
17
+
18
+ // Include on the query above will not work
19
+ // for virtuals so handle separately here.
20
+ for (const path of getVirtualReferencePaths(doc)) {
21
+ await doc.include(path);
22
+ this.set(path, doc[path]);
13
23
  }
14
24
 
15
- this.overwrite(doc);
25
+ // All data reloaded so mark as unmodified.
26
+ for (const path of this.modifiedPaths()) {
27
+ this.unmarkModified(path);
28
+ }
16
29
  });
17
30
  }
18
31
 
19
- function getPopulatedPaths(doc) {
20
- return getReferencePaths(doc.constructor.schema).filter((path) => {
21
- const value = doc.get(path);
22
- return value && !mongoose.isObjectIdOrHexString(value);
23
- });
32
+ function getPopulatedPaths(doc, base = []) {
33
+ const schema = doc.constructor.schema;
34
+ return getReferencePaths(schema)
35
+ .filter((name) => {
36
+ return doc.populated(name);
37
+ })
38
+ .flatMap((name) => {
39
+ const path = [...base, name];
40
+
41
+ const value = doc.get(name);
42
+ const inner = Array.isArray(value) ? value[0] : value;
43
+
44
+ return [path.join('.'), ...getPopulatedPaths(inner, path)];
45
+ });
24
46
  }
25
47
 
26
48
  function getReferencePaths(schema) {
49
+ return [
50
+ ...getRealReferencePaths(schema),
51
+ ...getVirtualReferencePaths(schema),
52
+ ];
53
+ }
54
+
55
+ function getRealReferencePaths(schema) {
27
56
  return getSchemaPaths(schema).filter((path) => {
28
57
  return isReferenceField(schema, path);
29
58
  });
30
59
  }
60
+
61
+ function getVirtualReferencePaths(arg) {
62
+ const schema = resolveSchema(arg);
63
+ return Object.keys(schema.virtuals).filter((key) => {
64
+ return schema.virtuals[key].options?.ref;
65
+ });
66
+ }
67
+
68
+ function resolveSchema(arg) {
69
+ if (arg instanceof mongoose.Document) {
70
+ // @ts-ignore
71
+ return arg.constructor.schema;
72
+ } else if (arg instanceof mongoose.Schema) {
73
+ return arg;
74
+ }
75
+ }
package/src/schema.js CHANGED
@@ -6,6 +6,7 @@ import { applyCache } from './cache';
6
6
  import { applyClone } from './clone';
7
7
  import { applyDeleteHooks } from './delete-hooks';
8
8
  import { applyDisallowed } from './disallowed';
9
+ import { applyExport } from './export';
9
10
  import { applyHydrate } from './hydrate';
10
11
  import { applyInclude } from './include';
11
12
  import { applyReload } from './reload';
@@ -63,6 +64,7 @@ export function createSchema(definition, options = {}) {
63
64
  applyCache(schema, definition);
64
65
  applyClone(schema);
65
66
  applyReload(schema);
67
+ applyExport(schema);
66
68
  applyDisallowed(schema);
67
69
  applyInclude(schema);
68
70
  applyHydrate(schema);
@@ -0,0 +1,2 @@
1
+ export function applyExport(schema: any): void;
2
+ //# sourceMappingURL=export.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../src/export.js"],"names":[],"mappings":"AAEA,+CAWC"}
@@ -1 +1 @@
1
- {"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../src/reload.js"],"names":[],"mappings":"AAKA,+CAWC"}
1
+ {"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../src/reload.js"],"names":[],"mappings":"AAKA,+CAwBC"}
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAwBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;YA+CX,CAAC;WAAa,CAAC;mBAEnC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;SA4HmB,CAAC;gBACL,CAAC;SAAW,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAlI9B;AAED,iEAsBC;qBAhGoB,UAAU"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAyBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;YA+CpB,CAAC;WAAa,CAAC;mBAAqB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;SA4Hf,CAAC;gBAA2B,CAAC;SAGhE,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAjIC;AAED,iEAsBC;qBAlGoB,UAAU"}