@klapeks/micro-db-utils 0.0.17 → 0.0.20
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/micro.sql.js +6 -1
- package/dist/migrations/super.migrations.js +61 -46
- package/dist/patch.typeorm.d.ts +1 -0
- package/dist/patch.typeorm.js +129 -0
- package/dist/sql/column.type.parser.d.ts +2 -2
- package/dist/sql/column.type.parser.js +7 -31
- package/dist/sql/commands/abstract.sql.command.d.ts +10 -5
- package/dist/sql/commands/abstract.sql.command.js +67 -12
- package/dist/sql/commands/alter.commands.d.ts +1 -1
- package/dist/sql/commands/alter.commands.js +113 -9
- package/dist/sql/commands/edit-data.commands.d.ts +1 -0
- package/dist/sql/commands/edit-data.commands.js +32 -2
- package/dist/sql/commands/select.commands.js +1 -0
- package/dist/sql/commands/tables.commands.js +11 -1
- package/dist/sql/connections/abstract.connection.d.ts +1 -1
- package/dist/sql/connections/abstract.connection.js +14 -2
- package/dist/sql/connections/index.d.ts +1 -0
- package/dist/sql/connections/index.js +4 -0
- package/dist/sql/connections/sqlite.connection.d.ts +13 -0
- package/dist/sql/connections/sqlite.connection.js +158 -0
- package/dist/sql/raw.where.utils.js +3 -1
- package/dist/utils/columns.types.d.ts +1 -8
- package/dist/utils/columns.types.js +1 -1
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/micro.sql.js
CHANGED
|
@@ -57,7 +57,12 @@ var MicroSQL;
|
|
|
57
57
|
_b.label = 3;
|
|
58
58
|
case 3:
|
|
59
59
|
query = (_a);
|
|
60
|
-
|
|
60
|
+
return [4 /*yield*/, (0, sql_1.toRawSQL)(dbType, query, {
|
|
61
|
+
database: dataSource.options.database,
|
|
62
|
+
runAdditionalSQL: dataSource.query
|
|
63
|
+
})];
|
|
64
|
+
case 4:
|
|
65
|
+
query = _b.sent();
|
|
61
66
|
return [2 /*return*/, dataSource.query(query.query, query.params)];
|
|
62
67
|
}
|
|
63
68
|
});
|
|
@@ -104,29 +104,34 @@ var SuperMigrations = /** @class */ (function () {
|
|
|
104
104
|
case 1: return [4 /*yield*/, _e.sent()];
|
|
105
105
|
case 2:
|
|
106
106
|
isTableExistsInfo = _e.sent();
|
|
107
|
-
if (!!isTableExistsInfo) return [3 /*break*/,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
107
|
+
if (!!isTableExistsInfo) return [3 /*break*/, 6];
|
|
108
|
+
return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
109
|
+
var sql;
|
|
110
|
+
var _a;
|
|
111
|
+
return __generator(this, function (_b) {
|
|
112
|
+
sql = (_a = todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations[0]) === null || _a === void 0 ? void 0 : _a.sql;
|
|
113
|
+
if (!sql)
|
|
114
|
+
return [2 /*return*/, null];
|
|
115
|
+
if (Array.isArray(sql))
|
|
116
|
+
sql = sql[0];
|
|
117
|
+
return [2 /*return*/, sqlInstance.toRawSQL(sql)];
|
|
118
|
+
});
|
|
119
|
+
}); })()];
|
|
120
|
+
case 3:
|
|
121
|
+
firstSQL = _e.sent();
|
|
122
|
+
if (!!((_b = (_a = firstSQL === null || firstSQL === void 0 ? void 0 : firstSQL.query) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes('create table'))) return [3 /*break*/, 6];
|
|
118
123
|
logger.log("Table ".concat(table, " not found. Migrations ").concat(todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length, " will be skipped"));
|
|
119
124
|
lastMigrationDate = todoMigrations.length ? (_c = todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations[todoMigrations.length - 1]) === null || _c === void 0 ? void 0 : _c.date : undefined;
|
|
120
|
-
if (!((todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length) && lastMigrationDate)) return [3 /*break*/,
|
|
125
|
+
if (!((todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length) && lastMigrationDate)) return [3 /*break*/, 5];
|
|
121
126
|
return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.editData(this.migrationTableName).upsert({
|
|
122
127
|
table: table,
|
|
123
128
|
lastMigrationTime: (0, iso_date_time_1.toISODate)(lastMigrationDate)
|
|
124
129
|
}, ['table']))];
|
|
125
|
-
case
|
|
130
|
+
case 4:
|
|
126
131
|
_e.sent();
|
|
127
|
-
_e.label =
|
|
128
|
-
case
|
|
129
|
-
case
|
|
132
|
+
_e.label = 5;
|
|
133
|
+
case 5: return [2 /*return*/];
|
|
134
|
+
case 6:
|
|
130
135
|
_local_runSQL = function (sql, params) { return __awaiter(_this, void 0, void 0, function () {
|
|
131
136
|
return __generator(this, function (_a) {
|
|
132
137
|
switch (_a.label) {
|
|
@@ -155,61 +160,71 @@ var SuperMigrations = /** @class */ (function () {
|
|
|
155
160
|
lastRealMigration = lastRealMigrationDateTime
|
|
156
161
|
? new Date(lastRealMigrationDateTime) : null;
|
|
157
162
|
_i = 0, todoMigrations_1 = todoMigrations;
|
|
158
|
-
_e.label =
|
|
159
|
-
case
|
|
160
|
-
if (!(_i < todoMigrations_1.length)) return [3 /*break*/,
|
|
163
|
+
_e.label = 7;
|
|
164
|
+
case 7:
|
|
165
|
+
if (!(_i < todoMigrations_1.length)) return [3 /*break*/, 20];
|
|
161
166
|
migration = todoMigrations_1[_i];
|
|
162
167
|
if (migration.table != table)
|
|
163
|
-
return [3 /*break*/,
|
|
168
|
+
return [3 /*break*/, 19];
|
|
164
169
|
logger;
|
|
165
170
|
if (lastRealMigration && lastRealMigration.getTime() >= migration.date.getTime()) {
|
|
166
|
-
return [3 /*break*/,
|
|
171
|
+
return [3 /*break*/, 19];
|
|
167
172
|
}
|
|
168
173
|
runnedMigrationsAmount = onMigrationComplete();
|
|
169
174
|
migrationName = '"' + table + ' ' + (0, iso_date_time_1.toISODate)(migration.date) + '"';
|
|
170
|
-
if (!Array.isArray(migration.sql)) return [3 /*break*/,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
175
|
+
if (!Array.isArray(migration.sql)) return [3 /*break*/, 13];
|
|
176
|
+
return [4 /*yield*/, Promise.all(migration.sql.filter(Boolean).map(function (sql) { return __awaiter(_this, void 0, void 0, function () {
|
|
177
|
+
var sql2;
|
|
178
|
+
return __generator(this, function (_a) {
|
|
179
|
+
switch (_a.label) {
|
|
180
|
+
case 0: return [4 /*yield*/, sqlInstance.toRawSQL(sql)];
|
|
181
|
+
case 1:
|
|
182
|
+
sql2 = _a.sent();
|
|
183
|
+
sql2.query = utils_1.utils.replaceAll(sql2.query.trim(), "%{table_name}", table);
|
|
184
|
+
return [2 /*return*/, sql2];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}); }))];
|
|
188
|
+
case 8:
|
|
189
|
+
sqls = _e.sent();
|
|
176
190
|
logger.log(runnedMigrationsAmount, "| Migrations will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sqls.map(function (s) { return s.query; }));
|
|
177
191
|
_d = 0, sqls_1 = sqls;
|
|
178
|
-
_e.label =
|
|
179
|
-
case
|
|
180
|
-
if (!(_d < sqls_1.length)) return [3 /*break*/,
|
|
192
|
+
_e.label = 9;
|
|
193
|
+
case 9:
|
|
194
|
+
if (!(_d < sqls_1.length)) return [3 /*break*/, 12];
|
|
181
195
|
sql = sqls_1[_d];
|
|
182
196
|
return [4 /*yield*/, _local_runSQL(sql.query, sql.params)];
|
|
183
|
-
case
|
|
197
|
+
case 10:
|
|
184
198
|
_e.sent();
|
|
185
|
-
_e.label =
|
|
186
|
-
case 9:
|
|
187
|
-
_d++;
|
|
188
|
-
return [3 /*break*/, 7];
|
|
189
|
-
case 10: return [3 /*break*/, 13];
|
|
199
|
+
_e.label = 11;
|
|
190
200
|
case 11:
|
|
191
|
-
|
|
201
|
+
_d++;
|
|
202
|
+
return [3 /*break*/, 9];
|
|
203
|
+
case 12: return [3 /*break*/, 16];
|
|
204
|
+
case 13: return [4 /*yield*/, sqlInstance.toRawSQL(migration.sql)];
|
|
205
|
+
case 14:
|
|
206
|
+
sql = _e.sent();
|
|
192
207
|
sql.query = utils_1.utils.replaceAll(sql.query.trim(), "%{table_name}", table);
|
|
193
208
|
logger.log(runnedMigrationsAmount, "| Migration will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sql.query);
|
|
194
209
|
return [4 /*yield*/, _local_runSQL(sql.query, sql.params)];
|
|
195
|
-
case
|
|
210
|
+
case 15:
|
|
196
211
|
_e.sent();
|
|
197
|
-
_e.label =
|
|
198
|
-
case
|
|
212
|
+
_e.label = 16;
|
|
213
|
+
case 16: return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.editData(this.migrationTableName).upsert({
|
|
199
214
|
table: table,
|
|
200
215
|
lastMigrationTime: (0, iso_date_time_1.toISODate)(migration.date)
|
|
201
216
|
}, ['table']))];
|
|
202
|
-
case
|
|
217
|
+
case 17:
|
|
203
218
|
_e.sent();
|
|
204
219
|
logger.log(runnedMigrationsAmount, "| Migration", migrationName, "successfully done");
|
|
205
220
|
return [4 /*yield*/, utils_1.utils.sleep(100)];
|
|
206
|
-
case
|
|
221
|
+
case 18:
|
|
207
222
|
_e.sent();
|
|
208
|
-
_e.label =
|
|
209
|
-
case
|
|
223
|
+
_e.label = 19;
|
|
224
|
+
case 19:
|
|
210
225
|
_i++;
|
|
211
|
-
return [3 /*break*/,
|
|
212
|
-
case
|
|
226
|
+
return [3 /*break*/, 7];
|
|
227
|
+
case 20: return [2 /*return*/];
|
|
213
228
|
}
|
|
214
229
|
});
|
|
215
230
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function patchTypeorm(): void;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.patchTypeorm = void 0;
|
|
51
|
+
var typeorm_1 = require("typeorm");
|
|
52
|
+
var _alreadyPatched = false;
|
|
53
|
+
function patchTypeorm() {
|
|
54
|
+
if (_alreadyPatched)
|
|
55
|
+
return;
|
|
56
|
+
{ // repository changes
|
|
57
|
+
var originalUpdate_1 = typeorm_1.Repository.prototype.update;
|
|
58
|
+
typeorm_1.Repository.prototype.update = function () {
|
|
59
|
+
var args = [];
|
|
60
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
61
|
+
args[_i] = arguments[_i];
|
|
62
|
+
}
|
|
63
|
+
var _wasDel = false;
|
|
64
|
+
for (var _a = 0, _b = Object.keys(args[0]); _a < _b.length; _a++) {
|
|
65
|
+
var key = _b[_a];
|
|
66
|
+
if (typeof args[1] === 'object' && key in args[1]) {
|
|
67
|
+
if (args[0][key] === args[1][key]) {
|
|
68
|
+
if (!_wasDel) {
|
|
69
|
+
args[1] = __assign({}, args[1]);
|
|
70
|
+
_wasDel = true;
|
|
71
|
+
}
|
|
72
|
+
delete args[1][key];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return originalUpdate_1.apply(this, args);
|
|
77
|
+
};
|
|
78
|
+
var originalUpsert_1 = typeorm_1.Repository.prototype.upsert;
|
|
79
|
+
typeorm_1.Repository.prototype.upsert = function () {
|
|
80
|
+
var args = [];
|
|
81
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
82
|
+
args[_i] = arguments[_i];
|
|
83
|
+
}
|
|
84
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
+
return __generator(this, function (_a) {
|
|
86
|
+
if (this.manager.connection.options.type === "mssql") {
|
|
87
|
+
return [2 /*return*/, this.save(args[0])];
|
|
88
|
+
}
|
|
89
|
+
return [2 /*return*/, originalUpsert_1.apply(this, args)];
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
{ // query builder changes changes
|
|
95
|
+
var whereFix_1 = function (dbtype, args) {
|
|
96
|
+
if (args[0] && typeof args[0] === 'string') {
|
|
97
|
+
if (dbtype == 'mssql') {
|
|
98
|
+
args[0] = args[0].replace(/\btrue\b/g, "1");
|
|
99
|
+
args[0] = args[0].replace(/\bfalse\b/g, "0");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return args;
|
|
103
|
+
};
|
|
104
|
+
var originalWhere_1 = typeorm_1.SelectQueryBuilder.prototype.where;
|
|
105
|
+
typeorm_1.SelectQueryBuilder.prototype.where = function () {
|
|
106
|
+
var _a;
|
|
107
|
+
var args = [];
|
|
108
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
109
|
+
args[_i] = arguments[_i];
|
|
110
|
+
}
|
|
111
|
+
var dbType = this.connection.options.type
|
|
112
|
+
|| ((_a = this.dataSource) === null || _a === void 0 ? void 0 : _a.options.type);
|
|
113
|
+
return originalWhere_1.apply(this, whereFix_1(dbType, args));
|
|
114
|
+
};
|
|
115
|
+
var originalAndWhere_1 = typeorm_1.SelectQueryBuilder.prototype.andWhere;
|
|
116
|
+
typeorm_1.SelectQueryBuilder.prototype.andWhere = function () {
|
|
117
|
+
var _a;
|
|
118
|
+
var args = [];
|
|
119
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
120
|
+
args[_i] = arguments[_i];
|
|
121
|
+
}
|
|
122
|
+
var dbType = this.connection.options.type
|
|
123
|
+
|| ((_a = this.dataSource) === null || _a === void 0 ? void 0 : _a.options.type);
|
|
124
|
+
return originalAndWhere_1.apply(this, whereFix_1(dbType, args));
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
_alreadyPatched = true;
|
|
128
|
+
}
|
|
129
|
+
exports.patchTypeorm = patchTypeorm;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DatabaseOptions } from "@klapeks/utils";
|
|
1
|
+
import { DatabaseOptions, getRawDatabaseColumnTypeOfTypeORM } from "@klapeks/utils";
|
|
2
2
|
import { ColumnType } from "typeorm";
|
|
3
|
-
export
|
|
3
|
+
export { getRawDatabaseColumnTypeOfTypeORM };
|
|
4
4
|
export interface MicroColumnTypeObject {
|
|
5
5
|
type: ColumnType;
|
|
6
6
|
primary?: boolean;
|
|
@@ -1,40 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MicroColumnTypeObject = exports.getRawDatabaseColumnTypeOfTypeORM = void 0;
|
|
4
|
-
var utils_1 = require("
|
|
5
|
-
|
|
6
|
-
// https://vscode.dev/github.com/typeorm/typeorm/blob/master/src/driver/sqlserver/SqlServerDriver.ts#L678
|
|
7
|
-
function getRawDatabaseColumnTypeOfTypeORM(dbtype, type) {
|
|
8
|
-
if (type === Number) {
|
|
9
|
-
if (dbtype == 'sqlite')
|
|
10
|
-
return 'integer';
|
|
11
|
-
if (dbtype == 'postgres')
|
|
12
|
-
return 'integer';
|
|
13
|
-
return 'int';
|
|
14
|
-
}
|
|
15
|
-
if (type === String) {
|
|
16
|
-
if (dbtype === 'postgres')
|
|
17
|
-
return 'character varying';
|
|
18
|
-
if (dbtype === 'mssql')
|
|
19
|
-
return 'nvarchar';
|
|
20
|
-
return 'varchar';
|
|
21
|
-
}
|
|
22
|
-
if (type === Boolean) {
|
|
23
|
-
if (dbtype === 'mssql')
|
|
24
|
-
return 'bit';
|
|
25
|
-
if (dbtype === 'mysql')
|
|
26
|
-
return 'tinyint';
|
|
27
|
-
return 'boolean';
|
|
28
|
-
}
|
|
29
|
-
if (type === Date)
|
|
30
|
-
return utils_1.MULTISQL_COLUMNS_TYPES.datetime;
|
|
31
|
-
return type;
|
|
32
|
-
}
|
|
33
|
-
exports.getRawDatabaseColumnTypeOfTypeORM = getRawDatabaseColumnTypeOfTypeORM;
|
|
4
|
+
var utils_1 = require("@klapeks/utils");
|
|
5
|
+
Object.defineProperty(exports, "getRawDatabaseColumnTypeOfTypeORM", { enumerable: true, get: function () { return utils_1.getRawDatabaseColumnTypeOfTypeORM; } });
|
|
34
6
|
var MicroColumnTypeObject;
|
|
35
7
|
(function (MicroColumnTypeObject) {
|
|
36
8
|
function toSQLQuery(dbType, options, queryType) {
|
|
37
|
-
var columnType = getRawDatabaseColumnTypeOfTypeORM(dbType, options.type);
|
|
9
|
+
var columnType = (0, utils_1.getRawDatabaseColumnTypeOfTypeORM)(dbType, options.type);
|
|
38
10
|
var columnLength = options.length || ((columnType === 'varchar'
|
|
39
11
|
|| columnType === 'nvarchar') ? 255 : undefined);
|
|
40
12
|
var str = columnType + (columnLength ? "(".concat(columnLength, ")") : '');
|
|
@@ -49,6 +21,7 @@ var MicroColumnTypeObject;
|
|
|
49
21
|
str += ' IDENTITY';
|
|
50
22
|
else if (dbType === 'mysql')
|
|
51
23
|
str += ' AUTO_INCREMENT';
|
|
24
|
+
else if (dbType === 'sqlite') { /** after primary key */ }
|
|
52
25
|
else
|
|
53
26
|
throw "options.generated is not supported yet for " + dbType;
|
|
54
27
|
}
|
|
@@ -85,6 +58,9 @@ var MicroColumnTypeObject;
|
|
|
85
58
|
if (options.primary) {
|
|
86
59
|
str += ' PRIMARY KEY';
|
|
87
60
|
}
|
|
61
|
+
if (dbType === 'sqlite' && options.generated) {
|
|
62
|
+
str += ' AUTOINCREMENT';
|
|
63
|
+
}
|
|
88
64
|
}
|
|
89
65
|
return str;
|
|
90
66
|
}
|
|
@@ -4,18 +4,23 @@ export interface SQLCommandData {
|
|
|
4
4
|
params?: any[];
|
|
5
5
|
}
|
|
6
6
|
export interface SQLCommandContext {
|
|
7
|
+
database: string;
|
|
8
|
+
runAdditionalSQL: (sql: string, params?: any[]) => any;
|
|
7
9
|
}
|
|
8
10
|
export interface ISQLCommandAdapter {
|
|
9
|
-
toMySQL(ctx?: SQLCommandContext): SQLCommandData | string
|
|
10
|
-
toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string
|
|
11
|
+
toMySQL(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
12
|
+
toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
13
|
+
toSQLite(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
11
14
|
}
|
|
12
15
|
export declare abstract class AbstractSQLCommand implements ISQLCommandAdapter {
|
|
13
|
-
abstract toMySQL(ctx?: SQLCommandContext): SQLCommandData | string
|
|
14
|
-
abstract toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string
|
|
16
|
+
abstract toMySQL(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
17
|
+
abstract toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
18
|
+
abstract toSQLite(ctx?: SQLCommandContext): SQLCommandData | string | Promise<string>;
|
|
15
19
|
}
|
|
16
20
|
export declare const rawSQL: (sqls: {
|
|
17
21
|
defaultQuery: string;
|
|
18
22
|
mysqlQuery?: string;
|
|
19
23
|
mssqlQuery?: string;
|
|
24
|
+
sqliteQuery?: string;
|
|
20
25
|
}) => ISQLCommandAdapter;
|
|
21
|
-
export declare function toRawSQL(dbType: DatabaseOptions['type'], query: ISQLCommandAdapter | SQLCommandData | string): SQLCommandData
|
|
26
|
+
export declare function toRawSQL(dbType: DatabaseOptions['type'], query: ISQLCommandAdapter | SQLCommandData | string, ctx?: SQLCommandContext): Promise<SQLCommandData>;
|
|
@@ -1,4 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.toRawSQL = exports.rawSQL = exports.AbstractSQLCommand = void 0;
|
|
4
40
|
var AbstractSQLCommand = /** @class */ (function () {
|
|
@@ -11,20 +47,39 @@ var rawSQL = function (sqls) {
|
|
|
11
47
|
return {
|
|
12
48
|
toMySQL: function () { return sqls.mysqlQuery || sqls.defaultQuery; },
|
|
13
49
|
toMSSQL: function () { return sqls.mssqlQuery || sqls.defaultQuery; },
|
|
50
|
+
toSQLite: function () { return sqls.sqliteQuery || sqls.defaultQuery; },
|
|
14
51
|
};
|
|
15
52
|
};
|
|
16
53
|
exports.rawSQL = rawSQL;
|
|
17
|
-
function toRawSQL(dbType, query) {
|
|
18
|
-
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
54
|
+
function toRawSQL(dbType, query, ctx) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
return __generator(this, function (_a) {
|
|
57
|
+
switch (_a.label) {
|
|
58
|
+
case 0:
|
|
59
|
+
if (typeof query === 'string')
|
|
60
|
+
return [2 /*return*/, { query: query }];
|
|
61
|
+
if (!(typeof query === 'object' && !('query' in query))) return [3 /*break*/, 7];
|
|
62
|
+
if (!(dbType === 'mysql')) return [3 /*break*/, 2];
|
|
63
|
+
return [4 /*yield*/, query.toMySQL(ctx)];
|
|
64
|
+
case 1:
|
|
65
|
+
query = _a.sent();
|
|
66
|
+
return [3 /*break*/, 7];
|
|
67
|
+
case 2:
|
|
68
|
+
if (!(dbType === 'mssql')) return [3 /*break*/, 4];
|
|
69
|
+
return [4 /*yield*/, query.toMSSQL(ctx)];
|
|
70
|
+
case 3:
|
|
71
|
+
query = _a.sent();
|
|
72
|
+
return [3 /*break*/, 7];
|
|
73
|
+
case 4:
|
|
74
|
+
if (!(dbType === 'sqlite')) return [3 /*break*/, 6];
|
|
75
|
+
return [4 /*yield*/, query.toSQLite(ctx)];
|
|
76
|
+
case 5:
|
|
77
|
+
query = _a.sent();
|
|
78
|
+
return [3 /*break*/, 7];
|
|
79
|
+
case 6: throw "Unknown database type: " + dbType;
|
|
80
|
+
case 7: return [2 /*return*/, typeof query === 'string' ? { query: query } : query];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
29
84
|
}
|
|
30
85
|
exports.toRawSQL = toRawSQL;
|
|
@@ -5,6 +5,6 @@ export declare class SQLAlterCommand {
|
|
|
5
5
|
constructor(table: string);
|
|
6
6
|
array(cb: (t: SQLAlterCommand) => ISQLCommandAdapter[]): ISQLCommandAdapter[];
|
|
7
7
|
renameColumn(old_name: string, new_name: string): ISQLCommandAdapter;
|
|
8
|
-
changeColumnType(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
9
8
|
addColumn(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
9
|
+
changeColumnType(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
10
10
|
}
|
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.SQLAlterCommand = void 0;
|
|
40
|
+
var utils_1 = require("@klapeks/utils");
|
|
4
41
|
var column_type_parser_1 = require("../column.type.parser");
|
|
5
42
|
var SQLAlterCommand = /** @class */ (function () {
|
|
6
43
|
function SQLAlterCommand(table) {
|
|
@@ -13,25 +50,92 @@ var SQLAlterCommand = /** @class */ (function () {
|
|
|
13
50
|
var _this = this;
|
|
14
51
|
return {
|
|
15
52
|
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` RENAME COLUMN `").concat(old_name, "` TO `").concat(new_name, "`;"); },
|
|
16
|
-
toMSSQL: function () { return "EXEC sp_rename '".concat(_this.table, ".").concat(old_name, "', '").concat(new_name, "', 'COLUMN';"); }
|
|
53
|
+
toMSSQL: function () { return "EXEC sp_rename '".concat(_this.table, ".").concat(old_name, "', '").concat(new_name, "', 'COLUMN';"); },
|
|
54
|
+
toSQLite: function () { return "ALTER TABLE \"".concat(_this.table, "\" RENAME COLUMN \"").concat(old_name, "\" TO \"").concat(new_name, "\";"); },
|
|
17
55
|
};
|
|
18
56
|
};
|
|
19
|
-
SQLAlterCommand.prototype.
|
|
57
|
+
SQLAlterCommand.prototype.addColumn = function (column, type) {
|
|
20
58
|
var _this = this;
|
|
21
59
|
return {
|
|
22
|
-
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "`
|
|
60
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` ADD COLUMN `").concat(column, "` ")
|
|
23
61
|
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mysql', type, 'alter-column') + ';'; },
|
|
24
|
-
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "]
|
|
25
|
-
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; }
|
|
62
|
+
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "] ADD [").concat(column, "] ")
|
|
63
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; },
|
|
64
|
+
toSQLite: function () { return "ALTER TABLE \"".concat(_this.table, "\" ADD COLUMN \"").concat(column, "\" ")
|
|
65
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery("sqlite", type, "alter-column") + ";"; }
|
|
26
66
|
};
|
|
27
67
|
};
|
|
28
|
-
SQLAlterCommand.prototype.
|
|
68
|
+
SQLAlterCommand.prototype.changeColumnType = function (column, type) {
|
|
29
69
|
var _this = this;
|
|
30
70
|
return {
|
|
31
|
-
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "`
|
|
71
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` MODIFY COLUMN `").concat(column, "` ")
|
|
32
72
|
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mysql', type, 'alter-column') + ';'; },
|
|
33
|
-
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "]
|
|
34
|
-
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; }
|
|
73
|
+
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "] ALTER COLUMN [").concat(column, "] ")
|
|
74
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; },
|
|
75
|
+
toSQLite: function (ctx) { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
+
var tableCreateOld, indexesToRecreate, tableCreateArray, _wasChanges, tableColumnNames, _loop_1, i, RECHANGE_TABLE_NAME;
|
|
77
|
+
var _a, _b;
|
|
78
|
+
return __generator(this, function (_c) {
|
|
79
|
+
switch (_c.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
if (!(ctx === null || ctx === void 0 ? void 0 : ctx.runAdditionalSQL))
|
|
82
|
+
throw "No runAdditionalSQL in context";
|
|
83
|
+
return [4 /*yield*/, ctx.runAdditionalSQL("\n SELECT sql FROM sqlite_master WHERE type = \"table\" AND name = \"".concat(this.table, "\";\n "))];
|
|
84
|
+
case 1:
|
|
85
|
+
tableCreateOld = (_b = (_a = (_c.sent())) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.sql;
|
|
86
|
+
if (!tableCreateOld)
|
|
87
|
+
throw "Table ".concat(this.table, " not exists");
|
|
88
|
+
return [4 /*yield*/, ctx.runAdditionalSQL("\n SELECT sql FROM sqlite_master WHERE type = \"index\"\n AND tbl_name = \"".concat(this.table, "\" AND sql IS NOT NULL;\n "))];
|
|
89
|
+
case 2:
|
|
90
|
+
indexesToRecreate = (_c.sent()).map(function (s) { return s.sql || s; }).filter(Boolean).join("; ") + ';';
|
|
91
|
+
tableCreateArray = tableCreateOld.split('\n');
|
|
92
|
+
_wasChanges = false;
|
|
93
|
+
tableColumnNames = [];
|
|
94
|
+
_loop_1 = function (i) {
|
|
95
|
+
if (tableCreateArray[i].trim()[0] == '"') {
|
|
96
|
+
var str = tableCreateArray[i].trim().substring(1);
|
|
97
|
+
tableColumnNames.push(str.substring(0, str.indexOf('"')));
|
|
98
|
+
}
|
|
99
|
+
if (!tableCreateArray[i].includes('"' + column + '"'))
|
|
100
|
+
return "continue";
|
|
101
|
+
var arr = tableCreateArray[i].split('\t');
|
|
102
|
+
var indexName = arr.findIndex(function (a) { return a == '"' + column + '"'; });
|
|
103
|
+
if (indexName < 0)
|
|
104
|
+
return "continue";
|
|
105
|
+
arr[indexName + 1] = (function () {
|
|
106
|
+
var str = column_type_parser_1.MicroColumnTypeObject.toSQLQuery('sqlite', type);
|
|
107
|
+
if (str.startsWith('text('))
|
|
108
|
+
str = str.replace("text(", "varchar(");
|
|
109
|
+
if (arr[indexName + 1].toLowerCase().includes(' unique'))
|
|
110
|
+
str += ' UNIQUE';
|
|
111
|
+
if (arr[indexName + 1].endsWith(','))
|
|
112
|
+
str += ',';
|
|
113
|
+
return str;
|
|
114
|
+
})();
|
|
115
|
+
// console.log("CHANGES ARR:", arr);
|
|
116
|
+
tableCreateArray[i] = arr.join('\t');
|
|
117
|
+
_wasChanges = true;
|
|
118
|
+
};
|
|
119
|
+
for (i = 0; i < tableCreateArray.length; i++) {
|
|
120
|
+
_loop_1(i);
|
|
121
|
+
}
|
|
122
|
+
if (!_wasChanges)
|
|
123
|
+
return [2 /*return*/, "SELECT 1"];
|
|
124
|
+
RECHANGE_TABLE_NAME = this.table + "___rechange_" + utils_1.utils.randomInclude(99);
|
|
125
|
+
tableCreateArray[0] = "CREATE TABLE \"".concat(RECHANGE_TABLE_NAME, "\" (");
|
|
126
|
+
// console.log("Table create:", tableCreateNew);
|
|
127
|
+
// console.log("INDEXES:", indexesToRecreate);
|
|
128
|
+
// console.log("Colums:", tableColumnNames);
|
|
129
|
+
// const hasColumnIndex = tableColumns.findIndex(
|
|
130
|
+
// (row) => row.name === column
|
|
131
|
+
// );
|
|
132
|
+
// if (hasColumnIndex < 0) return "SELECT 1";
|
|
133
|
+
// // ctx?.runAdditionalSQL
|
|
134
|
+
// // throw "Not implemented yet: changeColumnType for sqlite"
|
|
135
|
+
return [2 /*return*/, "\n BEGIN;\n ".concat(tableCreateArray.join('\n'), ";\n INSERT INTO ").concat(RECHANGE_TABLE_NAME, " (").concat(tableColumnNames.join(', '), ") \n SELECT ").concat(tableColumnNames.join(', '), " FROM ").concat(this.table, ";\n DROP TABLE ").concat(this.table, ";\n ALTER TABLE ").concat(RECHANGE_TABLE_NAME, " RENAME TO ").concat(this.table, ";\n ").concat(indexesToRecreate, "\n COMMIT;\n ")];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}); },
|
|
35
139
|
};
|
|
36
140
|
};
|
|
37
141
|
return SQLAlterCommand;
|
|
@@ -2,6 +2,7 @@ import { ISQLCommandAdapter } from "./abstract.sql.command";
|
|
|
2
2
|
export declare class SQLEditDataCommands {
|
|
3
3
|
private readonly table;
|
|
4
4
|
constructor(table: string);
|
|
5
|
+
insert(data: any): ISQLCommandAdapter;
|
|
5
6
|
update(data: any, where: any): ISQLCommandAdapter;
|
|
6
7
|
upsert(data: any, idKeys: string[]): ISQLCommandAdapter;
|
|
7
8
|
}
|
|
@@ -26,15 +26,35 @@ var SQLEditDataCommands = /** @class */ (function () {
|
|
|
26
26
|
function SQLEditDataCommands(table) {
|
|
27
27
|
this.table = table;
|
|
28
28
|
}
|
|
29
|
+
SQLEditDataCommands.prototype.insert = function (data) {
|
|
30
|
+
var _this = this;
|
|
31
|
+
if (!data)
|
|
32
|
+
throw "No data param";
|
|
33
|
+
var dataKeys = Object.keys(data);
|
|
34
|
+
return {
|
|
35
|
+
toMySQL: function () { return ({
|
|
36
|
+
query: "\n INSERT INTO `".concat(_this.table, "` \n (").concat(dataKeys.map(function (key) { return "`".concat(key, "`"); }).join(', '), ") \n VALUES (").concat(dataKeys.map(function () { return '?'; }).join(', '), ");\n "),
|
|
37
|
+
params: Object.values(data),
|
|
38
|
+
}); },
|
|
39
|
+
toMSSQL: function () { return ({
|
|
40
|
+
query: "\n INSERT INTO [".concat(_this.table, "]\n (").concat(dataKeys.map(function (key) { return "[".concat(key, "]"); }).join(', '), ")\n VALUES (").concat(dataKeys.map(function (key) { return '@' + key; }).join(', '), ");\n "),
|
|
41
|
+
params: data,
|
|
42
|
+
}); },
|
|
43
|
+
toSQLite: function () { return ({
|
|
44
|
+
query: "\n INSERT INTO \"".concat(_this.table, "\" \n (").concat(dataKeys.map(function (key) { return "\"".concat(key, "\""); }).join(', '), ") \n VALUES (").concat(dataKeys.map(function () { return '?'; }).join(', '), ");\n "),
|
|
45
|
+
params: Object.values(data),
|
|
46
|
+
}); },
|
|
47
|
+
};
|
|
48
|
+
};
|
|
29
49
|
SQLEditDataCommands.prototype.update = function (data, where) {
|
|
30
50
|
var _this = this;
|
|
31
51
|
return {
|
|
32
52
|
toMySQL: function () { return ({
|
|
33
|
-
query: "\n UPDATE `".concat(_this.table, "`\n SET ").concat(Object.keys(data).map(function (key) { return "`".concat(key, "` = ?"); }).join(','), "\n WHERE ").concat(Object.keys(where).map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('mysql', key, data[key], '?'); }).join(' AND '), ";\n "),
|
|
53
|
+
query: "\n UPDATE `".concat(_this.table, "`\n SET ").concat(Object.keys(data).map(function (key) { return "`".concat(key, "` = ?"); }).join(', '), "\n WHERE ").concat(Object.keys(where).map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('mysql', key, data[key], '?'); }).join(' AND '), ";\n "),
|
|
34
54
|
params: __spreadArray(__spreadArray([], Object.values(data), true), Object.values(where), true)
|
|
35
55
|
}); },
|
|
36
56
|
toMSSQL: function () { return ({
|
|
37
|
-
query: "\n UPDATE [".concat(_this.table, "] \n SET ").concat(Object.keys(data).map(function (key) { return "[".concat(key, "] = @").concat(key); }).join(','), "\n WHERE ").concat(Object.keys(where).map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('mssql', key, data[key], 'where_' + key); }).join(' AND '), ";\n "),
|
|
57
|
+
query: "\n UPDATE [".concat(_this.table, "] \n SET ").concat(Object.keys(data).map(function (key) { return "[".concat(key, "] = @").concat(key); }).join(', '), "\n WHERE ").concat(Object.keys(where).map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('mssql', key, data[key], 'where_' + key); }).join(' AND '), ";\n "),
|
|
38
58
|
params: (function () {
|
|
39
59
|
var params = __assign({}, data);
|
|
40
60
|
for (var _i = 0, _a = Object.keys(where); _i < _a.length; _i++) {
|
|
@@ -44,6 +64,10 @@ var SQLEditDataCommands = /** @class */ (function () {
|
|
|
44
64
|
return params;
|
|
45
65
|
})()
|
|
46
66
|
}); },
|
|
67
|
+
toSQLite: function () { return ({
|
|
68
|
+
query: "\n UPDATE \"".concat(_this.table, "\" \n SET ").concat(Object.keys(data).map(function (key) { return "\"".concat(key, "\" = ?"); }).join(', '), "\n WHERE ").concat(Object.keys(where).map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('sqlite', key, where[key], '?'); }).join(' AND '), ";"),
|
|
69
|
+
params: __spreadArray(__spreadArray([], Object.values(data), true), Object.values(where), true)
|
|
70
|
+
}); },
|
|
47
71
|
};
|
|
48
72
|
};
|
|
49
73
|
SQLEditDataCommands.prototype.upsert = function (data, idKeys) {
|
|
@@ -73,6 +97,12 @@ var SQLEditDataCommands = /** @class */ (function () {
|
|
|
73
97
|
query: "\n UPDATE [".concat(_this.table, "] \n SET ").concat(Object.keys(toUpdObj).map(function (key) { return "[".concat(key, "] = @").concat(key); }).join(','), "\n WHERE ").concat(idKeys.map(function (key) { return (0, raw_where_utils_1.converWhereQuery)('mssql', key, data[key], key); }).join(' AND '), ";\n\n IF @@ROWCOUNT = 0 INSERT INTO [").concat(_this.table, "]\n (").concat(dataKeys.map(function (a) { return "[".concat(a, "]"); }).join(', '), ")\n VALUES (").concat(dataKeys.map(function (key) { return '@' + key; }).join(', '), ");\n "),
|
|
74
98
|
params: data
|
|
75
99
|
}); },
|
|
100
|
+
toSQLite: function () { return ({
|
|
101
|
+
query: "\n INSERT INTO \"".concat(_this.table, "\" \n (").concat(dataKeys.map(function (key) { return "\"".concat(key, "\""); }).join(', '), ") \n VALUES (").concat(dataKeys.map(function () { return '?'; }).join(', '), ") \n ON CONFLICT (").concat(idKeys.map(function (key) { return "\"".concat(key, "\""); }).join(', '), ") \n DO UPDATE SET ").concat(Object.keys(toUpdObj).map(function (key) {
|
|
102
|
+
return "\"".concat(key, "\" = excluded.\"").concat(key, "\"");
|
|
103
|
+
}).join(', '), ";"),
|
|
104
|
+
params: Object.values(data)
|
|
105
|
+
}); },
|
|
76
106
|
};
|
|
77
107
|
};
|
|
78
108
|
return SQLEditDataCommands;
|
|
@@ -10,6 +10,7 @@ var SQLSelectCommands = /** @class */ (function () {
|
|
|
10
10
|
return {
|
|
11
11
|
toMySQL: function () { return "SELECT * FROM `".concat(_this.table, "`;"); },
|
|
12
12
|
toMSSQL: function () { return "SELECT * FROM [".concat(_this.table, "];"); },
|
|
13
|
+
toSQLite: function () { return "SELECT * FROM \"".concat(_this.table, "\";"); },
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
16
|
return SQLSelectCommands;
|
|
@@ -9,8 +9,8 @@ var SQLTablesCommands = /** @class */ (function () {
|
|
|
9
9
|
return {
|
|
10
10
|
toMySQL: function () { return "RENAME TABLE `".concat(old_name, "` TO `").concat(new_name, "`;"); },
|
|
11
11
|
toMSSQL: function () { return "EXEC sp_rename '".concat(old_name, "', '").concat(new_name, "';"); },
|
|
12
|
+
toSQLite: function () { return "ALTER TABLE \"".concat(old_name, "\" RENAME TO \"").concat(new_name, "\";"); },
|
|
12
13
|
// PG: ALTER TABLE "old_name" RENAME TO "new_name";
|
|
13
|
-
// SQLITE: ALTER TABLE "old_name" RENAME TO "new_name";
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
SQLTablesCommands.tableInfo = function (database, table) {
|
|
@@ -27,6 +27,12 @@ var SQLTablesCommands = /** @class */ (function () {
|
|
|
27
27
|
"AND TABLE_SCHEMA = 'dbo'",
|
|
28
28
|
"AND TABLE_NAME = '".concat(table, "'"),
|
|
29
29
|
].join(' '); },
|
|
30
|
+
toSQLite: function () { return [
|
|
31
|
+
"SELECT * FROM sqlite_master",
|
|
32
|
+
"WHERE type = 'table'",
|
|
33
|
+
"AND name = '".concat(table, "'"),
|
|
34
|
+
"LIMIT 1;"
|
|
35
|
+
].join(' '); },
|
|
30
36
|
};
|
|
31
37
|
};
|
|
32
38
|
SQLTablesCommands.createTable = function (options) {
|
|
@@ -42,6 +48,8 @@ var SQLTablesCommands = /** @class */ (function () {
|
|
|
42
48
|
str += "`".concat(columnName, "` ");
|
|
43
49
|
else if (dbtype === 'mssql')
|
|
44
50
|
str += "[".concat(columnName, "] ");
|
|
51
|
+
else if (dbtype === 'sqlite')
|
|
52
|
+
str += "\"".concat(columnName, "\" ");
|
|
45
53
|
else if (dbtype === 'postgres')
|
|
46
54
|
str += "\"".concat(columnName, "\" ");
|
|
47
55
|
else
|
|
@@ -54,12 +62,14 @@ var SQLTablesCommands = /** @class */ (function () {
|
|
|
54
62
|
return {
|
|
55
63
|
toMySQL: function () { return _createQuery('mysql'); },
|
|
56
64
|
toMSSQL: function () { return _createQuery('mssql'); },
|
|
65
|
+
toSQLite: function () { return _createQuery('sqlite'); },
|
|
57
66
|
};
|
|
58
67
|
};
|
|
59
68
|
SQLTablesCommands.getTablesSizes = function (database) {
|
|
60
69
|
return {
|
|
61
70
|
toMySQL: function () { return "\n SELECT \n TABLE_NAME as 'table_name', \n (DATA_LENGTH / 1024) as 'data_kb', \n (INDEX_LENGTH / 1024) as 'index_kb'\n FROM information_schema.TABLES\n WHERE table_schema = '".concat(database, "'\n ORDER BY (data_kb + index_kb) DESC;\n "); },
|
|
62
71
|
toMSSQL: function () { return "\n SELECT * FROM (\n SELECT\n t.name AS table_name,\n SUM(ps.in_row_data_page_count + ps.lob_used_page_count\n + ps.row_overflow_used_page_count\n ) * 8 AS data_kb,\n SUM(ps.used_page_count - ps.in_row_data_page_count\n - ps.lob_used_page_count - ps.row_overflow_used_page_count\n ) * 8 AS index_kb\n FROM sys.tables t\n JOIN sys.dm_db_partition_stats ps\n ON t.object_id = ps.object_id\n GROUP BY t.name\n ) t ORDER BY (data_kb + index_kb) DESC;\n "; },
|
|
72
|
+
toSQLite: function () { return "\n SELECT name AS table_name, \n (SUM(pgsize) / 1024.0) AS data_kb \n FROM dbstat WHERE name NOT LIKE 'sqlite_%' \n GROUP BY name ORDER BY data_kb DESC;\n "; },
|
|
63
73
|
// postgress
|
|
64
74
|
// SELECT
|
|
65
75
|
// relname AS table_name,
|
|
@@ -8,7 +8,7 @@ export declare abstract class AbstractSQLConnection {
|
|
|
8
8
|
abstract initConnection(): Promise<void>;
|
|
9
9
|
abstract destroyConnection(): Promise<void>;
|
|
10
10
|
protected abstract sendSQL<T = any>(query: string, params?: any[]): Promise<T[]>;
|
|
11
|
-
toRawSQL(sql: ISQLCommandAdapter | SQLCommandData | string): SQLCommandData
|
|
11
|
+
toRawSQL(sql: ISQLCommandAdapter | SQLCommandData | string): Promise<SQLCommandData>;
|
|
12
12
|
runSQL<T = any>(query: SQLCommandData): Promise<T[]>;
|
|
13
13
|
runSQL<T = any>(query: ISQLCommandAdapter): Promise<T[]>;
|
|
14
14
|
runSQL<T = any>(query: AbstractSQLCommand): Promise<T[]>;
|
|
@@ -43,6 +43,7 @@ var AbstractSQLConnection = /** @class */ (function () {
|
|
|
43
43
|
function AbstractSQLConnection(rawOptions, abstractCommandFunctionName) {
|
|
44
44
|
this.rawOptions = rawOptions;
|
|
45
45
|
this.abstractCommandFunctionName = abstractCommandFunctionName;
|
|
46
|
+
this.sendSQL = this.sendSQL.bind(this);
|
|
46
47
|
}
|
|
47
48
|
Object.defineProperty(AbstractSQLConnection.prototype, "databaseName", {
|
|
48
49
|
get: function () {
|
|
@@ -52,7 +53,17 @@ var AbstractSQLConnection = /** @class */ (function () {
|
|
|
52
53
|
configurable: true
|
|
53
54
|
});
|
|
54
55
|
AbstractSQLConnection.prototype.toRawSQL = function (sql) {
|
|
55
|
-
return (0,
|
|
56
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
57
|
+
return __generator(this, function (_a) {
|
|
58
|
+
switch (_a.label) {
|
|
59
|
+
case 0: return [4 /*yield*/, (0, commands_1.toRawSQL)(this.rawOptions.type, sql, {
|
|
60
|
+
database: this.databaseName,
|
|
61
|
+
runAdditionalSQL: this.sendSQL
|
|
62
|
+
})];
|
|
63
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
56
67
|
};
|
|
57
68
|
AbstractSQLConnection.prototype.runSQL = function (query, params) {
|
|
58
69
|
var _a;
|
|
@@ -71,7 +82,8 @@ var AbstractSQLConnection = /** @class */ (function () {
|
|
|
71
82
|
abstrKey = this.abstractCommandFunctionName;
|
|
72
83
|
if (!(abstrKey in query)) return [3 /*break*/, 2];
|
|
73
84
|
return [4 /*yield*/, ((_a = query === null || query === void 0 ? void 0 : query[abstrKey]) === null || _a === void 0 ? void 0 : _a.call(query, {
|
|
74
|
-
database: this.databaseName
|
|
85
|
+
database: this.databaseName,
|
|
86
|
+
runAdditionalSQL: this.sendSQL
|
|
75
87
|
}))];
|
|
76
88
|
case 1:
|
|
77
89
|
query = _b.sent();
|
|
@@ -8,4 +8,5 @@ export declare function createSQLConnection(options: DatabaseOptions): AbstractS
|
|
|
8
8
|
export * from './abstract.connection';
|
|
9
9
|
export * from './mysql.connection';
|
|
10
10
|
export * from './mssql.connection';
|
|
11
|
+
export * from './sqlite.connection';
|
|
11
12
|
export { MySQLConnection as RawMySQLConnection };
|
|
@@ -18,6 +18,7 @@ exports.RawMySQLConnection = exports.createSQLConnection = exports.isDatabaseTyp
|
|
|
18
18
|
var mysql_connection_1 = require("./mysql.connection");
|
|
19
19
|
Object.defineProperty(exports, "RawMySQLConnection", { enumerable: true, get: function () { return mysql_connection_1.MySQLConnection; } });
|
|
20
20
|
var mssql_connection_1 = require("./mssql.connection");
|
|
21
|
+
var sqlite_connection_1 = require("./sqlite.connection");
|
|
21
22
|
function isDatabaseTypeIs(options, type) {
|
|
22
23
|
return options.type === type;
|
|
23
24
|
}
|
|
@@ -27,9 +28,12 @@ function createSQLConnection(options) {
|
|
|
27
28
|
return new mysql_connection_1.MySQLConnection(options);
|
|
28
29
|
if (isDatabaseTypeIs(options, 'mssql'))
|
|
29
30
|
return new mssql_connection_1.MSSQLConnection(options);
|
|
31
|
+
if (isDatabaseTypeIs(options, 'sqlite'))
|
|
32
|
+
return new sqlite_connection_1.SQLiteConnection(options);
|
|
30
33
|
throw "Connection for database type " + options.type + ' is not implemented :(';
|
|
31
34
|
}
|
|
32
35
|
exports.createSQLConnection = createSQLConnection;
|
|
33
36
|
__exportStar(require("./abstract.connection"), exports);
|
|
34
37
|
__exportStar(require("./mysql.connection"), exports);
|
|
35
38
|
__exportStar(require("./mssql.connection"), exports);
|
|
39
|
+
__exportStar(require("./sqlite.connection"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DatabaseOptions } from '@klapeks/utils';
|
|
2
|
+
import { AbstractSQLConnection } from './abstract.connection';
|
|
3
|
+
import type { Database as SQLite3Database } from 'sqlite3';
|
|
4
|
+
export declare class SQLiteConnection extends AbstractSQLConnection {
|
|
5
|
+
private _db;
|
|
6
|
+
constructor(options: DatabaseOptions & {
|
|
7
|
+
type: "sqlite";
|
|
8
|
+
});
|
|
9
|
+
initConnection(): Promise<void>;
|
|
10
|
+
getConnection(): Promise<SQLite3Database>;
|
|
11
|
+
destroyConnection(): Promise<void>;
|
|
12
|
+
sendSQL<T = any>(query: string, params?: any): Promise<T[]>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.SQLiteConnection = void 0;
|
|
55
|
+
var utils_1 = require("@klapeks/utils");
|
|
56
|
+
var abstract_connection_1 = require("./abstract.connection");
|
|
57
|
+
var quiet_require_1 = require("../../utils/quiet.require");
|
|
58
|
+
var sqlite3 = (0, quiet_require_1.quietRequire)('sqlite3');
|
|
59
|
+
var logger = new utils_1.Logger('SQLite');
|
|
60
|
+
var SQLiteConnection = /** @class */ (function (_super) {
|
|
61
|
+
__extends(SQLiteConnection, _super);
|
|
62
|
+
function SQLiteConnection(options) {
|
|
63
|
+
return _super.call(this, options, 'toSQLite') || this;
|
|
64
|
+
}
|
|
65
|
+
SQLiteConnection.prototype.initConnection = function () {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0: return [4 /*yield*/, this.getConnection()];
|
|
70
|
+
case 1:
|
|
71
|
+
_a.sent();
|
|
72
|
+
return [2 /*return*/];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
SQLiteConnection.prototype.getConnection = function () {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
79
|
+
return __generator(this, function (_a) {
|
|
80
|
+
if (!sqlite3)
|
|
81
|
+
throw "sqlite3 module is not installed";
|
|
82
|
+
if (this._db)
|
|
83
|
+
return [2 /*return*/, this._db];
|
|
84
|
+
this._db = new sqlite3.Database(this.rawOptions.database);
|
|
85
|
+
this._db.exec("\n PRAGMA journal_mode = WAL;\n PRAGMA busy_timeout = 15000;\n ");
|
|
86
|
+
return [2 /*return*/, this._db];
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
SQLiteConnection.prototype.destroyConnection = function () {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
92
|
+
var _this = this;
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
95
|
+
if (!_this._db)
|
|
96
|
+
return resolve();
|
|
97
|
+
try {
|
|
98
|
+
_this._db.close(function (err) {
|
|
99
|
+
logger.log('Database disconnected');
|
|
100
|
+
if (err)
|
|
101
|
+
reject(err);
|
|
102
|
+
else
|
|
103
|
+
resolve();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
reject(err);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
_this._db = undefined;
|
|
111
|
+
}
|
|
112
|
+
})];
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
SQLiteConnection.prototype.sendSQL = function (query, params) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
118
|
+
var connection;
|
|
119
|
+
var _this = this;
|
|
120
|
+
return __generator(this, function (_a) {
|
|
121
|
+
switch (_a.label) {
|
|
122
|
+
case 0: return [4 /*yield*/, this.getConnection()];
|
|
123
|
+
case 1:
|
|
124
|
+
connection = _a.sent();
|
|
125
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
126
|
+
_this.debugQuery(query, params);
|
|
127
|
+
var upperSQL = query.trim().toUpperCase();
|
|
128
|
+
if (upperSQL.startsWith('BEGIN')
|
|
129
|
+
|| upperSQL.startsWith('COMMIT')
|
|
130
|
+
|| upperSQL.startsWith('ROLLBACK')) {
|
|
131
|
+
return connection.exec(query, function (err) {
|
|
132
|
+
if (err)
|
|
133
|
+
return reject(err);
|
|
134
|
+
resolve([]);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (!params)
|
|
138
|
+
params = [];
|
|
139
|
+
if (upperSQL.startsWith('SELECT') || upperSQL.startsWith("PRAGMA")) {
|
|
140
|
+
return connection.all(query, params, function (err1, rows) {
|
|
141
|
+
if (err1)
|
|
142
|
+
return reject(err1);
|
|
143
|
+
resolve((rows || []));
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return connection.run(query, params, function (err1) {
|
|
147
|
+
if (err1)
|
|
148
|
+
return reject(err1);
|
|
149
|
+
resolve(this);
|
|
150
|
+
});
|
|
151
|
+
})];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
return SQLiteConnection;
|
|
157
|
+
}(abstract_connection_1.AbstractSQLConnection));
|
|
158
|
+
exports.SQLiteConnection = SQLiteConnection;
|
|
@@ -7,6 +7,8 @@ function sqlQuoteKey(dbType, str) {
|
|
|
7
7
|
return "`".concat(str, "`");
|
|
8
8
|
if (dbType === 'mssql')
|
|
9
9
|
return "[".concat(str, "]");
|
|
10
|
+
if (dbType === 'sqlite')
|
|
11
|
+
return "\"".concat(str, "\"");
|
|
10
12
|
return str;
|
|
11
13
|
}
|
|
12
14
|
function converWhereQuery(dbType, key, value, keyAlias) {
|
|
@@ -24,7 +26,7 @@ function converWhereQuery(dbType, key, value, keyAlias) {
|
|
|
24
26
|
if (value === null) {
|
|
25
27
|
return sqlQuoteKey(dbType, key) + ' IS NULL';
|
|
26
28
|
}
|
|
27
|
-
if (keyAlias === '?' || dbType === 'mysql') {
|
|
29
|
+
if (keyAlias === '?' || dbType === 'mysql' || dbType === 'sqlite') {
|
|
28
30
|
return sqlQuoteKey(dbType, key) + " = ?";
|
|
29
31
|
}
|
|
30
32
|
if (dbType === 'mssql') {
|
|
@@ -9,14 +9,7 @@ export declare function createRelation(target: string, type: "one-to-many" | "on
|
|
|
9
9
|
cascade: boolean;
|
|
10
10
|
inverseSide: string;
|
|
11
11
|
};
|
|
12
|
-
export declare const MULTISQL_COLUMNS_TYPES:
|
|
13
|
-
dbtype: "sqlite" | "mysql" | "postgres" | "mssql";
|
|
14
|
-
enum: "simple-enum" | "enum";
|
|
15
|
-
datetime: "datetime" | "datetime2" | "timestamp";
|
|
16
|
-
float32: "float" | "double";
|
|
17
|
-
float64: "float" | "double";
|
|
18
|
-
json: "simple-json" | "json";
|
|
19
|
-
};
|
|
12
|
+
export declare const MULTISQL_COLUMNS_TYPES: import("@klapeks/utils").IDatabaseColumnTypesList;
|
|
20
13
|
export declare const FloatingColumn: MicroColumnTypeObject;
|
|
21
14
|
export declare const NullableFloatingColumn: MicroColumnTypeObject;
|
|
22
15
|
export declare const Float64Column: MicroColumnTypeObject;
|
|
@@ -18,7 +18,7 @@ function createRelation(target, type, inverseSide, cascade) {
|
|
|
18
18
|
return { target: target, type: type, cascade: cascade, inverseSide: inverseSide };
|
|
19
19
|
}
|
|
20
20
|
exports.createRelation = createRelation;
|
|
21
|
-
exports.MULTISQL_COLUMNS_TYPES = (0, utils_1.
|
|
21
|
+
exports.MULTISQL_COLUMNS_TYPES = (0, utils_1.getEnvDatabaseColumnTypes)();
|
|
22
22
|
exports.FloatingColumn = {
|
|
23
23
|
type: exports.MULTISQL_COLUMNS_TYPES.float32, default: 0
|
|
24
24
|
// type: "decimal", precision: 10, scale: 6, default: 0
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klapeks/micro-db-utils",
|
|
3
3
|
"description": "klapeks utils for db and other storages",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.20",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"author": "Klapeks",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"patch": "npm version patch -f --no-git-tag-version",
|
|
10
10
|
"build": "npm run patch && tsc --skipLibCheck",
|
|
11
|
+
"build:no-patch": "tsc --skipLibCheck",
|
|
11
12
|
"prepublishOnly": "tsc --skipLibCheck"
|
|
12
13
|
},
|
|
13
14
|
"keywords": [
|
|
@@ -22,15 +23,17 @@
|
|
|
22
23
|
"@aws-sdk/client-s3": "^3.842.0",
|
|
23
24
|
"@aws-sdk/s3-request-presigner": "^3.844.0",
|
|
24
25
|
"@klapeks/api-creation-tools": "^0.0.7",
|
|
25
|
-
"@klapeks/utils": "^0.1
|
|
26
|
+
"@klapeks/utils": "^0.2.1",
|
|
26
27
|
"@types/mssql": "^12.3.0",
|
|
27
28
|
"@types/node": "^18.11.18",
|
|
29
|
+
"@types/pg": "^8.23.1",
|
|
28
30
|
"axios": "^1.7.7",
|
|
29
31
|
"ioredis": "^5.6.1",
|
|
30
32
|
"kafkajs": "^2.2.4",
|
|
31
33
|
"mongoose": "^8.16.1",
|
|
32
34
|
"mssql": "^11.0.1",
|
|
33
35
|
"mysql2": "^3.11.3",
|
|
36
|
+
"pg": "^8.23.0",
|
|
34
37
|
"redis": "^4.7.0",
|
|
35
38
|
"sqlite3": "^5.1.7",
|
|
36
39
|
"typeorm": "^0.3.21",
|