@klapeks/micro-db-utils 0.0.4 → 0.0.6

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.
@@ -2,7 +2,7 @@ import mysql2 from 'mysql2';
2
2
  import { DatabaseOptions } from '@klapeks/utils';
3
3
  export declare class RawMySQLConnection {
4
4
  private _pool;
5
- private _poolOptions;
5
+ readonly poolOptions: mysql2.PoolOptions;
6
6
  constructor(options: DatabaseOptions & {
7
7
  type: "mysql";
8
8
  });
@@ -45,7 +45,7 @@ var utils_1 = require("@klapeks/utils");
45
45
  var logger = new utils_1.Logger('MySQL');
46
46
  var RawMySQLConnection = /** @class */ (function () {
47
47
  function RawMySQLConnection(options) {
48
- this._poolOptions = {
48
+ this.poolOptions = {
49
49
  user: options.username,
50
50
  password: options.password,
51
51
  host: options.host,
@@ -68,7 +68,7 @@ var RawMySQLConnection = /** @class */ (function () {
68
68
  if (this._pool)
69
69
  return [2 /*return*/, this._pool];
70
70
  // if (this.options.type != 'mysql') throw "Not a mysql";
71
- this._pool = mysql2_1.default.createPool(this._poolOptions);
71
+ this._pool = mysql2_1.default.createPool(this.poolOptions);
72
72
  this._pool.on('connection', function () {
73
73
  logger.log('Database connected');
74
74
  });
@@ -2,5 +2,6 @@ import { DataSource, EntitySchema } from "typeorm";
2
2
  export declare class MySQLMigrations {
3
3
  static get migrationTableName(): string;
4
4
  static addMigration(entity: EntitySchema, date: Date, sql: string | string[]): void;
5
+ private static checkIsCreatesTable;
5
6
  static runMigrations(dataSource: DataSource): Promise<void>;
6
7
  }
@@ -71,6 +71,13 @@ var MySQLMigrations = /** @class */ (function () {
71
71
  sql: sql
72
72
  });
73
73
  };
74
+ MySQLMigrations.checkIsCreatesTable = function (migrations) {
75
+ if (!migrations)
76
+ return false;
77
+ if (Array.isArray(migrations))
78
+ return this.checkIsCreatesTable(migrations[0]);
79
+ return migrations.trim().toLowerCase().startsWith('create table');
80
+ };
74
81
  MySQLMigrations.runMigrations = function (dataSource) {
75
82
  var _a;
76
83
  return __awaiter(this, void 0, void 0, function () {
@@ -104,29 +111,54 @@ var MySQLMigrations = /** @class */ (function () {
104
111
  migrationRecords_1 = _b.sent();
105
112
  runnedMigrationsAmount_1 = 0;
106
113
  runTableMigration = function (table) { return __awaiter(_this, void 0, void 0, function () {
107
- var todoMigrations, tableInfo, lastMigrationDate, lastRealMigration, _i, todoMigrations_1, migration, migrationName, sqls, _a, sqls_1, sql, sql;
108
- var _b, _c;
109
- return __generator(this, function (_d) {
110
- switch (_d.label) {
114
+ var todoMigrations, isTableExistsInfo, lastMigrationDate, lastRealMigration, _local_runSQL, _i, todoMigrations_1, migration, migrationName, sqls, _a, sqls_1, sql, sql;
115
+ var _this = this;
116
+ var _b, _c, _d;
117
+ return __generator(this, function (_e) {
118
+ switch (_e.label) {
111
119
  case 0:
112
120
  todoMigrations = _migrations.sort(function (c1, c2) { return c1.date.getTime() - c2.date.getTime(); });
113
121
  todoMigrations = __spreadArray([], todoMigrations, true).filter(function (m) { return m.table == table; });
114
122
  return [4 /*yield*/, getTable_1(table)];
115
123
  case 1:
116
- tableInfo = _d.sent();
117
- if (!!tableInfo) return [3 /*break*/, 4];
124
+ isTableExistsInfo = _e.sent();
125
+ if (!(!isTableExistsInfo && !this.checkIsCreatesTable((_b = todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations[0]) === null || _b === void 0 ? void 0 : _b.sql))) return [3 /*break*/, 4];
118
126
  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 ? (_b = todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations[todoMigrations.length - 1]) === null || _b === void 0 ? void 0 : _b.date : undefined;
127
+ 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
128
  if (!((todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length) && lastMigrationDate)) return [3 /*break*/, 3];
121
129
  return [4 /*yield*/, mysqlInstance_1.runSQL("\n INSERT INTO `".concat(this.migrationTableName, "` (`table`, `lastMigrationTime`)\n VALUES ('").concat(table, "', '").concat((0, iso_date_time_1.toISODate)(lastMigrationDate), "')\n ON DUPLICATE KEY UPDATE `lastMigrationTime` = '").concat((0, iso_date_time_1.toISODate)(lastMigrationDate), "'\n "))];
122
130
  case 2:
123
- _d.sent();
124
- _d.label = 3;
131
+ _e.sent();
132
+ _e.label = 3;
125
133
  case 3: return [2 /*return*/];
126
134
  case 4:
127
- lastRealMigration = (_c = migrationRecords_1 === null || migrationRecords_1 === void 0 ? void 0 : migrationRecords_1.find(function (r) { return r.table == table; })) === null || _c === void 0 ? void 0 : _c.lastMigrationTime;
135
+ lastRealMigration = (_d = migrationRecords_1 === null || migrationRecords_1 === void 0 ? void 0 : migrationRecords_1.find(function (r) { return r.table == table; })) === null || _d === void 0 ? void 0 : _d.lastMigrationTime;
136
+ _local_runSQL = function (sql) { return __awaiter(_this, void 0, void 0, function () {
137
+ return __generator(this, function (_a) {
138
+ switch (_a.label) {
139
+ case 0:
140
+ if (!sql.toLowerCase().startsWith("alter table")) return [3 /*break*/, 2];
141
+ // log error if error
142
+ return [4 /*yield*/, mysqlInstance_1.runSQL(sql).catch(function (err) {
143
+ logger.error("Error while alter table:", err);
144
+ })];
145
+ case 1:
146
+ // log error if error
147
+ _a.sent();
148
+ return [3 /*break*/, 4];
149
+ case 2:
150
+ // throw error if error
151
+ return [4 /*yield*/, mysqlInstance_1.runSQL(sql)];
152
+ case 3:
153
+ // throw error if error
154
+ _a.sent();
155
+ _a.label = 4;
156
+ case 4: return [2 /*return*/];
157
+ }
158
+ });
159
+ }); };
128
160
  _i = 0, todoMigrations_1 = todoMigrations;
129
- _d.label = 5;
161
+ _e.label = 5;
130
162
  case 5:
131
163
  if (!(_i < todoMigrations_1.length)) return [3 /*break*/, 16];
132
164
  migration = todoMigrations_1[_i];
@@ -143,14 +175,14 @@ var MySQLMigrations = /** @class */ (function () {
143
175
  });
144
176
  logger.log(runnedMigrationsAmount_1, "| Migrations will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sqls);
145
177
  _a = 0, sqls_1 = sqls;
146
- _d.label = 6;
178
+ _e.label = 6;
147
179
  case 6:
148
180
  if (!(_a < sqls_1.length)) return [3 /*break*/, 9];
149
181
  sql = sqls_1[_a];
150
- return [4 /*yield*/, mysqlInstance_1.runSQL(sql)];
182
+ return [4 /*yield*/, _local_runSQL(sql)];
151
183
  case 7:
152
- _d.sent();
153
- _d.label = 8;
184
+ _e.sent();
185
+ _e.label = 8;
154
186
  case 8:
155
187
  _a++;
156
188
  return [3 /*break*/, 6];
@@ -158,18 +190,18 @@ var MySQLMigrations = /** @class */ (function () {
158
190
  case 10:
159
191
  sql = utils_1.utils.replaceAll(migration.sql, "%{table_name}", table);
160
192
  logger.log(runnedMigrationsAmount_1, "| Migration will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sql.trim());
161
- return [4 /*yield*/, mysqlInstance_1.runSQL(sql)];
193
+ return [4 /*yield*/, _local_runSQL(sql)];
162
194
  case 11:
163
- _d.sent();
164
- _d.label = 12;
195
+ _e.sent();
196
+ _e.label = 12;
165
197
  case 12: return [4 /*yield*/, mysqlInstance_1.runSQL("\n INSERT INTO `".concat(this.migrationTableName, "` (`table`, `lastMigrationTime`)\n VALUES ('").concat(table, "', '").concat((0, iso_date_time_1.toISODate)(migration.date), "')\n ON DUPLICATE KEY UPDATE `lastMigrationTime` = '").concat((0, iso_date_time_1.toISODate)(migration.date), "'\n "))];
166
198
  case 13:
167
- _d.sent();
199
+ _e.sent();
168
200
  logger.log(runnedMigrationsAmount_1, "| Migration", migrationName, "successfully done");
169
201
  return [4 /*yield*/, utils_1.utils.sleep(100)];
170
202
  case 14:
171
- _d.sent();
172
- _d.label = 15;
203
+ _e.sent();
204
+ _e.label = 15;
173
205
  case 15:
174
206
  _i++;
175
207
  return [3 /*break*/, 5];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@klapeks/micro-db-utils",
3
3
  "description": "klapeks utils for db and other storages",
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "type": "commonjs",
6
6
  "author": "Klapeks",
7
7
  "license": "ISC",