tspace-mysql 1.0.2 → 1.0.3

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.
@@ -288,6 +288,7 @@ var Model = /** @class */ (function (_super) {
288
288
  return __generator(this, function (_a) {
289
289
  switch (_a.label) {
290
290
  case 0:
291
+ this.$db.set('SOFT_DELETE', false);
291
292
  this.whereNotNull(this._isPatternSnakeCase() ? 'deleted_at' : 'deletedAt');
292
293
  sql = this._getSQLModel();
293
294
  return [4 /*yield*/, this._exec(sql, 'GET')];
@@ -687,6 +688,8 @@ var Model = /** @class */ (function (_super) {
687
688
  return __generator(this, function (_g) {
688
689
  switch (_g.label) {
689
690
  case 0:
691
+ limit = +limit;
692
+ page = +page;
690
693
  if ((_b = this.$logger) === null || _b === void 0 ? void 0 : _b.check('limit'))
691
694
  throw new Error("this [pagination] can't used [limit] method");
692
695
  if (!((_c = this.$db.get('EXCEPT')) === null || _c === void 0 ? void 0 : _c.length)) return [3 /*break*/, 2];
@@ -710,31 +713,12 @@ var Model = /** @class */ (function (_super) {
710
713
  });
711
714
  };
712
715
  Model.prototype.paginate = function (_a) {
713
- var _b, _c;
714
- var _d = _a === void 0 ? {} : _a, _e = _d.limit, limit = _e === void 0 ? 15 : _e, _f = _d.page, page = _f === void 0 ? 1 : _f;
716
+ var _b = _a === void 0 ? {} : _a, _c = _b.limit, limit = _c === void 0 ? 15 : _c, _d = _b.page, page = _d === void 0 ? 1 : _d;
715
717
  return __awaiter(this, void 0, void 0, function () {
716
- var offset, sql;
717
- return __generator(this, function (_g) {
718
- switch (_g.label) {
719
- case 0:
720
- if ((_b = this.$logger) === null || _b === void 0 ? void 0 : _b.check('limit'))
721
- throw new Error("this [pagination] can't used [limit] method");
722
- if (!((_c = this.$db.get('EXCEPT')) === null || _c === void 0 ? void 0 : _c.length)) return [3 /*break*/, 2];
723
- return [4 /*yield*/, this._exceptColumns()];
724
- case 1:
725
- _g.sent();
726
- _g.label = 2;
727
- case 2:
728
- offset = (page - 1) * limit;
729
- this.$db.set('PER_PAGE', limit);
730
- this.$db.set('PAGE', page);
731
- sql = this._getSQLModel();
732
- if (!sql.includes(this.$utils().constants('LIMIT')))
733
- sql = "".concat(sql, " ").concat(this.$utils().constants('LIMIT'), " ").concat(limit, " ").concat(this.$utils().constants('OFFSET'), " ").concat(offset);
734
- else
735
- sql = sql.replace(this.$db.get('LIMIT'), "".concat(this.$utils().constants('LIMIT'), " ").concat(limit, " ").concat(this.$utils().constants('OFFSET'), " ").concat(offset));
736
- return [4 /*yield*/, this._exec(sql, 'PAGINATION')];
737
- case 3: return [2 /*return*/, _g.sent()];
718
+ return __generator(this, function (_e) {
719
+ switch (_e.label) {
720
+ case 0: return [4 /*yield*/, this.pagination({ limit: limit, page: page })];
721
+ case 1: return [2 /*return*/, _e.sent()];
738
722
  }
739
723
  });
740
724
  });
@@ -928,8 +912,8 @@ var Model = /** @class */ (function (_super) {
928
912
  Model.prototype._isPatternSnakeCase = function () {
929
913
  return this.$db.get('PATTERN') === this.$utils().constants('PATTERN').snake_case;
930
914
  };
931
- Model.prototype._classToTableName = function (className, belongsTo) {
932
- if (belongsTo === void 0) { belongsTo = false; }
915
+ Model.prototype._classToTableName = function (className, _a) {
916
+ var _b = _a === void 0 ? {} : _a, _c = _b.belongsTo, belongsTo = _c === void 0 ? false : _c;
933
917
  if (className == null)
934
918
  className = this.constructor.name;
935
919
  var tb = className.replace(/([A-Z])/g, function (str) { return '_' + str.toLowerCase(); }).slice(1);
@@ -955,15 +939,19 @@ var Model = /** @class */ (function (_super) {
955
939
  throw new Error('model missing');
956
940
  var patternId = this._isPatternSnakeCase() ? '_id' : 'Id';
957
941
  var pk = data.pk ? data.pk : 'id';
958
- var fk = data.fk ? data.fk : this._classToTableName(null, true) + patternId;
942
+ var fk = data.fk ? data.fk : this._classToTableName(null, { belongsTo: true }) + patternId;
959
943
  if (data.pk == null && data.fk == null && relation === this.$utils().constants('RELATIONSHIP').belongsTo) {
960
944
  fk = pk;
961
- pk = this._classToTableName(model, true) + patternId;
945
+ pk = this._classToTableName(model, { belongsTo: true }) + patternId;
962
946
  }
963
947
  return { name: name, as: as, relation: relation, table: table, pk: pk, fk: fk, model: model };
964
948
  };
965
949
  Model.prototype._getSQLModel = function () {
966
950
  var sql = [];
951
+ if (this.$db.get('SOFT_DELETE')) {
952
+ var deletedAt = this._isPatternSnakeCase() ? 'deleted_at' : 'deletedAt';
953
+ this.whereNull(deletedAt);
954
+ }
967
955
  if (this.$db.get('INSERT')) {
968
956
  sql = [
969
957
  this.$db.get('INSERT')
@@ -1038,7 +1026,7 @@ var Model = /** @class */ (function (_super) {
1038
1026
  };
1039
1027
  Model.prototype._exec = function (sql, type) {
1040
1028
  return __awaiter(this, void 0, void 0, function () {
1041
- var mains, emptyData, relations, relations_1, relations_1_1, relation, subs, e_1_1, resultData, err_3;
1029
+ var result, emptyData, relations, relations_1, relations_1_1, relation, dataFromChilds, e_1_1, resultData, err_3;
1042
1030
  var e_1, _a;
1043
1031
  return __generator(this, function (_b) {
1044
1032
  switch (_b.label) {
@@ -1046,9 +1034,9 @@ var Model = /** @class */ (function (_super) {
1046
1034
  _b.trys.push([0, 12, , 13]);
1047
1035
  return [4 /*yield*/, this._queryStatementModel(sql)];
1048
1036
  case 1:
1049
- mains = _b.sent();
1050
- emptyData = this._returnEmpty(type, mains);
1051
- if (!mains.length)
1037
+ result = _b.sent();
1038
+ emptyData = this._returnEmpty(type, result);
1039
+ if (!result.length)
1052
1040
  return [2 /*return*/, emptyData];
1053
1041
  relations = this.$db.get('WITH');
1054
1042
  if (!relations.length) return [3 /*break*/, 11];
@@ -1061,14 +1049,14 @@ var Model = /** @class */ (function (_super) {
1061
1049
  if (!!relations_1_1.done) return [3 /*break*/, 8];
1062
1050
  relation = relations_1_1.value;
1063
1051
  if (!(relation.relation === this.$utils().constants('RELATIONSHIP').belongsToMany)) return [3 /*break*/, 5];
1064
- return [4 /*yield*/, this._belongsToMany(type, mains, relation)];
1052
+ return [4 /*yield*/, this._belongsToMany(type, result, relation)];
1065
1053
  case 4:
1066
- mains = _b.sent();
1054
+ result = _b.sent();
1067
1055
  return [3 /*break*/, 7];
1068
- case 5: return [4 /*yield*/, this._relation(mains, relation)];
1056
+ case 5: return [4 /*yield*/, this._relation(result, relation)];
1069
1057
  case 6:
1070
- subs = _b.sent();
1071
- mains = this._relationFilter(mains, subs, relation);
1058
+ dataFromChilds = _b.sent();
1059
+ result = this._relationFilter(result, dataFromChilds, relation);
1072
1060
  _b.label = 7;
1073
1061
  case 7:
1074
1062
  relations_1_1 = relations_1.next();
@@ -1086,8 +1074,8 @@ var Model = /** @class */ (function (_super) {
1086
1074
  return [7 /*endfinally*/];
1087
1075
  case 11:
1088
1076
  if (this.$db.get('HIDDEN').length)
1089
- this._hiddenColumnModel(mains);
1090
- resultData = this._returnResult(type, mains);
1077
+ this._hiddenColumnModel(result);
1078
+ resultData = this._returnResult(type, result);
1091
1079
  return [2 /*return*/, resultData || emptyData];
1092
1080
  case 12:
1093
1081
  err_3 = _b.sent();
@@ -1097,17 +1085,17 @@ var Model = /** @class */ (function (_super) {
1097
1085
  });
1098
1086
  });
1099
1087
  };
1100
- Model.prototype._execGroup = function (mains, type) {
1088
+ Model.prototype._execGroup = function (dataParents, type) {
1101
1089
  var _a;
1102
1090
  if (type === void 0) { type = 'GET'; }
1103
1091
  return __awaiter(this, void 0, void 0, function () {
1104
- var emptyData, relations, relations_2, relations_2_1, relation, subs, e_2_1, resultData;
1092
+ var emptyData, relations, relations_2, relations_2_1, relation, dataChilds, e_2_1, resultData;
1105
1093
  var e_2, _b;
1106
1094
  return __generator(this, function (_c) {
1107
1095
  switch (_c.label) {
1108
1096
  case 0:
1109
- emptyData = this._returnEmpty(type, mains);
1110
- if (!mains.length)
1097
+ emptyData = this._returnEmpty(type, dataParents);
1098
+ if (!dataParents.length)
1111
1099
  return [2 /*return*/, emptyData];
1112
1100
  relations = this.$db.get('WITH');
1113
1101
  if (!relations.length) return [3 /*break*/, 8];
@@ -1119,12 +1107,13 @@ var Model = /** @class */ (function (_super) {
1119
1107
  case 2:
1120
1108
  if (!!relations_2_1.done) return [3 /*break*/, 5];
1121
1109
  relation = relations_2_1.value;
1122
- if (relation.relation === this.$utils().constants('RELATIONSHIP').belongsToMany)
1123
- return [2 /*return*/, this._belongsToMany(type, mains, relation)];
1124
- return [4 /*yield*/, this._relation(mains, relation)];
1110
+ if (relation.relation === this.$utils().constants('RELATIONSHIP').belongsToMany) {
1111
+ return [2 /*return*/, this._belongsToMany(type, dataParents, relation)];
1112
+ }
1113
+ return [4 /*yield*/, this._relation(dataParents, relation)];
1125
1114
  case 3:
1126
- subs = _c.sent();
1127
- mains = this._relationFilter(mains, subs, relation);
1115
+ dataChilds = _c.sent();
1116
+ dataParents = this._relationFilter(dataParents, dataChilds, relation);
1128
1117
  _c.label = 4;
1129
1118
  case 4:
1130
1119
  relations_2_1 = relations_2.next();
@@ -1142,170 +1131,172 @@ var Model = /** @class */ (function (_super) {
1142
1131
  return [7 /*endfinally*/];
1143
1132
  case 8:
1144
1133
  if ((_a = this.$db.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
1145
- this._hiddenColumnModel(mains);
1146
- resultData = this._returnResult(type, mains);
1134
+ this._hiddenColumnModel(dataParents);
1135
+ resultData = this._returnResult(type, dataParents);
1147
1136
  return [2 /*return*/, resultData || emptyData];
1148
1137
  }
1149
1138
  });
1150
1139
  });
1151
1140
  };
1152
- Model.prototype._relationFilter = function (mains, subs, relations) {
1141
+ Model.prototype._relationFilter = function (dataParents, dataChilds, relations) {
1153
1142
  var _this = this;
1154
1143
  var _a = this._valueInRelation(relations), name = _a.name, as = _a.as, relation = _a.relation, pk = _a.pk, fk = _a.fk;
1155
1144
  var keyRelation = as !== null && as !== void 0 ? as : name;
1156
- mains.forEach(function (main) {
1145
+ dataParents.forEach(function (dataPerent) {
1157
1146
  if (relation === _this.$utils().constants('RELATIONSHIP').hasOne || relation === _this.$utils().constants('RELATIONSHIP').belongsTo)
1158
- main[keyRelation] = null;
1147
+ dataPerent[keyRelation] = null;
1159
1148
  else
1160
- main[keyRelation] = [];
1161
- if (subs.length) {
1162
- subs.forEach(function (sub) {
1163
- if (sub[fk] === main[pk]) {
1149
+ dataPerent[keyRelation] = [];
1150
+ if (dataChilds.length) {
1151
+ dataChilds.forEach(function (sub) {
1152
+ if (sub[fk] === dataPerent[pk]) {
1164
1153
  if (relation === _this.$utils().constants('RELATIONSHIP').hasOne || relation === _this.$utils().constants('RELATIONSHIP').belongsTo) {
1165
- main[keyRelation] = main[keyRelation] || sub;
1154
+ dataPerent[keyRelation] = dataPerent[keyRelation] || sub;
1166
1155
  }
1167
1156
  else {
1168
- if (main[keyRelation] == null)
1169
- main[keyRelation] = [];
1170
- main[keyRelation].push(sub);
1157
+ if (dataPerent[keyRelation] == null)
1158
+ dataPerent[keyRelation] = [];
1159
+ dataPerent[keyRelation].push(sub);
1171
1160
  }
1172
1161
  }
1173
1162
  });
1174
1163
  }
1175
1164
  });
1176
1165
  if (this.$db.get('WITH_EXISTS')) {
1177
- return mains.filter(function (main) {
1178
- if (Array.isArray(main[keyRelation]))
1179
- return main[keyRelation].length;
1180
- return main[keyRelation] != null;
1166
+ return dataParents.filter(function (dataPerent) {
1167
+ if (Array.isArray(dataPerent[keyRelation]))
1168
+ return dataPerent[keyRelation].length;
1169
+ return dataPerent[keyRelation] != null;
1181
1170
  });
1182
1171
  }
1183
- return mains;
1172
+ return dataParents;
1184
1173
  };
1185
- Model.prototype._relation = function (mains, relations) {
1174
+ Model.prototype._relation = function (parentData, relation) {
1186
1175
  return __awaiter(this, void 0, void 0, function () {
1187
- var _a, pk, fk, pkId, mainId, query, subs;
1176
+ var _a, pk, fk, pkId, dataPerentId, query, childData;
1188
1177
  return __generator(this, function (_b) {
1189
1178
  switch (_b.label) {
1190
1179
  case 0:
1191
- if (!Object.keys(relations).length)
1180
+ if (!Object.keys(relation).length)
1192
1181
  return [2 /*return*/, []];
1193
- _a = this._valueInRelation(relations), pk = _a.pk, fk = _a.fk;
1194
- pkId = mains.map(function (main) { return main[pk]; }).filter(function (data) { return data != null; });
1195
- mainId = Array.from(new Set(pkId)) || [];
1196
- if (!mainId.length && !this.$db.get('WITH_EXISTS'))
1182
+ _a = this._valueInRelation(relation), pk = _a.pk, fk = _a.fk;
1183
+ pkId = parentData.map(function (dataPerent) { return dataPerent[pk]; }).filter(function (data) { return data != null; });
1184
+ dataPerentId = Array.from(new Set(pkId)) || [];
1185
+ if (!dataPerentId.length && !this.$db.get('WITH_EXISTS'))
1197
1186
  throw new Error("can't relationship without primary or foreign key");
1198
- if (!mainId.length && this.$db.get('WITH_EXISTS'))
1187
+ if (!dataPerentId.length && this.$db.get('WITH_EXISTS'))
1199
1188
  return [2 /*return*/, []];
1200
- return [4 /*yield*/, relations.query];
1189
+ return [4 /*yield*/, relation.query];
1201
1190
  case 1:
1202
1191
  query = _b.sent();
1203
- return [4 /*yield*/, query.whereIn(fk, mainId).debug(this.$db.get('DEBUG')).get()];
1192
+ if (query == null)
1193
+ throw new Error("unknow callback queries in [relation : ".concat(relation.name, "]"));
1194
+ return [4 /*yield*/, query.whereIn(fk, dataPerentId).debug(this.$db.get('DEBUG')).get()];
1204
1195
  case 2:
1205
- subs = _b.sent();
1206
- return [2 /*return*/, subs];
1196
+ childData = _b.sent();
1197
+ return [2 /*return*/, childData];
1207
1198
  }
1208
1199
  });
1209
1200
  });
1210
1201
  };
1211
- Model.prototype._belongsToMany = function (type, mains, dataRelation) {
1202
+ Model.prototype._belongsToMany = function (type, dataFromParent, relation) {
1212
1203
  var _a, _b;
1213
1204
  return __awaiter(this, void 0, void 0, function () {
1214
- var _c, name_1, pk_1, fk_1, pkId, mainId, local, modelOther, other_1, pivotTable, otherPk_1, otherFk_1, sqlSubs, subs_1, otherId, otherArrId, otherSubs_1, err_4, _d, name_2, pk_2, fk_2, pkId, mainId, local, modelOther, other_2, pivotTable, otherPk_2, otherFk_2, sqlSubs, subs_2, otherId, otherArrId, otherSubs_2, err_5;
1205
+ var _c, name_1, pk_1, fk_1, pkId, dataPerentId, local, modelOther, other_1, pivotTable, otherPk_1, otherFk_1, sqldataChilds, dataChilds_1, otherId, otherArrId, otherdataChilds_1, err_4, _d, name_2, pk_2, fk_2, pkId, dataPerentId, local, modelOther, other_2, pivotTable, otherPk_2, otherFk_2, sqldataChilds, dataChilds_2, otherId, otherArrId, otherdataChilds_2, err_5;
1215
1206
  return __generator(this, function (_e) {
1216
1207
  switch (_e.label) {
1217
1208
  case 0:
1218
1209
  _e.trys.push([0, 3, , 9]);
1219
- _c = this._valueInRelation(dataRelation), name_1 = _c.name, pk_1 = _c.pk, fk_1 = _c.fk;
1220
- pkId = mains.map(function (main) { return main[pk_1]; }).filter(function (data) { return data != null; });
1221
- mainId = Array.from(new Set(pkId)).join(',') || [];
1222
- if (!mainId.length)
1210
+ _c = this._valueInRelation(relation), name_1 = _c.name, pk_1 = _c.pk, fk_1 = _c.fk;
1211
+ pkId = dataFromParent.map(function (dataPerent) { return dataPerent[pk_1]; }).filter(function (data) { return data != null; });
1212
+ dataPerentId = Array.from(new Set(pkId)).join(',') || [];
1213
+ if (!dataPerentId.length)
1223
1214
  throw new Error("can't relationship without primary or foreign key");
1224
1215
  local = this.$utils().columnRelation(this.constructor.name);
1225
- modelOther = new dataRelation.model();
1226
- other_1 = this._classToTableName(modelOther.constructor.name, true);
1227
- pivotTable = (_a = dataRelation.freezeTable) !== null && _a !== void 0 ? _a : "".concat(local, "_").concat(other_1);
1216
+ modelOther = new relation.model();
1217
+ other_1 = this._classToTableName(modelOther.constructor.name, { belongsTo: true });
1218
+ pivotTable = (_a = relation.freezeTable) !== null && _a !== void 0 ? _a : "".concat(local, "_").concat(other_1);
1228
1219
  pk_1 = 'id';
1229
1220
  fk_1 = this._isPatternSnakeCase() ? "".concat(local, "_id") : "".concat(local, "Id");
1230
1221
  otherPk_1 = 'id';
1231
1222
  otherFk_1 = this._isPatternSnakeCase() ? "".concat(other_1, "_id") : "".concat(other_1, "Id");
1232
- sqlSubs = "".concat(this.$utils().constants('SELECT'), " * ").concat(this.$utils().constants('FROM'), " ").concat(pivotTable, " ").concat(this.$utils().constants('WHERE'), " ").concat(fk_1, " ").concat(this.$utils().constants('IN'), " (").concat(mainId, ")");
1233
- return [4 /*yield*/, this._queryStatementModel(sqlSubs)];
1223
+ sqldataChilds = "".concat(this.$utils().constants('SELECT'), " * ").concat(this.$utils().constants('FROM'), " ").concat(pivotTable, " ").concat(this.$utils().constants('WHERE'), " ").concat(fk_1, " ").concat(this.$utils().constants('IN'), " (").concat(dataPerentId, ")");
1224
+ return [4 /*yield*/, this._queryStatementModel(sqldataChilds)];
1234
1225
  case 1:
1235
- subs_1 = _e.sent();
1236
- otherId = subs_1.map(function (sub) { return sub[otherFk_1]; }).filter(function (data) { return data != null; });
1226
+ dataChilds_1 = _e.sent();
1227
+ otherId = dataChilds_1.map(function (sub) { return sub[otherFk_1]; }).filter(function (data) { return data != null; });
1237
1228
  otherArrId = Array.from(new Set(otherId)) || [];
1238
1229
  return [4 /*yield*/, modelOther.whereIn(otherPk_1, otherArrId).get()];
1239
1230
  case 2:
1240
- otherSubs_1 = _e.sent();
1241
- subs_1.forEach(function (sub) {
1231
+ otherdataChilds_1 = _e.sent();
1232
+ dataChilds_1.forEach(function (sub) {
1242
1233
  sub[other_1] = [];
1243
- otherSubs_1.forEach(function (otherSub) {
1234
+ otherdataChilds_1.forEach(function (otherSub) {
1244
1235
  if (otherSub[otherPk_1] === sub[otherFk_1]) {
1245
1236
  sub[other_1] = otherSub;
1246
1237
  }
1247
1238
  });
1248
1239
  });
1249
- mains.forEach(function (main) {
1250
- if (main[name_1] == null)
1251
- main[name_1] = [];
1252
- subs_1.forEach(function (sub) {
1253
- if (sub[fk_1] === main[pk_1]) {
1254
- main[name_1].push(sub);
1240
+ dataFromParent.forEach(function (dataPerent) {
1241
+ if (dataPerent[name_1] == null)
1242
+ dataPerent[name_1] = [];
1243
+ dataChilds_1.forEach(function (sub) {
1244
+ if (sub[fk_1] === dataPerent[pk_1]) {
1245
+ dataPerent[name_1].push(sub);
1255
1246
  }
1256
1247
  });
1257
1248
  });
1258
1249
  if (this.$db.get('HIDDEN').length)
1259
- this._hiddenColumnModel(mains);
1260
- return [2 /*return*/, mains];
1250
+ this._hiddenColumnModel(dataFromParent);
1251
+ return [2 /*return*/, dataFromParent];
1261
1252
  case 3:
1262
1253
  err_4 = _e.sent();
1263
1254
  _e.label = 4;
1264
1255
  case 4:
1265
1256
  _e.trys.push([4, 7, , 8]);
1266
- _d = this._valueInRelation(dataRelation), name_2 = _d.name, pk_2 = _d.pk, fk_2 = _d.fk;
1267
- pkId = mains.map(function (main) { return main[pk_2]; }).filter(function (data) { return data != null; });
1268
- mainId = Array.from(new Set(pkId)).join(',') || [];
1269
- if (!mainId.length)
1257
+ _d = this._valueInRelation(relation), name_2 = _d.name, pk_2 = _d.pk, fk_2 = _d.fk;
1258
+ pkId = dataFromParent.map(function (dataPerent) { return dataPerent[pk_2]; }).filter(function (data) { return data != null; });
1259
+ dataPerentId = Array.from(new Set(pkId)).join(',') || [];
1260
+ if (!dataPerentId.length)
1270
1261
  throw new Error("can't relationship without primary or foreign key");
1271
1262
  local = this.$utils().columnRelation(this.constructor.name);
1272
- modelOther = new dataRelation.model();
1263
+ modelOther = new relation.model();
1273
1264
  other_2 = modelOther.constructor.name.toLocaleLowerCase();
1274
- pivotTable = (_b = dataRelation.freezeTable) !== null && _b !== void 0 ? _b : "".concat(other_2, "_").concat(local);
1265
+ pivotTable = (_b = relation.freezeTable) !== null && _b !== void 0 ? _b : "".concat(other_2, "_").concat(local);
1275
1266
  pk_2 = 'id';
1276
1267
  fk_2 = this._isPatternSnakeCase() ? "".concat(local, "_id") : "".concat(local, "Id");
1277
1268
  otherPk_2 = 'id';
1278
1269
  otherFk_2 = this._isPatternSnakeCase() ? "".concat(other_2, "_id") : "".concat(other_2, "Id");
1279
- sqlSubs = "".concat(this.$utils().constants('SELECT'), " * ").concat(this.$utils().constants('FROM'), " ").concat(pivotTable, " ").concat(this.$utils().constants('WHERE'), " ").concat(fk_2, " ").concat(this.$utils().constants('IN'), " (").concat(mainId, ")");
1280
- return [4 /*yield*/, this._queryStatementModel(sqlSubs)];
1270
+ sqldataChilds = "".concat(this.$utils().constants('SELECT'), " * ").concat(this.$utils().constants('FROM'), " ").concat(pivotTable, " ").concat(this.$utils().constants('WHERE'), " ").concat(fk_2, " ").concat(this.$utils().constants('IN'), " (").concat(dataPerentId, ")");
1271
+ return [4 /*yield*/, this._queryStatementModel(sqldataChilds)];
1281
1272
  case 5:
1282
- subs_2 = _e.sent();
1283
- otherId = subs_2.map(function (sub) { return sub[otherFk_2]; }).filter(function (data) { return data != null; });
1273
+ dataChilds_2 = _e.sent();
1274
+ otherId = dataChilds_2.map(function (sub) { return sub[otherFk_2]; }).filter(function (data) { return data != null; });
1284
1275
  otherArrId = Array.from(new Set(otherId)) || [];
1285
1276
  return [4 /*yield*/, this._queryStatementModel(modelOther
1286
1277
  .whereIn(otherPk_2, otherArrId)
1287
1278
  .toString())];
1288
1279
  case 6:
1289
- otherSubs_2 = _e.sent();
1290
- subs_2.forEach(function (sub) {
1291
- otherSubs_2.forEach(function (otherSub) {
1280
+ otherdataChilds_2 = _e.sent();
1281
+ dataChilds_2.forEach(function (sub) {
1282
+ otherdataChilds_2.forEach(function (otherSub) {
1292
1283
  if (otherSub[otherPk_2] === sub[otherFk_2]) {
1293
1284
  sub[other_2] = otherSub;
1294
1285
  }
1295
1286
  });
1296
1287
  });
1297
- mains.forEach(function (main) {
1298
- if (main[name_2] == null)
1299
- main[name_2] = [];
1300
- subs_2.forEach(function (sub) {
1301
- if (sub[fk_2] === main[pk_2]) {
1302
- main[name_2].push(sub);
1288
+ dataFromParent.forEach(function (dataPerent) {
1289
+ if (dataPerent[name_2] == null)
1290
+ dataPerent[name_2] = [];
1291
+ dataChilds_2.forEach(function (sub) {
1292
+ if (sub[fk_2] === dataPerent[pk_2]) {
1293
+ dataPerent[name_2].push(sub);
1303
1294
  }
1304
1295
  });
1305
1296
  });
1306
1297
  if (this.$db.get('HIDDEN').length)
1307
- this._hiddenColumnModel(mains);
1308
- return [2 /*return*/, mains];
1298
+ this._hiddenColumnModel(dataFromParent);
1299
+ return [2 /*return*/, dataFromParent];
1309
1300
  case 7:
1310
1301
  err_5 = _e.sent();
1311
1302
  throw new Error(err_5.message);
@@ -1328,7 +1319,7 @@ var Model = /** @class */ (function (_super) {
1328
1319
  return [4 /*yield*/, this._queryStatementModel(sql)];
1329
1320
  case 1:
1330
1321
  res = _b.sent();
1331
- total = res.shift().total || 0;
1322
+ total = (res === null || res === void 0 ? void 0 : res.shift().total) || 0;
1332
1323
  limit = this.$db.get('PER_PAGE');
1333
1324
  lastPage = Math.ceil(total / limit) || 0;
1334
1325
  lastPage = lastPage > 1 ? lastPage : 1;
@@ -1412,7 +1403,19 @@ var Model = /** @class */ (function (_super) {
1412
1403
  }
1413
1404
  switch (type) {
1414
1405
  case 'FIRST': return this._result((_c = data[0]) !== null && _c !== void 0 ? _c : {});
1415
- case 'GET': return this._result(data);
1406
+ case 'GET': {
1407
+ if (this.$db.get('CHUNK')) {
1408
+ var result = data.reduce(function (resultArray, item, index) {
1409
+ var chunkIndex = Math.floor(index / _this.$db.get('CHUNK'));
1410
+ if (!resultArray[chunkIndex])
1411
+ resultArray[chunkIndex] = [];
1412
+ resultArray[chunkIndex].push(item);
1413
+ return resultArray;
1414
+ }, []);
1415
+ return this._result(result);
1416
+ }
1417
+ return this._result(data);
1418
+ }
1416
1419
  case 'PAGINATION': return this._pagination(data);
1417
1420
  default: throw new Error('Missing method first get or pagination');
1418
1421
  }
@@ -1442,7 +1445,7 @@ var Model = /** @class */ (function (_super) {
1442
1445
  if (!relation)
1443
1446
  throw new Error("unknow name relation [".concat(name, "] in model"));
1444
1447
  thisTable = this.$utils().columnRelation(this.constructor.name);
1445
- relationTable = this._classToTableName(relation.model.name, true);
1448
+ relationTable = this._classToTableName(relation.model.name, { belongsTo: true });
1446
1449
  result = this.$db.get('RESULT');
1447
1450
  _b.label = 1;
1448
1451
  case 1:
@@ -1494,7 +1497,7 @@ var Model = /** @class */ (function (_super) {
1494
1497
  if (!relation)
1495
1498
  throw new Error("unknow name relation [".concat(name, "] in model"));
1496
1499
  thisTable = this.$utils().columnRelation(this.constructor.name);
1497
- relationTable = this._classToTableName(relation.model.name, true);
1500
+ relationTable = this._classToTableName(relation.model.name, { belongsTo: true });
1498
1501
  result = this.$db.get('RESULT');
1499
1502
  _c.label = 1;
1500
1503
  case 1:
@@ -2007,7 +2010,7 @@ var Model = /** @class */ (function (_super) {
2007
2010
  };
2008
2011
  };
2009
2012
  Model.prototype._setupModel = function () {
2010
- var modelData = {
2013
+ var db = {
2011
2014
  TRANSACTION: { query: [{
2012
2015
  table: '',
2013
2016
  id: ''
@@ -2023,6 +2026,7 @@ var Model = /** @class */ (function (_super) {
2023
2026
  SELECT: '',
2024
2027
  ONLY: [],
2025
2028
  EXCEPT: [],
2029
+ CHUNK: 0,
2026
2030
  COUNT: '',
2027
2031
  FROM: '',
2028
2032
  JOIN: '',
@@ -2054,17 +2058,16 @@ var Model = /** @class */ (function (_super) {
2054
2058
  };
2055
2059
  return {
2056
2060
  get: function (key) {
2057
- if (key) {
2058
- if (!modelData.hasOwnProperty(key))
2059
- throw new Error("can't get this [".concat(key, "]"));
2060
- return modelData[key];
2061
- }
2062
- return modelData;
2061
+ if (key == null)
2062
+ return db;
2063
+ if (!db.hasOwnProperty(key))
2064
+ throw new Error("can't get this [".concat(key, "]"));
2065
+ return db[key];
2063
2066
  },
2064
2067
  set: function (key, value) {
2065
- if (!modelData.hasOwnProperty(key))
2068
+ if (!db.hasOwnProperty(key))
2066
2069
  throw new Error("can't set this [".concat(key, "]"));
2067
- modelData[key] = value;
2070
+ db[key] = value;
2068
2071
  return;
2069
2072
  }
2070
2073
  };
@@ -1,7 +1,8 @@
1
1
  import Database from "./Database";
2
2
  export declare class Schema extends Database {
3
- protected timeStamp: Array<string>;
4
- table: (table: string, schemas: any) => Promise<void>;
3
+ table: (table: string, schemas: {
4
+ [x: string]: any;
5
+ }) => Promise<void>;
5
6
  }
6
7
  export declare class Blueprint {
7
8
  protected type: string | undefined;
@@ -85,7 +85,6 @@ var Schema = /** @class */ (function (_super) {
85
85
  __extends(Schema, _super);
86
86
  function Schema() {
87
87
  var _this = _super !== null && _super.apply(this, arguments) || this;
88
- _this.timeStamp = ['created_at timestamp NULL', 'updated_at timestamp NULL'];
89
88
  _this.table = function (table, schemas) { return __awaiter(_this, void 0, void 0, function () {
90
89
  var columns, key, data, type, attrbuites, sql, err_1;
91
90
  var _a;
@@ -99,7 +98,6 @@ var Schema = /** @class */ (function (_super) {
99
98
  type = data.type, attrbuites = data.attrbuites;
100
99
  columns = __spreadArray(__spreadArray([], __read(columns), false), ["".concat(key, " ").concat(type, " ").concat(attrbuites === null || attrbuites === void 0 ? void 0 : attrbuites.join(' '))], false);
101
100
  }
102
- columns = __spreadArray(__spreadArray([], __read(columns), false), __read(this.timeStamp), false);
103
101
  sql = "CREATE TABLE ".concat(table, " (").concat(columns === null || columns === void 0 ? void 0 : columns.join(','), ") ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8");
104
102
  return [4 /*yield*/, this.rawQuery(sql)];
105
103
  case 1:
@@ -9,7 +9,7 @@ declare const _default: {
9
9
  escape: (str: any) => any;
10
10
  escapeSubQuery: (str: any) => any;
11
11
  generateUUID: () => string;
12
- constants: (name?: string | undefined) => any;
12
+ constants: (name?: string | undefined) => string | Object | undefined;
13
13
  covertBooleanToNumber: (data: any) => any;
14
14
  snakeCase: (obj: any) => any;
15
15
  camelCase: (obj: any) => any;
@@ -86,7 +86,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
86
86
  return (mod && mod.__esModule) ? mod : { "default": mod };
87
87
  };
88
88
  Object.defineProperty(exports, "__esModule", { value: true });
89
- var constant_1 = __importDefault(require("./constant"));
89
+ var constants_1 = __importDefault(require("../constants"));
90
90
  var connections_1 = __importDefault(require("../connections"));
91
91
  var timestamp = function () {
92
92
  var d = new Date();
@@ -162,9 +162,9 @@ var generateUUID = function () {
162
162
  var constants = function (name) {
163
163
  var e_1, _a;
164
164
  if (!name)
165
- return constant_1.default;
165
+ return constants_1.default;
166
166
  try {
167
- for (var _b = __values(Object === null || Object === void 0 ? void 0 : Object.entries(constant_1.default)), _c = _b.next(); !_c.done; _c = _b.next()) {
167
+ for (var _b = __values(Object === null || Object === void 0 ? void 0 : Object.entries(constants_1.default)), _c = _b.next(); !_c.done; _c = _b.next()) {
168
168
  var _d = __read(_c.value, 2), index = _d[0], _const = _d[1];
169
169
  if (index === name)
170
170
  return _const;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",