spice-js 2.6.51 → 2.6.53
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/build/models/SpiceModel.js +160 -123
- package/package.json +1 -1
- package/src/models/SpiceModel.js +125 -88
|
@@ -19,6 +19,8 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
19
19
|
|
|
20
20
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
21
21
|
|
|
22
|
+
global.mapping_dept = "*";
|
|
23
|
+
|
|
22
24
|
var co = require("co");
|
|
23
25
|
|
|
24
26
|
var regeneratorRuntime = require("regenerator-runtime");
|
|
@@ -34,7 +36,8 @@ var SDate = require("sonover-date"),
|
|
|
34
36
|
_disable_lifecycle_events = Symbol(),
|
|
35
37
|
_external_modifier_loaded = Symbol(),
|
|
36
38
|
_skip_cache = Symbol(),
|
|
37
|
-
_serializers = Symbol()
|
|
39
|
+
_serializers = Symbol(),
|
|
40
|
+
_level = Symbol(); //const _type = Symbol("type");
|
|
38
41
|
|
|
39
42
|
|
|
40
43
|
var that;
|
|
@@ -56,7 +59,7 @@ class SpiceModel {
|
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
try {
|
|
59
|
-
var _args$args, _args2, _args2$args, _args3, _args3$args;
|
|
62
|
+
var _args$args, _args2, _args2$args, _args3, _args3$args, _args4, _args4$args;
|
|
60
63
|
|
|
61
64
|
var dbtype = spice.config.database.connections[args.connection].type || "couchbase";
|
|
62
65
|
|
|
@@ -69,6 +72,7 @@ class SpiceModel {
|
|
|
69
72
|
this[_disable_lifecycle_events] = ((_args$args = args.args) == null ? void 0 : _args$args.disable_lifecycle_events) || false;
|
|
70
73
|
this[_ctx] = (_args2 = args) == null ? void 0 : (_args2$args = _args2.args) == null ? void 0 : _args2$args.ctx;
|
|
71
74
|
this[_skip_cache] = ((_args3 = args) == null ? void 0 : (_args3$args = _args3.args) == null ? void 0 : _args3$args.skip_cache) || false;
|
|
75
|
+
this[_level] = ((_args4 = args) == null ? void 0 : (_args4$args = _args4.args) == null ? void 0 : _args4$args._level) || 0;
|
|
72
76
|
this[_hooks] = {
|
|
73
77
|
create: {
|
|
74
78
|
before: [],
|
|
@@ -562,6 +566,10 @@ class SpiceModel {
|
|
|
562
566
|
|
|
563
567
|
return _asyncToGenerator(function* () {
|
|
564
568
|
try {
|
|
569
|
+
var _args5;
|
|
570
|
+
|
|
571
|
+
global.mapping_dept = ((_args5 = args) == null ? void 0 : _args5.mapping_dept) ? Number(args.mapping_dept) : global.mapping_dept;
|
|
572
|
+
|
|
565
573
|
if (!args) {
|
|
566
574
|
args = {};
|
|
567
575
|
}
|
|
@@ -748,7 +756,7 @@ class SpiceModel {
|
|
|
748
756
|
}
|
|
749
757
|
|
|
750
758
|
var db_data = cover_obj.new || _this8;
|
|
751
|
-
yield _this8.database.update(args.id, db_data);
|
|
759
|
+
yield _this8.database.update(args.id, db_data, args._ttl);
|
|
752
760
|
|
|
753
761
|
_this8.setMonitor();
|
|
754
762
|
|
|
@@ -811,7 +819,7 @@ class SpiceModel {
|
|
|
811
819
|
yield _this9.run_hook(workingForm, "create", "before");
|
|
812
820
|
}
|
|
813
821
|
|
|
814
|
-
var results = yield _this9.database.insert(id, workingForm, args.
|
|
822
|
+
var results = yield _this9.database.insert(id, workingForm, args._ttl);
|
|
815
823
|
|
|
816
824
|
_this9.setMonitor();
|
|
817
825
|
|
|
@@ -835,7 +843,7 @@ class SpiceModel {
|
|
|
835
843
|
})();
|
|
836
844
|
}
|
|
837
845
|
|
|
838
|
-
|
|
846
|
+
touch(args) {
|
|
839
847
|
var _this10 = this;
|
|
840
848
|
|
|
841
849
|
return _asyncToGenerator(function* () {
|
|
@@ -845,27 +853,48 @@ class SpiceModel {
|
|
|
845
853
|
throw new Error(_this10.type + " does not exist.");
|
|
846
854
|
}
|
|
847
855
|
|
|
848
|
-
|
|
856
|
+
try {
|
|
857
|
+
var touch_response = {};
|
|
858
|
+
touch_response = yield _this10.database.touch(args.id, args._ttl);
|
|
859
|
+
return touch_response;
|
|
860
|
+
} catch (e) {
|
|
861
|
+
console.log(e.stack);
|
|
862
|
+
throw e;
|
|
863
|
+
}
|
|
864
|
+
})();
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
delete(args) {
|
|
868
|
+
var _this11 = this;
|
|
869
|
+
|
|
870
|
+
return _asyncToGenerator(function* () {
|
|
871
|
+
var item_exist = yield _this11.exist(args.id);
|
|
872
|
+
|
|
873
|
+
if (!item_exist) {
|
|
874
|
+
throw new Error(_this11.type + " does not exist.");
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
var results = yield _this11.database.get(args.id);
|
|
849
878
|
|
|
850
879
|
try {
|
|
851
880
|
if (args.skip_hooks != true) {
|
|
852
|
-
yield
|
|
881
|
+
yield _this11.run_hook(args, "delete", "before");
|
|
853
882
|
}
|
|
854
883
|
|
|
855
884
|
var delete_response = {};
|
|
856
885
|
|
|
857
886
|
if (args.hard) {
|
|
858
|
-
delete_response = yield
|
|
887
|
+
delete_response = yield _this11.database.delete(args.id);
|
|
859
888
|
|
|
860
|
-
|
|
889
|
+
_this11.setMonitor();
|
|
861
890
|
} else {
|
|
862
891
|
delete results["id"];
|
|
863
892
|
results.deleted = true;
|
|
864
|
-
delete_response = yield
|
|
893
|
+
delete_response = yield _this11.database.update(args.id, "");
|
|
865
894
|
}
|
|
866
895
|
|
|
867
896
|
if (args.skip_hooks != true) {
|
|
868
|
-
yield
|
|
897
|
+
yield _this11.run_hook(results, "delete", "after", results);
|
|
869
898
|
}
|
|
870
899
|
|
|
871
900
|
return {};
|
|
@@ -956,7 +985,7 @@ class SpiceModel {
|
|
|
956
985
|
}
|
|
957
986
|
|
|
958
987
|
list(args) {
|
|
959
|
-
var
|
|
988
|
+
var _this12 = this;
|
|
960
989
|
|
|
961
990
|
return _asyncToGenerator(function* () {
|
|
962
991
|
if (args === void 0) {
|
|
@@ -964,15 +993,15 @@ class SpiceModel {
|
|
|
964
993
|
}
|
|
965
994
|
|
|
966
995
|
try {
|
|
967
|
-
var
|
|
996
|
+
var _args6, _args7, _args8, _args9;
|
|
968
997
|
|
|
969
|
-
args.columns =
|
|
970
|
-
var nestings = [...
|
|
998
|
+
args.columns = _this12.prepColumns(args.columns);
|
|
999
|
+
var nestings = [..._this12.extractNestings((_args6 = args) == null ? void 0 : _args6.query, _this12.type), ..._this12.extractNestings((_args7 = args) == null ? void 0 : _args7.columns, _this12.type), ..._this12.extractNestings((_args8 = args) == null ? void 0 : _args8.sort, _this12.type)];
|
|
971
1000
|
|
|
972
1001
|
var mappedNestings = _.compact(_.uniq(nestings).map(nesting => {
|
|
973
1002
|
var _prop$map;
|
|
974
1003
|
|
|
975
|
-
var prop =
|
|
1004
|
+
var prop = _this12.props[nesting];
|
|
976
1005
|
|
|
977
1006
|
if ((prop == null ? void 0 : (_prop$map = prop.map) == null ? void 0 : _prop$map.type) === _2.MapType.MODEL) {
|
|
978
1007
|
return {
|
|
@@ -984,13 +1013,13 @@ class SpiceModel {
|
|
|
984
1013
|
}
|
|
985
1014
|
}));
|
|
986
1015
|
|
|
987
|
-
args._join =
|
|
1016
|
+
args._join = _this12.createJoinSection(mappedNestings);
|
|
988
1017
|
var query = "";
|
|
989
1018
|
|
|
990
1019
|
if (args.is_full_text === "true" || args.is_custom_query === "true") {
|
|
991
1020
|
query = args.query;
|
|
992
1021
|
} else {
|
|
993
|
-
query = args.filters ?
|
|
1022
|
+
query = args.filters ? _this12.makeQueryFromFilter(args.filters) : args.query ? args.query + " AND (`" + _this12.type + "`.deleted = false OR `" + _this12.type + "`.deleted IS MISSING)" : "(`" + _this12.type + "`.deleted = false OR `" + _this12.type + "`.deleted IS MISSING)";
|
|
994
1023
|
}
|
|
995
1024
|
|
|
996
1025
|
if ((0, _Security.hasSQLInjection)(query)) {
|
|
@@ -999,40 +1028,41 @@ class SpiceModel {
|
|
|
999
1028
|
|
|
1000
1029
|
args.limit = Number(args.limit) || undefined;
|
|
1001
1030
|
args.offset = Number(args.offset) || 0;
|
|
1002
|
-
|
|
1031
|
+
global.mapping_dept = ((_args9 = args) == null ? void 0 : _args9.mapping_dept) ? Number(args.mapping_dept) : global.mapping_dept;
|
|
1032
|
+
args.sort = args.sort ? args.sort.split(",").map(item => item.includes(".") ? item : "`" + _this12.type + "`." + _this12.formatSortComponent(item)).join(",") : "`" + _this12.type + "`.created_at DESC";
|
|
1003
1033
|
|
|
1004
1034
|
if (args.skip_hooks !== true) {
|
|
1005
|
-
yield
|
|
1035
|
+
yield _this12.run_hook(_this12, "list", "before");
|
|
1006
1036
|
}
|
|
1007
1037
|
|
|
1008
|
-
var cacheKey =
|
|
1038
|
+
var cacheKey = _this12.removeSpaceAndSpecialCharacters("list::" + _this12.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);
|
|
1009
1039
|
|
|
1010
1040
|
var results;
|
|
1011
1041
|
|
|
1012
|
-
if (
|
|
1013
|
-
var cachedResults = yield
|
|
1042
|
+
if (_this12.shouldUseCache(_this12.type)) {
|
|
1043
|
+
var cachedResults = yield _this12.getCacheProviderObject(_this12.type).get(cacheKey);
|
|
1014
1044
|
results = cachedResults == null ? void 0 : cachedResults.value;
|
|
1015
1045
|
|
|
1016
|
-
if (!results || (yield
|
|
1017
|
-
results = yield
|
|
1018
|
-
yield
|
|
1046
|
+
if (!results || (yield _this12.shouldForceRefresh(cachedResults))) {
|
|
1047
|
+
results = yield _this12.fetchResults(args, query);
|
|
1048
|
+
yield _this12.getCacheProviderObject(_this12.type).set(cacheKey, {
|
|
1019
1049
|
value: results,
|
|
1020
1050
|
time: new Date().getTime()
|
|
1021
|
-
},
|
|
1051
|
+
}, _this12.getCacheConfig(_this12.type));
|
|
1022
1052
|
}
|
|
1023
1053
|
} else {
|
|
1024
|
-
results = yield
|
|
1054
|
+
results = yield _this12.fetchResults(args, query);
|
|
1025
1055
|
}
|
|
1026
1056
|
|
|
1027
1057
|
if (args.skip_read_serialize !== true && args.skip_serialize !== true) {
|
|
1028
|
-
results.data = yield
|
|
1058
|
+
results.data = yield _this12.do_serialize(results.data, "read", {}, args, (yield _this12.propsToBeRemoved(results.data)));
|
|
1029
1059
|
}
|
|
1030
1060
|
|
|
1031
1061
|
if (args.skip_hooks !== true) {
|
|
1032
|
-
yield
|
|
1062
|
+
yield _this12.run_hook(results.data, "list", "after");
|
|
1033
1063
|
}
|
|
1034
1064
|
|
|
1035
|
-
results.data =
|
|
1065
|
+
results.data = _this12.filterResultsByColumns(results.data, args.columns);
|
|
1036
1066
|
return results;
|
|
1037
1067
|
} catch (e) {
|
|
1038
1068
|
console.log(e.stack);
|
|
@@ -1042,15 +1072,15 @@ class SpiceModel {
|
|
|
1042
1072
|
}
|
|
1043
1073
|
|
|
1044
1074
|
fetchResults(args, query) {
|
|
1045
|
-
var
|
|
1075
|
+
var _this13 = this;
|
|
1046
1076
|
|
|
1047
1077
|
return _asyncToGenerator(function* () {
|
|
1048
1078
|
if (args.is_custom_query === "true" && args.ids.length > 0) {
|
|
1049
|
-
return yield
|
|
1079
|
+
return yield _this13.database.query(query);
|
|
1050
1080
|
} else if (args.is_full_text === "true") {
|
|
1051
|
-
return yield
|
|
1081
|
+
return yield _this13.database.full_text_search(_this13.type, query || "", args.limit, args.offset, args._join);
|
|
1052
1082
|
} else {
|
|
1053
|
-
var result = yield
|
|
1083
|
+
var result = yield _this13.database.search(_this13.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent, args._join);
|
|
1054
1084
|
return result;
|
|
1055
1085
|
}
|
|
1056
1086
|
})();
|
|
@@ -1067,26 +1097,26 @@ class SpiceModel {
|
|
|
1067
1097
|
}
|
|
1068
1098
|
|
|
1069
1099
|
run_hook(data, op, when, old_data) {
|
|
1070
|
-
var
|
|
1100
|
+
var _this14 = this;
|
|
1071
1101
|
|
|
1072
1102
|
return _asyncToGenerator(function* () {
|
|
1073
1103
|
try {
|
|
1074
|
-
if (
|
|
1104
|
+
if (_this14[_disable_lifecycle_events] == false) {
|
|
1075
1105
|
var resourceLifecycleTriggered = new _ResourceLifecycleTriggered.default({
|
|
1076
1106
|
data: {
|
|
1077
1107
|
data,
|
|
1078
1108
|
operation: op,
|
|
1079
1109
|
when,
|
|
1080
1110
|
old_data,
|
|
1081
|
-
resource:
|
|
1082
|
-
ctx:
|
|
1111
|
+
resource: _this14.type,
|
|
1112
|
+
ctx: _this14[_ctx]
|
|
1083
1113
|
}
|
|
1084
1114
|
});
|
|
1085
1115
|
resourceLifecycleTriggered.dispatch();
|
|
1086
1116
|
}
|
|
1087
1117
|
|
|
1088
|
-
if (
|
|
1089
|
-
for (var i of
|
|
1118
|
+
if (_this14[_hooks] && _this14[_hooks][op] && _this14[_hooks][op][when]) {
|
|
1119
|
+
for (var i of _this14[_hooks][op][when]) {
|
|
1090
1120
|
data = yield i(data, old_data);
|
|
1091
1121
|
}
|
|
1092
1122
|
}
|
|
@@ -1119,7 +1149,7 @@ class SpiceModel {
|
|
|
1119
1149
|
}
|
|
1120
1150
|
|
|
1121
1151
|
mapToObject(data, Class, source_property, store_property, property) {
|
|
1122
|
-
var
|
|
1152
|
+
var _this15 = this;
|
|
1123
1153
|
|
|
1124
1154
|
return _asyncToGenerator(function* () {
|
|
1125
1155
|
var original_is_array = _.isArray(data);
|
|
@@ -1128,41 +1158,45 @@ class SpiceModel {
|
|
|
1128
1158
|
data = Array.of(data);
|
|
1129
1159
|
}
|
|
1130
1160
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
_.each(data, result => {
|
|
1135
|
-
if (_.isString(result[source_property]) && result[source_property] != "") {
|
|
1136
|
-
ids = _.union(ids, [result[source_property]]);
|
|
1137
|
-
}
|
|
1138
|
-
});
|
|
1161
|
+
if (_this15[_level] < global.mapping_dept || global.mapping_dept == "*") {
|
|
1162
|
+
var classes = _.isArray(Class) ? Class : [Class];
|
|
1163
|
+
var ids = [];
|
|
1139
1164
|
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
skip_hooks: true,
|
|
1145
|
-
ids: ids
|
|
1165
|
+
_.each(data, result => {
|
|
1166
|
+
if (_.isString(result[source_property]) && result[source_property] != "") {
|
|
1167
|
+
ids = _.union(ids, [result[source_property]]);
|
|
1168
|
+
}
|
|
1146
1169
|
});
|
|
1147
|
-
}));
|
|
1148
1170
|
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1171
|
+
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
1172
|
+
return new obj(_extends({}, _this15[_args], {
|
|
1173
|
+
skip_cache: _this15[_skip_cache],
|
|
1174
|
+
_level: _this15[_level] + 1
|
|
1175
|
+
})).getMulti({
|
|
1176
|
+
skip_hooks: true,
|
|
1177
|
+
ids: ids
|
|
1178
|
+
});
|
|
1179
|
+
}));
|
|
1180
|
+
|
|
1181
|
+
var ug = _.flatten(_.compact(_.map(returned_all, returned_obj => {
|
|
1182
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
1183
|
+
})));
|
|
1184
|
+
|
|
1185
|
+
data = _.map(data, result => {
|
|
1186
|
+
var result_found = _.find(ug, g => {
|
|
1187
|
+
return g.id == result[source_property];
|
|
1188
|
+
}) || {};
|
|
1189
|
+
result[store_property] = result_found;
|
|
1190
|
+
return result;
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1152
1193
|
|
|
1153
|
-
data = _.map(data, result => {
|
|
1154
|
-
var result_found = _.find(ug, g => {
|
|
1155
|
-
return g.id == result[source_property];
|
|
1156
|
-
}) || {};
|
|
1157
|
-
result[store_property] = result_found;
|
|
1158
|
-
return result;
|
|
1159
|
-
});
|
|
1160
1194
|
return original_is_array ? data : data[0];
|
|
1161
1195
|
})();
|
|
1162
1196
|
}
|
|
1163
1197
|
|
|
1164
1198
|
mapToObjectArray(data, Class, source_property, store_property, property) {
|
|
1165
|
-
var
|
|
1199
|
+
var _this16 = this;
|
|
1166
1200
|
|
|
1167
1201
|
return _asyncToGenerator(function* () {
|
|
1168
1202
|
var original_is_array = _.isArray(data);
|
|
@@ -1171,55 +1205,58 @@ class SpiceModel {
|
|
|
1171
1205
|
data = Array.of(data);
|
|
1172
1206
|
}
|
|
1173
1207
|
|
|
1174
|
-
|
|
1208
|
+
if (_this16[_level] < global.mapping_dept || global.mapping_dept == "*") {
|
|
1209
|
+
var ids = [];
|
|
1175
1210
|
|
|
1176
|
-
|
|
1177
|
-
|
|
1211
|
+
_.each(data, result => {
|
|
1212
|
+
var value = [];
|
|
1178
1213
|
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
if (_.isString(result[source_property])) {
|
|
1184
|
-
value = [result[source_property]];
|
|
1185
|
-
}
|
|
1214
|
+
if (_.isArray(result[source_property])) {
|
|
1215
|
+
value = result[source_property];
|
|
1216
|
+
}
|
|
1186
1217
|
|
|
1187
|
-
|
|
1218
|
+
if (_.isString(result[source_property])) {
|
|
1219
|
+
value = [result[source_property]];
|
|
1220
|
+
}
|
|
1188
1221
|
|
|
1189
|
-
|
|
1190
|
-
});
|
|
1222
|
+
var items = _.filter(value, obj => _.isString(obj) && obj != "");
|
|
1191
1223
|
|
|
1192
|
-
|
|
1193
|
-
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
1194
|
-
return new obj(_extends({}, _this15[_args], {
|
|
1195
|
-
skip_cache: _this15[_skip_cache]
|
|
1196
|
-
})).getMulti({
|
|
1197
|
-
skip_hooks: true,
|
|
1198
|
-
ids: ids
|
|
1224
|
+
ids = _.union(ids, items);
|
|
1199
1225
|
});
|
|
1200
|
-
}));
|
|
1201
1226
|
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1227
|
+
var classes = _.isArray(Class) ? Class : [Class];
|
|
1228
|
+
var returned_all = yield Promise.allSettled(_.map(classes, obj => {
|
|
1229
|
+
return new obj(_extends({}, _this16[_args], {
|
|
1230
|
+
skip_cache: _this16[_skip_cache],
|
|
1231
|
+
_level: _this16[_level] + 1
|
|
1232
|
+
})).getMulti({
|
|
1233
|
+
skip_hooks: true,
|
|
1234
|
+
ids: ids
|
|
1235
|
+
});
|
|
1236
|
+
}));
|
|
1237
|
+
|
|
1238
|
+
var returned_objects = _.flatten(_.compact(_.map(returned_all, returned_obj => {
|
|
1239
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
1240
|
+
})));
|
|
1241
|
+
/* let returned_objects = await new Class().getMulti({
|
|
1206
1242
|
ids: ids,
|
|
1207
|
-
|
|
1243
|
+
}); */
|
|
1208
1244
|
|
|
1209
1245
|
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1246
|
+
_.each(data, result => {
|
|
1247
|
+
if (_.isString(result[store_property])) {
|
|
1248
|
+
result[store_property] = [result[store_property]];
|
|
1249
|
+
}
|
|
1214
1250
|
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1251
|
+
if (!_.has(result, source_property)) {
|
|
1252
|
+
result[source_property] = [];
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1219
1255
|
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1256
|
+
result[store_property] = _.map(result[source_property], pid => _.find(returned_objects, p => p.id === pid));
|
|
1257
|
+
result[store_property] = _.reject(result[store_property], obj => obj === null || obj === undefined);
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1223
1260
|
|
|
1224
1261
|
return original_is_array ? data : data[0];
|
|
1225
1262
|
})();
|
|
@@ -1254,7 +1291,7 @@ class SpiceModel {
|
|
|
1254
1291
|
}
|
|
1255
1292
|
|
|
1256
1293
|
createMofifier(properties) {
|
|
1257
|
-
var
|
|
1294
|
+
var _this17 = this;
|
|
1258
1295
|
|
|
1259
1296
|
var _loop = function _loop(i) {
|
|
1260
1297
|
if (properties[i].map) {
|
|
@@ -1265,11 +1302,11 @@ class SpiceModel {
|
|
|
1265
1302
|
case String:
|
|
1266
1303
|
case "string":
|
|
1267
1304
|
{
|
|
1268
|
-
|
|
1305
|
+
_this17.addModifier({
|
|
1269
1306
|
when: properties[i].map.when || "read",
|
|
1270
1307
|
execute: function () {
|
|
1271
1308
|
var _execute = _asyncToGenerator(function* (data) {
|
|
1272
|
-
return yield
|
|
1309
|
+
return yield _this17.mapToObject(data, _.isString(properties[i].map.reference) ? spice.models[properties[i].map.reference] : properties[i].map.reference, i, properties[i].map.destination || i, properties[i]);
|
|
1273
1310
|
});
|
|
1274
1311
|
|
|
1275
1312
|
function execute(_x) {
|
|
@@ -1286,11 +1323,11 @@ class SpiceModel {
|
|
|
1286
1323
|
case Array:
|
|
1287
1324
|
case "array":
|
|
1288
1325
|
{
|
|
1289
|
-
|
|
1326
|
+
_this17.addModifier({
|
|
1290
1327
|
when: properties[i].map.when || "read",
|
|
1291
1328
|
execute: function () {
|
|
1292
1329
|
var _execute2 = _asyncToGenerator(function* (data) {
|
|
1293
|
-
return yield
|
|
1330
|
+
return yield _this17.mapToObjectArray(data, _.isString(properties[i].map.reference) ? spice.models[properties[i].map.reference] : properties[i].map.reference, i, properties[i].map.destination || i, properties[i]);
|
|
1294
1331
|
});
|
|
1295
1332
|
|
|
1296
1333
|
function execute(_x2) {
|
|
@@ -1322,7 +1359,7 @@ class SpiceModel {
|
|
|
1322
1359
|
}
|
|
1323
1360
|
|
|
1324
1361
|
do_serialize(data, type, old_data, args, path_to_be_removed) {
|
|
1325
|
-
var
|
|
1362
|
+
var _this18 = this;
|
|
1326
1363
|
|
|
1327
1364
|
return _asyncToGenerator(function* () {
|
|
1328
1365
|
//console.log("CTX INside Model DO", this[_ctx]);
|
|
@@ -1332,16 +1369,16 @@ class SpiceModel {
|
|
|
1332
1369
|
path_to_be_removed = [];
|
|
1333
1370
|
}
|
|
1334
1371
|
|
|
1335
|
-
if (
|
|
1336
|
-
if (
|
|
1337
|
-
|
|
1372
|
+
if (_this18.shouldSerializerRun(args, type)) {
|
|
1373
|
+
if (_this18.type != "" && _this18.type != undefined && _this18[_external_modifier_loaded] != true) {
|
|
1374
|
+
_this18.addExternalModifiers(_this18.type);
|
|
1338
1375
|
|
|
1339
|
-
|
|
1376
|
+
_this18[_external_modifier_loaded] = true;
|
|
1340
1377
|
}
|
|
1341
1378
|
|
|
1342
|
-
for (var i of
|
|
1379
|
+
for (var i of _this18[_serializers][type]["modifiers"]) {
|
|
1343
1380
|
try {
|
|
1344
|
-
data = yield i(data, old_data,
|
|
1381
|
+
data = yield i(data, old_data, _this18[_ctx], _this18.type);
|
|
1345
1382
|
} catch (e) {
|
|
1346
1383
|
console.log(e.stack);
|
|
1347
1384
|
}
|
|
@@ -1357,12 +1394,12 @@ class SpiceModel {
|
|
|
1357
1394
|
|
|
1358
1395
|
var defaults = {};
|
|
1359
1396
|
|
|
1360
|
-
for (var _i in
|
|
1361
|
-
if (
|
|
1362
|
-
defaults[_i] = _.isFunction(
|
|
1397
|
+
for (var _i in _this18.props) {
|
|
1398
|
+
if (_this18.props[_i].defaults != undefined && _this18.props[_i].defaults[type] != undefined && _this18.props[_i].defaults[type] != undefined) {
|
|
1399
|
+
defaults[_i] = _.isFunction(_this18.props[_i].defaults[type]) ? _this18.props[_i].defaults[type]({
|
|
1363
1400
|
old_data: data,
|
|
1364
1401
|
new_data: old_data
|
|
1365
|
-
}) :
|
|
1402
|
+
}) : _this18.props[_i].defaults[type];
|
|
1366
1403
|
}
|
|
1367
1404
|
} // apply defaults
|
|
1368
1405
|
|
|
@@ -1375,8 +1412,8 @@ class SpiceModel {
|
|
|
1375
1412
|
if (type == "read") {
|
|
1376
1413
|
var props_to_clean = ["deleted", "type", ...path_to_be_removed];
|
|
1377
1414
|
|
|
1378
|
-
for (var _i3 in
|
|
1379
|
-
if (
|
|
1415
|
+
for (var _i3 in _this18.props) {
|
|
1416
|
+
if (_this18.props[_i3].hide) {
|
|
1380
1417
|
props_to_clean.push(_i3);
|
|
1381
1418
|
}
|
|
1382
1419
|
}
|
package/package.json
CHANGED
package/src/models/SpiceModel.js
CHANGED
|
@@ -18,9 +18,12 @@ var SDate = require("sonover-date"),
|
|
|
18
18
|
_disable_lifecycle_events = Symbol(),
|
|
19
19
|
_external_modifier_loaded = Symbol(),
|
|
20
20
|
_skip_cache = Symbol(),
|
|
21
|
-
_serializers = Symbol()
|
|
21
|
+
_serializers = Symbol(),
|
|
22
|
+
_level = Symbol(),
|
|
23
|
+
_mapping_dept = Symbol();
|
|
22
24
|
|
|
23
25
|
//const _type = Symbol("type");
|
|
26
|
+
|
|
24
27
|
let that;
|
|
25
28
|
if (!Promise.allSettled) {
|
|
26
29
|
Promise.allSettled = (promises) =>
|
|
@@ -44,6 +47,7 @@ export default class SpiceModel {
|
|
|
44
47
|
var dbtype =
|
|
45
48
|
spice.config.database.connections[args.connection].type || "couchbase";
|
|
46
49
|
let Database = require(`spice-${dbtype}`);
|
|
50
|
+
this[_mapping_dept] = args?.args?.mapping_dept || 100;
|
|
47
51
|
this.type = "";
|
|
48
52
|
this.collection = args.connection;
|
|
49
53
|
this[_args] = args.args;
|
|
@@ -52,6 +56,7 @@ export default class SpiceModel {
|
|
|
52
56
|
args.args?.disable_lifecycle_events || false;
|
|
53
57
|
this[_ctx] = args?.args?.ctx;
|
|
54
58
|
this[_skip_cache] = args?.args?.skip_cache || false;
|
|
59
|
+
this[_level] = args?.args?._level || 0;
|
|
55
60
|
this[_hooks] = {
|
|
56
61
|
create: {
|
|
57
62
|
before: [],
|
|
@@ -474,6 +479,8 @@ export default class SpiceModel {
|
|
|
474
479
|
|
|
475
480
|
async get(args) {
|
|
476
481
|
try {
|
|
482
|
+
if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
|
|
483
|
+
|
|
477
484
|
if (!args) {
|
|
478
485
|
args = {};
|
|
479
486
|
}
|
|
@@ -662,7 +669,7 @@ export default class SpiceModel {
|
|
|
662
669
|
}
|
|
663
670
|
let db_data = cover_obj.new || this;
|
|
664
671
|
|
|
665
|
-
await this.database.update(args.id, db_data);
|
|
672
|
+
await this.database.update(args.id, db_data, args._ttl);
|
|
666
673
|
this.setMonitor();
|
|
667
674
|
|
|
668
675
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
@@ -721,7 +728,7 @@ export default class SpiceModel {
|
|
|
721
728
|
await this.run_hook(workingForm, "create", "before");
|
|
722
729
|
}
|
|
723
730
|
|
|
724
|
-
let results = await this.database.insert(id, workingForm, args.
|
|
731
|
+
let results = await this.database.insert(id, workingForm, args._ttl);
|
|
725
732
|
this.setMonitor();
|
|
726
733
|
|
|
727
734
|
if (args.skip_read_serialize != true && args.skip_serialize != true) {
|
|
@@ -750,6 +757,22 @@ export default class SpiceModel {
|
|
|
750
757
|
}
|
|
751
758
|
}
|
|
752
759
|
|
|
760
|
+
async touch(args) {
|
|
761
|
+
let item_exist = await this.exist(args.id);
|
|
762
|
+
|
|
763
|
+
if (!item_exist) {
|
|
764
|
+
throw new Error(`${this.type} does not exist.`);
|
|
765
|
+
}
|
|
766
|
+
try {
|
|
767
|
+
let touch_response = {};
|
|
768
|
+
touch_response = await this.database.touch(args.id, args._ttl);
|
|
769
|
+
return touch_response;
|
|
770
|
+
} catch (e) {
|
|
771
|
+
console.log(e.stack);
|
|
772
|
+
throw e;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
753
776
|
async delete(args) {
|
|
754
777
|
let item_exist = await this.exist(args.id);
|
|
755
778
|
|
|
@@ -854,9 +877,13 @@ export default class SpiceModel {
|
|
|
854
877
|
nesting.type === Array ||
|
|
855
878
|
nesting.type === "array"
|
|
856
879
|
) {
|
|
857
|
-
return `LEFT NEST \`${fixCollection(nesting.reference)}\` AS \`${
|
|
880
|
+
return `LEFT NEST \`${fixCollection(nesting.reference)}\` AS \`${
|
|
881
|
+
nesting.alias
|
|
882
|
+
}\` ON KEYS \`${this.type}\`.\`${nesting.alias}\``;
|
|
858
883
|
} else {
|
|
859
|
-
return `LEFT JOIN \`${fixCollection(nesting.reference)}\` AS \`${
|
|
884
|
+
return `LEFT JOIN \`${fixCollection(nesting.reference)}\` AS \`${
|
|
885
|
+
nesting.alias
|
|
886
|
+
}\` ON KEYS \`${this.type}\`.\`${nesting.alias}\``;
|
|
860
887
|
}
|
|
861
888
|
})
|
|
862
889
|
.join(" ");
|
|
@@ -875,6 +902,7 @@ export default class SpiceModel {
|
|
|
875
902
|
async list(args = {}) {
|
|
876
903
|
try {
|
|
877
904
|
args.columns = this.prepColumns(args.columns);
|
|
905
|
+
if (args.mapping_dept) this[_mapping_dept] = args.mapping_dept;
|
|
878
906
|
|
|
879
907
|
const nestings = [
|
|
880
908
|
...this.extractNestings(args?.query, this.type),
|
|
@@ -1055,46 +1083,51 @@ export default class SpiceModel {
|
|
|
1055
1083
|
if (!original_is_array) {
|
|
1056
1084
|
data = Array.of(data);
|
|
1057
1085
|
}
|
|
1086
|
+
this[_mapping_dept];
|
|
1058
1087
|
|
|
1059
|
-
|
|
1088
|
+
if (this[_level] + 1 < this[_mapping_dept]) {
|
|
1089
|
+
let classes = _.isArray(Class) ? Class : [Class];
|
|
1060
1090
|
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1091
|
+
let ids = [];
|
|
1092
|
+
_.each(data, (result) => {
|
|
1093
|
+
if (
|
|
1094
|
+
_.isString(result[source_property]) &&
|
|
1095
|
+
result[source_property] != ""
|
|
1096
|
+
) {
|
|
1097
|
+
ids = _.union(ids, [result[source_property]]);
|
|
1098
|
+
}
|
|
1099
|
+
});
|
|
1070
1100
|
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
let ug = _.flatten(
|
|
1083
|
-
_.compact(
|
|
1084
|
-
_.map(returned_all, (returned_obj) => {
|
|
1085
|
-
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
1101
|
+
var returned_all = await Promise.allSettled(
|
|
1102
|
+
_.map(classes, (obj) => {
|
|
1103
|
+
return new obj({
|
|
1104
|
+
...this[_args],
|
|
1105
|
+
skip_cache: this[_skip_cache],
|
|
1106
|
+
_level: this[_level] + 1,
|
|
1107
|
+
mapping_dept: this[_mapping_dept],
|
|
1108
|
+
}).getMulti({
|
|
1109
|
+
skip_hooks: true,
|
|
1110
|
+
ids: ids,
|
|
1111
|
+
});
|
|
1086
1112
|
})
|
|
1087
|
-
)
|
|
1088
|
-
|
|
1113
|
+
);
|
|
1114
|
+
let ug = _.flatten(
|
|
1115
|
+
_.compact(
|
|
1116
|
+
_.map(returned_all, (returned_obj) => {
|
|
1117
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
1118
|
+
})
|
|
1119
|
+
)
|
|
1120
|
+
);
|
|
1089
1121
|
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1122
|
+
data = _.map(data, (result) => {
|
|
1123
|
+
let result_found =
|
|
1124
|
+
_.find(ug, (g) => {
|
|
1125
|
+
return g.id == result[source_property];
|
|
1126
|
+
}) || {};
|
|
1127
|
+
result[store_property] = result_found;
|
|
1128
|
+
return result;
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1098
1131
|
return original_is_array ? data : data[0];
|
|
1099
1132
|
}
|
|
1100
1133
|
|
|
@@ -1109,64 +1142,68 @@ export default class SpiceModel {
|
|
|
1109
1142
|
if (!original_is_array) {
|
|
1110
1143
|
data = Array.of(data);
|
|
1111
1144
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
if (_.isArray(result[source_property])) {
|
|
1117
|
-
value = result[source_property];
|
|
1118
|
-
}
|
|
1145
|
+
if (this[_level] + 1 < this[_mapping_dept]) {
|
|
1146
|
+
let ids = [];
|
|
1147
|
+
_.each(data, (result) => {
|
|
1148
|
+
let value = [];
|
|
1119
1149
|
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1150
|
+
if (_.isArray(result[source_property])) {
|
|
1151
|
+
value = result[source_property];
|
|
1152
|
+
}
|
|
1123
1153
|
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1154
|
+
if (_.isString(result[source_property])) {
|
|
1155
|
+
value = [result[source_property]];
|
|
1156
|
+
}
|
|
1127
1157
|
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
return new obj({
|
|
1132
|
-
...this[_args],
|
|
1133
|
-
skip_cache: this[_skip_cache],
|
|
1134
|
-
}).getMulti({
|
|
1135
|
-
skip_hooks: true,
|
|
1136
|
-
ids: ids,
|
|
1137
|
-
});
|
|
1138
|
-
})
|
|
1139
|
-
);
|
|
1158
|
+
let items = _.filter(value, (obj) => _.isString(obj) && obj != "");
|
|
1159
|
+
ids = _.union(ids, items);
|
|
1160
|
+
});
|
|
1140
1161
|
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
_.map(
|
|
1144
|
-
|
|
1162
|
+
let classes = _.isArray(Class) ? Class : [Class];
|
|
1163
|
+
var returned_all = await Promise.allSettled(
|
|
1164
|
+
_.map(classes, (obj) => {
|
|
1165
|
+
return new obj({
|
|
1166
|
+
...this[_args],
|
|
1167
|
+
skip_cache: this[_skip_cache],
|
|
1168
|
+
_level: this[_level] + 1,
|
|
1169
|
+
mapping_dept: this[_mapping_dept],
|
|
1170
|
+
}).getMulti({
|
|
1171
|
+
skip_hooks: true,
|
|
1172
|
+
ids: ids,
|
|
1173
|
+
});
|
|
1145
1174
|
})
|
|
1146
|
-
)
|
|
1147
|
-
);
|
|
1175
|
+
);
|
|
1148
1176
|
|
|
1149
|
-
|
|
1177
|
+
var returned_objects = _.flatten(
|
|
1178
|
+
_.compact(
|
|
1179
|
+
_.map(returned_all, (returned_obj) => {
|
|
1180
|
+
if (returned_obj.status == "fulfilled") return returned_obj.value;
|
|
1181
|
+
})
|
|
1182
|
+
)
|
|
1183
|
+
);
|
|
1184
|
+
|
|
1185
|
+
/* let returned_objects = await new Class().getMulti({
|
|
1150
1186
|
ids: ids,
|
|
1151
1187
|
}); */
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1188
|
+
_.each(data, (result) => {
|
|
1189
|
+
if (_.isString(result[store_property])) {
|
|
1190
|
+
result[store_property] = [result[store_property]];
|
|
1191
|
+
}
|
|
1156
1192
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1193
|
+
if (!_.has(result, source_property)) {
|
|
1194
|
+
result[source_property] = [];
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1161
1197
|
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1198
|
+
result[store_property] = _.map(result[source_property], (pid) =>
|
|
1199
|
+
_.find(returned_objects, (p) => p.id === pid)
|
|
1200
|
+
);
|
|
1201
|
+
result[store_property] = _.reject(
|
|
1202
|
+
result[store_property],
|
|
1203
|
+
(obj) => obj === null || obj === undefined
|
|
1204
|
+
);
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1170
1207
|
return original_is_array ? data : data[0];
|
|
1171
1208
|
}
|
|
1172
1209
|
|