@klapeks/micro-db-utils 0.0.5 → 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,28 +111,28 @@ 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, _local_runSQL, _i, todoMigrations_1, migration, migrationName, sqls, _a, sqls_1, sql, sql;
114
+ var todoMigrations, isTableExistsInfo, lastMigrationDate, lastRealMigration, _local_runSQL, _i, todoMigrations_1, migration, migrationName, sqls, _a, sqls_1, sql, sql;
108
115
  var _this = this;
109
- var _b, _c;
110
- return __generator(this, function (_d) {
111
- switch (_d.label) {
116
+ var _b, _c, _d;
117
+ return __generator(this, function (_e) {
118
+ switch (_e.label) {
112
119
  case 0:
113
120
  todoMigrations = _migrations.sort(function (c1, c2) { return c1.date.getTime() - c2.date.getTime(); });
114
121
  todoMigrations = __spreadArray([], todoMigrations, true).filter(function (m) { return m.table == table; });
115
122
  return [4 /*yield*/, getTable_1(table)];
116
123
  case 1:
117
- tableInfo = _d.sent();
118
- 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];
119
126
  logger.log("Table ".concat(table, " not found. Migrations ").concat(todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length, " will be skipped"));
120
- 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;
121
128
  if (!((todoMigrations === null || todoMigrations === void 0 ? void 0 : todoMigrations.length) && lastMigrationDate)) return [3 /*break*/, 3];
122
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 "))];
123
130
  case 2:
124
- _d.sent();
125
- _d.label = 3;
131
+ _e.sent();
132
+ _e.label = 3;
126
133
  case 3: return [2 /*return*/];
127
134
  case 4:
128
- 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;
129
136
  _local_runSQL = function (sql) { return __awaiter(_this, void 0, void 0, function () {
130
137
  return __generator(this, function (_a) {
131
138
  switch (_a.label) {
@@ -151,7 +158,7 @@ var MySQLMigrations = /** @class */ (function () {
151
158
  });
152
159
  }); };
153
160
  _i = 0, todoMigrations_1 = todoMigrations;
154
- _d.label = 5;
161
+ _e.label = 5;
155
162
  case 5:
156
163
  if (!(_i < todoMigrations_1.length)) return [3 /*break*/, 16];
157
164
  migration = todoMigrations_1[_i];
@@ -168,14 +175,14 @@ var MySQLMigrations = /** @class */ (function () {
168
175
  });
169
176
  logger.log(runnedMigrationsAmount_1, "| Migrations will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sqls);
170
177
  _a = 0, sqls_1 = sqls;
171
- _d.label = 6;
178
+ _e.label = 6;
172
179
  case 6:
173
180
  if (!(_a < sqls_1.length)) return [3 /*break*/, 9];
174
181
  sql = sqls_1[_a];
175
182
  return [4 /*yield*/, _local_runSQL(sql)];
176
183
  case 7:
177
- _d.sent();
178
- _d.label = 8;
184
+ _e.sent();
185
+ _e.label = 8;
179
186
  case 8:
180
187
  _a++;
181
188
  return [3 /*break*/, 6];
@@ -185,16 +192,16 @@ var MySQLMigrations = /** @class */ (function () {
185
192
  logger.log(runnedMigrationsAmount_1, "| Migration will be runned:", migrationName, '|\n' + utils_1.terminalColors.cyan, sql.trim());
186
193
  return [4 /*yield*/, _local_runSQL(sql)];
187
194
  case 11:
188
- _d.sent();
189
- _d.label = 12;
195
+ _e.sent();
196
+ _e.label = 12;
190
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 "))];
191
198
  case 13:
192
- _d.sent();
199
+ _e.sent();
193
200
  logger.log(runnedMigrationsAmount_1, "| Migration", migrationName, "successfully done");
194
201
  return [4 /*yield*/, utils_1.utils.sleep(100)];
195
202
  case 14:
196
- _d.sent();
197
- _d.label = 15;
203
+ _e.sent();
204
+ _e.label = 15;
198
205
  case 15:
199
206
  _i++;
200
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.5",
4
+ "version": "0.0.6",
5
5
  "type": "commonjs",
6
6
  "author": "Klapeks",
7
7
  "license": "ISC",