@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,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SQLTablesCommands = void 0;
|
|
4
|
+
var column_type_parser_1 = require("../column.type.parser");
|
|
5
|
+
var SQLTablesCommands = /** @class */ (function () {
|
|
6
|
+
function SQLTablesCommands() {
|
|
7
|
+
}
|
|
8
|
+
SQLTablesCommands.tableInfo = function (database, table) {
|
|
9
|
+
return {
|
|
10
|
+
toMySQL: function () { return [
|
|
11
|
+
"SELECT * FROM information_schema.tables",
|
|
12
|
+
"WHERE table_schema = '".concat(database, "'"),
|
|
13
|
+
"AND table_name = '".concat(table, "'"),
|
|
14
|
+
"LIMIT 1;",
|
|
15
|
+
].join(' '); },
|
|
16
|
+
toMSSQL: function () { return [
|
|
17
|
+
"SELECT TOP 1 * FROM INFORMATION_SCHEMA.TABLES",
|
|
18
|
+
"WHERE TABLE_CATALOG = '".concat(database, "'"),
|
|
19
|
+
"AND TABLE_SCHEMA = 'dbo'",
|
|
20
|
+
"AND TABLE_NAME = '".concat(table, "'"),
|
|
21
|
+
].join(' '); },
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
SQLTablesCommands.createTable = function (options) {
|
|
25
|
+
var _createQuery = function (dbtype) {
|
|
26
|
+
var str = "CREATE TABLE ".concat(options.table, " (");
|
|
27
|
+
var _index = 0;
|
|
28
|
+
for (var _i = 0, _a = Object.entries(options.columns); _i < _a.length; _i++) {
|
|
29
|
+
var _b = _a[_i], columnName = _b[0], columnType = _b[1];
|
|
30
|
+
if (_index)
|
|
31
|
+
str += ', ';
|
|
32
|
+
_index += 1;
|
|
33
|
+
if (dbtype === 'mysql')
|
|
34
|
+
str += "`".concat(columnName, "` ");
|
|
35
|
+
else if (dbtype === 'mssql')
|
|
36
|
+
str += "[".concat(columnName, "] ");
|
|
37
|
+
else if (dbtype === 'postgres')
|
|
38
|
+
str += "\"".concat(columnName, "\" ");
|
|
39
|
+
else
|
|
40
|
+
str += "".concat(columnName, " ");
|
|
41
|
+
str += column_type_parser_1.MicroColumnTypeObject.toSQLQuery(dbtype, columnType, 'create-table');
|
|
42
|
+
}
|
|
43
|
+
str += ');';
|
|
44
|
+
return str;
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
toMySQL: function () { return _createQuery('mysql'); },
|
|
48
|
+
toMSSQL: function () { return _createQuery('mssql'); },
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
SQLTablesCommands.getTablesSizes = function (database) {
|
|
52
|
+
return {
|
|
53
|
+
toMySQL: function () { return "\n SELECT \n TABLE_NAME as 'table_name', \n (DATA_LENGTH / 1024) as 'data_kb', \n (INDEX_LENGTH / 1024) as 'index_kb'\n FROM information_schema.TABLES\n WHERE table_schema = '".concat(database, "'\n ORDER BY (data_kb + index_kb) DESC;\n "); },
|
|
54
|
+
toMSSQL: function () { return "\n SELECT\n t.name AS table_name,\n SUM(ps.in_row_data_page_count + ps.lob_used_page_count\n + ps.row_overflow_used_page_count\n ) * 8 AS data_kb,\n SUM(ps.used_page_count - ps.in_row_data_page_count\n - ps.lob_used_page_count - ps.row_overflow_used_page_count\n ) * 8 AS index_kb\n FROM sys.tables t\n JOIN sys.dm_db_partition_stats ps\n ON t.object_id = ps.object_id\n GROUP BY t.name\n ORDER BY (data_kb + index_kb) DESC;\n "; },
|
|
55
|
+
// postgress
|
|
56
|
+
// SELECT
|
|
57
|
+
// relname AS table_name,
|
|
58
|
+
// pg_relation_size(relid) / 1024 AS data_kb,
|
|
59
|
+
// pg_indexes_size(relid) / 1024 AS index_kb
|
|
60
|
+
// FROM pg_catalog.pg_statio_user_tables
|
|
61
|
+
// ORDER BY pg_total_relation_size(relid) DESC;
|
|
62
|
+
// sqlite
|
|
63
|
+
// 1. SELECT
|
|
64
|
+
// name AS table_name
|
|
65
|
+
// FROM sqlite_master
|
|
66
|
+
// WHERE type = 'table';
|
|
67
|
+
// 2. SELECT
|
|
68
|
+
// SUM(pgsize) / 1024.0 AS data_kb
|
|
69
|
+
// FROM dbstat
|
|
70
|
+
// WHERE name = ?;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
return SQLTablesCommands;
|
|
74
|
+
}());
|
|
75
|
+
exports.SQLTablesCommands = SQLTablesCommands;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SQLTimeCommandsExpressions = void 0;
|
|
4
|
+
var SQLTimeCommandsExpressions = /** @class */ (function () {
|
|
5
|
+
function SQLTimeCommandsExpressions(dbtype) {
|
|
6
|
+
this.dbtype = dbtype;
|
|
7
|
+
}
|
|
8
|
+
SQLTimeCommandsExpressions.prototype.time = function (expr) {
|
|
9
|
+
if (this.dbtype === 'mssql'
|
|
10
|
+
|| this.dbtype == 'postgres') {
|
|
11
|
+
return "CAST(".concat(expr, " AS TIME(0))");
|
|
12
|
+
}
|
|
13
|
+
return "time(".concat(expr, ")");
|
|
14
|
+
};
|
|
15
|
+
SQLTimeCommandsExpressions.prototype.date = function (expr) {
|
|
16
|
+
if (this.dbtype === 'mssql'
|
|
17
|
+
|| this.dbtype == 'postgres') {
|
|
18
|
+
return "CAST(".concat(expr, " AS DATE)");
|
|
19
|
+
}
|
|
20
|
+
return "time(".concat(expr, ")");
|
|
21
|
+
};
|
|
22
|
+
return SQLTimeCommandsExpressions;
|
|
23
|
+
}());
|
|
24
|
+
exports.SQLTimeCommandsExpressions = SQLTimeCommandsExpressions;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DatabaseOptions } from "@klapeks/utils";
|
|
2
|
+
import { AbstractSQLCommand, ISQLCommandAdapter, SQLCommandData } from "../commands";
|
|
3
|
+
export declare abstract class AbstractSQLConnection {
|
|
4
|
+
readonly rawOptions: DatabaseOptions;
|
|
5
|
+
readonly abstractCommandFunctionName: keyof ISQLCommandAdapter;
|
|
6
|
+
protected constructor(rawOptions: DatabaseOptions, abstractCommandFunctionName: keyof ISQLCommandAdapter);
|
|
7
|
+
get databaseName(): string;
|
|
8
|
+
abstract initConnection(): Promise<void>;
|
|
9
|
+
abstract destroyConnection(): Promise<void>;
|
|
10
|
+
protected abstract sendSQL<T = any>(query: string, params?: any[]): Promise<T[]>;
|
|
11
|
+
toRawSQL(sql: ISQLCommandAdapter | SQLCommandData | string): SQLCommandData;
|
|
12
|
+
runSQL<T = any>(query: SQLCommandData): Promise<T[]>;
|
|
13
|
+
runSQL<T = any>(query: ISQLCommandAdapter): Promise<T[]>;
|
|
14
|
+
runSQL<T = any>(query: AbstractSQLCommand): Promise<T[]>;
|
|
15
|
+
runSQL<T = any>(query: string, params?: any): Promise<T[]>;
|
|
16
|
+
runSQL_One<T = any>(query: SQLCommandData): Promise<T | null>;
|
|
17
|
+
runSQL_One<T = any>(query: ISQLCommandAdapter): Promise<T | null>;
|
|
18
|
+
runSQL_One<T = any>(query: AbstractSQLCommand): Promise<T | null>;
|
|
19
|
+
runSQL_One<T = any>(query: string, params?: any): Promise<T | null>;
|
|
20
|
+
get isDebugLoggerEnabled(): boolean;
|
|
21
|
+
protected debugQuery(query: string, params: any): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.AbstractSQLConnection = void 0;
|
|
40
|
+
var utils_1 = require("@klapeks/utils");
|
|
41
|
+
var commands_1 = require("../commands");
|
|
42
|
+
var AbstractSQLConnection = /** @class */ (function () {
|
|
43
|
+
function AbstractSQLConnection(rawOptions, abstractCommandFunctionName) {
|
|
44
|
+
this.rawOptions = rawOptions;
|
|
45
|
+
this.abstractCommandFunctionName = abstractCommandFunctionName;
|
|
46
|
+
}
|
|
47
|
+
Object.defineProperty(AbstractSQLConnection.prototype, "databaseName", {
|
|
48
|
+
get: function () {
|
|
49
|
+
return this.rawOptions.database;
|
|
50
|
+
},
|
|
51
|
+
enumerable: false,
|
|
52
|
+
configurable: true
|
|
53
|
+
});
|
|
54
|
+
AbstractSQLConnection.prototype.toRawSQL = function (sql) {
|
|
55
|
+
return (0, commands_1.toRawSQL)(this.rawOptions.type, sql);
|
|
56
|
+
};
|
|
57
|
+
AbstractSQLConnection.prototype.runSQL = function (query, params) {
|
|
58
|
+
var _a;
|
|
59
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
+
var abstrKey;
|
|
61
|
+
return __generator(this, function (_b) {
|
|
62
|
+
switch (_b.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
if (!query)
|
|
65
|
+
throw "Invalid arg";
|
|
66
|
+
if (typeof query === 'string') {
|
|
67
|
+
return [2 /*return*/, this.sendSQL(query, params)];
|
|
68
|
+
}
|
|
69
|
+
if (typeof query !== 'object')
|
|
70
|
+
throw "Invalid arg";
|
|
71
|
+
abstrKey = this.abstractCommandFunctionName;
|
|
72
|
+
if (!(abstrKey in query)) return [3 /*break*/, 2];
|
|
73
|
+
return [4 /*yield*/, ((_a = query === null || query === void 0 ? void 0 : query[abstrKey]) === null || _a === void 0 ? void 0 : _a.call(query, {
|
|
74
|
+
database: this.databaseName
|
|
75
|
+
}))];
|
|
76
|
+
case 1:
|
|
77
|
+
query = _b.sent();
|
|
78
|
+
_b.label = 2;
|
|
79
|
+
case 2:
|
|
80
|
+
if (!query)
|
|
81
|
+
throw "Invalid arg";
|
|
82
|
+
if (typeof query === 'object') {
|
|
83
|
+
if (!('query' in query))
|
|
84
|
+
throw "Invalid arg";
|
|
85
|
+
params = query.params || params;
|
|
86
|
+
query = query.query;
|
|
87
|
+
}
|
|
88
|
+
if (typeof query !== 'string')
|
|
89
|
+
throw "Invalid arg";
|
|
90
|
+
return [2 /*return*/, this.sendSQL(query, params)];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
AbstractSQLConnection.prototype.runSQL_One = function (query, params) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
97
|
+
var res;
|
|
98
|
+
return __generator(this, function (_a) {
|
|
99
|
+
switch (_a.label) {
|
|
100
|
+
case 0: return [4 /*yield*/, this.runSQL(query, params)];
|
|
101
|
+
case 1:
|
|
102
|
+
res = _a.sent();
|
|
103
|
+
if (res && Array.isArray(res)) {
|
|
104
|
+
return [2 /*return*/, res[0] || null];
|
|
105
|
+
}
|
|
106
|
+
return [2 /*return*/, res || null];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
Object.defineProperty(AbstractSQLConnection.prototype, "isDebugLoggerEnabled", {
|
|
112
|
+
get: function () {
|
|
113
|
+
if (this.rawOptions.logging)
|
|
114
|
+
return true;
|
|
115
|
+
return process.env['DATABASE_LOG_MICRO_SQL'] == 'true';
|
|
116
|
+
},
|
|
117
|
+
enumerable: false,
|
|
118
|
+
configurable: true
|
|
119
|
+
});
|
|
120
|
+
AbstractSQLConnection.prototype.debugQuery = function (query, params) {
|
|
121
|
+
if (!this.isDebugLoggerEnabled)
|
|
122
|
+
return;
|
|
123
|
+
utils_1.logger.log("Running SQL command for " + this.rawOptions.type + ":", utils_1.terminalColors.magenta + query, utils_1.terminalColors.reset, params ? ('\n| with params:') : '', params || '');
|
|
124
|
+
};
|
|
125
|
+
return AbstractSQLConnection;
|
|
126
|
+
}());
|
|
127
|
+
exports.AbstractSQLConnection = AbstractSQLConnection;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DatabaseOptions } from '@klapeks/utils';
|
|
2
|
+
import { AbstractSQLConnection } from './abstract.connection';
|
|
3
|
+
import { MySQLConnection } from './mysql.connection';
|
|
4
|
+
export declare function isDatabaseTypeIs<T extends DatabaseOptions['type']>(options: DatabaseOptions, type: T): options is (DatabaseOptions & {
|
|
5
|
+
type: T;
|
|
6
|
+
});
|
|
7
|
+
export declare function createSQLConnection(options: DatabaseOptions): AbstractSQLConnection;
|
|
8
|
+
export * from './abstract.connection';
|
|
9
|
+
export * from './mysql.connection';
|
|
10
|
+
export * from './mssql.connection';
|
|
11
|
+
export { MySQLConnection as RawMySQLConnection };
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
exports.RawMySQLConnection = exports.createSQLConnection = exports.isDatabaseTypeIs = void 0;
|
|
18
|
+
var mysql_connection_1 = require("./mysql.connection");
|
|
19
|
+
Object.defineProperty(exports, "RawMySQLConnection", { enumerable: true, get: function () { return mysql_connection_1.MySQLConnection; } });
|
|
20
|
+
var mssql_connection_1 = require("./mssql.connection");
|
|
21
|
+
function isDatabaseTypeIs(options, type) {
|
|
22
|
+
return options.type === type;
|
|
23
|
+
}
|
|
24
|
+
exports.isDatabaseTypeIs = isDatabaseTypeIs;
|
|
25
|
+
function createSQLConnection(options) {
|
|
26
|
+
if (isDatabaseTypeIs(options, 'mysql'))
|
|
27
|
+
return new mysql_connection_1.MySQLConnection(options);
|
|
28
|
+
if (isDatabaseTypeIs(options, 'mssql'))
|
|
29
|
+
return new mssql_connection_1.MSSQLConnection(options);
|
|
30
|
+
throw "Connection for database type " + options.type + ' is not implemented :(';
|
|
31
|
+
}
|
|
32
|
+
exports.createSQLConnection = createSQLConnection;
|
|
33
|
+
__exportStar(require("./abstract.connection"), exports);
|
|
34
|
+
__exportStar(require("./mysql.connection"), exports);
|
|
35
|
+
__exportStar(require("./mssql.connection"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DatabaseOptions } from '@klapeks/utils';
|
|
2
|
+
import { AbstractSQLConnection } from './abstract.connection';
|
|
3
|
+
import type { ConnectionPool as MSSQL_ConnectionPool, config as MSSQL_ConnectionOptions } from 'mssql';
|
|
4
|
+
export declare class MSSQLConnection extends AbstractSQLConnection {
|
|
5
|
+
private _pool;
|
|
6
|
+
readonly poolOptions: MSSQL_ConnectionOptions;
|
|
7
|
+
constructor(options: DatabaseOptions & {
|
|
8
|
+
type: "mssql";
|
|
9
|
+
});
|
|
10
|
+
initConnection(): Promise<void>;
|
|
11
|
+
get pool(): MSSQL_ConnectionPool | undefined;
|
|
12
|
+
getPoolConnection(): Promise<MSSQL_ConnectionPool>;
|
|
13
|
+
destroyConnection(): Promise<void>;
|
|
14
|
+
sendSQL<T = any>(query: string, params?: any): Promise<T[]>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
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;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.MSSQLConnection = void 0;
|
|
55
|
+
var utils_1 = require("@klapeks/utils");
|
|
56
|
+
var abstract_connection_1 = require("./abstract.connection");
|
|
57
|
+
var quiet_require_1 = require("../../utils/quiet.require");
|
|
58
|
+
var mssql = (0, quiet_require_1.quietRequire)('mssql');
|
|
59
|
+
var logger = new utils_1.Logger('MSSQL');
|
|
60
|
+
var MSSQLConnection = /** @class */ (function (_super) {
|
|
61
|
+
__extends(MSSQLConnection, _super);
|
|
62
|
+
function MSSQLConnection(options) {
|
|
63
|
+
var _this = _super.call(this, options, 'toMSSQL') || this;
|
|
64
|
+
_this.poolOptions = {
|
|
65
|
+
user: options.username,
|
|
66
|
+
password: options.password,
|
|
67
|
+
server: options.host,
|
|
68
|
+
port: options.port,
|
|
69
|
+
database: options.database,
|
|
70
|
+
// charset: options.charset,
|
|
71
|
+
options: options.extra || options.options,
|
|
72
|
+
pool: {
|
|
73
|
+
max: 10,
|
|
74
|
+
min: 0,
|
|
75
|
+
idleTimeoutMillis: 30000,
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return _this;
|
|
79
|
+
}
|
|
80
|
+
MSSQLConnection.prototype.initConnection = function () {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
+
return __generator(this, function (_a) {
|
|
83
|
+
switch (_a.label) {
|
|
84
|
+
case 0: return [4 /*yield*/, this.getPoolConnection()];
|
|
85
|
+
case 1:
|
|
86
|
+
_a.sent();
|
|
87
|
+
return [2 /*return*/];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
Object.defineProperty(MSSQLConnection.prototype, "pool", {
|
|
93
|
+
get: function () {
|
|
94
|
+
return this._pool;
|
|
95
|
+
},
|
|
96
|
+
enumerable: false,
|
|
97
|
+
configurable: true
|
|
98
|
+
});
|
|
99
|
+
MSSQLConnection.prototype.getPoolConnection = function () {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
+
var _a;
|
|
102
|
+
return __generator(this, function (_b) {
|
|
103
|
+
switch (_b.label) {
|
|
104
|
+
case 0:
|
|
105
|
+
if (!mssql)
|
|
106
|
+
throw "mssql module is not installed";
|
|
107
|
+
if (this._pool)
|
|
108
|
+
return [2 /*return*/, this._pool];
|
|
109
|
+
_a = this;
|
|
110
|
+
return [4 /*yield*/, new mssql.ConnectionPool(this.poolOptions).connect()];
|
|
111
|
+
case 1:
|
|
112
|
+
_a._pool = _b.sent();
|
|
113
|
+
return [2 /*return*/, this._pool];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
MSSQLConnection.prototype.destroyConnection = function () {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
120
|
+
var _this = this;
|
|
121
|
+
return __generator(this, function (_a) {
|
|
122
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
123
|
+
if (!_this._pool)
|
|
124
|
+
return resolve();
|
|
125
|
+
_this._pool.close(function (err) {
|
|
126
|
+
_this._pool = undefined;
|
|
127
|
+
if (err)
|
|
128
|
+
reject(err);
|
|
129
|
+
else
|
|
130
|
+
resolve();
|
|
131
|
+
});
|
|
132
|
+
})];
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
MSSQLConnection.prototype.sendSQL = function (query, params) {
|
|
137
|
+
var _a;
|
|
138
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
139
|
+
var connection, request, i, _i, _b, _c, key, value, result;
|
|
140
|
+
return __generator(this, function (_d) {
|
|
141
|
+
switch (_d.label) {
|
|
142
|
+
case 0: return [4 /*yield*/, this.getPoolConnection()];
|
|
143
|
+
case 1:
|
|
144
|
+
connection = _d.sent();
|
|
145
|
+
request = connection.request();
|
|
146
|
+
if (params) {
|
|
147
|
+
if (Array.isArray(params) && params.length) {
|
|
148
|
+
for (i = 0; i < params.length; i++) {
|
|
149
|
+
query = query.replace('?', '@arg' + i);
|
|
150
|
+
request.input('arg' + i, params[i]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
for (_i = 0, _b = Object.entries(params); _i < _b.length; _i++) {
|
|
155
|
+
_c = _b[_i], key = _c[0], value = _c[1];
|
|
156
|
+
request.input(key, value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
this.debugQuery(query, params);
|
|
161
|
+
return [4 /*yield*/, request.query(query)];
|
|
162
|
+
case 2:
|
|
163
|
+
result = _d.sent();
|
|
164
|
+
return [2 /*return*/, ((_a = result.recordset) !== null && _a !== void 0 ? _a : [])];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
return MSSQLConnection;
|
|
170
|
+
}(abstract_connection_1.AbstractSQLConnection));
|
|
171
|
+
exports.MSSQLConnection = MSSQLConnection;
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import mysql2 from 'mysql2';
|
|
2
2
|
import { DatabaseOptions } from '@klapeks/utils';
|
|
3
|
-
|
|
3
|
+
import { AbstractSQLConnection } from './abstract.connection';
|
|
4
|
+
export declare class MySQLConnection extends AbstractSQLConnection {
|
|
4
5
|
private _pool;
|
|
5
6
|
readonly poolOptions: mysql2.PoolOptions;
|
|
6
7
|
constructor(options: DatabaseOptions & {
|
|
7
8
|
type: "mysql";
|
|
8
9
|
});
|
|
10
|
+
initConnection(): Promise<void>;
|
|
9
11
|
get pool(): mysql2.Pool | undefined;
|
|
10
12
|
takePool(): Promise<mysql2.Pool>;
|
|
11
13
|
destroyConnection(): Promise<void>;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
runSQL_One<T = any>(query: string, params?: any[]): Promise<T | null>;
|
|
14
|
+
getPoolConnection(): Promise<mysql2.PoolConnection>;
|
|
15
|
+
sendSQL<T = any>(query: string, params?: any): Promise<T[]>;
|
|
15
16
|
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
2
17
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
18
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
19
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -39,13 +54,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
54
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
55
|
};
|
|
41
56
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.
|
|
57
|
+
exports.MySQLConnection = void 0;
|
|
43
58
|
var mysql2_1 = __importDefault(require("mysql2"));
|
|
44
59
|
var utils_1 = require("@klapeks/utils");
|
|
60
|
+
var abstract_connection_1 = require("./abstract.connection");
|
|
45
61
|
var logger = new utils_1.Logger('MySQL');
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
var MySQLConnection = /** @class */ (function (_super) {
|
|
63
|
+
__extends(MySQLConnection, _super);
|
|
64
|
+
function MySQLConnection(options) {
|
|
65
|
+
var _this = _super.call(this, options, 'toMySQL') || this;
|
|
66
|
+
_this.poolOptions = {
|
|
49
67
|
user: options.username,
|
|
50
68
|
password: options.password,
|
|
51
69
|
host: options.host,
|
|
@@ -53,15 +71,28 @@ var RawMySQLConnection = /** @class */ (function () {
|
|
|
53
71
|
database: options.database,
|
|
54
72
|
charset: options.charset,
|
|
55
73
|
};
|
|
74
|
+
return _this;
|
|
56
75
|
}
|
|
57
|
-
|
|
76
|
+
MySQLConnection.prototype.initConnection = function () {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
78
|
+
return __generator(this, function (_a) {
|
|
79
|
+
switch (_a.label) {
|
|
80
|
+
case 0: return [4 /*yield*/, this.takePool()];
|
|
81
|
+
case 1:
|
|
82
|
+
_a.sent();
|
|
83
|
+
return [2 /*return*/];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
Object.defineProperty(MySQLConnection.prototype, "pool", {
|
|
58
89
|
get: function () {
|
|
59
90
|
return this._pool;
|
|
60
91
|
},
|
|
61
92
|
enumerable: false,
|
|
62
93
|
configurable: true
|
|
63
94
|
});
|
|
64
|
-
|
|
95
|
+
MySQLConnection.prototype.takePool = function () {
|
|
65
96
|
return __awaiter(this, void 0, void 0, function () {
|
|
66
97
|
var _this = this;
|
|
67
98
|
return __generator(this, function (_a) {
|
|
@@ -82,7 +113,7 @@ var RawMySQLConnection = /** @class */ (function () {
|
|
|
82
113
|
});
|
|
83
114
|
});
|
|
84
115
|
};
|
|
85
|
-
|
|
116
|
+
MySQLConnection.prototype.destroyConnection = function () {
|
|
86
117
|
return __awaiter(this, void 0, void 0, function () {
|
|
87
118
|
var _this = this;
|
|
88
119
|
return __generator(this, function (_a) {
|
|
@@ -100,7 +131,7 @@ var RawMySQLConnection = /** @class */ (function () {
|
|
|
100
131
|
});
|
|
101
132
|
});
|
|
102
133
|
};
|
|
103
|
-
|
|
134
|
+
MySQLConnection.prototype.getPoolConnection = function () {
|
|
104
135
|
return __awaiter(this, void 0, void 0, function () {
|
|
105
136
|
var _a;
|
|
106
137
|
var _this = this;
|
|
@@ -126,16 +157,17 @@ var RawMySQLConnection = /** @class */ (function () {
|
|
|
126
157
|
});
|
|
127
158
|
});
|
|
128
159
|
};
|
|
129
|
-
|
|
160
|
+
MySQLConnection.prototype.sendSQL = function (query, params) {
|
|
130
161
|
return __awaiter(this, void 0, void 0, function () {
|
|
131
162
|
var connection;
|
|
163
|
+
var _this = this;
|
|
132
164
|
return __generator(this, function (_a) {
|
|
133
165
|
switch (_a.label) {
|
|
134
|
-
case 0: return [4 /*yield*/, this.
|
|
166
|
+
case 0: return [4 /*yield*/, this.getPoolConnection()];
|
|
135
167
|
case 1:
|
|
136
168
|
connection = _a.sent();
|
|
137
169
|
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
138
|
-
|
|
170
|
+
_this.debugQuery(query, params);
|
|
139
171
|
connection.query(query, params || [], function (err, result) {
|
|
140
172
|
connection.release();
|
|
141
173
|
if (err)
|
|
@@ -147,19 +179,6 @@ var RawMySQLConnection = /** @class */ (function () {
|
|
|
147
179
|
});
|
|
148
180
|
});
|
|
149
181
|
};
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return __generator(this, function (_a) {
|
|
154
|
-
switch (_a.label) {
|
|
155
|
-
case 0: return [4 /*yield*/, this.runSQL(query, params)];
|
|
156
|
-
case 1:
|
|
157
|
-
res = _a.sent();
|
|
158
|
-
return [2 /*return*/, res.length ? (res[0] || res) : null];
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
return RawMySQLConnection;
|
|
164
|
-
}());
|
|
165
|
-
exports.RawMySQLConnection = RawMySQLConnection;
|
|
182
|
+
return MySQLConnection;
|
|
183
|
+
}(abstract_connection_1.AbstractSQLConnection));
|
|
184
|
+
exports.MySQLConnection = MySQLConnection;
|