spice-js 2.6.13 → 2.6.14

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.
@@ -832,43 +832,90 @@ class SpiceModel {
832
832
  var _this11 = this;
833
833
 
834
834
  return _asyncToGenerator(function* () {
835
- function filterResultsByColumns(data, columns) {
836
- if (columns && columns != "") {
837
- columns = columns.replace(/`/g, "");
838
- columns = columns.replace(/meta\(\)\.id/g, "id");
839
- var columnList = columns.split(","); // Convert comma-separated string to array
835
+ try {
836
+ var _args4;
837
+
838
+ if (!args) {
839
+ args = {};
840
+ }
841
+
842
+ function filterResultsByColumns(data, columns) {
843
+ if (columns && columns != "") {
844
+ columns = columns.replace(/`/g, "");
845
+ columns = columns.replace(/meta\(\)\.id/g, "id");
846
+ var columnList = columns.split(","); // Convert comma-separated string to array
847
+
848
+ columnList.push("_permissions"); // Always include _permissions
849
+
850
+ columnList.push("id"); // Always include _permissions
840
851
 
841
- columnList.push("_permissions"); // Always include _permissions
852
+ return _.map(data, entry => _.pick(entry, columnList));
853
+ }
854
+
855
+ return data;
856
+ }
842
857
 
843
- columnList.push("id"); // Always include _permissions
858
+ function extractNestings(string, localType) {
859
+ var returnVal = [];
860
+ var regex = /(\w+)\./g;
861
+ var match;
844
862
 
845
- return _.map(data, entry => _.pick(entry, columnList));
863
+ while (match = regex.exec(string)) {
864
+ if (match[1] != localType) returnVal.push(match[1]);
865
+ }
866
+
867
+ return returnVal;
846
868
  }
847
869
 
848
- return data;
849
- }
870
+ var nestings = extractNestings((_args4 = args) == null ? void 0 : _args4.query, _this11.type);
850
871
 
851
- function prepColumns(columns) {
852
- if (columns && columns != "") {
853
- var columnList = columns.split(","); // Convert comma-separated string to array
872
+ var mappedNestings = _.compact(_.map(nestings, nesting => {
873
+ var prop = _this11.props[nesting];
854
874
 
855
- return _.join(_.compact(_.map(columnList, column => {
856
- if (column == "meta().id") {
857
- column = undefined;
858
- } else if (!column.startsWith("`") && !column.endsWith("`")) {
859
- return "`" + column + "`";
875
+ if (prop) {
876
+ if (prop.map) {
877
+ if (prop.map.type == _2.MapType.MODEL) {
878
+ return {
879
+ alias: nesting,
880
+ reference: prop.map.reference.toLowerCase(),
881
+ value_field: prop.map.value_field
882
+ };
883
+ }
860
884
  }
885
+ }
886
+ }));
887
+
888
+ function createJoinSection(nestings) {
889
+ //create join section with nestings
890
+ var joinSection = "";
891
+
892
+ _.each(nestings, nesting => {
893
+ joinSection += "LEFT JOIN `" + nesting.reference + "` AS `" + nesting.alias + "` ON KEYS `" + that.type + "`.`" + nesting.alias + "` ";
894
+ });
861
895
 
862
- return column;
863
- })), ",");
896
+ return joinSection;
864
897
  }
865
898
 
866
- return columns;
867
- }
899
+ var _join = createJoinSection(mappedNestings);
868
900
 
869
- try {
870
- if (!args) {
871
- args = {};
901
+ args._join = _join;
902
+
903
+ function prepColumns(columns) {
904
+ if (columns && columns != "") {
905
+ var columnList = columns.split(","); // Convert comma-separated string to array
906
+
907
+ return _.join(_.compact(_.map(columnList, column => {
908
+ if (column == "meta().id") {
909
+ column = undefined;
910
+ } else if (!column.startsWith("`") && !column.endsWith("`")) {
911
+ return "`" + column + "`";
912
+ }
913
+
914
+ return column;
915
+ })), ",");
916
+ }
917
+
918
+ return columns;
872
919
  }
873
920
 
874
921
  var query = "";
@@ -881,9 +928,9 @@ class SpiceModel {
881
928
  query = _this11.makeQueryFromFilter(args.filters);
882
929
  } else {
883
930
  if (args.query) {
884
- query = args.query + " AND (deleted = false OR deleted IS MISSING) ";
931
+ query = args.query + (" AND (`" + _this11.type + "`.deleted = false OR `" + _this11.type + "`.deleted IS MISSING) ");
885
932
  } else {
886
- query = "(deleted = false OR deleted IS MISSING) ";
933
+ query = "(`" + _this11.type + "`.deleted = false OR `" + _this11.type + "`.deleted IS MISSING) ";
887
934
  }
888
935
  }
889
936
  }
@@ -901,7 +948,7 @@ class SpiceModel {
901
948
  }
902
949
 
903
950
  if (!args.sort) {
904
- args.sort = "created_at DESC";
951
+ args.sort = "`" + _this11.type + "`.created_at DESC";
905
952
  }
906
953
 
907
954
  if (args.skip_hooks != true) {
@@ -912,7 +959,7 @@ class SpiceModel {
912
959
  return str.replace(/[^a-zA-Z0-9]/g, "");
913
960
  }
914
961
 
915
- var key = removeSpaceAndSpecialCharacters(_this11.type + "::" + query + "::" + args.limit + "::" + args.offset + "::" + args.sort + "::" + args.do_count + "::" + args.statement_consistent + "::" + args.columns + "::" + args.is_full_text + "::" + args.is_custom_query);
962
+ var key = removeSpaceAndSpecialCharacters(_this11.type + "::" + args._join + "::" + query + "::" + args.limit + "::" + args.offset + "::" + args.sort + "::" + args.do_count + "::" + args.statement_consistent + "::" + args.columns + "::" + args.is_full_text + "::" + args.is_custom_query);
916
963
  var results;
917
964
 
918
965
  if (args.is_custom_query && args.is_custom_query === "true") {
@@ -941,7 +988,7 @@ class SpiceModel {
941
988
  results = _cached_results == null ? void 0 : _cached_results.value;
942
989
 
943
990
  if ((_cached_results == null ? void 0 : _cached_results.value) == undefined || (yield _this11.shouldForceRefresh(_cached_results))) {
944
- results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset);
991
+ results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset, args._join);
945
992
 
946
993
  _this11.getCacheProviderObject(_this11.type).set(key, {
947
994
  value: results,
@@ -949,7 +996,7 @@ class SpiceModel {
949
996
  }, _this11.getCacheConfig(_this11.type));
950
997
  }
951
998
  } else {
952
- results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset);
999
+ results = yield _this11.database.full_text_search(_this11.type, query || "", args.limit, args.offset, args._join);
953
1000
  }
954
1001
  } else {
955
1002
  if (_this11.shouldUseCache(_this11.type)) {
@@ -959,7 +1006,7 @@ class SpiceModel {
959
1006
  var shouleForceRefresh = yield _this11.shouldForceRefresh(_cached_results2);
960
1007
 
961
1008
  if ((_cached_results2 == null ? void 0 : _cached_results2.value) == undefined || shouleForceRefresh) {
962
- results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
1009
+ results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent, args._join);
963
1010
 
964
1011
  _this11.getCacheProviderObject(_this11.type).set(key, {
965
1012
  value: results,
@@ -967,7 +1014,7 @@ class SpiceModel {
967
1014
  }, _this11.getCacheConfig(_this11.type));
968
1015
  }
969
1016
  } else {
970
- results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
1017
+ results = yield _this11.database.search(_this11.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent, args._join);
971
1018
 
972
1019
  if (_this11.type == "user") {
973
1020
  console.log("results from DB No Chache available", results);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.6.13",
3
+ "version": "2.6.14",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -738,40 +738,79 @@ export default class SpiceModel {
738
738
  }
739
739
 
740
740
  async list(args) {
741
- function filterResultsByColumns(data, columns) {
742
- if (columns && columns != "") {
743
- columns = columns.replace(/`/g, "");
744
- columns = columns.replace(/meta\(\)\.id/g, "id");
745
- let columnList = columns.split(","); // Convert comma-separated string to array
746
- columnList.push("_permissions"); // Always include _permissions
747
- columnList.push("id"); // Always include _permissions
748
- return _.map(data, (entry) => _.pick(entry, columnList));
741
+ try {
742
+ if (!args) {
743
+ args = {};
744
+ }
745
+
746
+ function filterResultsByColumns(data, columns) {
747
+ if (columns && columns != "") {
748
+ columns = columns.replace(/`/g, "");
749
+ columns = columns.replace(/meta\(\)\.id/g, "id");
750
+ let columnList = columns.split(","); // Convert comma-separated string to array
751
+ columnList.push("_permissions"); // Always include _permissions
752
+ columnList.push("id"); // Always include _permissions
753
+ return _.map(data, (entry) => _.pick(entry, columnList));
754
+ }
755
+ return data;
756
+ }
757
+ function extractNestings(string, localType) {
758
+ let returnVal = [];
759
+ let regex = /(\w+)\./g;
760
+ let match;
761
+ while ((match = regex.exec(string))) {
762
+ if (match[1] != localType) returnVal.push(match[1]);
763
+ }
764
+ return returnVal;
749
765
  }
750
- return data;
751
- }
752
766
 
753
- function prepColumns(columns) {
754
- if (columns && columns != "") {
755
- let columnList = columns.split(","); // Convert comma-separated string to array
756
- return _.join(
757
- _.compact(
758
- _.map(columnList, (column) => {
759
- if (column == "meta().id") {
760
- column = undefined;
761
- } else if (!column.startsWith("`") && !column.endsWith("`")) {
762
- return `\`${column}\``;
767
+ let nestings = extractNestings(args?.query, this.type);
768
+ let mappedNestings = _.compact(
769
+ _.map(nestings, (nesting) => {
770
+ let prop = this.props[nesting];
771
+ if (prop) {
772
+ if (prop.map) {
773
+ if (prop.map.type == MapType.MODEL) {
774
+ return {
775
+ alias: nesting,
776
+ reference: prop.map.reference.toLowerCase(),
777
+ value_field: prop.map.value_field,
778
+ };
763
779
  }
764
- return column;
765
- })
766
- ),
767
- ","
768
- );
780
+ }
781
+ }
782
+ })
783
+ );
784
+
785
+ function createJoinSection(nestings) {
786
+ //create join section with nestings
787
+ let joinSection = "";
788
+ _.each(nestings, (nesting) => {
789
+ joinSection += `LEFT JOIN \`${nesting.reference}\` AS \`${nesting.alias}\` ON KEYS \`${that.type}\`.\`${nesting.alias}\` `;
790
+ });
791
+ return joinSection;
769
792
  }
770
- return columns;
771
- }
772
- try {
773
- if (!args) {
774
- args = {};
793
+ let _join = createJoinSection(mappedNestings);
794
+ args._join = _join;
795
+
796
+ function prepColumns(columns) {
797
+ if (columns && columns != "") {
798
+ let columnList = columns.split(","); // Convert comma-separated string to array
799
+ return _.join(
800
+ _.compact(
801
+ _.map(columnList, (column) => {
802
+ if (column == "meta().id") {
803
+ column = undefined;
804
+ } else if (!column.startsWith("`") && !column.endsWith("`")) {
805
+ return `\`${column}\``;
806
+ }
807
+ return column;
808
+ })
809
+ ),
810
+ ","
811
+ );
812
+ }
813
+ return columns;
775
814
  }
776
815
 
777
816
  let query = "";
@@ -787,9 +826,10 @@ export default class SpiceModel {
787
826
  } else {
788
827
  if (args.query) {
789
828
  query =
790
- args.query + " AND (deleted = false OR deleted IS MISSING) ";
829
+ args.query +
830
+ ` AND (\`${this.type}\`.deleted = false OR \`${this.type}\`.deleted IS MISSING) `;
791
831
  } else {
792
- query = "(deleted = false OR deleted IS MISSING) ";
832
+ query = `(\`${this.type}\`.deleted = false OR \`${this.type}\`.deleted IS MISSING) `;
793
833
  }
794
834
  }
795
835
  }
@@ -805,7 +845,7 @@ export default class SpiceModel {
805
845
  }
806
846
 
807
847
  if (!args.sort) {
808
- args.sort = "created_at DESC";
848
+ args.sort = `\`${this.type}\`.created_at DESC`;
809
849
  }
810
850
  if (args.skip_hooks != true) {
811
851
  await this.run_hook(this, "list", "before");
@@ -814,7 +854,7 @@ export default class SpiceModel {
814
854
  return str.replace(/[^a-zA-Z0-9]/g, "");
815
855
  }
816
856
  let key = removeSpaceAndSpecialCharacters(
817
- `${this.type}::${query}::${args.limit}::${args.offset}::${args.sort}::${args.do_count}::${args.statement_consistent}::${args.columns}::${args.is_full_text}::${args.is_custom_query}`
857
+ `${this.type}::${args._join}::${query}::${args.limit}::${args.offset}::${args.sort}::${args.do_count}::${args.statement_consistent}::${args.columns}::${args.is_full_text}::${args.is_custom_query}`
818
858
  );
819
859
 
820
860
  let results;
@@ -855,7 +895,8 @@ export default class SpiceModel {
855
895
  this.type,
856
896
  query || "",
857
897
  args.limit,
858
- args.offset
898
+ args.offset,
899
+ args._join
859
900
  );
860
901
  this.getCacheProviderObject(this.type).set(
861
902
  key,
@@ -868,7 +909,8 @@ export default class SpiceModel {
868
909
  this.type,
869
910
  query || "",
870
911
  args.limit,
871
- args.offset
912
+ args.offset,
913
+ args._join
872
914
  );
873
915
  }
874
916
  } else {
@@ -889,7 +931,8 @@ export default class SpiceModel {
889
931
  args.offset,
890
932
  args.sort,
891
933
  args.do_count,
892
- args.statement_consistent
934
+ args.statement_consistent,
935
+ args._join
893
936
  );
894
937
  this.getCacheProviderObject(this.type).set(
895
938
  key,
@@ -906,7 +949,8 @@ export default class SpiceModel {
906
949
  args.offset,
907
950
  args.sort,
908
951
  args.do_count,
909
- args.statement_consistent
952
+ args.statement_consistent,
953
+ args._join
910
954
  );
911
955
  if (this.type == "user") {
912
956
  console.log("results from DB No Chache available", results);