@klapeks/micro-db-utils 0.0.12 → 0.0.14
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 +312 -0
- package/dist/selectors/raw-mysql.select.handler.d.ts +2 -2
- package/dist/sql/column.type.parser.d.ts +14 -0
- package/dist/sql/column.type.parser.js +92 -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 +10 -0
- package/dist/sql/commands/alter.commands.js +39 -0
- package/dist/sql/commands/edit-data.commands.d.ts +7 -0
- package/dist/sql/commands/edit-data.commands.js +80 -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 +8 -0
- package/dist/sql/commands/time.commands.js +31 -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 +4 -0
- package/dist/sql/index.js +20 -0
- package/dist/sql/raw.where.utils.d.ts +2 -0
- package/dist/sql/raw.where.utils.js +35 -0
- package/dist/utils/columns.types.d.ts +12 -97
- package/dist/utils/where.utils.d.ts +1 -0
- package/dist/utils/where.utils.js +6 -6
- 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,312 @@
|
|
|
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
|
+
if (!!_existsMigrationTable) return [3 /*break*/, 6];
|
|
240
|
+
// creating migration table if not exists
|
|
241
|
+
logger.log("Creating migration table..");
|
|
242
|
+
return [4 /*yield*/, sqlInstance.runSQL_One(tables_commands_1.SQLTablesCommands.createTable({
|
|
243
|
+
table: SuperMigrations.migrationTableName,
|
|
244
|
+
columns: {
|
|
245
|
+
table: { type: String, length: 255, primary: true },
|
|
246
|
+
lastMigrationTime: { type: String, length: 128 }
|
|
247
|
+
}
|
|
248
|
+
}))];
|
|
249
|
+
case 5:
|
|
250
|
+
_d.sent();
|
|
251
|
+
_d.label = 6;
|
|
252
|
+
case 6:
|
|
253
|
+
_c = utils_1.mapOf;
|
|
254
|
+
return [4 /*yield*/, sqlInstance.runSQL(micro_sql_1.MicroSQL.select(SuperMigrations.migrationTableName).all())];
|
|
255
|
+
case 7:
|
|
256
|
+
migrationRecords = _c.apply(void 0, [_d.sent(), 'table']);
|
|
257
|
+
runnedMigrationsAmount_1 = 0;
|
|
258
|
+
tablesToMigrate = Object.values(dataSource.options.entities || []).map(function (m) {
|
|
259
|
+
if (m instanceof typeorm_1.EntitySchema)
|
|
260
|
+
return m.options.name;
|
|
261
|
+
return undefined;
|
|
262
|
+
}).filter(Boolean);
|
|
263
|
+
_i = 0, tablesToMigrate_1 = tablesToMigrate;
|
|
264
|
+
_d.label = 8;
|
|
265
|
+
case 8:
|
|
266
|
+
if (!(_i < tablesToMigrate_1.length)) return [3 /*break*/, 11];
|
|
267
|
+
table = tablesToMigrate_1[_i];
|
|
268
|
+
return [4 /*yield*/, SuperMigrations.runMigrationForTable(sqlInstance, table, (_a = migrationRecords.get(table)) === null || _a === void 0 ? void 0 : _a.lastMigrationTime, function () { return ++runnedMigrationsAmount_1; })];
|
|
269
|
+
case 9:
|
|
270
|
+
_d.sent();
|
|
271
|
+
_d.label = 10;
|
|
272
|
+
case 10:
|
|
273
|
+
_i++;
|
|
274
|
+
return [3 /*break*/, 8];
|
|
275
|
+
case 11:
|
|
276
|
+
if (runnedMigrationsAmount_1) {
|
|
277
|
+
logger.log(utils_1.terminalColors.green + "All migrations completed:"
|
|
278
|
+
+ utils_1.terminalColors.reset, runnedMigrationsAmount_1);
|
|
279
|
+
}
|
|
280
|
+
return [4 /*yield*/, sqlInstance.destroyConnection().catch(function (err) {
|
|
281
|
+
logger.error("Error while closing pool:", err);
|
|
282
|
+
})];
|
|
283
|
+
case 12:
|
|
284
|
+
_d.sent();
|
|
285
|
+
return [3 /*break*/, 15];
|
|
286
|
+
case 13:
|
|
287
|
+
err_1 = _d.sent();
|
|
288
|
+
return [4 /*yield*/, sqlInstance.destroyConnection().catch(function (err) {
|
|
289
|
+
logger.error("Error while closing pool:", err);
|
|
290
|
+
})];
|
|
291
|
+
case 14:
|
|
292
|
+
_d.sent();
|
|
293
|
+
throw err_1;
|
|
294
|
+
case 15: return [3 /*break*/, 17];
|
|
295
|
+
case 16:
|
|
296
|
+
err_2 = _d.sent();
|
|
297
|
+
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"))
|
|
298
|
+
return [2 /*return*/];
|
|
299
|
+
// logger.error("err of musql:", err.sqlMessage);
|
|
300
|
+
throw err_2;
|
|
301
|
+
case 17: return [2 /*return*/];
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
SuperMigrations._migrations = [];
|
|
307
|
+
return SuperMigrations;
|
|
308
|
+
}());
|
|
309
|
+
exports.SuperMigrations = SuperMigrations;
|
|
310
|
+
SuperMigrations.addMigration = SuperMigrations.addMigration.bind(SuperMigrations);
|
|
311
|
+
SuperMigrations.addRawMigration = SuperMigrations.addRawMigration.bind(SuperMigrations);
|
|
312
|
+
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,14 @@
|
|
|
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
|
+
default?: any;
|
|
10
|
+
generated?: true | "increment";
|
|
11
|
+
}
|
|
12
|
+
export declare namespace MicroColumnTypeObject {
|
|
13
|
+
function toSQLQuery(dbType: DatabaseOptions['type'], options: MicroColumnTypeObject, queryType?: "create-table" | "alter-column"): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
// nullable
|
|
42
|
+
if (options.nullable)
|
|
43
|
+
str += ' NULL';
|
|
44
|
+
else
|
|
45
|
+
str += ' NOT NULL';
|
|
46
|
+
// auto increment
|
|
47
|
+
if (options.generated) {
|
|
48
|
+
if (dbType === 'mssql')
|
|
49
|
+
str += ' IDENTITY';
|
|
50
|
+
else if (dbType === 'mysql')
|
|
51
|
+
str += ' AUTO_INCREMENT';
|
|
52
|
+
else
|
|
53
|
+
throw "options.generated is not supported yet for " + dbType;
|
|
54
|
+
}
|
|
55
|
+
// default
|
|
56
|
+
if (options.default !== undefined) {
|
|
57
|
+
var value = options.default;
|
|
58
|
+
if (value === null || value === 'null' || value === 'NULL') {
|
|
59
|
+
value = "NULL";
|
|
60
|
+
}
|
|
61
|
+
else if (typeof value === 'number') {
|
|
62
|
+
value = value.toString(); // :)
|
|
63
|
+
}
|
|
64
|
+
else if (typeof value === 'boolean') {
|
|
65
|
+
if (dbType === 'mssql') {
|
|
66
|
+
value = value ? '1' : '0';
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
value = value ? "true" : "false";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (typeof value === 'string') {
|
|
73
|
+
value = "'".concat(value.replace(/'/g, "''"), "'");
|
|
74
|
+
}
|
|
75
|
+
else if (typeof value === 'object') {
|
|
76
|
+
value = "'".concat(JSON.stringify(value), "'");
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
value = "'".concat(value, "'");
|
|
80
|
+
}
|
|
81
|
+
str += " DEFAULT ".concat(value);
|
|
82
|
+
}
|
|
83
|
+
// primary key
|
|
84
|
+
if (queryType === 'create-table') {
|
|
85
|
+
if (options.primary) {
|
|
86
|
+
str += ' PRIMARY KEY';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return str;
|
|
90
|
+
}
|
|
91
|
+
MicroColumnTypeObject.toSQLQuery = toSQLQuery;
|
|
92
|
+
})(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,10 @@
|
|
|
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
|
+
array(cb: (t: SQLAlterCommand) => ISQLCommandAdapter[]): ISQLCommandAdapter[];
|
|
7
|
+
renameColumn(old_name: string, new_name: string): ISQLCommandAdapter;
|
|
8
|
+
changeColumnType(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
9
|
+
addColumn(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
|
|
10
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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.array = function (cb) {
|
|
10
|
+
return cb(this);
|
|
11
|
+
};
|
|
12
|
+
SQLAlterCommand.prototype.renameColumn = function (old_name, new_name) {
|
|
13
|
+
var _this = this;
|
|
14
|
+
return {
|
|
15
|
+
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';"); }
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
SQLAlterCommand.prototype.changeColumnType = function (column, type) {
|
|
20
|
+
var _this = this;
|
|
21
|
+
return {
|
|
22
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` MODIFY COLUMN `").concat(column, "` ")
|
|
23
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mysql', type, 'alter-column') + ';'; },
|
|
24
|
+
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "] ALTER COLUMN [").concat(column, "] ")
|
|
25
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; }
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
SQLAlterCommand.prototype.addColumn = function (column, type) {
|
|
29
|
+
var _this = this;
|
|
30
|
+
return {
|
|
31
|
+
toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` ADD COLUMN `").concat(column, "` ")
|
|
32
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mysql', type, 'alter-column') + ';'; },
|
|
33
|
+
toMSSQL: function () { return "ALTER TABLE [".concat(_this.table, "] ADD [").concat(column, "] ")
|
|
34
|
+
+ column_type_parser_1.MicroColumnTypeObject.toSQLQuery('mssql', type, 'alter-column') + ';'; }
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
return SQLAlterCommand;
|
|
38
|
+
}());
|
|
39
|
+
exports.SQLAlterCommand = SQLAlterCommand;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ISQLCommandAdapter } from "./abstract.sql.command";
|
|
2
|
+
export declare class SQLEditDataCommands {
|
|
3
|
+
private readonly table;
|
|
4
|
+
constructor(table: string);
|
|
5
|
+
update(data: any, where: any): ISQLCommandAdapter;
|
|
6
|
+
upsert(data: any, idKeys: string[]): ISQLCommandAdapter;
|
|
7
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
+
if (ar || !(i in from)) {
|
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
+
ar[i] = from[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.SQLEditDataCommands = void 0;
|
|
24
|
+
var raw_where_utils_1 = require("../raw.where.utils");
|
|
25
|
+
var SQLEditDataCommands = /** @class */ (function () {
|
|
26
|
+
function SQLEditDataCommands(table) {
|
|
27
|
+
this.table = table;
|
|
28
|
+
}
|
|
29
|
+
SQLEditDataCommands.prototype.update = function (data, where) {
|
|
30
|
+
var _this = this;
|
|
31
|
+
return {
|
|
32
|
+
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 "),
|
|
34
|
+
params: __spreadArray(__spreadArray([], Object.values(data), true), Object.values(where), true)
|
|
35
|
+
}); },
|
|
36
|
+
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 "),
|
|
38
|
+
params: (function () {
|
|
39
|
+
var params = __assign({}, data);
|
|
40
|
+
for (var _i = 0, _a = Object.keys(where); _i < _a.length; _i++) {
|
|
41
|
+
var key = _a[_i];
|
|
42
|
+
params['where_' + key] = where[key];
|
|
43
|
+
}
|
|
44
|
+
return params;
|
|
45
|
+
})()
|
|
46
|
+
}); },
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
SQLEditDataCommands.prototype.upsert = function (data, idKeys) {
|
|
50
|
+
var _this = this;
|
|
51
|
+
if (!data)
|
|
52
|
+
throw "No data param";
|
|
53
|
+
if (!(idKeys === null || idKeys === void 0 ? void 0 : idKeys.length))
|
|
54
|
+
throw "No idKeys param";
|
|
55
|
+
if (Object.keys(data).length <= idKeys.length) {
|
|
56
|
+
throw "Nothing to update";
|
|
57
|
+
}
|
|
58
|
+
var dataKeys = Object.keys(data);
|
|
59
|
+
var toUpdObj = {};
|
|
60
|
+
for (var _i = 0, dataKeys_1 = dataKeys; _i < dataKeys_1.length; _i++) {
|
|
61
|
+
var key = dataKeys_1[_i];
|
|
62
|
+
if (idKeys.includes(key))
|
|
63
|
+
continue;
|
|
64
|
+
toUpdObj[key] = data[key];
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
toMySQL: function () { return ({
|
|
68
|
+
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)
|
|
69
|
+
.map(function (key) { return "`".concat(key, "` = ?"); }).join(', '), ";\n "),
|
|
70
|
+
params: __spreadArray(__spreadArray([], Object.values(data), true), Object.values(toUpdObj), true)
|
|
71
|
+
}); },
|
|
72
|
+
toMSSQL: function () { return ({
|
|
73
|
+
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
|
+
params: data
|
|
75
|
+
}); },
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
return SQLEditDataCommands;
|
|
79
|
+
}());
|
|
80
|
+
exports.SQLEditDataCommands = SQLEditDataCommands;
|