@maiyunnet/kebab 4.1.0 → 5.0.1

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/db/pool.js ADDED
@@ -0,0 +1,281 @@
1
+ import * as mysql2 from 'mysql2/promise';
2
+ import * as pg from 'pg';
3
+ import * as lCore from '#kebab/lib/core.js';
4
+ import * as lTime from '#kebab/lib/time.js';
5
+ import * as lSql from '#kebab/lib/sql.js';
6
+ import * as lDb from '#kebab/lib/db.js';
7
+ import { Connection } from './conn.js';
8
+ import { Transaction } from './tran.js';
9
+ /** --- 连接列表池 --- */
10
+ const connections = [];
11
+ /**
12
+ * --- 获取当前连接池中所有连接的信息 ---
13
+ */
14
+ export function getConnectionList() {
15
+ const list = [];
16
+ for (let i = 0; i < connections.length; ++i) {
17
+ const connection = connections[i];
18
+ const etc = connection.getEtc();
19
+ list.push({
20
+ 'id': i,
21
+ 'service': connection.getService(),
22
+ 'last': connection.getLast(),
23
+ 'host': etc.host,
24
+ 'port': etc.port,
25
+ 'name': etc.name,
26
+ 'user': etc.user,
27
+ 'lost': connection.isLost(),
28
+ 'using': connection.isUsing(),
29
+ 'transaction': connection.isTransaction()
30
+ });
31
+ }
32
+ return list;
33
+ }
34
+ /**
35
+ * --- 计划任务 10 秒一次,关闭超过 30 秒不活动的连接,回滚独占时间过长的连接 ---
36
+ */
37
+ async function checkConnection() {
38
+ const now = lTime.stamp();
39
+ for (let i = 0; i < connections.length; ++i) {
40
+ const connection = connections[i];
41
+ if (connection.isLost()) {
42
+ // --- 连接已经丢失,移除 ---
43
+ await connection.end();
44
+ connections.splice(i, 1);
45
+ --i;
46
+ continue;
47
+ }
48
+ if (connection.isUsing()) {
49
+ // --- 连接正在被使用,看看是否空闲了超过 30 秒,超过则不是正常状态 ---
50
+ if (connection.getLast() <= now - 30) {
51
+ // --- 30 秒之前开始的 ---
52
+ const ls = connection.getLastSql();
53
+ const newarr = ls.map(item => {
54
+ if (!item.values) {
55
+ return item.sql;
56
+ }
57
+ return lSql.format(item.sql, item.values);
58
+ });
59
+ const msg = `[DB][checkConnection] There is a transactional connection[${i}] that is not closed, last sql: ${newarr.join(', ')}.`;
60
+ lCore.display(msg);
61
+ lCore.log({}, msg, '-error');
62
+ await connection.rollback();
63
+ }
64
+ continue;
65
+ }
66
+ if (connection.getLast() <= now - 30) {
67
+ // --- 超 30 秒未被使用,则关闭 ---
68
+ await connection.end();
69
+ connections.splice(i, 1);
70
+ --i;
71
+ continue;
72
+ }
73
+ // --- 30 秒内使用过,看看连接是否正常 ---
74
+ if (await connection.isAvailable(false)) {
75
+ // --- 正常 ---
76
+ continue;
77
+ }
78
+ // --- 连接有问题,直接关闭 ---
79
+ await connection.end();
80
+ connections.splice(i, 1);
81
+ --i;
82
+ }
83
+ setTimeout(function () {
84
+ checkConnection().catch(e => { lCore.display('[DB][checkConnection]', e); });
85
+ }, 10_000);
86
+ }
87
+ setTimeout(function () {
88
+ checkConnection().catch(e => { lCore.display('[DB][checkConnection]', e); });
89
+ }, 10_000);
90
+ /** --- 数据库连接池对象 --- */
91
+ export class Pool {
92
+ constructor(etc, opt) {
93
+ /** --- SQL 执行次数 --- */
94
+ this._queries = 0;
95
+ this._etc = etc;
96
+ this._service = opt.service;
97
+ }
98
+ /** --- 获取当前连接的服务商 --- */
99
+ getService() {
100
+ return this._service;
101
+ }
102
+ /**
103
+ * --- 执行一条 SQL,无视顺序和相同连接,随用随取 ---
104
+ * @param sql 执行的 SQL 字符串
105
+ * @param values 要替换的 data 数据
106
+ * @returns error.errno = -500 表示系统错误
107
+ */
108
+ async query(sql, values) {
109
+ ++this._queries;
110
+ // --- 获取并自动 using ---
111
+ const conn = await this._getConnection();
112
+ if (!conn) {
113
+ return {
114
+ 'rows': null,
115
+ 'fields': [],
116
+ 'error': {
117
+ 'message': 'false',
118
+ 'errno': 0,
119
+ },
120
+ 'result': -500,
121
+ };
122
+ }
123
+ // --- 执行一次后自动解除 using ---
124
+ return conn.query(sql, values);
125
+ }
126
+ /**
127
+ * --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
128
+ * @param sql 执行的 SQL 字符串
129
+ * @param values 要替换的 data 数据
130
+ */
131
+ async execute(sql, values) {
132
+ ++this._queries;
133
+ const conn = await this._getConnection();
134
+ if (!conn) {
135
+ return {
136
+ 'packet': null,
137
+ 'fields': [],
138
+ 'error': {
139
+ 'message': 'null',
140
+ 'errno': 0,
141
+ },
142
+ 'result': -500,
143
+ };
144
+ }
145
+ return conn.execute(sql, values);
146
+ }
147
+ /**
148
+ * --- 开启事务,返回事务对象并锁定连接,别人任何人不可用,有 ctr 的话必传 this,独立执行时可传 null ---
149
+ */
150
+ async beginTransaction(ctr) {
151
+ const conn = await this._getConnection();
152
+ if (!conn) {
153
+ return null;
154
+ }
155
+ if (!await conn.beginTransaction()) {
156
+ return null;
157
+ }
158
+ return new Transaction(ctr, conn);
159
+ }
160
+ /**
161
+ * --- 获取一个连接,自动变为 using 状态,;连接失败会返回 null ---
162
+ */
163
+ async _getConnection() {
164
+ let conn = null;
165
+ for (const connection of connections) {
166
+ const etc = connection.getEtc();
167
+ const service = connection.getService();
168
+ if ((etc.host !== this._etc.host) ||
169
+ (etc.port !== this._etc.port) ||
170
+ (etc.name !== this._etc.name) ||
171
+ (etc.user !== this._etc.user) ||
172
+ (service !== this._service)) {
173
+ // --- 配置项连接项不匹配 ---
174
+ continue;
175
+ }
176
+ if (!connection.using()) {
177
+ // --- 正在被使用,或者已经 lost ---
178
+ continue;
179
+ }
180
+ // --- 匹配且可用 ---
181
+ conn = connection;
182
+ break;
183
+ }
184
+ if (!conn) {
185
+ // --- 没有找到合适的连接,创建一个 ---
186
+ loop: for (let i = 0; i < 3; ++i) {
187
+ try {
188
+ switch (this._service) {
189
+ case lDb.ESERVICE.MYSQL: {
190
+ // --- MYSQL ---
191
+ const link = await mysql2.createConnection({
192
+ 'host': this._etc.host,
193
+ 'port': this._etc.port,
194
+ 'charset': this._etc.charset,
195
+ 'database': this._etc.name,
196
+ 'user': this._etc.user,
197
+ 'password': this._etc.pwd,
198
+ 'connectTimeout': 3_000,
199
+ typeCast: (field, next) => {
200
+ if (field.type !== 'GEOMETRY') {
201
+ return next();
202
+ }
203
+ const geo = field.geometry();
204
+ if (!geo) {
205
+ return null;
206
+ }
207
+ if (!Array.isArray(geo)) {
208
+ return geo;
209
+ }
210
+ if (geo[0].x && geo[0].y) {
211
+ return geo;
212
+ }
213
+ return geo[0];
214
+ },
215
+ });
216
+ const c = new Connection(this._etc, link);
217
+ c.using();
218
+ link.on('error', function (err) {
219
+ c.setLost();
220
+ if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
221
+ lCore.debug('[DB][_getConnection][MYSQL][error]', err);
222
+ lCore.log({}, '[DB][_getConnection][MYSQL][error] ' + err.message, '-error');
223
+ }
224
+ }).on('end', () => {
225
+ // lCore.debug('[DB][_getConnection] connection end.');
226
+ c.setLost();
227
+ }).on('close', () => {
228
+ c.setLost();
229
+ });
230
+ conn = c;
231
+ connections.push(conn);
232
+ break loop;
233
+ }
234
+ case lDb.ESERVICE.PGSQL: {
235
+ // --- PGSQL ---
236
+ const link = new pg.Client({
237
+ 'host': this._etc.host,
238
+ 'port': this._etc.port,
239
+ 'database': this._etc.name,
240
+ 'user': this._etc.user,
241
+ 'password': this._etc.pwd,
242
+ 'connectionTimeoutMillis': 3_000,
243
+ });
244
+ await link.connect();
245
+ const c = new Connection(this._etc, link);
246
+ c.using();
247
+ link.on('error', err => {
248
+ c.setLost();
249
+ lCore.debug('[DB][_getConnection][PGSQL][error]', err);
250
+ lCore.log({}, '[DB][_getConnection][PGSQL][error] ' + err.message, '-error');
251
+ }).on('end', () => {
252
+ c.setLost();
253
+ });
254
+ conn = c;
255
+ connections.push(conn);
256
+ break loop;
257
+ }
258
+ }
259
+ }
260
+ catch (err) {
261
+ if (err.message.includes('ETIMEOUT') || err.message.includes('EHOSTUNREACH') || err.message.includes('ECONNREFUSED')) {
262
+ // lCore.debug(`[DB][_getConnection][${lDb.ESERVICE[this._service]}]`, err);
263
+ await lCore.sleep(300);
264
+ continue;
265
+ }
266
+ const msg = `[DB][_getConnection][${lDb.ESERVICE[this._service]}] ${err.message}(${this._etc.host}:${this._etc.port})`;
267
+ lCore.debug(msg);
268
+ lCore.log({}, msg, '-error');
269
+ break;
270
+ }
271
+ }
272
+ }
273
+ return conn;
274
+ }
275
+ /**
276
+ * --- 获取 SQL 执行次数 ---
277
+ */
278
+ getQueries() {
279
+ return this._queries;
280
+ }
281
+ }
@@ -0,0 +1,33 @@
1
+ import * as sCtr from '#kebab/sys/ctr.js';
2
+ import * as lDb from '#kebab/lib/db.js';
3
+ import * as kebab from '#kebab/index.js';
4
+ import * as conn from './conn.js';
5
+ /** --- 事务连接对象,commit 和 rollback 后将无法使用 --- */
6
+ export declare class Transaction {
7
+ /** --- SQL 执行次数 --- */
8
+ private _queries;
9
+ /** --- 连接对象 --- */
10
+ private _conn;
11
+ private readonly _ctr;
12
+ private readonly _timer;
13
+ constructor(ctr: sCtr.Ctr | null, conn: conn.Connection, opts?: {
14
+ 'warning'?: number;
15
+ 'danger'?: number;
16
+ });
17
+ /** --- 获取当前连接的服务商 --- */
18
+ getService(): lDb.ESERVICE | null;
19
+ /**
20
+ * --- 在事务连接中执行一条 SQL ---
21
+ * @param sql 执行的 SQL 字符串
22
+ * @param values 要替换的 data 数据
23
+ */
24
+ query(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData>;
25
+ /**
26
+ * --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
27
+ * @param sql 执行的 SQL 字符串
28
+ * @param values 要替换的 data 数据
29
+ */
30
+ execute(sql: string, values?: kebab.DbValue[]): Promise<lDb.IPacket>;
31
+ commit(): Promise<boolean>;
32
+ rollback(): Promise<boolean>;
33
+ }
package/lib/db/tran.js ADDED
@@ -0,0 +1,122 @@
1
+ import * as lCore from '#kebab/lib/core.js';
2
+ /** --- 事务连接对象,commit 和 rollback 后将无法使用 --- */
3
+ export class Transaction {
4
+ constructor(ctr, conn, opts = {}) {
5
+ /** --- SQL 执行次数 --- */
6
+ this._queries = 0;
7
+ // --- 事务时长监听 timer ---
8
+ this._timer = {
9
+ 'warning': undefined,
10
+ 'danger': undefined
11
+ };
12
+ // --- 进来的连接对象直接是事务独占模式 ---
13
+ this._ctr = ctr;
14
+ if (ctr) {
15
+ ++ctr.getPrototype('_waitInfo').transaction;
16
+ }
17
+ this._conn = conn;
18
+ // --- 事务时长监听 ---
19
+ const warning = opts.warning ?? 1_500;
20
+ this._timer.warning = setTimeout(() => {
21
+ this._timer.warning = undefined;
22
+ lCore.display('[WARNING][DB][Transaction] time too long, ms: ' + warning + ' ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'));
23
+ }, warning);
24
+ const danger = opts.danger ?? 5_000;
25
+ this._timer.danger = setTimeout(() => {
26
+ this._timer.danger = undefined;
27
+ lCore.display('[DANGER][DB][Transaction] time too long, ms:', danger, this._ctr?.getPrototype('_config').const.path ?? 'no ctr');
28
+ }, danger);
29
+ }
30
+ /** --- 获取当前连接的服务商 --- */
31
+ getService() {
32
+ return this._conn?.getService() ?? null;
33
+ }
34
+ /**
35
+ * --- 在事务连接中执行一条 SQL ---
36
+ * @param sql 执行的 SQL 字符串
37
+ * @param values 要替换的 data 数据
38
+ */
39
+ async query(sql, values) {
40
+ if (!this._conn) {
41
+ // --- 当前连接已不可用 ---
42
+ lCore.display('[DB][Transaction][query] has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql);
43
+ lCore.log({}, '[DB][Transaction][query] has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql, '-error');
44
+ return {
45
+ 'rows': null,
46
+ 'fields': [],
47
+ 'error': {
48
+ 'message': 'false',
49
+ 'errno': 0,
50
+ },
51
+ 'result': -500,
52
+ };
53
+ }
54
+ ++this._queries;
55
+ return this._conn.query(sql, values);
56
+ }
57
+ /**
58
+ * --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
59
+ * @param sql 执行的 SQL 字符串
60
+ * @param values 要替换的 data 数据
61
+ */
62
+ async execute(sql, values) {
63
+ if (!this._conn) {
64
+ // --- 当前连接已不可用 ---
65
+ lCore.display('[DB][Transaction][execute] has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql);
66
+ lCore.log({}, '(db.Transaction.execute) has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql, '-error');
67
+ return {
68
+ 'packet': null,
69
+ 'fields': [],
70
+ 'error': {
71
+ 'message': 'null',
72
+ 'errno': 0,
73
+ },
74
+ 'result': -500,
75
+ };
76
+ }
77
+ ++this._queries;
78
+ return this._conn.execute(sql, values);
79
+ }
80
+ async commit() {
81
+ if (!this._conn) {
82
+ // --- 当前连接已不可用 ---
83
+ lCore.display('[DB][Transaction][commit] has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'));
84
+ lCore.log({}, '[DB][Transaction][commit] has been closed ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'), '-error');
85
+ return false;
86
+ }
87
+ const r = await this._conn.commit();
88
+ if (!r) {
89
+ return false;
90
+ }
91
+ this._conn = null;
92
+ if (this._ctr) {
93
+ --this._ctr.getPrototype('_waitInfo').transaction;
94
+ }
95
+ clearTimeout(this._timer.warning);
96
+ this._timer.warning = undefined;
97
+ clearTimeout(this._timer.danger);
98
+ this._timer.danger = undefined;
99
+ return true;
100
+ }
101
+ async rollback() {
102
+ if (!this._conn) {
103
+ // --- 当前连接已不可用 ---
104
+ lCore.display('[DB][Transaction][rollback] has been closed: ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'));
105
+ lCore.log({}, '[DB][Transaction][rollback] has been closed: ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'), '-error');
106
+ return false;
107
+ }
108
+ const r = await this._conn.rollback();
109
+ if (!r) {
110
+ return false;
111
+ }
112
+ this._conn = null;
113
+ if (this._ctr) {
114
+ --this._ctr.getPrototype('_waitInfo').transaction;
115
+ }
116
+ clearTimeout(this._timer.warning);
117
+ this._timer.warning = undefined;
118
+ clearTimeout(this._timer.danger);
119
+ this._timer.danger = undefined;
120
+ return true;
121
+ }
122
+ }
package/lib/db.d.ts CHANGED
@@ -1,196 +1,62 @@
1
1
  /**
2
2
  * Project: Kebab, User: JianSuoQiYue
3
3
  * Date: 2019-4-15 13:40
4
- * Last: 2020-4-13 15:34:45, 2022-09-12 13:10:34, 2023-5-24 18:29:38, 2024-7-11 14:37:54, 2024-8-25 00:32:53, 2024-9-22 17:30:47, 2025-8-3 20:24:03
4
+ * Last: 2020-4-13 15:34:45, 2022-09-12 13:10:34, 2023-5-24 18:29:38, 2024-7-11 14:37:54, 2024-8-25 00:32:53, 2024-9-22 17:30:47, 2025-8-3 20:24:03, 2025-11-8 19:13:03
5
5
  */
6
- import * as mysql2 from 'mysql2/promise';
7
6
  import * as kebab from '#kebab/index.js';
8
7
  import * as sCtr from '#kebab/sys/ctr.js';
8
+ import { Connection } from './db/conn.js';
9
+ import { Pool } from './db/pool.js';
10
+ import { Transaction } from './db/tran.js';
11
+ /** --- 服务商定义 --- */
12
+ export declare enum ESERVICE {
13
+ 'MYSQL' = 0,
14
+ 'PGSQL' = 1
15
+ }
9
16
  /** --- query 返回的数据 --- */
10
17
  export interface IData {
11
- 'rows': any[] | null;
12
- 'fields': mysql2.FieldPacket[];
18
+ 'rows': Array<Record<string, any>> | null;
19
+ 'fields': Array<{
20
+ /** --- 字段名 --- */
21
+ 'name': string;
22
+ /** --- 字段格式长度 --- */
23
+ 'length': number;
24
+ }>;
13
25
  'error': {
14
26
  'message': string;
15
27
  'errno': number;
16
- [key: string]: any;
17
28
  } | null;
18
29
  /** --- 1-正常,-500-服务器错误 --- */
19
30
  'result': number;
20
31
  }
21
32
  /** --- exec 返回对象 --- */
22
33
  export interface IPacket {
23
- 'packet': mysql2.ResultSetHeader | null;
24
- 'fields': mysql2.FieldPacket[];
34
+ 'packet': {
35
+ /** --- 受影响的行数 --- */
36
+ 'affected': number;
37
+ /** --- 插入的 id --- */
38
+ 'insert': number;
39
+ } | null;
40
+ 'fields': Array<{
41
+ /** --- 字段名 --- */
42
+ 'name': string;
43
+ /** --- 字段格式长度 --- */
44
+ 'length': number;
45
+ }>;
25
46
  'error': {
26
47
  'message': string;
27
48
  'errno': number;
28
49
  [key: string]: any;
29
50
  } | null;
30
- }
31
- /** --- 连接信息 --- */
32
- export interface IConnectionInfo {
33
- 'id': number;
34
- 'last': number;
35
- 'host': string;
36
- 'port': number;
37
- 'name': string;
38
- 'user': string;
39
- 'lost': boolean;
40
- 'using': boolean;
41
- 'transaction': boolean;
42
- }
43
- /** --- 数据库连接池对象 --- */
44
- export declare class Pool {
45
- /** --- SQL 执行次数 --- */
46
- private _queries;
47
- /** --- 当前 Pool 对象的数据库连接信息 --- */
48
- private readonly _etc;
49
- constructor(etc: kebab.IConfigDb);
50
- /**
51
- * --- 执行一条 SQL,无视顺序和相同连接,随用随取 ---
52
- * @param sql 执行的 SQL 字符串
53
- * @param values 要替换的 data 数据
54
- * @returns error.errno = -500 表示系统错误
55
- */
56
- query(sql: string, values?: kebab.DbValue[]): Promise<IData>;
57
- /**
58
- * --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
59
- * @param sql 执行的 SQL 字符串
60
- * @param values 要替换的 data 数据
61
- */
62
- execute(sql: string, values?: kebab.DbValue[]): Promise<IPacket>;
63
- /**
64
- * --- 开启事务,返回事务对象并锁定连接,别人任何人不可用,有 ctr 的话必传 this,独立执行时可传 null ---
65
- */
66
- beginTransaction(ctr: sCtr.Ctr | null): Promise<Transaction | null>;
67
- /**
68
- * --- 获取一个连接,自动变为 using 状态,;连接失败会返回 null ---
69
- */
70
- private _getConnection;
71
- /**
72
- * --- 获取 SQL 执行次数 ---
73
- */
74
- getQueries(): number;
75
- }
76
- /** --- 事务连接对象,commit 和 rollback 后将无法使用 --- */
77
- export declare class Transaction {
78
- /** --- SQL 执行次数 --- */
79
- private _queries;
80
- /** --- 连接对象 --- */
81
- private _conn;
82
- private readonly _ctr;
83
- private readonly _timer;
84
- constructor(ctr: sCtr.Ctr | null, conn: Connection, opts?: {
85
- 'warning'?: number;
86
- 'danger'?: number;
87
- });
88
- /**
89
- * --- 在事务连接中执行一条 SQL ---
90
- * @param sql 执行的 SQL 字符串
91
- * @param values 要替换的 data 数据
92
- */
93
- query(sql: string, values?: kebab.DbValue[]): Promise<IData>;
94
- /**
95
- * --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
96
- * @param sql 执行的 SQL 字符串
97
- * @param values 要替换的 data 数据
98
- */
99
- execute(sql: string, values?: kebab.DbValue[]): Promise<IPacket>;
100
- commit(): Promise<boolean>;
101
- rollback(): Promise<boolean>;
102
- }
103
- /** --- 数据库连接对象 --- */
104
- export declare class Connection {
105
- /** --- 本连接最后一次使用时间 --- */
106
- private _last;
107
- /** --- 最后两次执行的 sql 完整串 --- */
108
- private readonly _lastSql;
109
- /** --- 数据库连接对象 --- */
110
- private readonly _link;
111
- /** --- 当前连接是否正在被独占使用 --- */
112
- private _using;
113
- /** --- 当发生断开,则需从连接池移除连接 --- */
114
- private _lost;
115
- /** --- 当前正在处理事务 --- */
116
- private _transaction;
117
- /** --- 当前的连接配置信息 --- */
118
- private readonly _etc;
119
- constructor(etc: kebab.IConfigDb, link: mysql2.Connection);
120
- /**
121
- * --- 获取连接 etc 信息 ---
122
- */
123
- getEtc(): kebab.IConfigDb;
124
- /**
125
- * --- 获取最后一次获取连接的时间 ---
126
- */
127
- getLast(): number;
128
- /**
129
- * --- 获取最后两次执行的 sql 字符串 ---
130
- */
131
- getLastSql(): Array<{
132
- 'sql': string;
133
- 'values'?: kebab.DbValue[];
134
- }>;
135
- /**
136
- * --- 将本条连接设置为不可用 ---
137
- */
138
- setLost(): void;
139
- /**
140
- * --- 是否已经丢失 ---
141
- */
142
- isLost(): boolean;
143
- /**
144
- * --- 是否是开启事务状态 ---
145
- */
146
- isTransaction(): boolean;
147
- /**
148
- * --- 获取当前状态是否正在被使用中 ---
149
- */
150
- isUsing(): boolean;
151
- /**
152
- * --- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功并自动刷新最后时间 ---
153
- */
154
- using(): boolean;
155
- /**
156
- * --- 取消占用 ---
157
- */
158
- used(): void;
159
- /**
160
- * --- 设定最后使用时间 ---
161
- */
162
- refreshLast(): void;
163
- /**
164
- * --- 通过执行一条语句判断当前连接是否可用 ---
165
- * @param last 是否刷新最后使用时间(默认刷新)
166
- */
167
- isAvailable(last?: boolean): Promise<boolean>;
168
- /**
169
- * --- 执行一条 SQL 并获得返回数据 ---
170
- * @param sql 执行的 SQL 字符串
171
- * @param values 要替换的 data 数据
172
- */
173
- query(sql: string, values?: kebab.DbValue[]): Promise<IData>;
174
- /**
175
- * --- 执行一条 SQL 并获得影响行数对象 packet ---
176
- * @param sql 执行的 SQL 字符串
177
- * @param values 要替换的 data 数据
178
- */
179
- execute(sql: string, values?: kebab.DbValue[]): Promise<IPacket>;
180
- /**
181
- * --- 关闭连接,一般情况下不使用 ---
182
- */
183
- end(): Promise<boolean>;
184
- beginTransaction(): Promise<boolean>;
185
- commit(): Promise<boolean>;
186
- rollback(): Promise<boolean>;
51
+ /** --- 1-正常,-500-服务器错误 --- */
52
+ 'result': number;
187
53
  }
188
54
  /**
189
55
  * --- 获取 Db Pool 对象 ---
190
56
  * @param etc 配置信息可留空
191
57
  */
192
- export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigDb): Pool;
193
- /**
194
- * --- 获取当前连接池中所有连接的信息 ---
195
- */
196
- export declare function getConnectionList(): IConnectionInfo[];
58
+ export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigDb, opt?: {
59
+ /** --- 服务商,默认 PGSQL --- */
60
+ 'service'?: ESERVICE;
61
+ }): Pool;
62
+ export { Connection, Pool, Transaction };