@onehat/data 1.21.20 → 1.22.0

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.21.20",
3
+ "version": "1.22.0",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1625,16 +1625,16 @@ class Entity extends EventEmitter {
1625
1625
  /**
1626
1626
  * Loads the children of this TreeNode from repository.
1627
1627
  */
1628
- loadChildren = async (depth) => {
1628
+ loadChildren = async (depth = 1) => {
1629
1629
  this.ensureTree();
1630
1630
  if (this.isDestroyed) {
1631
1631
  throw Error('this.loadChildren is no longer valid. TreeNode has been destroyed.');
1632
1632
  }
1633
- if (!this.repository?.loadChildNodes) {
1634
- throw Error('repository.loadChildNodes is not defined.');
1633
+ if (!this.repository?.loadNode) {
1634
+ throw Error('repository.loadNode is not defined.');
1635
1635
  }
1636
1636
 
1637
- const children = await this.repository.loadChildNodes(this, depth);
1637
+ const children = await this.repository.loadNode(this, depth);
1638
1638
  this.areChildrenLoaded = true;
1639
1639
  return children;
1640
1640
  }
@@ -1642,8 +1642,8 @@ class Entity extends EventEmitter {
1642
1642
  /**
1643
1643
  * Alias for loadChildren
1644
1644
  */
1645
- reloadChildren = () => { // alias
1646
- return this.loadChildren();
1645
+ reloadChildren = (depth = 1) => { // alias
1646
+ return this.loadChildren(depth);
1647
1647
  }
1648
1648
 
1649
1649
  /**
@@ -201,6 +201,13 @@ 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
+ }
204
211
 
205
212
 
206
213
  // ____
@@ -429,10 +436,12 @@ class AjaxRepository extends Repository {
429
436
  this.setParams(params);
430
437
  }
431
438
 
432
- const repository = this;
433
- const data = _.merge({}, this._baseParams, this._params);
439
+ const
440
+ repository = this,
441
+ url = this._getModel() + '/' + this.api.get,
442
+ data = _.merge({}, this._baseParams, this._params);
434
443
 
435
- return this._send(this.methods.get, this.api.get, data)
444
+ return this._send(this.methods.get, url, data)
436
445
  .then(result => {
437
446
  if (this.debugMode) {
438
447
  console.log('Response for ' + this.name, result);
@@ -519,8 +528,10 @@ class AjaxRepository extends Repository {
519
528
  if (this.debugMode) {
520
529
  console.log('reloadEntity ' + entity.id, params);
521
530
  }
531
+
532
+ const url = this._getModel() + '/' + this.api.get;
522
533
 
523
- return this._send(this.methods.get, this.api.get, params)
534
+ return this._send(this.methods.get, url, params)
524
535
  .then(result => {
525
536
  if (this.debugMode) {
526
537
  console.log('reloadEntity response ' + entity.id, result);
@@ -604,7 +615,7 @@ class AjaxRepository extends Repository {
604
615
 
605
616
  const
606
617
  method = this.methods.add,
607
- url = this.api.add,
618
+ url = this._getModel() + '/' + this.api.add,
608
619
  data = entity.getSubmitValues();
609
620
 
610
621
  return this._send(method, url, data)
@@ -658,7 +669,7 @@ class AjaxRepository extends Repository {
658
669
 
659
670
  const
660
671
  method = this.methods.add,
661
- url = this.api.batchAdd,
672
+ url = this._getModel() + '/' + this.api.batchAdd,
662
673
  data = {
663
674
  entities: _.map(entities, entity => {
664
675
  const values = entity.submitValues;
@@ -723,7 +734,7 @@ class AjaxRepository extends Repository {
723
734
 
724
735
  const
725
736
  method = this.methods.edit,
726
- url = this.api.edit,
737
+ url = this._getModel() + '/' + this.api.edit,
727
738
  data = entity.getSubmitValues();
728
739
 
729
740
  return this._send(method, url, data)
@@ -777,7 +788,7 @@ class AjaxRepository extends Repository {
777
788
 
778
789
  const
779
790
  method = this.methods.edit,
780
- url = this.api.batchEdit,
791
+ url = this._getModel() + '/' + this.api.batchEdit,
781
792
  data = {
782
793
  entities: _.map(entities, entity => {
783
794
  const values = entity.submitValues;
@@ -846,7 +857,7 @@ class AjaxRepository extends Repository {
846
857
 
847
858
  const
848
859
  method = this.methods.delete,
849
- url = this.api.delete,
860
+ url = this._getModel() + '/' + this.api.delete,
850
861
  data = {
851
862
  id: entity.id,
852
863
  };
@@ -906,7 +917,7 @@ class AjaxRepository extends Repository {
906
917
 
907
918
  const
908
919
  method = this.methods.delete,
909
- url = this.api.batchDelete,
920
+ url = this._getModel() + '/' + this.api.batchDelete,
910
921
  ids = _.map(entities, (entity) => {
911
922
  entity.isSaving = true;
912
923
  return entity.id;
@@ -1,6 +1,5 @@
1
1
  /** @module Repository */
2
2
 
3
- import Repository from './Repository.js';
4
3
  import AjaxRepository from './Ajax.js';
5
4
  import qs from 'qs';
6
5
  import _ from 'lodash';
@@ -11,9 +10,11 @@ const nonConditionFilters = [
11
10
  'fields',
12
11
  'distinct',
13
12
  'leftJoinWith',
13
+ 'innerJoin',
14
14
  'join',
15
15
  'where',
16
16
  'matching',
17
+ 'groupBy',
17
18
  'contain',
18
19
  'order',
19
20
  'limit',
@@ -30,21 +31,22 @@ class OneBuildRepository extends AjaxRepository {
30
31
  constructor(config = {}) {
31
32
  super(...arguments);
32
33
 
33
- const model = this._getModel();
34
34
  const defaults = {
35
35
 
36
36
  isAutoLoad: false,
37
37
  isAutoSave: false,
38
38
 
39
39
  api: {
40
- get: model + '/get',
41
- add: model + '/add',
42
- edit: model + '/edit',
43
- delete: model + '/delete',
44
- batchAdd: model + '/batchAdd',
45
- batchEdit: model + '/batchEdit',
46
- batchDelete: model + '/batchDelete',
47
- getLastModifiedDate: model + '/getLastModifiedDate',
40
+ get: 'get',
41
+ add: 'add',
42
+ edit: 'edit',
43
+ delete: 'delete',
44
+ batchAdd: 'batchAdd',
45
+ batchEdit: 'batchEdit',
46
+ batchDelete: 'batchDelete',
47
+ reorder: 'reorder',
48
+ duplicate: 'duplicate',
49
+ getLastModifiedDate: 'getLastModifiedDate',
48
50
  },
49
51
 
50
52
  methods: {
@@ -83,7 +85,6 @@ class OneBuildRepository extends AjaxRepository {
83
85
 
84
86
  this.registerEvents([
85
87
  'logout',
86
- 'loadRootNodes',
87
88
  ]);
88
89
 
89
90
  await super.initialize();
@@ -149,13 +150,6 @@ class OneBuildRepository extends AjaxRepository {
149
150
  });
150
151
  }
151
152
 
152
- _getModel() {
153
- if (!this.isUnique) {
154
- return this.name;
155
- }
156
- return this.name.match(/^([^-]*)-(.*)/)[1]; // converts 'ModelName-22f9915c-79f5-4e86-a25b-9446c7b85b63' to 'ModelName'
157
- }
158
-
159
153
  /**
160
154
  * Helper for reloadEntity.
161
155
  * @private
@@ -304,7 +298,7 @@ class OneBuildRepository extends AjaxRepository {
304
298
  }
305
299
 
306
300
  const data = {
307
- url: this._getModel() + '/reorder',
301
+ url: this._getModel() + '/' + this.api.reorder,
308
302
  data: qs.stringify({
309
303
  ids,
310
304
  dropPosition,
@@ -341,9 +335,9 @@ class OneBuildRepository extends AjaxRepository {
341
335
  this.markLoading();
342
336
 
343
337
  const
344
- Model = this.getSchema().name,
338
+ url = this._getModel() + '/' + this.api.duplicate,
345
339
  id = entity.id,
346
- result = await this._send('POST', Model + '/duplicate', { id });
340
+ result = await this._send('POST', url, { id });
347
341
 
348
342
  if (!result) {
349
343
  this.markLoading(false);
@@ -412,13 +406,15 @@ class OneBuildRepository extends AjaxRepository {
412
406
  const params = {};
413
407
  params['conditions[' + idPropertyName + ']'] = id;
414
408
 
415
- const data = _.merge(params, this._baseParams);
409
+ const
410
+ url = this._getModel() + '/' + this.api.get,
411
+ data = _.merge(params, this._baseParams);
416
412
 
417
413
  if (this.debugMode) {
418
414
  console.log('getSingleEntityFromServer', data);
419
415
  }
420
416
 
421
- return this._send(this.methods.get, this.api.get, data)
417
+ return this._send(this.methods.get, url, data)
422
418
  .then(result => {
423
419
  if (this.debugMode) {
424
420
  console.log('Response for getSingleEntityFromServer for ' + this.name, result);
@@ -468,7 +464,9 @@ class OneBuildRepository extends AjaxRepository {
468
464
  console.log('getLastModifiedDate');
469
465
  }
470
466
 
471
- return this._send(this.methods.get, this.api.getLastModifiedDate, this._baseParams)
467
+ const url = this._getModel() + '/' + this.api.getLastModifiedDate;
468
+
469
+ return this._send(this.methods.get, url, this._baseParams)
472
470
  .then(result => {
473
471
  if (this.debugMode) {
474
472
  console.log('Response for getLastModifiedDate for ' + this.name, result);
@@ -618,396 +616,6 @@ class OneBuildRepository extends AjaxRepository {
618
616
  });
619
617
  }
620
618
 
621
-
622
- // ______
623
- // /_ __/_______ ___ _____
624
- // / / / ___/ _ \/ _ \/ ___/
625
- // / / / / / __/ __(__ )
626
- // /_/ /_/ \___/\___/____/
627
-
628
- /**
629
- * Loads the root nodes of this tree.
630
- */
631
- loadRootNodes(depth) {
632
- this.ensureTree();
633
- if (this.isDestroyed) {
634
- this.throwError('this.setRootNode is no longer valid. Repository has been destroyed.');
635
- return;
636
- }
637
- if (!this.isOnline) {
638
- this.throwError('Offline');
639
- return;
640
- }
641
-
642
- this.emit('beforeLoad'); // TODO: canceling beforeLoad will cancel the load operation
643
- this.markLoading();
644
-
645
- const data = _.merge({ depth }, this._baseParams, this._params);
646
-
647
- if (this.debugMode) {
648
- console.log('loadRootNodes', data);
649
- }
650
-
651
- return this._send('POST', this._getModel() + '/getNodes', data)
652
- .then((result) => {
653
- if (this.debugMode) {
654
- console.log('Response for loadRootNodes', result);
655
- }
656
-
657
- if (this.isDestroyed) {
658
- // If this repository gets destroyed before it has a chance
659
- // to process the Ajax request, just ignore the response.
660
- return;
661
- }
662
-
663
- const {
664
- root,
665
- success,
666
- total,
667
- message
668
- } = this._processServerResponse(result);
669
-
670
- if (!success) {
671
- this.throwError(message);
672
- return;
673
- }
674
-
675
- this._destroyEntities();
676
-
677
- // Set the current entities
678
- const oThis = this;
679
- this.entities = _.map(root, (data) => {
680
- const entity = Repository._createEntity(oThis.schema, data, this, true);
681
- oThis._relayEntityEvents(entity);
682
- return entity;
683
- });
684
-
685
- this.assembleTreeNodes();
686
-
687
- // Set the total records that pass filter
688
- this.total = total;
689
- this._setPaginationVars();
690
-
691
- this.areRootNodesLoaded = true;
692
-
693
-
694
- // Don't emit events for root nodes...
695
- this.rehash();
696
- this.emit('loadRootNodes', this);
697
- // this.emit('changeData', this.entities);
698
-
699
- return this.getBy((entity) => {
700
- return entity.isRoot;
701
- });
702
- })
703
- .finally(() => {
704
- this.markLoading(false);
705
- });
706
- }
707
-
708
- /**
709
- * Loads (or reloads) the supplied treeNode
710
- */
711
- loadNode(treeNode, depth = 1) {
712
- this.ensureTree();
713
- if (this.isDestroyed) {
714
- this.throwError('this.loadNode is no longer valid. Repository has been destroyed.');
715
- return;
716
- }
717
- if (!this.isOnline) {
718
- this.throwError('Offline');
719
- return;
720
- }
721
-
722
- // If children already exist, remove them from the repository
723
- // This way, we can reload just a portion of the tree
724
- if (!_.isEmpty(treeNode.children)) {
725
- const children = treeNode.children;
726
- treeNode.children = [];
727
-
728
- const oThis = this;
729
- _.each(children, (child) => {
730
- oThis.removeEntity(child);
731
- });
732
- }
733
-
734
- this.markLoading();
735
-
736
- const data = _.merge({ depth, nodeId: treeNode.id, }, this._baseParams, this._params);
737
-
738
- if (this.debugMode) {
739
- console.log('loadNode', data);
740
- }
741
-
742
- return this._send('POST', this._getModel() + '/getNodes', data)
743
- .then((result) => {
744
- if (this.debugMode) {
745
- console.log('Response for loadNode', result);
746
- }
747
-
748
- if (this.isDestroyed) {
749
- // If this repository gets destroyed before it has a chance
750
- // to process the Ajax request, just ignore the response.
751
- return;
752
- }
753
-
754
- const {
755
- root,
756
- success,
757
- total,
758
- message
759
- } = this._processServerResponse(result);
760
-
761
- if (!success) {
762
- this.throwError(message);
763
- return;
764
- }
765
-
766
- // Set the current entities
767
- const oThis = this;
768
- const children = _.map(root, (data) => {
769
- const entity = Repository._createEntity(oThis.schema, data, this, true);
770
- oThis._relayEntityEvents(entity);
771
- return entity;
772
- });
773
-
774
- this.entities = this.entities.concat(children);
775
-
776
- this.assembleTreeNodes();
777
-
778
- this._setPaginationVars();
779
-
780
- this.rehash();
781
- // this.emit('changeData', this.entities);
782
- this.emit('load', this);
783
-
784
- return children;
785
- })
786
- .finally(() => {
787
- this.markLoading(false);
788
- });
789
- }
790
-
791
- /**
792
- * Loads (or reloads) the children of the supplied treeNode
793
- */
794
- loadChildNodes(treeNode, depth = 1) {
795
- this.ensureTree();
796
- if (this.isDestroyed) {
797
- this.throwError('this.loadChildNodes is no longer valid. Repository has been destroyed.');
798
- return;
799
- }
800
- if (!this.isOnline) {
801
- this.throwError('Offline');
802
- return;
803
- }
804
-
805
- // If children already exist, remove them from the repository
806
- // This way, we can reload just a portion of the tree
807
- if (!_.isEmpty(treeNode.children)) {
808
- _.each(treeNode.children, (child) => {
809
- treeNode.repository.removeTreeNode(child);
810
- });
811
- treeNode.children = [];
812
- }
813
-
814
- this.markLoading();
815
-
816
- const data = _.merge({ depth, parentId: treeNode.id, }, this._baseParams, this._params);
817
-
818
- if (this.debugMode) {
819
- console.log('loadChildNodes', data);
820
- }
821
-
822
- return this._send('POST', this._getModel() + '/getNodes', data)
823
- .then((result) => {
824
- if (this.debugMode) {
825
- console.log('Response for loadChildNodes', result);
826
- }
827
-
828
- if (this.isDestroyed) {
829
- // If this repository gets destroyed before it has a chance
830
- // to process the Ajax request, just ignore the response.
831
- return;
832
- }
833
-
834
- const {
835
- root,
836
- success,
837
- total,
838
- message
839
- } = this._processServerResponse(result);
840
-
841
- if (!success) {
842
- this.throwError(message);
843
- return;
844
- }
845
-
846
- // Set the current entities
847
- const oThis = this;
848
- const children = _.map(root, (data) => {
849
- const entity = Repository._createEntity(oThis.schema, data, this, true);
850
- oThis._relayEntityEvents(entity);
851
- return entity;
852
- });
853
-
854
- this.entities = this.entities.concat(children);
855
-
856
- this.assembleTreeNodes();
857
-
858
- this._setPaginationVars();
859
-
860
- this.rehash();
861
- // this.emit('changeData', this.entities);
862
- this.emit('load', this);
863
-
864
- return children;
865
- })
866
- .finally(() => {
867
- this.markLoading(false);
868
- });
869
- }
870
-
871
- /**
872
- * Override the AjaxRepository to we can reload a treeNode if needed
873
- */
874
- reloadEntity(entity, callback = null) {
875
- if (!entity.isTree) {
876
- return super.reloadEntity(entity, callback);
877
- }
878
-
879
- return this.loadNode(entity, 1);
880
- }
881
-
882
- /**
883
- * Searches all nodes for the supplied text.
884
- * This basically takes the search query and returns whatever the server sends
885
- */
886
- searchNodes(q) {
887
- this.ensureTree();
888
- if (this.isDestroyed) {
889
- this.throwError('this.searchNodes is no longer valid. Repository has been destroyed.');
890
- return;
891
- }
892
- if (!this.isOnline) {
893
- this.throwError('Offline');
894
- return;
895
- }
896
-
897
- const data = _.merge({ q, }, this._baseParams, this._params);
898
-
899
- if (this.debugMode) {
900
- console.log('searchNodes', data);
901
- }
902
-
903
- return this._send('POST', this._getModel() + '/searchNodes', data)
904
- .then((result) => {
905
- if (this.debugMode) {
906
- console.log('Response for searchNodes', result);
907
- }
908
-
909
- if (this.isDestroyed) {
910
- // If this repository gets destroyed before it has a chance
911
- // to process the Ajax request, just ignore the response.
912
- return;
913
- }
914
-
915
- const {
916
- root,
917
- success,
918
- total,
919
- message
920
- } = this._processServerResponse(result);
921
-
922
- if (!success) {
923
- this.throwError(message);
924
- return;
925
- }
926
-
927
- return root;
928
- })
929
- .finally(() => {
930
- this.markLoading(false);
931
- });
932
- }
933
-
934
- /**
935
- * Alias for loadChildren
936
- */
937
- reloadChildren(treeNode, depth) {
938
- return this.loadChildren(treeNode, depth);
939
- }
940
-
941
- /**
942
- * Moves the supplied treeNode to a new position on the tree
943
- * @returns id of common ancestor node
944
- */
945
- moveTreeNode(treeNode, newParentId) {
946
- this.ensureTree();
947
- if (this.isDestroyed) {
948
- this.throwError('this.moveTreeNode is no longer valid. Repository has been destroyed.');
949
- return;
950
- }
951
- if (!this.isOnline) {
952
- this.throwError('Offline');
953
- return;
954
- }
955
-
956
- const oldParentId = treeNode.parent?.id;
957
-
958
- const data = _.merge({ nodeId: treeNode.id, parentId: newParentId, }, this._baseParams, this._params);
959
-
960
- if (this.debugMode) {
961
- console.log('moveTreeNode', data);
962
- }
963
-
964
- return this._send('POST', this._getModel() + '/moveNode', data)
965
- .then((result) => {
966
- if (this.debugMode) {
967
- console.log('Response for searchNodes', result);
968
- }
969
-
970
- if (this.isDestroyed) {
971
- // If this repository gets destroyed before it has a chance
972
- // to process the Ajax request, just ignore the response.
973
- return;
974
- }
975
-
976
- const {
977
- root: {
978
- commonAncestorId,
979
- oldParent,
980
- newParent,
981
- node,
982
- },
983
- success,
984
- total,
985
- message
986
- } = this._processServerResponse(result);
987
-
988
- if (!success) {
989
- this.throwError(message);
990
- return;
991
- }
992
-
993
- // move it from oldParent.children to newParent.children
994
- const
995
- oldParentRecord = this.getById(oldParentId),
996
- newParentRecord = this.getById(newParentId);
997
-
998
- oldParentRecord?.loadOriginalData(oldParent);
999
- newParentRecord.loadOriginalData(newParent);
1000
- treeNode.loadOriginalData(node);
1001
-
1002
- this.assembleTreeNodes();
1003
-
1004
- return commonAncestorId;
1005
- })
1006
- .finally(() => {
1007
- this.markLoading(false);
1008
- });
1009
- }
1010
-
1011
619
  }
1012
620
 
1013
621