@bedrockio/model 0.2.21 → 0.2.22

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.
@@ -4,22 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.hasAccess = hasAccess;
7
- exports.hasReadAccess = hasReadAccess;
8
- exports.hasWriteAccess = hasWriteAccess;
9
7
  var _errors = require("./errors");
10
8
  var _warn = _interopRequireDefault(require("./warn"));
11
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
- function hasReadAccess(allowed, options) {
13
- return hasAccess('read', allowed, options);
14
- }
15
- function hasWriteAccess(allowed, options) {
16
- return hasAccess('write', allowed, options);
17
- }
18
-
19
10
  /**
20
11
  * @param {string|string[]} allowed
21
12
  */
22
- function hasAccess(type, allowed = 'all', options = {}) {
13
+ function hasAccess(allowed = 'all', options = {}) {
23
14
  if (allowed === 'all') {
24
15
  return true;
25
16
  } else if (allowed === 'none') {
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.INCLUDE_FIELD_SCHEMA = void 0;
7
7
  exports.applyInclude = applyInclude;
8
8
  exports.checkSelects = checkSelects;
9
- exports.getIncludes = getIncludes;
9
+ exports.getDocumentParams = getDocumentParams;
10
+ exports.getParams = getParams;
10
11
  var _mongoose = _interopRequireDefault(require("mongoose"));
11
12
  var _lodash = require("lodash");
12
13
  var _yada = _interopRequireDefault(require("@bedrockio/yada"));
@@ -36,7 +37,7 @@ function applyInclude(schema) {
36
37
  const {
37
38
  select,
38
39
  populate
39
- } = getQueryIncludes(this, filter.include);
40
+ } = getQueryParams(this, filter.include);
40
41
  this.select(select);
41
42
  this.populate(populate);
42
43
  delete filter.include;
@@ -78,7 +79,7 @@ function applyInclude(schema) {
78
79
  const {
79
80
  select,
80
81
  populate
81
- } = getDocumentIncludes(this, include);
82
+ } = getDocumentParams(this, include);
82
83
  this.$locals.select = select;
83
84
  this.$locals.populate = populate;
84
85
  }
@@ -103,7 +104,7 @@ function applyInclude(schema) {
103
104
  const {
104
105
  select,
105
106
  populate
106
- } = getDocumentIncludes(this, include);
107
+ } = getDocumentParams(this, include);
107
108
  this.$locals.select = select;
108
109
  await this.populate(populate);
109
110
  }
@@ -147,16 +148,32 @@ function checkSelects(doc, ret) {
147
148
  }
148
149
 
149
150
  // Exported for testing.
150
- function getIncludes(modelName, arg) {
151
+ function getParams(modelName, arg) {
151
152
  const paths = Array.isArray(arg) ? arg : [arg];
152
153
  const node = pathsToNode(paths, modelName);
153
154
  return nodeToPopulates(node);
154
155
  }
155
- function getQueryIncludes(query, arg) {
156
- return getIncludes(query.model.modelName, arg);
156
+
157
+ // Exported for testing.
158
+ function getDocumentParams(doc, arg) {
159
+ const params = getParams(doc.constructor.modelName, arg);
160
+ params.populate = params.populate.filter(p => {
161
+ return !isDocumentPopulated(doc, p);
162
+ });
163
+ return params;
164
+ }
165
+ function isDocumentPopulated(doc, params) {
166
+ if (doc.populated(params.path)) {
167
+ const sub = doc.get(params.path);
168
+ return params.populate.every(p => {
169
+ return isDocumentPopulated(sub, p);
170
+ });
171
+ } else {
172
+ return false;
173
+ }
157
174
  }
158
- function getDocumentIncludes(doc, arg) {
159
- return getIncludes(doc.constructor.modelName, arg);
175
+ function getQueryParams(query, arg) {
176
+ return getParams(query.model.modelName, arg);
160
177
  }
161
178
 
162
179
  // Note that:
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.serializeOptions = void 0;
7
7
  var _lodash = require("lodash");
8
8
  var _include = require("./include");
9
- var _access = require("./access");
10
9
  var _utils = require("./utils");
10
+ var _access = require("./access");
11
11
  const DISALLOWED_FIELDS = ['deleted', 'deletedRefs'];
12
12
  const serializeOptions = exports.serializeOptions = {
13
13
  getters: true,
@@ -63,7 +63,7 @@ function isAllowedField(key, field, options) {
63
63
  readAccess
64
64
  } = (0, _utils.getField)(field, key);
65
65
  try {
66
- return (0, _access.hasReadAccess)(readAccess, options);
66
+ return (0, _access.hasAccess)(readAccess, options);
67
67
  } catch {
68
68
  return false;
69
69
  }
@@ -406,7 +406,7 @@ function validateAccess(type, schema, allowed, options) {
406
406
  const document = options[(0, _lodash.lowerFirst)(modelName)] || options['document'];
407
407
  let isAllowed;
408
408
  try {
409
- isAllowed = (0, _access.hasAccess)(type, allowed, {
409
+ isAllowed = (0, _access.hasAccess)(allowed, {
410
410
  ...options,
411
411
  document
412
412
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/access.js CHANGED
@@ -1,18 +1,10 @@
1
1
  import { ImplementationError } from './errors';
2
2
  import warn from './warn';
3
3
 
4
- export function hasReadAccess(allowed, options) {
5
- return hasAccess('read', allowed, options);
6
- }
7
-
8
- export function hasWriteAccess(allowed, options) {
9
- return hasAccess('write', allowed, options);
10
- }
11
-
12
4
  /**
13
5
  * @param {string|string[]} allowed
14
6
  */
15
- export function hasAccess(type, allowed = 'all', options = {}) {
7
+ export function hasAccess(allowed = 'all', options = {}) {
16
8
  if (allowed === 'all') {
17
9
  return true;
18
10
  } else if (allowed === 'none') {
package/src/include.js CHANGED
@@ -31,7 +31,7 @@ export function applyInclude(schema) {
31
31
  schema.pre(/^find/, function (next) {
32
32
  const filter = this.getFilter();
33
33
  if (filter.include) {
34
- const { select, populate } = getQueryIncludes(this, filter.include);
34
+ const { select, populate } = getQueryParams(this, filter.include);
35
35
  this.select(select);
36
36
  this.populate(populate);
37
37
  delete filter.include;
@@ -70,7 +70,7 @@ export function applyInclude(schema) {
70
70
  this.assign(rest);
71
71
 
72
72
  if (include) {
73
- const { select, populate } = getDocumentIncludes(this, include);
73
+ const { select, populate } = getDocumentParams(this, include);
74
74
  this.$locals.select = select;
75
75
  this.$locals.populate = populate;
76
76
  }
@@ -91,7 +91,7 @@ export function applyInclude(schema) {
91
91
  // during serialization.
92
92
  schema.method('include', async function include(include) {
93
93
  if (include) {
94
- const { select, populate } = getDocumentIncludes(this, include);
94
+ const { select, populate } = getDocumentParams(this, include);
95
95
  this.$locals.select = select;
96
96
  await this.populate(populate);
97
97
  }
@@ -133,18 +133,34 @@ export function checkSelects(doc, ret) {
133
133
  }
134
134
 
135
135
  // Exported for testing.
136
- export function getIncludes(modelName, arg) {
136
+ export function getParams(modelName, arg) {
137
137
  const paths = Array.isArray(arg) ? arg : [arg];
138
138
  const node = pathsToNode(paths, modelName);
139
139
  return nodeToPopulates(node);
140
140
  }
141
141
 
142
- function getQueryIncludes(query, arg) {
143
- return getIncludes(query.model.modelName, arg);
142
+ // Exported for testing.
143
+ export function getDocumentParams(doc, arg) {
144
+ const params = getParams(doc.constructor.modelName, arg);
145
+ params.populate = params.populate.filter((p) => {
146
+ return !isDocumentPopulated(doc, p);
147
+ });
148
+ return params;
149
+ }
150
+
151
+ function isDocumentPopulated(doc, params) {
152
+ if (doc.populated(params.path)) {
153
+ const sub = doc.get(params.path);
154
+ return params.populate.every((p) => {
155
+ return isDocumentPopulated(sub, p);
156
+ });
157
+ } else {
158
+ return false;
159
+ }
144
160
  }
145
161
 
146
- function getDocumentIncludes(doc, arg) {
147
- return getIncludes(doc.constructor.modelName, arg);
162
+ function getQueryParams(query, arg) {
163
+ return getParams(query.model.modelName, arg);
148
164
  }
149
165
 
150
166
  // Note that:
@@ -1,8 +1,8 @@
1
1
  import { isPlainObject } from 'lodash';
2
2
 
3
3
  import { checkSelects } from './include';
4
- import { hasReadAccess } from './access';
5
4
  import { getField, getInnerField } from './utils';
5
+ import { hasAccess } from './access';
6
6
 
7
7
  const DISALLOWED_FIELDS = ['deleted', 'deletedRefs'];
8
8
 
@@ -60,7 +60,7 @@ function isAllowedField(key, field, options) {
60
60
  } else {
61
61
  const { readAccess } = getField(field, key);
62
62
  try {
63
- return hasReadAccess(readAccess, options);
63
+ return hasAccess(readAccess, options);
64
64
  } catch {
65
65
  return false;
66
66
  }
package/src/validation.js CHANGED
@@ -464,7 +464,7 @@ function validateAccess(type, schema, allowed, options) {
464
464
 
465
465
  let isAllowed;
466
466
  try {
467
- isAllowed = hasAccess(type, allowed, {
467
+ isAllowed = hasAccess(allowed, {
468
468
  ...options,
469
469
  document,
470
470
  });
package/types/access.d.ts CHANGED
@@ -1,7 +1,5 @@
1
- export function hasReadAccess(allowed: any, options: any): boolean;
2
- export function hasWriteAccess(allowed: any, options: any): boolean;
3
1
  /**
4
2
  * @param {string|string[]} allowed
5
3
  */
6
- export function hasAccess(type: any, allowed?: string | string[], options?: {}): boolean;
4
+ export function hasAccess(allowed?: string | string[], options?: {}): boolean;
7
5
  //# sourceMappingURL=access.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.js"],"names":[],"mappings":"AAGA,mEAEC;AAED,oEAEC;AAED;;GAEG;AACH,+CAFW,MAAM,GAAC,MAAM,EAAE,yBA2BzB"}
1
+ {"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.js"],"names":[],"mappings":"AAGA;;GAEG;AACH,oCAFW,MAAM,GAAC,MAAM,EAAE,yBA2BzB"}
@@ -1,6 +1,7 @@
1
1
  export function applyInclude(schema: any): void;
2
2
  export function checkSelects(doc: any, ret: any): void;
3
- export function getIncludes(modelName: any, arg: any): any;
3
+ export function getParams(modelName: any, arg: any): any;
4
+ export function getDocumentParams(doc: any, arg: any): any;
4
5
  export const INCLUDE_FIELD_SCHEMA: {
5
6
  setup: any;
6
7
  getFields: any;
@@ -1 +1 @@
1
- {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAuEC;AAMD,uDA4BC;AAGD,2DAIC;AAvHD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}
1
+ {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAuEC;AAMD,uDA4BC;AAGD,yDAIC;AAGD,2DAMC;AAhID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}