spice-js 2.6.13 → 2.6.15

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