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.
@@ -1,46 +1,47 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MySqlConnector = void 0;
4
- var tslib_1 = require("tslib");
5
- var mysql2_1 = tslib_1.__importDefault(require("mysql2"));
6
- var uuid_1 = require("uuid");
7
- var assert_1 = tslib_1.__importDefault(require("assert"));
8
- var MySqlConnector = /** @class */ (function () {
9
- function MySqlConnector(configuration) {
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
- MySqlConnector.prototype.connect = function () {
16
+ connect() {
14
17
  this.pool = mysql2_1.default.createPool(this.configuration);
15
- };
16
- MySqlConnector.prototype.disconnect = function () {
18
+ }
19
+ disconnect() {
17
20
  this.pool.end();
18
- };
19
- MySqlConnector.prototype.startTransaction = function (option) {
20
- var _this = this;
21
- return new Promise(function (resolve, reject) {
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
- var isolationLevel = (option || {}).isolationLevel;
27
- var startTxn = function () {
28
- var sql = 'START TRANSACTION;';
29
- connection.query(sql, function (err2) {
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
- var id = (0, uuid_1.v4)();
36
- Object.assign(_this.txnDict, (_a = {},
37
- _a[id] = connection,
38
- _a));
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("SET TRANSACTION ISOLATION LEVEL ".concat(isolationLevel, ";"), function (err2) {
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
- MySqlConnector.prototype.exec = function (sql, txn) {
58
- return tslib_1.__awaiter(this, void 0, void 0, function () {
59
- var connection_1;
60
- var _this = this;
61
- return tslib_1.__generator(this, function (_a) {
62
- if (process.env.NODE_ENV === 'development') {
63
- console.log(sql);
64
- }
65
- if (txn) {
66
- connection_1 = this.txnDict[txn];
67
- (0, assert_1.default)(connection_1);
68
- return [2 /*return*/, new Promise(function (resolve, reject) {
69
- connection_1.query(sql, function (err, result) {
70
- if (err) {
71
- console.error("sql exec err: ".concat(sql), err);
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
- MySqlConnector.prototype.commitTransaction = function (txn) {
97
- var connection = this.txnDict[txn];
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(function (resolve, reject) {
100
- connection.query('COMMIT;', function (err) {
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
- MySqlConnector.prototype.rollbackTransaction = function (txn) {
110
- var connection = this.txnDict[txn];
102
+ }
103
+ rollbackTransaction(txn) {
104
+ const connection = this.txnDict[txn];
111
105
  (0, assert_1.default)(connection);
112
- return new Promise(function (resolve, reject) {
113
- connection.query('ROLLBACK;', function (err) {
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
- return MySqlConnector;
123
- }());
115
+ }
116
+ }
124
117
  exports.MySqlConnector = MySqlConnector;
@@ -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>;