oak-db 3.0.1 → 3.0.2
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/lib/MySQL/connector.js +68 -75
- package/lib/MySQL/store.d.ts +1 -0
- package/lib/MySQL/store.js +131 -269
- package/lib/MySQL/translator.d.ts +1 -1
- package/lib/MySQL/translator.js +344 -359
- package/lib/MySQL/types/Configuration.d.ts +2 -2
- package/lib/index.js +1 -1
- package/lib/sqlTranslator.d.ts +1 -1
- package/lib/sqlTranslator.js +359 -365
- package/package.json +7 -7
package/lib/MySQL/connector.js
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MySqlConnector = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const mysql2_1 = tslib_1.__importDefault(require("mysql2"));
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
8
|
+
class MySqlConnector {
|
|
9
|
+
pool;
|
|
10
|
+
configuration;
|
|
11
|
+
txnDict;
|
|
12
|
+
constructor(configuration) {
|
|
10
13
|
this.configuration = configuration;
|
|
11
14
|
this.txnDict = {};
|
|
12
15
|
}
|
|
13
|
-
|
|
16
|
+
connect() {
|
|
14
17
|
this.pool = mysql2_1.default.createPool(this.configuration);
|
|
15
|
-
}
|
|
16
|
-
|
|
18
|
+
}
|
|
19
|
+
disconnect() {
|
|
17
20
|
this.pool.end();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
_this.pool.getConnection(function (err, connection) {
|
|
21
|
+
}
|
|
22
|
+
startTransaction(option) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
this.pool.getConnection((err, connection) => {
|
|
23
25
|
if (err) {
|
|
24
26
|
return reject(err);
|
|
25
27
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
connection.query(sql,
|
|
30
|
-
var _a;
|
|
28
|
+
const { isolationLevel } = option || {};
|
|
29
|
+
const startTxn = () => {
|
|
30
|
+
let sql = 'START TRANSACTION;';
|
|
31
|
+
connection.query(sql, (err2) => {
|
|
31
32
|
if (err2) {
|
|
32
33
|
connection.release();
|
|
33
34
|
return reject(err2);
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
Object.assign(
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const id = (0, uuid_1.v4)();
|
|
37
|
+
Object.assign(this.txnDict, {
|
|
38
|
+
[id]: connection,
|
|
39
|
+
});
|
|
39
40
|
resolve(id);
|
|
40
41
|
});
|
|
41
42
|
};
|
|
42
43
|
if (isolationLevel) {
|
|
43
|
-
connection.query(
|
|
44
|
+
connection.query(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel};`, (err2) => {
|
|
44
45
|
if (err2) {
|
|
45
46
|
connection.release();
|
|
46
47
|
return reject(err2);
|
|
@@ -53,51 +54,44 @@ var MySqlConnector = /** @class */ (function () {
|
|
|
53
54
|
}
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return reject(err);
|
|
73
|
-
}
|
|
74
|
-
resolve(result);
|
|
75
|
-
});
|
|
76
|
-
})];
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
80
|
-
// if (process.env.DEBUG) {
|
|
81
|
-
// console.log(sql);
|
|
82
|
-
//}
|
|
83
|
-
_this.pool.query(sql, function (err, result) {
|
|
84
|
-
if (err) {
|
|
85
|
-
console.error("sql exec err: ".concat(sql), err);
|
|
86
|
-
return reject(err);
|
|
87
|
-
}
|
|
88
|
-
resolve(result);
|
|
89
|
-
});
|
|
90
|
-
})];
|
|
91
|
-
}
|
|
92
|
-
return [2 /*return*/];
|
|
57
|
+
}
|
|
58
|
+
async exec(sql, txn) {
|
|
59
|
+
if (process.env.NODE_ENV === 'development') {
|
|
60
|
+
console.log(sql);
|
|
61
|
+
}
|
|
62
|
+
if (txn) {
|
|
63
|
+
const connection = this.txnDict[txn];
|
|
64
|
+
(0, assert_1.default)(connection);
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
connection.query(sql, (err, result) => {
|
|
67
|
+
if (err) {
|
|
68
|
+
console.error(`sql exec err: ${sql}`, err);
|
|
69
|
+
return reject(err);
|
|
70
|
+
}
|
|
71
|
+
resolve(result);
|
|
72
|
+
});
|
|
93
73
|
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
// if (process.env.DEBUG) {
|
|
78
|
+
// console.log(sql);
|
|
79
|
+
//}
|
|
80
|
+
this.pool.query(sql, (err, result) => {
|
|
81
|
+
if (err) {
|
|
82
|
+
console.error(`sql exec err: ${sql}`, err);
|
|
83
|
+
return reject(err);
|
|
84
|
+
}
|
|
85
|
+
resolve(result);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
commitTransaction(txn) {
|
|
91
|
+
const connection = this.txnDict[txn];
|
|
98
92
|
(0, assert_1.default)(connection);
|
|
99
|
-
return new Promise(
|
|
100
|
-
connection.query('COMMIT;',
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
connection.query('COMMIT;', (err) => {
|
|
101
95
|
if (err) {
|
|
102
96
|
return reject(err);
|
|
103
97
|
}
|
|
@@ -105,12 +99,12 @@ var MySqlConnector = /** @class */ (function () {
|
|
|
105
99
|
resolve();
|
|
106
100
|
});
|
|
107
101
|
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
}
|
|
103
|
+
rollbackTransaction(txn) {
|
|
104
|
+
const connection = this.txnDict[txn];
|
|
111
105
|
(0, assert_1.default)(connection);
|
|
112
|
-
return new Promise(
|
|
113
|
-
connection.query('ROLLBACK;',
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
connection.query('ROLLBACK;', (err) => {
|
|
114
108
|
if (err) {
|
|
115
109
|
return reject(err);
|
|
116
110
|
}
|
|
@@ -118,7 +112,6 @@ var MySqlConnector = /** @class */ (function () {
|
|
|
118
112
|
resolve();
|
|
119
113
|
});
|
|
120
114
|
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
}());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
124
117
|
exports.MySqlConnector = MySqlConnector;
|
package/lib/MySQL/store.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt exte
|
|
|
23
23
|
protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
|
|
24
24
|
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
|
|
25
25
|
select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
|
|
26
|
+
protected countAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number>;
|
|
26
27
|
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
|
|
27
28
|
begin(option?: TxnOption): Promise<string>;
|
|
28
29
|
commit(txnId: string): Promise<void>;
|