@onehat/data 1.22.0 → 1.22.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.22.0",
3
+ "version": "1.22.2",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -113,7 +113,7 @@ class Entity extends EventEmitter {
113
113
  /**
114
114
  * @member {boolean} isTree - Whether this Entity is a TreeNode
115
115
  */
116
- this.isTree = schema.model.isTree || false;
116
+ this.isTree = schema.repository.type === 'tree' || false;
117
117
 
118
118
  if (this.isTree && !schema.model.parentIdProperty) {
119
119
  throw new Error('parentIdProperty cannot be empty for a TreeNode');
@@ -1768,6 +1768,18 @@ class Entity extends EventEmitter {
1768
1768
  return parentIds.reverse().join('/');
1769
1769
  }
1770
1770
 
1771
+ /**
1772
+ * Gets the model that this entity uses
1773
+ * @return {string} model
1774
+ */
1775
+ getModel = () => {
1776
+ if (this.isTree && this.nodeType) {
1777
+ // Trees can have entities of different models
1778
+ return this.nodeType;
1779
+ }
1780
+ return this.repository.getModel();
1781
+ }
1782
+
1771
1783
  /**
1772
1784
  * Moves this TreeNode to another parentId.
1773
1785
  */
@@ -1777,7 +1789,7 @@ class Entity extends EventEmitter {
1777
1789
  throw Error('this.moveTreeNode is no longer valid. TreeNode has been destroyed.');
1778
1790
  }
1779
1791
  if (!this.repository?.moveTreeNode) {
1780
- throw Error('repository.moveTreeNode is not defined.');
1792
+ throw Error('repository.moveTreeNode is not defined.');
1781
1793
  }
1782
1794
 
1783
1795
  return this.repository.moveTreeNode(this, newParentId);
@@ -1789,7 +1801,7 @@ class Entity extends EventEmitter {
1789
1801
  */
1790
1802
  ensureTree = () => {
1791
1803
  if (!this.isTree) {
1792
- this.throwError('This Entity is not a tree!');
1804
+ throw Error('This Entity is not a tree!');
1793
1805
  return false;
1794
1806
  }
1795
1807
  return true;
@@ -1,6 +1,6 @@
1
1
  /** @module Repository */
2
2
 
3
- import Repository from './Repository.js';
3
+ import Repository from './Repository.js'; // so we can use static methods
4
4
  import ReaderTypes from '../Reader/index.js';
5
5
  import WriterTypes from '../Writer/index.js';
6
6
  import axios from 'axios';
@@ -201,13 +201,6 @@ class AjaxRepository extends Repository {
201
201
  this._onChangeSorters();
202
202
  }
203
203
  }
204
-
205
- _getModel(entity) {
206
- if (!this.isUnique) {
207
- return this.name;
208
- }
209
- return this.name.match(/^([^-]*)-(.*)/)[1]; // converts 'ModelName-22f9915c-79f5-4e86-a25b-9446c7b85b63' to 'ModelName'
210
- }
211
204
 
212
205
 
213
206
  // ____
@@ -223,18 +216,23 @@ class AjaxRepository extends Repository {
223
216
  * @param {boolean} isBaseParam - Whether param is a base param (to be sent on every request).
224
217
  */
225
218
  setParam(name, value, isBaseParam = false) {
226
- const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
219
+ const
220
+ re = /^([^\[]+)\[([^\]]+)\](.*)$/,
227
221
  matches = name.match(re),
228
222
  paramsToChange = isBaseParam ? this._baseParams : this._params;
229
223
 
230
224
  if (matches) { // name has array notation like 'conditions[username]'
231
- const first = matches[1],
225
+ const
226
+ first = matches[1],
232
227
  second = matches[2];
233
228
  if (paramsToChange && !paramsToChange.hasOwnProperty(first)) {
234
229
  paramsToChange[first] = {};
235
230
  }
236
231
  if (_.isNil(value) && paramsToChange[first] && paramsToChange[first].hasOwnProperty(second)) {
237
232
  delete paramsToChange[first][second];
233
+ if (_.isEmpty(paramsToChange[first])) {
234
+ delete paramsToChange[first];
235
+ }
238
236
  return;
239
237
  }
240
238
  paramsToChange[first][second] = value;
@@ -254,7 +252,8 @@ class AjaxRepository extends Repository {
254
252
  * @param {boolean} isBaseParam - Whether param is a base param (to be sent on every request).
255
253
  */
256
254
  setValuelessParam(name, isBaseParam = false) {
257
- const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
255
+ const
256
+ re = /^([^\[]+)\[([^\]]+)\](.*)$/,
258
257
  matches = name.match(re),
259
258
  paramsToChange = isBaseParam ? this._baseParams : this._params;
260
259
 
@@ -291,7 +290,22 @@ class AjaxRepository extends Repository {
291
290
  * @param {string} name - Param name
292
291
  */
293
292
  hasBaseParam(name) {
294
- return this._baseParams.hasOwnProperty(name);
293
+ if (this._baseParams.hasOwnProperty(name)) {
294
+ return true;
295
+ }
296
+
297
+ // Check for array notation
298
+ const keys = name.split(/[\[\].]+/).filter(Boolean);
299
+ let current = this._baseParams,
300
+ key;
301
+
302
+ for(key of keys) {
303
+ if (!current || !current.hasOwnProperty(key)) {
304
+ return false;
305
+ }
306
+ current = current[key];
307
+ }
308
+ return true;
295
309
  }
296
310
 
297
311
  /**
@@ -438,7 +452,7 @@ class AjaxRepository extends Repository {
438
452
 
439
453
  const
440
454
  repository = this,
441
- url = this._getModel() + '/' + this.api.get,
455
+ url = this.getModel() + '/' + this.api.get,
442
456
  data = _.merge({}, this._baseParams, this._params);
443
457
 
444
458
  return this._send(this.methods.get, url, data)
@@ -529,7 +543,7 @@ class AjaxRepository extends Repository {
529
543
  console.log('reloadEntity ' + entity.id, params);
530
544
  }
531
545
 
532
- const url = this._getModel() + '/' + this.api.get;
546
+ const url = entity.getModel() + '/' + this.api.get;
533
547
 
534
548
  return this._send(this.methods.get, url, params)
535
549
  .then(result => {
@@ -615,7 +629,7 @@ class AjaxRepository extends Repository {
615
629
 
616
630
  const
617
631
  method = this.methods.add,
618
- url = this._getModel() + '/' + this.api.add,
632
+ url = entity.getModel() + '/' + this.api.add,
619
633
  data = entity.getSubmitValues();
620
634
 
621
635
  return this._send(method, url, data)
@@ -669,7 +683,7 @@ class AjaxRepository extends Repository {
669
683
 
670
684
  const
671
685
  method = this.methods.add,
672
- url = this._getModel() + '/' + this.api.batchAdd,
686
+ url = this.getModel() + '/' + this.api.batchAdd,
673
687
  data = {
674
688
  entities: _.map(entities, entity => {
675
689
  const values = entity.submitValues;
@@ -734,7 +748,7 @@ class AjaxRepository extends Repository {
734
748
 
735
749
  const
736
750
  method = this.methods.edit,
737
- url = this._getModel() + '/' + this.api.edit,
751
+ url = entity.getModel() + '/' + this.api.edit,
738
752
  data = entity.getSubmitValues();
739
753
 
740
754
  return this._send(method, url, data)
@@ -788,7 +802,7 @@ class AjaxRepository extends Repository {
788
802
 
789
803
  const
790
804
  method = this.methods.edit,
791
- url = this._getModel() + '/' + this.api.batchEdit,
805
+ url = this.getModel() + '/' + this.api.batchEdit,
792
806
  data = {
793
807
  entities: _.map(entities, entity => {
794
808
  const values = entity.submitValues;
@@ -857,7 +871,7 @@ class AjaxRepository extends Repository {
857
871
 
858
872
  const
859
873
  method = this.methods.delete,
860
- url = this._getModel() + '/' + this.api.delete,
874
+ url = entity.getModel() + '/' + this.api.delete,
861
875
  data = {
862
876
  id: entity.id,
863
877
  };
@@ -917,7 +931,7 @@ class AjaxRepository extends Repository {
917
931
 
918
932
  const
919
933
  method = this.methods.delete,
920
- url = this._getModel() + '/' + this.api.batchDelete,
934
+ url = this.getModel() + '/' + this.api.batchDelete,
921
935
  ids = _.map(entities, (entity) => {
922
936
  entity.isSaving = true;
923
937
  return entity.id;
@@ -298,7 +298,7 @@ class OneBuildRepository extends AjaxRepository {
298
298
  }
299
299
 
300
300
  const data = {
301
- url: this._getModel() + '/' + this.api.reorder,
301
+ url: this.getModel() + '/' + this.api.reorder,
302
302
  data: qs.stringify({
303
303
  ids,
304
304
  dropPosition,
@@ -335,7 +335,7 @@ class OneBuildRepository extends AjaxRepository {
335
335
  this.markLoading();
336
336
 
337
337
  const
338
- url = this._getModel() + '/' + this.api.duplicate,
338
+ url = entity.getModel() + '/' + this.api.duplicate,
339
339
  id = entity.id,
340
340
  result = await this._send('POST', url, { id });
341
341
 
@@ -407,7 +407,7 @@ class OneBuildRepository extends AjaxRepository {
407
407
  params['conditions[' + idPropertyName + ']'] = id;
408
408
 
409
409
  const
410
- url = this._getModel() + '/' + this.api.get,
410
+ url = this.getModel() + '/' + this.api.get,
411
411
  data = _.merge(params, this._baseParams);
412
412
 
413
413
  if (this.debugMode) {
@@ -464,7 +464,7 @@ class OneBuildRepository extends AjaxRepository {
464
464
  console.log('getLastModifiedDate');
465
465
  }
466
466
 
467
- const url = this._getModel() + '/' + this.api.getLastModifiedDate;
467
+ const url = this.getModel() + '/' + this.api.getLastModifiedDate;
468
468
 
469
469
  return this._send(this.methods.get, url, this._baseParams)
470
470
  .then(result => {
@@ -396,6 +396,12 @@ export default class Repository extends EventEmitter {
396
396
  }
397
397
  }
398
398
 
399
+ getModel() {
400
+ if (!this.isUnique) {
401
+ return this.name;
402
+ }
403
+ return this.name.match(/^([^-]*)-(.*)/)[1]; // converts 'ModelName-22f9915c-79f5-4e86-a25b-9446c7b85b63' to 'ModelName'
404
+ }
399
405
 
400
406
  // __ __
401
407
  // / / ____ ____ _____/ /
@@ -1,5 +1,6 @@
1
1
  /** @module Repository */
2
2
 
3
+ import Repository from './Repository.js'; // so we can use static methods
3
4
  import OneBuildRepository from './OneBuild.js';
4
5
  import _ from 'lodash';
5
6
 
@@ -17,7 +18,7 @@ class TreeRepository extends OneBuildRepository {
17
18
  const defaults = {
18
19
 
19
20
  isTree: true,
20
- rootNodeType: this._getModel(), // e.g. 'Fleets'
21
+ rootNodeType: this.getModel(), // e.g. 'Fleets'
21
22
 
22
23
  api: {
23
24
  getNodes: 'getNodes',
@@ -25,9 +26,9 @@ class TreeRepository extends OneBuildRepository {
25
26
  searchNodes: 'searchNodes',
26
27
  },
27
28
 
28
- // TODO: modify all tree methods to handle multiple node types
29
- // SOME are updated, but many are not.
30
- // I'll need to modify all generated models to use TreeRepository instead of OneBuild with isTree: true.
29
+ editableNodeTypes: [
30
+ this.getModel(),
31
+ ],
31
32
 
32
33
  };
33
34
  _.merge(this, defaults, config);
@@ -156,7 +157,7 @@ class TreeRepository extends OneBuildRepository {
156
157
 
157
158
  const
158
159
  data = _.merge({ depth, nodeId: treeNode.id, }, this._baseParams, this._params),
159
- url = this.getModelFromTreeNode(treeNode) + '/' + this.api.getNodes;
160
+ url = treeNode.getModel() + '/' + this.api.getNodes;
160
161
 
161
162
  if (this.debugMode) {
162
163
  console.log('loadNode', data);
@@ -7,6 +7,7 @@ import MemoryRepository from './Memory.js';
7
7
  import NullRepository from './Null.js';
8
8
  import OneBuildRepository from './OneBuild.js';
9
9
  import RestRepository from './Rest.js';
10
+ import TreeRepository from './Tree.js';
10
11
 
11
12
  const CoreRepositoryTypes = {
12
13
  [AjaxRepository.type]: AjaxRepository,
@@ -16,6 +17,7 @@ const CoreRepositoryTypes = {
16
17
  [NullRepository.type]: NullRepository,
17
18
  [OneBuildRepository.type]: OneBuildRepository,
18
19
  [RestRepository.type]: RestRepository,
20
+ [TreeRepository.type]: TreeRepository,
19
21
  };
20
22
 
21
23
  export default CoreRepositoryTypes;
@@ -89,11 +89,6 @@ export default class Schema extends EventEmitter {
89
89
  */
90
90
  hasChildrenProperty: null,
91
91
 
92
- /**
93
- * @member {boolean} isTree - Whether this model has hierarchical tree data
94
- */
95
- isTree: false,
96
-
97
92
  /**
98
93
  * @member {boolean} isAdjacencyList - Whether this tree is an Adjacency List
99
94
  */