@klapeks/micro-db-utils 0.0.5 → 0.0.7
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/mysql.connection.d.ts +1 -1
- package/dist/connections/mysql.connection.js +2 -2
- package/dist/connections/nosql/mongo.d.ts +1 -1
- package/dist/connections/nosql/mongo.js +6 -2
- package/dist/migrations/mysql.migrations.d.ts +1 -0
- package/dist/migrations/mysql.migrations.js +26 -19
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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.
|
|
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.
|
|
71
|
+
this._pool = mysql2_1.default.createPool(this.poolOptions);
|
|
72
72
|
this._pool.on('connection', function () {
|
|
73
73
|
logger.log('Database connected');
|
|
74
74
|
});
|
|
@@ -43,7 +43,7 @@ export declare class MongoDBConnection {
|
|
|
43
43
|
readonly set: (model: T, value: number) => Promise<any>;
|
|
44
44
|
};
|
|
45
45
|
static getOptions(): MongoDBConnectOptions;
|
|
46
|
-
static init(databaseName: string): Promise<void>;
|
|
46
|
+
static init(databaseName: string, replicaSet?: string): Promise<void>;
|
|
47
47
|
}
|
|
48
48
|
export declare const MongoGeoPoint: MongooseSchema<Point, import("mongoose").Model<Point, any, any, any, import("mongoose").Document<unknown, any, Point, any> & Point & {
|
|
49
49
|
_id: import("mongoose").Types.ObjectId;
|
|
@@ -139,7 +139,7 @@ var MongoDBConnection = /** @class */ (function () {
|
|
|
139
139
|
database: ""
|
|
140
140
|
};
|
|
141
141
|
};
|
|
142
|
-
MongoDBConnection.init = function (databaseName) {
|
|
142
|
+
MongoDBConnection.init = function (databaseName, replicaSet) {
|
|
143
143
|
return __awaiter(this, void 0, void 0, function () {
|
|
144
144
|
var _a, host, port, password, username, mongoURI;
|
|
145
145
|
return __generator(this, function (_b) {
|
|
@@ -147,8 +147,12 @@ var MongoDBConnection = /** @class */ (function () {
|
|
|
147
147
|
case 0:
|
|
148
148
|
if (_databaseName)
|
|
149
149
|
return [2 /*return*/];
|
|
150
|
+
if (!replicaSet && process.env.MONGO_DATABASE_REPLICA_SET) {
|
|
151
|
+
replicaSet = process.env.MONGO_DATABASE_REPLICA_SET;
|
|
152
|
+
}
|
|
150
153
|
_a = this.getOptions(), host = _a.host, port = _a.port, password = _a.password, username = _a.username;
|
|
151
|
-
mongoURI = 'mongodb://' + host + ':' + port + '/' + databaseName
|
|
154
|
+
mongoURI = 'mongodb://' + host + ':' + port + '/' + databaseName
|
|
155
|
+
+ (replicaSet ? '?replicaSet=' + replicaSet : undefined);
|
|
152
156
|
// logger.log(mongoURI);
|
|
153
157
|
if (!mongooseModule)
|
|
154
158
|
throw new Error("No mongoose module");
|
|
@@ -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,
|
|
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 (
|
|
111
|
-
switch (
|
|
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
|
-
|
|
118
|
-
if (
|
|
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 ? (
|
|
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
|
-
|
|
125
|
-
|
|
131
|
+
_e.sent();
|
|
132
|
+
_e.label = 3;
|
|
126
133
|
case 3: return [2 /*return*/];
|
|
127
134
|
case 4:
|
|
128
|
-
lastRealMigration = (
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
178
|
-
|
|
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
|
-
|
|
189
|
-
|
|
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
|
-
|
|
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
|
-
|
|
197
|
-
|
|
203
|
+
_e.sent();
|
|
204
|
+
_e.label = 15;
|
|
198
205
|
case 15:
|
|
199
206
|
_i++;
|
|
200
207
|
return [3 /*break*/, 5];
|