spice-js 2.6.12 → 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,33 +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
+ }
840
841
 
841
- columnList.push("_permissions"); // Always include _permissions
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
851
+
852
+ return _.map(data, entry => _.pick(entry, columnList));
853
+ }
842
854
 
843
- return _.map(data, entry => _.pick(entry, columnList));
855
+ return data;
844
856
  }
845
857
 
846
- return data;
847
- }
858
+ function extractNestings(string, localType) {
859
+ var returnVal = [];
860
+ var regex = /(\w+)\./g;
861
+ var match;
848
862
 
849
- function prepColumns(columns) {
850
- if (columns && columns != "") {
851
- var columnList = columns.split(","); // Convert comma-separated string to array
863
+ while (match = regex.exec(string)) {
864
+ if (match[1] != localType) returnVal.push(match[1]);
865
+ }
852
866
 
853
- return _.join(_.map(columnList, column => "`" + column + "`"), ",");
867
+ return returnVal;
854
868
  }
855
869
 
856
- return columns;
857
- }
870
+ var nestings = extractNestings((_args4 = args) == null ? void 0 : _args4.query, _this11.type);
858
871
 
859
- try {
860
- if (!args) {
861
- args = {};
872
+ var mappedNestings = _.compact(_.map(nestings, nesting => {
873
+ var prop = _this11.props[nesting];
874
+
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
+ }
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
+ });
895
+
896
+ return joinSection;
897
+ }
898
+
899
+ var _join = createJoinSection(mappedNestings);
900
+
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;
862
919
  }
863
920
 
864
921
  var query = "";
@@ -871,9 +928,9 @@ class SpiceModel {
871
928
  query = _this11.makeQueryFromFilter(args.filters);
872
929
  } else {
873
930
  if (args.query) {
874
- 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) ");
875
932
  } else {
876
- query = "(deleted = false OR deleted IS MISSING) ";
933
+ query = "(`" + _this11.type + "`.deleted = false OR `" + _this11.type + "`.deleted IS MISSING) ";
877
934
  }
878
935
  }
879
936
  }
@@ -891,7 +948,7 @@ class SpiceModel {
891
948
  }
892
949
 
893
950
  if (!args.sort) {
894
- args.sort = "created_at DESC";
951
+ args.sort = "`" + _this11.type + "`.created_at DESC";
895
952
  }
896
953
 
897
954
  if (args.skip_hooks != true) {
@@ -902,7 +959,7 @@ class SpiceModel {
902
959
  return str.replace(/[^a-zA-Z0-9]/g, "");
903
960
  }
904
961
 
905
- 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);
906
963
  var results;
907
964
 
908
965
  if (args.is_custom_query && args.is_custom_query === "true") {
@@ -931,7 +988,7 @@ class SpiceModel {
931
988
  results = _cached_results == null ? void 0 : _cached_results.value;
932
989
 
933
990
  if ((_cached_results == null ? void 0 : _cached_results.value) == undefined || (yield _this11.shouldForceRefresh(_cached_results))) {
934
- 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);
935
992
 
936
993
  _this11.getCacheProviderObject(_this11.type).set(key, {
937
994
  value: results,
@@ -939,7 +996,7 @@ class SpiceModel {
939
996
  }, _this11.getCacheConfig(_this11.type));
940
997
  }
941
998
  } else {
942
- 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);
943
1000
  }
944
1001
  } else {
945
1002
  if (_this11.shouldUseCache(_this11.type)) {
@@ -949,7 +1006,7 @@ class SpiceModel {
949
1006
  var shouleForceRefresh = yield _this11.shouldForceRefresh(_cached_results2);
950
1007
 
951
1008
  if ((_cached_results2 == null ? void 0 : _cached_results2.value) == undefined || shouleForceRefresh) {
952
- 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);
953
1010
 
954
1011
  _this11.getCacheProviderObject(_this11.type).set(key, {
955
1012
  value: results,
@@ -957,7 +1014,7 @@ class SpiceModel {
957
1014
  }, _this11.getCacheConfig(_this11.type));
958
1015
  }
959
1016
  } else {
960
- 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);
961
1018
 
962
1019
  if (_this11.type == "user") {
963
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.12",
3
+ "version": "2.6.14",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -738,32 +738,81 @@ 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
- return _.map(data, (entry) => _.pick(entry, columnList));
748
- }
749
- return data;
750
- }
751
-
752
- function prepColumns(columns) {
753
- if (columns && columns != "") {
754
- let columnList = columns.split(","); // Convert comma-separated string to array
755
- return _.join(
756
- _.map(columnList, (column) => `\`${column}\``),
757
- ","
758
- );
759
- }
760
- return columns;
761
- }
762
741
  try {
763
742
  if (!args) {
764
743
  args = {};
765
744
  }
766
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;
765
+ }
766
+
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
+ };
779
+ }
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;
792
+ }
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;
814
+ }
815
+
767
816
  let query = "";
768
817
  args.columns = prepColumns(args.columns);
769
818
  if (
@@ -777,9 +826,10 @@ export default class SpiceModel {
777
826
  } else {
778
827
  if (args.query) {
779
828
  query =
780
- args.query + " AND (deleted = false OR deleted IS MISSING) ";
829
+ args.query +
830
+ ` AND (\`${this.type}\`.deleted = false OR \`${this.type}\`.deleted IS MISSING) `;
781
831
  } else {
782
- query = "(deleted = false OR deleted IS MISSING) ";
832
+ query = `(\`${this.type}\`.deleted = false OR \`${this.type}\`.deleted IS MISSING) `;
783
833
  }
784
834
  }
785
835
  }
@@ -795,7 +845,7 @@ export default class SpiceModel {
795
845
  }
796
846
 
797
847
  if (!args.sort) {
798
- args.sort = "created_at DESC";
848
+ args.sort = `\`${this.type}\`.created_at DESC`;
799
849
  }
800
850
  if (args.skip_hooks != true) {
801
851
  await this.run_hook(this, "list", "before");
@@ -804,7 +854,7 @@ export default class SpiceModel {
804
854
  return str.replace(/[^a-zA-Z0-9]/g, "");
805
855
  }
806
856
  let key = removeSpaceAndSpecialCharacters(
807
- `${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}`
808
858
  );
809
859
 
810
860
  let results;
@@ -845,7 +895,8 @@ export default class SpiceModel {
845
895
  this.type,
846
896
  query || "",
847
897
  args.limit,
848
- args.offset
898
+ args.offset,
899
+ args._join
849
900
  );
850
901
  this.getCacheProviderObject(this.type).set(
851
902
  key,
@@ -858,7 +909,8 @@ export default class SpiceModel {
858
909
  this.type,
859
910
  query || "",
860
911
  args.limit,
861
- args.offset
912
+ args.offset,
913
+ args._join
862
914
  );
863
915
  }
864
916
  } else {
@@ -879,7 +931,8 @@ export default class SpiceModel {
879
931
  args.offset,
880
932
  args.sort,
881
933
  args.do_count,
882
- args.statement_consistent
934
+ args.statement_consistent,
935
+ args._join
883
936
  );
884
937
  this.getCacheProviderObject(this.type).set(
885
938
  key,
@@ -896,7 +949,8 @@ export default class SpiceModel {
896
949
  args.offset,
897
950
  args.sort,
898
951
  args.do_count,
899
- args.statement_consistent
952
+ args.statement_consistent,
953
+ args._join
900
954
  );
901
955
  if (this.type == "user") {
902
956
  console.log("results from DB No Chache available", results);