@klapeks/micro-db-utils 0.0.15 → 0.0.17

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.
@@ -38,6 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.AbstractSelectHandler = void 0;
40
40
  var iso_date_time_1 = require("../utils/iso.date.time");
41
+ var micro_sql_1 = require("../micro.sql");
41
42
  var AbstractSelectHandler = /** @class */ (function () {
42
43
  function AbstractSelectHandler(options) {
43
44
  this.options = options;
@@ -96,26 +97,27 @@ var AbstractSelectHandler = /** @class */ (function () {
96
97
  });
97
98
  };
98
99
  AbstractSelectHandler.prototype.getSumStatistic = function (sumField, strWhere) {
99
- var _a;
100
+ var _a, _b;
100
101
  return __awaiter(this, void 0, void 0, function () {
101
- var stat, time, response, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
102
- var _p, _q;
102
+ var stat, time, response, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
103
+ var _q, _r;
103
104
  var _this = this;
104
- return __generator(this, function (_r) {
105
- switch (_r.label) {
105
+ return __generator(this, function (_s) {
106
+ switch (_s.label) {
106
107
  case 0:
107
108
  stat = function (fromDate, time) { return __awaiter(_this, void 0, void 0, function () {
108
- var isoDate, sum;
109
+ var expr, isoDate, sum;
109
110
  var _a;
110
111
  return __generator(this, function (_b) {
111
112
  switch (_b.label) {
112
113
  case 0:
114
+ expr = micro_sql_1.MicroSQL.timeExpressions(this.getDatabaseType());
113
115
  if (typeof fromDate == 'function') {
114
116
  fromDate = (0, iso_date_time_1.dateFromNow)(fromDate);
115
117
  }
116
118
  isoDate = (0, iso_date_time_1.toISODate)(fromDate, 'yyyy-mm-dd');
117
119
  return [4 /*yield*/, this.runSQL_One("\n SELECT SUM(".concat(sumField, ") as sum\n FROM ").concat(this.getTableName(), "\n WHERE ").concat(time ? "createdAt >= '".concat(isoDate, " ").concat(time, "'")
118
- : "date(createdAt) >= date('".concat(isoDate, "')"), "\n ").concat(strWhere ? ('AND (' + strWhere + ')') : '', "\n "))];
120
+ : (expr.date('createdAt') + ' >= ' + expr.date("'".concat(isoDate, "'"))), "\n ").concat(strWhere ? ('AND (' + strWhere + ')') : '', "\n "))];
119
121
  case 1:
120
122
  sum = ((_a = (_b.sent())) === null || _a === void 0 ? void 0 : _a.sum) || 0;
121
123
  return [2 /*return*/, {
@@ -126,65 +128,65 @@ var AbstractSelectHandler = /** @class */ (function () {
126
128
  });
127
129
  }); };
128
130
  time = new Date().toLocaleTimeString('uk-UA');
129
- _p = {
131
+ _q = {
130
132
  serverTime: time
131
133
  };
132
- return [4 /*yield*/, this.getTotal()];
134
+ return [4 /*yield*/, this.runSQL_One("\n SELECT COUNT(1) AS count\n FROM ".concat(this.getTableName(), " \n ").concat(strWhere ? ("WHERE " + strWhere) : '', "\n "))];
133
135
  case 1:
134
- response = (_p.count = _r.sent(),
135
- _p);
136
- _b = response;
137
- _q = {
136
+ response = (_q.count = (_a = (_s.sent())) === null || _a === void 0 ? void 0 : _a.count,
137
+ _q);
138
+ _c = response;
139
+ _r = {
138
140
  from: new Date(0)
139
141
  };
140
- return [4 /*yield*/, this.runSQL_One("\n SELECT SUM(".concat(sumField, ") as sum\n FROM ").concat(this.getTableName(), "\n ").concat(strWhere ? ("WHERE " + strWhere) : '', "\n "))];
142
+ return [4 /*yield*/, this.runSQL_One("\n SELECT SUM(".concat(sumField, ") AS sum\n FROM ").concat(this.getTableName(), "\n ").concat(strWhere ? ("WHERE " + strWhere) : '', "\n "))];
141
143
  case 2:
142
- _b.total = (_q.sum = (_a = (_r.sent())) === null || _a === void 0 ? void 0 : _a.sum,
143
- _q);
144
- _c = response;
144
+ _c.total = (_r.sum = (_b = (_s.sent())) === null || _b === void 0 ? void 0 : _b.sum,
145
+ _r);
146
+ _d = response;
145
147
  return [4 /*yield*/, stat(new Date())];
146
148
  case 3:
147
- _c.thisDay = _r.sent();
148
- _d = response;
149
+ _d.thisDay = _s.sent();
150
+ _e = response;
149
151
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - (d.getDay() || 7) + 1); })];
150
152
  case 4:
151
- _d.thisWeek = _r.sent();
152
- _e = response;
153
+ _e.thisWeek = _s.sent();
154
+ _f = response;
153
155
  return [4 /*yield*/, stat(function (d) { return d.setDate(1); })];
154
156
  case 5:
155
- _e.thisMonth = _r.sent();
156
- _f = response;
157
+ _f.thisMonth = _s.sent();
158
+ _g = response;
157
159
  return [4 /*yield*/, stat(function (d) { d.setMonth(0); d.setDate(1); })];
158
160
  case 6:
159
- _f.thisYear = _r.sent();
160
- _g = response;
161
+ _g.thisYear = _s.sent();
162
+ _h = response;
161
163
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - 1); }, time)];
162
164
  case 7:
163
- _g.past24hours = _r.sent();
164
- _h = response;
165
+ _h.past24hours = _s.sent();
166
+ _j = response;
165
167
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - 2); })];
166
168
  case 8:
167
- _h.past3days = _r.sent();
168
- _j = response;
169
+ _j.past3days = _s.sent();
170
+ _k = response;
169
171
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - 6); })];
170
172
  case 9:
171
- _j.past7days = _r.sent();
172
- _k = response;
173
+ _k.past7days = _s.sent();
174
+ _l = response;
173
175
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - 13); })];
174
176
  case 10:
175
- _k.past14days = _r.sent();
176
- _l = response;
177
+ _l.past14days = _s.sent();
178
+ _m = response;
177
179
  return [4 /*yield*/, stat(function (d) { return d.setDate(d.getDate() - 29); })];
178
180
  case 11:
179
- _l.past30days = _r.sent();
180
- _m = response;
181
+ _m.past30days = _s.sent();
182
+ _o = response;
181
183
  return [4 /*yield*/, stat(function (d) { return d.setMonth(d.getMonth() - 6); })];
182
184
  case 12:
183
- _m.past6months = _r.sent();
184
- _o = response;
185
+ _o.past6months = _s.sent();
186
+ _p = response;
185
187
  return [4 /*yield*/, stat(function (d) { return d.setMonth(d.getMonth() - 12); })];
186
188
  case 13:
187
- _o.past12months = _r.sent();
189
+ _p.past12months = _s.sent();
188
190
  return [2 /*return*/, response];
189
191
  }
190
192
  });
@@ -1,6 +1,7 @@
1
1
  import { ISQLCommandAdapter } from "./abstract.sql.command";
2
2
  import { MicroColumnTypeObject } from "../column.type.parser";
3
3
  export declare class SQLTablesCommands {
4
+ static renameTable(old_name: string, new_name: string): ISQLCommandAdapter;
4
5
  static tableInfo(database: string, table: string): ISQLCommandAdapter;
5
6
  static createTable(options: {
6
7
  table: string;
@@ -5,6 +5,14 @@ var column_type_parser_1 = require("../column.type.parser");
5
5
  var SQLTablesCommands = /** @class */ (function () {
6
6
  function SQLTablesCommands() {
7
7
  }
8
+ SQLTablesCommands.renameTable = function (old_name, new_name) {
9
+ return {
10
+ toMySQL: function () { return "RENAME TABLE `".concat(old_name, "` TO `").concat(new_name, "`;"); },
11
+ toMSSQL: function () { return "EXEC sp_rename '".concat(old_name, "', '").concat(new_name, "';"); },
12
+ // PG: ALTER TABLE "old_name" RENAME TO "new_name";
13
+ // SQLITE: ALTER TABLE "old_name" RENAME TO "new_name";
14
+ };
15
+ };
8
16
  SQLTablesCommands.tableInfo = function (database, table) {
9
17
  return {
10
18
  toMySQL: function () { return [
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@klapeks/micro-db-utils",
3
3
  "description": "klapeks utils for db and other storages",
4
- "version": "0.0.15",
4
+ "version": "0.0.17",
5
5
  "type": "commonjs",
6
6
  "author": "Klapeks",
7
7
  "license": "ISC",
@@ -22,7 +22,7 @@
22
22
  "@aws-sdk/client-s3": "^3.842.0",
23
23
  "@aws-sdk/s3-request-presigner": "^3.844.0",
24
24
  "@klapeks/api-creation-tools": "^0.0.7",
25
- "@klapeks/utils": "^0.1.7",
25
+ "@klapeks/utils": "^0.1.8",
26
26
  "@types/mssql": "^12.3.0",
27
27
  "@types/node": "^18.11.18",
28
28
  "axios": "^1.7.7",