@klapeks/micro-db-utils 0.0.12 → 0.0.13
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/connections/cloud/s3.js +1 -1
- package/dist/connections/index.d.ts +0 -1
- package/dist/connections/index.js +2 -2
- package/dist/connections/kafka/kafka.js +1 -1
- package/dist/connections/nosql/mongo.js +1 -1
- package/dist/connections/nosql/redis.js +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/micro.sql.d.ts +11 -0
- package/dist/micro.sql.js +87 -0
- package/dist/migrations/index.d.ts +1 -0
- package/dist/migrations/index.js +1 -0
- package/dist/migrations/mysql.migrations.d.ts +0 -2
- package/dist/migrations/mysql.migrations.js +6 -201
- package/dist/migrations/super.migrations.d.ts +13 -0
- package/dist/migrations/super.migrations.js +313 -0
- package/dist/selectors/raw-mysql.select.handler.d.ts +2 -2
- package/dist/sql/column.type.parser.d.ts +12 -0
- package/dist/sql/column.type.parser.js +53 -0
- package/dist/sql/commands/abstract.sql.command.d.ts +21 -0
- package/dist/sql/commands/abstract.sql.command.js +30 -0
- package/dist/sql/commands/alter.commands.d.ts +8 -0
- package/dist/sql/commands/alter.commands.js +27 -0
- package/dist/sql/commands/edit-data.commands.d.ts +6 -0
- package/dist/sql/commands/edit-data.commands.js +48 -0
- package/dist/sql/commands/index.d.ts +6 -0
- package/dist/sql/commands/index.js +22 -0
- package/dist/sql/commands/select.commands.d.ts +6 -0
- package/dist/sql/commands/select.commands.js +17 -0
- package/dist/sql/commands/tables.commands.d.ts +10 -0
- package/dist/sql/commands/tables.commands.js +75 -0
- package/dist/sql/commands/time.commands.d.ts +7 -0
- package/dist/sql/commands/time.commands.js +24 -0
- package/dist/sql/connections/abstract.connection.d.ts +22 -0
- package/dist/sql/connections/abstract.connection.js +127 -0
- package/dist/sql/connections/index.d.ts +11 -0
- package/dist/sql/connections/index.js +35 -0
- package/dist/sql/connections/mssql.connection.d.ts +15 -0
- package/dist/sql/connections/mssql.connection.js +171 -0
- package/dist/{connections → sql/connections}/mysql.connection.d.ts +5 -4
- package/dist/{connections → sql/connections}/mysql.connection.js +46 -27
- package/dist/sql/index.d.ts +2 -0
- package/dist/sql/index.js +18 -0
- package/dist/utils/columns.types.d.ts +11 -11
- package/package.json +4 -2
- /package/dist/{connections → utils}/quiet.require.d.ts +0 -0
- /package/dist/{connections → utils}/quiet.require.js +0 -0
|
@@ -0,0 +1,313 @@
|
|
|
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
|
+
};
|
|
38
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.SuperMigrations = void 0;
|
|
49
|
+
var typeorm_1 = require("typeorm");
|
|
50
|
+
var sql_1 = require("../sql");
|
|
51
|
+
var micro_sql_1 = require("../micro.sql");
|
|
52
|
+
var utils_1 = require("@klapeks/utils");
|
|
53
|
+
var tables_commands_1 = require("../sql/commands/tables.commands");
|
|
54
|
+
var iso_date_time_1 = require("../utils/iso.date.time");
|
|
55
|
+
var logger = new utils_1.Logger("SuperMigrations");
|
|
56
|
+
var SuperMigrations = /** @class */ (function () {
|
|
57
|
+
function SuperMigrations() {
|
|
58
|
+
}
|
|
59
|
+
Object.defineProperty(SuperMigrations, "migrationTableName", {
|
|
60
|
+
get: function () {
|
|
61
|
+
return process.env.DB_UTILS_MIGRATION_TABLE_NAME || "_kldb_mini_migrations";
|
|
62
|
+
},
|
|
63
|
+
enumerable: false,
|
|
64
|
+
configurable: true
|
|
65
|
+
});
|
|
66
|
+
SuperMigrations.addRawMigration = function (dbtype, entity, date, sql) {
|
|
67
|
+
if (typeof date == 'string')
|
|
68
|
+
date = new Date(date);
|
|
69
|
+
SuperMigrations._migrations.push({
|
|
70
|
+
dbtype: dbtype == 'all' ? undefined : dbtype,
|
|
71
|
+
table: entity.options.tableName || entity.options.name,
|
|
72
|
+
date: date,
|
|
73
|
+
sql: sql
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
SuperMigrations.addMigration = function (entity, date, sql) {
|
|
77
|
+
SuperMigrations.addRawMigration('all', entity, date, sql);
|
|
78
|
+
};
|
|
79
|
+
SuperMigrations.runMigrationForTable = function (sqlInstance, table, lastRealMigrationDateTime, onMigrationComplete) {
|
|
80
|
+
var _a, _b, _c;
|
|
81
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
+
var databaseName, todoMigrations, isTableExistsInfo, firstSQL, lastMigrationDate, _local_runSQL, lastRealMigration, _i, todoMigrations_1, migration, runnedMigrationsAmount, migrationName, sqls, _d, sqls_1, sql, sql;
|
|
83
|
+
var _this = this;
|
|
84
|
+
return __generator(this, function (_e) {
|
|
85
|
+
switch (_e.label) {
|
|
86
|
+
case 0:
|
|
87
|
+
databaseName = sqlInstance.databaseName;
|
|
88
|
+
todoMigrations = SuperMigrations._migrations.sort(function (c1, c2) {
|
|
89
|
+
return c1.date.getTime() - c2.date.getTime();
|
|
90
|
+
});
|
|
91
|
+
todoMigrations = __spreadArray([], todoMigrations, true).filter(function (migration) {
|
|
92
|
+
if (migration.table != table)
|
|
93
|
+
return false;
|
|
94
|
+
if (migration.dbtype && migration.dbtype != 'all') {
|
|
95
|
+
if (migration.dbtype != sqlInstance.rawOptions.type)
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
// logger.log("TODO MIGRATIONG FOR TABLE", table, todoMigrations);
|
|
101
|
+
if (!(todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length))
|
|
102
|
+
return [2 /*return*/];
|
|
103
|
+
return [4 /*yield*/, sqlInstance.runSQL_One(tables_commands_1.SQLTablesCommands.tableInfo(databaseName, table))];
|
|
104
|
+
case 1: return [4 /*yield*/, _e.sent()];
|
|
105
|
+
case 2:
|
|
106
|
+
isTableExistsInfo = _e.sent();
|
|
107
|
+
if (!!isTableExistsInfo) return [3 /*break*/, 5];
|
|
108
|
+
firstSQL = (function () {
|
|
109
|
+
var _a;
|
|
110
|
+
var sql = (_a = todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations[0]) === null || _a === void 0 ? void 0 : _a.sql;
|
|
111
|
+
if (!sql)
|
|
112
|
+
return null;
|
|
113
|
+
if (Array.isArray(sql))
|
|
114
|
+
sql = sql[0];
|
|
115
|
+
return sqlInstance.toRawSQL(sql);
|
|
116
|
+
})();
|
|
117
|
+
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*/, 5];
|
|
118
|
+
logger.log("Table ".concat(table, " not found. Migrations ").concat(todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length, " will be skipped"));
|
|
119
|
+
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*/, 4];
|
|
121
|
+
return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.editData(this.migrationTableName).upsert({
|
|
122
|
+
table: table,
|
|
123
|
+
lastMigrationTime: (0, iso_date_time_1.toISODate)(lastMigrationDate)
|
|
124
|
+
}, ['table']))];
|
|
125
|
+
case 3:
|
|
126
|
+
_e.sent();
|
|
127
|
+
_e.label = 4;
|
|
128
|
+
case 4: return [2 /*return*/];
|
|
129
|
+
case 5:
|
|
130
|
+
_local_runSQL = function (sql, params) { return __awaiter(_this, void 0, void 0, function () {
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
switch (_a.label) {
|
|
133
|
+
case 0:
|
|
134
|
+
if (!(sql.toLowerCase().startsWith("alter table")
|
|
135
|
+
|| sql.toLowerCase().startsWith('exec sp_rename'))) return [3 /*break*/, 2];
|
|
136
|
+
// log error if error
|
|
137
|
+
return [4 /*yield*/, sqlInstance.runSQL(sql, params).catch(function (err) {
|
|
138
|
+
logger.error("Error while alter table:", err);
|
|
139
|
+
})];
|
|
140
|
+
case 1:
|
|
141
|
+
// log error if error
|
|
142
|
+
_a.sent();
|
|
143
|
+
return [3 /*break*/, 4];
|
|
144
|
+
case 2:
|
|
145
|
+
// throw error if error
|
|
146
|
+
return [4 /*yield*/, sqlInstance.runSQL(sql, params)];
|
|
147
|
+
case 3:
|
|
148
|
+
// throw error if error
|
|
149
|
+
_a.sent();
|
|
150
|
+
_a.label = 4;
|
|
151
|
+
case 4: return [2 /*return*/];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}); };
|
|
155
|
+
lastRealMigration = lastRealMigrationDateTime
|
|
156
|
+
? new Date(lastRealMigrationDateTime) : null;
|
|
157
|
+
_i = 0, todoMigrations_1 = todoMigrations;
|
|
158
|
+
_e.label = 6;
|
|
159
|
+
case 6:
|
|
160
|
+
if (!(_i < todoMigrations_1.length)) return [3 /*break*/, 17];
|
|
161
|
+
migration = todoMigrations_1[_i];
|
|
162
|
+
if (migration.table != table)
|
|
163
|
+
return [3 /*break*/, 16];
|
|
164
|
+
logger;
|
|
165
|
+
if (lastRealMigration && lastRealMigration.getTime() >= migration.date.getTime()) {
|
|
166
|
+
return [3 /*break*/, 16];
|
|
167
|
+
}
|
|
168
|
+
runnedMigrationsAmount = onMigrationComplete();
|
|
169
|
+
migrationName = '"' + table + ' ' + (0, iso_date_time_1.toISODate)(migration.date) + '"';
|
|
170
|
+
if (!Array.isArray(migration.sql)) return [3 /*break*/, 11];
|
|
171
|
+
sqls = migration.sql.filter(Boolean).map(function (sql) {
|
|
172
|
+
var sql2 = sqlInstance.toRawSQL(sql);
|
|
173
|
+
sql2.query = utils_1.utils.replaceAll(sql2.query.trim(), "%{table_name}", table);
|
|
174
|
+
return sql2;
|
|
175
|
+
});
|
|
176
|
+
logger.log(runnedMigrationsAmount, "| Migrations will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sqls.map(function (s) { return s.query; }));
|
|
177
|
+
_d = 0, sqls_1 = sqls;
|
|
178
|
+
_e.label = 7;
|
|
179
|
+
case 7:
|
|
180
|
+
if (!(_d < sqls_1.length)) return [3 /*break*/, 10];
|
|
181
|
+
sql = sqls_1[_d];
|
|
182
|
+
return [4 /*yield*/, _local_runSQL(sql.query, sql.params)];
|
|
183
|
+
case 8:
|
|
184
|
+
_e.sent();
|
|
185
|
+
_e.label = 9;
|
|
186
|
+
case 9:
|
|
187
|
+
_d++;
|
|
188
|
+
return [3 /*break*/, 7];
|
|
189
|
+
case 10: return [3 /*break*/, 13];
|
|
190
|
+
case 11:
|
|
191
|
+
sql = sqlInstance.toRawSQL(migration.sql);
|
|
192
|
+
sql.query = utils_1.utils.replaceAll(sql.query.trim(), "%{table_name}", table);
|
|
193
|
+
logger.log(runnedMigrationsAmount, "| Migration will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sql.query);
|
|
194
|
+
return [4 /*yield*/, _local_runSQL(sql.query, sql.params)];
|
|
195
|
+
case 12:
|
|
196
|
+
_e.sent();
|
|
197
|
+
_e.label = 13;
|
|
198
|
+
case 13: return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.editData(this.migrationTableName).upsert({
|
|
199
|
+
table: table,
|
|
200
|
+
lastMigrationTime: (0, iso_date_time_1.toISODate)(migration.date)
|
|
201
|
+
}, ['table']))];
|
|
202
|
+
case 14:
|
|
203
|
+
_e.sent();
|
|
204
|
+
logger.log(runnedMigrationsAmount, "| Migration", migrationName, "successfully done");
|
|
205
|
+
return [4 /*yield*/, utils_1.utils.sleep(100)];
|
|
206
|
+
case 15:
|
|
207
|
+
_e.sent();
|
|
208
|
+
_e.label = 16;
|
|
209
|
+
case 16:
|
|
210
|
+
_i++;
|
|
211
|
+
return [3 /*break*/, 6];
|
|
212
|
+
case 17: return [2 /*return*/];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
SuperMigrations.runMigrations = function (dataSource) {
|
|
218
|
+
var _a, _b;
|
|
219
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
220
|
+
var options, sqlInstance, _existsMigrationTable, migrationRecords, _c, runnedMigrationsAmount_1, tablesToMigrate, _i, tablesToMigrate_1, table, err_1, err_2;
|
|
221
|
+
return __generator(this, function (_d) {
|
|
222
|
+
switch (_d.label) {
|
|
223
|
+
case 0:
|
|
224
|
+
options = (0, utils_1.dataSourceOptions)();
|
|
225
|
+
_d.label = 1;
|
|
226
|
+
case 1:
|
|
227
|
+
_d.trys.push([1, 16, , 17]);
|
|
228
|
+
sqlInstance = (0, sql_1.createSQLConnection)(options);
|
|
229
|
+
return [4 /*yield*/, sqlInstance.initConnection()];
|
|
230
|
+
case 2:
|
|
231
|
+
_d.sent();
|
|
232
|
+
_d.label = 3;
|
|
233
|
+
case 3:
|
|
234
|
+
_d.trys.push([3, 13, , 15]);
|
|
235
|
+
return [4 /*yield*/, sqlInstance
|
|
236
|
+
.runSQL_One(tables_commands_1.SQLTablesCommands.tableInfo(options.database, SuperMigrations.migrationTableName))];
|
|
237
|
+
case 4:
|
|
238
|
+
_existsMigrationTable = _d.sent();
|
|
239
|
+
logger.debug("Migrations table info:", _existsMigrationTable);
|
|
240
|
+
if (!!_existsMigrationTable) return [3 /*break*/, 6];
|
|
241
|
+
// creating migration table if not exists
|
|
242
|
+
logger.log("Creating migration table..");
|
|
243
|
+
return [4 /*yield*/, sqlInstance.runSQL_One(tables_commands_1.SQLTablesCommands.createTable({
|
|
244
|
+
table: SuperMigrations.migrationTableName,
|
|
245
|
+
columns: {
|
|
246
|
+
table: { type: String, length: 255, primary: true },
|
|
247
|
+
lastMigrationTime: { type: String, length: 128 }
|
|
248
|
+
}
|
|
249
|
+
}))];
|
|
250
|
+
case 5:
|
|
251
|
+
_d.sent();
|
|
252
|
+
_d.label = 6;
|
|
253
|
+
case 6:
|
|
254
|
+
_c = utils_1.mapOf;
|
|
255
|
+
return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.select(SuperMigrations.migrationTableName).all())];
|
|
256
|
+
case 7:
|
|
257
|
+
migrationRecords = _c.apply(void 0, [_d.sent(), 'table']);
|
|
258
|
+
runnedMigrationsAmount_1 = 0;
|
|
259
|
+
tablesToMigrate = Object.values(dataSource.options.entities || []).map(function (m) {
|
|
260
|
+
if (m instanceof typeorm_1.EntitySchema)
|
|
261
|
+
return m.options.name;
|
|
262
|
+
return undefined;
|
|
263
|
+
}).filter(Boolean);
|
|
264
|
+
_i = 0, tablesToMigrate_1 = tablesToMigrate;
|
|
265
|
+
_d.label = 8;
|
|
266
|
+
case 8:
|
|
267
|
+
if (!(_i < tablesToMigrate_1.length)) return [3 /*break*/, 11];
|
|
268
|
+
table = tablesToMigrate_1[_i];
|
|
269
|
+
return [4 /*yield*/, SuperMigrations.runMigrationForTable(sqlInstance, table, (_a = migrationRecords.get(table)) === null || _a === void 0 ? void 0 : _a.lastMigrationTime, function () { return ++runnedMigrationsAmount_1; })];
|
|
270
|
+
case 9:
|
|
271
|
+
_d.sent();
|
|
272
|
+
_d.label = 10;
|
|
273
|
+
case 10:
|
|
274
|
+
_i++;
|
|
275
|
+
return [3 /*break*/, 8];
|
|
276
|
+
case 11:
|
|
277
|
+
if (runnedMigrationsAmount_1) {
|
|
278
|
+
logger.log(utils_1.terminalColors.green + "All migrations completed:"
|
|
279
|
+
+ utils_1.terminalColors.reset, runnedMigrationsAmount_1);
|
|
280
|
+
}
|
|
281
|
+
return [4 /*yield*/, sqlInstance.destroyConnection().catch(function (err) {
|
|
282
|
+
logger.error("Error while closing pool:", err);
|
|
283
|
+
})];
|
|
284
|
+
case 12:
|
|
285
|
+
_d.sent();
|
|
286
|
+
return [3 /*break*/, 15];
|
|
287
|
+
case 13:
|
|
288
|
+
err_1 = _d.sent();
|
|
289
|
+
return [4 /*yield*/, sqlInstance.destroyConnection().catch(function (err) {
|
|
290
|
+
logger.error("Error while closing pool:", err);
|
|
291
|
+
})];
|
|
292
|
+
case 14:
|
|
293
|
+
_d.sent();
|
|
294
|
+
throw err_1;
|
|
295
|
+
case 15: return [3 /*break*/, 17];
|
|
296
|
+
case 16:
|
|
297
|
+
err_2 = _d.sent();
|
|
298
|
+
if ((_b = err_2 === null || err_2 === void 0 ? void 0 : err_2.sqlMessage) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes("unknown database"))
|
|
299
|
+
return [2 /*return*/];
|
|
300
|
+
// logger.error("err of musql:", err.sqlMessage);
|
|
301
|
+
throw err_2;
|
|
302
|
+
case 17: return [2 /*return*/];
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
SuperMigrations._migrations = [];
|
|
308
|
+
return SuperMigrations;
|
|
309
|
+
}());
|
|
310
|
+
exports.SuperMigrations = SuperMigrations;
|
|
311
|
+
SuperMigrations.addMigration = SuperMigrations.addMigration.bind(SuperMigrations);
|
|
312
|
+
SuperMigrations.addRawMigration = SuperMigrations.addRawMigration.bind(SuperMigrations);
|
|
313
|
+
SuperMigrations.runMigrations = SuperMigrations.runMigrations.bind(SuperMigrations);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type SelectOptions } from "@klapeks/api-creation-tools";
|
|
2
|
-
import { RawMySQLConnection } from "../connections";
|
|
3
2
|
import { AbstractSelectHandler, SelectEntityHandlerOptions } from "./abstract.select.handler";
|
|
4
3
|
import { DatabaseOptions } from "@klapeks/utils";
|
|
4
|
+
import { MySQLConnection } from "../sql";
|
|
5
5
|
export type MySQLSelectEntityHandlerOptions<T extends object, K extends string> = SelectEntityHandlerOptions<T, K> & {
|
|
6
6
|
schemaTableName: string;
|
|
7
|
-
mysql:
|
|
7
|
+
mysql: MySQLConnection;
|
|
8
8
|
};
|
|
9
9
|
export declare class MySQLSelectEntityHandler<T extends object, K extends string> extends AbstractSelectHandler<T, K, MySQLSelectEntityHandlerOptions<T, K>> {
|
|
10
10
|
constructor(options: MySQLSelectEntityHandlerOptions<T, K>);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DatabaseOptions } from "@klapeks/utils";
|
|
2
|
+
import { ColumnType } from "typeorm";
|
|
3
|
+
export declare function getRawDatabaseColumnTypeOfTypeORM(dbtype: DatabaseOptions['type'], type: ColumnType): ColumnType;
|
|
4
|
+
export interface MicroColumnTypeObject {
|
|
5
|
+
type: ColumnType;
|
|
6
|
+
primary?: boolean;
|
|
7
|
+
length?: number;
|
|
8
|
+
nullable?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare namespace MicroColumnTypeObject {
|
|
11
|
+
function toSQLQuery(dbType: DatabaseOptions['type'], options: MicroColumnTypeObject, queryType?: "create-table" | "alter-column"): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MicroColumnTypeObject = exports.getRawDatabaseColumnTypeOfTypeORM = void 0;
|
|
4
|
+
var utils_1 = require("../utils");
|
|
5
|
+
// https://vscode.dev/github.com/typeorm/typeorm/blob/master/src/driver/mysql/MysqlDriver.ts#L750
|
|
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;
|
|
34
|
+
var MicroColumnTypeObject;
|
|
35
|
+
(function (MicroColumnTypeObject) {
|
|
36
|
+
function toSQLQuery(dbType, options, queryType) {
|
|
37
|
+
var columnType = getRawDatabaseColumnTypeOfTypeORM(dbType, options.type);
|
|
38
|
+
var columnLength = options.length || ((columnType === 'varchar'
|
|
39
|
+
|| columnType === 'nvarchar') ? 255 : undefined);
|
|
40
|
+
var str = columnType + (columnLength ? "(".concat(columnLength, ")") : '');
|
|
41
|
+
if (options.nullable)
|
|
42
|
+
str += ' NULL';
|
|
43
|
+
else
|
|
44
|
+
str += ' NOT NULL';
|
|
45
|
+
if (queryType === 'create-table') {
|
|
46
|
+
if (options.primary) {
|
|
47
|
+
str += ' PRIMARY KEY';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return str;
|
|
51
|
+
}
|
|
52
|
+
MicroColumnTypeObject.toSQLQuery = toSQLQuery;
|
|
53
|
+
})(MicroColumnTypeObject = exports.MicroColumnTypeObject || (exports.MicroColumnTypeObject = {}));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DatabaseOptions } from "@klapeks/utils";
|
|
2
|
+
export interface SQLCommandData {
|
|
3
|
+
query: string;
|
|
4
|
+
params?: any[];
|
|
5
|
+
}
|
|
6
|
+
export interface SQLCommandContext {
|
|
7
|
+
}
|
|
8
|
+
export interface ISQLCommandAdapter {
|
|
9
|
+
toMySQL(ctx?: SQLCommandContext): SQLCommandData | string;
|
|
10
|
+
toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string;
|
|
11
|
+
}
|
|
12
|
+
export declare abstract class AbstractSQLCommand implements ISQLCommandAdapter {
|
|
13
|
+
abstract toMySQL(ctx?: SQLCommandContext): SQLCommandData | string;
|
|
14
|
+
abstract toMSSQL(ctx?: SQLCommandContext): SQLCommandData | string;
|
|
15
|
+
}
|
|
16
|
+
export declare const rawSQL: (sqls: {
|
|
17
|
+
defaultQuery: string;
|
|
18
|
+
mysqlQuery?: string;
|
|
19
|
+
mssqlQuery?: string;
|
|
20
|
+
}) => ISQLCommandAdapter;
|
|
21
|
+
export declare function toRawSQL(dbType: DatabaseOptions['type'], query: ISQLCommandAdapter | SQLCommandData | string): SQLCommandData;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toRawSQL = exports.rawSQL = exports.AbstractSQLCommand = void 0;
|
|
4
|
+
var AbstractSQLCommand = /** @class */ (function () {
|
|
5
|
+
function AbstractSQLCommand() {
|
|
6
|
+
}
|
|
7
|
+
return AbstractSQLCommand;
|
|
8
|
+
}());
|
|
9
|
+
exports.AbstractSQLCommand = AbstractSQLCommand;
|
|
10
|
+
var rawSQL = function (sqls) {
|
|
11
|
+
return {
|
|
12
|
+
toMySQL: function () { return sqls.mysqlQuery || sqls.defaultQuery; },
|
|
13
|
+
toMSSQL: function () { return sqls.mssqlQuery || sqls.defaultQuery; },
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.rawSQL = rawSQL;
|
|
17
|
+
function toRawSQL(dbType, query) {
|
|
18
|
+
if (typeof query === 'string')
|
|
19
|
+
return { query: query };
|
|
20
|
+
if (typeof query === 'object' && !('query' in query)) {
|
|
21
|
+
if (dbType === 'mysql')
|
|
22
|
+
query = query.toMySQL({});
|
|
23
|
+
else if (dbType === 'mssql')
|
|
24
|
+
query = query.toMSSQL({});
|
|
25
|
+
else
|
|
26
|
+
throw "Unknown database type: " + dbType;
|
|
27
|
+
}
|
|
28
|
+
return typeof query === 'string' ? { query: query } : query;
|
|
29
|
+
}
|
|
30
|
+
exports.toRawSQL = toRawSQL;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MicroColumnTypeObject } from "../column.type.parser";
|
|
2
|
+
import { ISQLCommandAdapter } from "./abstract.sql.command";
|
|
3
|
+
export declare class SQLAlterCommand {
|
|
4
|
+
private readonly table;
|
|
5
|
+
constructor(table: string);
|
|
6
|
+
renameColumn(old_name: string, new_name: string): ISQLCommandAdapter;
|
|
7
|
+
changeColumnType(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
8
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SQLAlterCommand = void 0;
|
|
4
|
+
var column_type_parser_1 = require("../column.type.parser");
|
|
5
|
+
var SQLAlterCommand = /** @class */ (function () {
|
|
6
|
+
function SQLAlterCommand(table) {
|
|
7
|
+
this.table = table;
|
|
8
|
+
}
|
|
9
|
+
SQLAlterCommand.prototype.renameColumn = function (old_name, new_name) {
|
|
10
|
+
var _this = this;
|
|
11
|
+
return {
|
|
12
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` RENAME COLUMN `").concat(old_name, "` TO `").concat(new_name, "`;"); },
|
|
13
|
+
toMSSQL: function () { return "EXEC sp_rename '".concat(_this.table, ".").concat(old_name, "', '").concat(new_name, "', 'COLUMN';"); }
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
SQLAlterCommand.prototype.changeColumnType = function (column, type) {
|
|
17
|
+
var _this = this;
|
|
18
|
+
return {
|
|
19
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` MODIFY COLUMN `").concat(column, "` ")
|
|
20
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mysql', type, 'alter-column') + ';'; },
|
|
21
|
+
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "] ALTER COLUMN [").concat(column, "] ")
|
|
22
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; }
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
return SQLAlterCommand;
|
|
26
|
+
}());
|
|
27
|
+
exports.SQLAlterCommand = SQLAlterCommand;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SQLEditDataCommands = void 0;
|
|
13
|
+
var SQLEditDataCommands = /** @class */ (function () {
|
|
14
|
+
function SQLEditDataCommands(table) {
|
|
15
|
+
this.table = table;
|
|
16
|
+
}
|
|
17
|
+
SQLEditDataCommands.prototype.upsert = function (data, idKeys) {
|
|
18
|
+
var _this = this;
|
|
19
|
+
if (!data)
|
|
20
|
+
throw "No data param";
|
|
21
|
+
if (!(idKeys === null || idKeys === void 0 ? void 0 : idKeys.length))
|
|
22
|
+
throw "No idKeys param";
|
|
23
|
+
if (Object.keys(data).length <= idKeys.length) {
|
|
24
|
+
throw "Nothing to update";
|
|
25
|
+
}
|
|
26
|
+
var dataKeys = Object.keys(data);
|
|
27
|
+
var toUpdObj = {};
|
|
28
|
+
for (var _i = 0, dataKeys_1 = dataKeys; _i < dataKeys_1.length; _i++) {
|
|
29
|
+
var key = dataKeys_1[_i];
|
|
30
|
+
if (idKeys.includes(key))
|
|
31
|
+
continue;
|
|
32
|
+
toUpdObj[key] = data[key];
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
toMySQL: function () { return ({
|
|
36
|
+
query: "\n INSERT INTO `".concat(_this.table, "` \n (").concat(dataKeys.map(function (a) { return "`".concat(a, "`"); }).join(', '), ")\n VALUES (").concat(dataKeys.map(function () { return '?'; }).join(', '), ")\n ON DUPLICATE KEY UPDATE ").concat(Object.keys(toUpdObj)
|
|
37
|
+
.map(function (key) { return "`".concat(key, "` = ?"); }).join(', '), ";\n "),
|
|
38
|
+
params: __spreadArray(__spreadArray([], Object.values(data), true), Object.values(toUpdObj), true)
|
|
39
|
+
}); },
|
|
40
|
+
toMSSQL: function () { return ({
|
|
41
|
+
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 "[".concat(key, "] = @").concat(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 "),
|
|
42
|
+
params: data
|
|
43
|
+
}); },
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
return SQLEditDataCommands;
|
|
47
|
+
}());
|
|
48
|
+
exports.SQLEditDataCommands = SQLEditDataCommands;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract.sql.command"), exports);
|
|
18
|
+
__exportStar(require("./alter.commands"), exports);
|
|
19
|
+
__exportStar(require("./tables.commands"), exports);
|
|
20
|
+
__exportStar(require("./select.commands"), exports);
|
|
21
|
+
__exportStar(require("./edit-data.commands"), exports);
|
|
22
|
+
__exportStar(require("./time.commands"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SQLSelectCommands = void 0;
|
|
4
|
+
var SQLSelectCommands = /** @class */ (function () {
|
|
5
|
+
function SQLSelectCommands(table) {
|
|
6
|
+
this.table = table;
|
|
7
|
+
}
|
|
8
|
+
SQLSelectCommands.prototype.all = function () {
|
|
9
|
+
var _this = this;
|
|
10
|
+
return {
|
|
11
|
+
toMySQL: function () { return "SELECT * FROM `".concat(_this.table, "`;"); },
|
|
12
|
+
toMSSQL: function () { return "SELECT * FROM [".concat(_this.table, "];"); },
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
return SQLSelectCommands;
|
|
16
|
+
}());
|
|
17
|
+
exports.SQLSelectCommands = SQLSelectCommands;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ISQLCommandAdapter } from "./abstract.sql.command";
|
|
2
|
+
import { MicroColumnTypeObject } from "../column.type.parser";
|
|
3
|
+
export declare class SQLTablesCommands {
|
|
4
|
+
static tableInfo(database: string, table: string): ISQLCommandAdapter;
|
|
5
|
+
static createTable(options: {
|
|
6
|
+
table: string;
|
|
7
|
+
columns: Record<string, MicroColumnTypeObject>;
|
|
8
|
+
}): ISQLCommandAdapter;
|
|
9
|
+
static getTablesSizes(database: string): ISQLCommandAdapter;
|
|
10
|
+
}
|