@onehat/data 1.22.0 → 1.22.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.22.0",
3
+ "version": "1.22.1",
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
  // ____
@@ -438,7 +431,7 @@ class AjaxRepository extends Repository {
438
431
 
439
432
  const
440
433
  repository = this,
441
- url = this._getModel() + '/' + this.api.get,
434
+ url = this.getModel() + '/' + this.api.get,
442
435
  data = _.merge({}, this._baseParams, this._params);
443
436
 
444
437
  return this._send(this.methods.get, url, data)
@@ -529,7 +522,7 @@ class AjaxRepository extends Repository {
529
522
  console.log('reloadEntity ' + entity.id, params);
530
523
  }
531
524
 
532
- const url = this._getModel() + '/' + this.api.get;
525
+ const url = entity.getModel() + '/' + this.api.get;
533
526
 
534
527
  return this._send(this.methods.get, url, params)
535
528
  .then(result => {
@@ -615,7 +608,7 @@ class AjaxRepository extends Repository {
615
608
 
616
609
  const
617
610
  method = this.methods.add,
618
- url = this._getModel() + '/' + this.api.add,
611
+ url = entity.getModel() + '/' + this.api.add,
619
612
  data = entity.getSubmitValues();
620
613
 
621
614
  return this._send(method, url, data)
@@ -669,7 +662,7 @@ class AjaxRepository extends Repository {
669
662
 
670
663
  const
671
664
  method = this.methods.add,
672
- url = this._getModel() + '/' + this.api.batchAdd,
665
+ url = this.getModel() + '/' + this.api.batchAdd,
673
666
  data = {
674
667
  entities: _.map(entities, entity => {
675
668
  const values = entity.submitValues;
@@ -734,7 +727,7 @@ class AjaxRepository extends Repository {
734
727
 
735
728
  const
736
729
  method = this.methods.edit,
737
- url = this._getModel() + '/' + this.api.edit,
730
+ url = entity.getModel() + '/' + this.api.edit,
738
731
  data = entity.getSubmitValues();
739
732
 
740
733
  return this._send(method, url, data)
@@ -788,7 +781,7 @@ class AjaxRepository extends Repository {
788
781
 
789
782
  const
790
783
  method = this.methods.edit,
791
- url = this._getModel() + '/' + this.api.batchEdit,
784
+ url = this.getModel() + '/' + this.api.batchEdit,
792
785
  data = {
793
786
  entities: _.map(entities, entity => {
794
787
  const values = entity.submitValues;
@@ -857,7 +850,7 @@ class AjaxRepository extends Repository {
857
850
 
858
851
  const
859
852
  method = this.methods.delete,
860
- url = this._getModel() + '/' + this.api.delete,
853
+ url = entity.getModel() + '/' + this.api.delete,
861
854
  data = {
862
855
  id: entity.id,
863
856
  };
@@ -917,7 +910,7 @@ class AjaxRepository extends Repository {
917
910
 
918
911
  const
919
912
  method = this.methods.delete,
920
- url = this._getModel() + '/' + this.api.batchDelete,
913
+ url = this.getModel() + '/' + this.api.batchDelete,
921
914
  ids = _.map(entities, (entity) => {
922
915
  entity.isSaving = true;
923
916
  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
  */