@contember/database 1.1.0-beta.3 → 1.2.0-alpha.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/dist/src/client/Connection.d.ts +11 -17
- package/dist/src/client/Connection.d.ts.map +1 -1
- package/dist/src/client/Connection.js +21 -20
- package/dist/src/client/Connection.js.map +1 -1
- package/dist/src/client/Pool.d.ts +124 -0
- package/dist/src/client/Pool.d.ts.map +1 -0
- package/dist/src/client/Pool.js +368 -0
- package/dist/src/client/Pool.js.map +1 -0
- package/dist/src/client/SingleConnection.d.ts +3 -2
- package/dist/src/client/SingleConnection.d.ts.map +1 -1
- package/dist/src/client/SingleConnection.js +16 -10
- package/dist/src/client/SingleConnection.js.map +1 -1
- package/dist/src/client/errors.d.ts +1 -1
- package/dist/src/client/errors.d.ts.map +1 -1
- package/dist/src/client/errors.js +7 -5
- package/dist/src/client/errors.js.map +1 -1
- package/dist/src/client/execution.d.ts +0 -1
- package/dist/src/client/execution.d.ts.map +1 -1
- package/dist/src/client/execution.js +1 -10
- package/dist/src/client/execution.js.map +1 -1
- package/dist/src/client/index.d.ts +1 -0
- package/dist/src/client/index.d.ts.map +1 -1
- package/dist/src/client/index.js +1 -0
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types.d.ts +4 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/createDatabaseIfNotExists.d.ts +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.d.ts.map +1 -1
- package/dist/src/utils/createDatabaseIfNotExists.js +2 -2
- package/dist/src/utils/createDatabaseIfNotExists.js.map +1 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/pgClientFactory.d.ts +5 -0
- package/dist/src/utils/pgClientFactory.d.ts.map +1 -0
- package/dist/src/utils/pgClientFactory.js +12 -0
- package/dist/src/utils/pgClientFactory.js.map +1 -0
- package/dist/tests/cases/unit/pool.test.d.ts +2 -0
- package/dist/tests/cases/unit/pool.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/pool.test.js +323 -0
- package/dist/tests/cases/unit/pool.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/Connection.ts +29 -31
- package/src/client/Pool.ts +455 -0
- package/src/client/SingleConnection.ts +18 -11
- package/src/client/errors.ts +10 -6
- package/src/client/execution.ts +0 -9
- package/src/client/index.ts +1 -0
- package/src/index.ts +1 -0
- package/src/types.ts +4 -1
- package/src/utils/createDatabaseIfNotExists.ts +5 -5
- package/src/utils/index.ts +1 -0
- package/src/utils/pgClientFactory.ts +10 -0
- package/tests/cases/unit/pool.test.ts +350 -0
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { PoolConfig } from 'pg';
|
|
2
1
|
import { EventManager } from './EventManager';
|
|
3
2
|
import { Client } from './Client';
|
|
4
|
-
import {
|
|
3
|
+
import { Pool, PoolConfig, PoolStatus } from './Pool';
|
|
4
|
+
import { DatabaseConfig } from '../types';
|
|
5
5
|
declare class Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {
|
|
6
|
-
readonly
|
|
6
|
+
private readonly pool;
|
|
7
7
|
private readonly queryConfig;
|
|
8
8
|
readonly eventManager: EventManager;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
constructor(pool: Pool, queryConfig: Connection.QueryConfig, eventManager?: EventManager);
|
|
10
|
+
static create({ pool, ...config }: DatabaseConfig & {
|
|
11
|
+
pool?: PoolConfig;
|
|
12
|
+
}, queryConfig?: Connection.QueryConfig): Connection;
|
|
11
13
|
createClient(schema: string, queryMeta: Record<string, any>): Client;
|
|
14
|
+
clearPool(): Promise<void>;
|
|
12
15
|
transaction<Result>(callback: (connection: Connection.TransactionLike) => Promise<Result> | Result, options?: {
|
|
13
16
|
eventManager?: EventManager;
|
|
14
17
|
}): Promise<Result>;
|
|
15
18
|
end(): Promise<void>;
|
|
16
19
|
query<Row extends Record<string, any>>(sql: string, parameters?: any[], meta?: Record<string, any>, { eventManager, ...config }?: Connection.QueryConfig): Promise<Connection.Result<Row>>;
|
|
17
|
-
getPoolStatus():
|
|
20
|
+
getPoolStatus(): PoolStatus;
|
|
18
21
|
}
|
|
19
22
|
declare namespace Connection {
|
|
20
23
|
interface QueryConfig {
|
|
@@ -30,16 +33,14 @@ declare namespace Connection {
|
|
|
30
33
|
eventManager?: EventManager;
|
|
31
34
|
}): Promise<Result>;
|
|
32
35
|
}
|
|
33
|
-
type ConnectionType = Connection.ConnectionLike & Connection.ClientFactory & Connection.PoolStatusProvider
|
|
34
|
-
config: DatabaseCredentials;
|
|
35
|
-
};
|
|
36
|
+
type ConnectionType = Connection.ConnectionLike & Connection.ClientFactory & Connection.PoolStatusProvider;
|
|
36
37
|
interface ConnectionLike extends Transactional, Queryable {
|
|
37
38
|
}
|
|
38
39
|
interface ClientFactory {
|
|
39
40
|
createClient(schema: string, queryMeta: Record<string, any>): Client;
|
|
40
41
|
}
|
|
41
42
|
interface PoolStatusProvider {
|
|
42
|
-
getPoolStatus(): PoolStatus;
|
|
43
|
+
getPoolStatus(): PoolStatus | undefined;
|
|
43
44
|
}
|
|
44
45
|
interface TransactionLike extends ConnectionLike {
|
|
45
46
|
readonly isClosed: boolean;
|
|
@@ -54,7 +55,6 @@ declare namespace Connection {
|
|
|
54
55
|
readonly parameters: any[];
|
|
55
56
|
readonly meta: Record<string, any>;
|
|
56
57
|
}
|
|
57
|
-
type Credentials = Pick<PoolConfig, 'host' | 'port' | 'user' | 'password' | 'database'>;
|
|
58
58
|
interface Result<Row extends Record<string, any> = Record<string, any>> {
|
|
59
59
|
readonly rowCount: number;
|
|
60
60
|
readonly rows: Row[];
|
|
@@ -64,12 +64,6 @@ declare namespace Connection {
|
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
66
|
const REPEATABLE_READ = "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
|
|
67
|
-
type PoolStatus = {
|
|
68
|
-
totalCount: number;
|
|
69
|
-
idleCount: number;
|
|
70
|
-
waitingCount: number;
|
|
71
|
-
maxCount: number;
|
|
72
|
-
};
|
|
73
67
|
}
|
|
74
68
|
export { Connection };
|
|
75
69
|
//# sourceMappingURL=Connection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Connection.d.ts","sourceRoot":"","sources":["../../../src/client/Connection.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Connection.d.ts","sourceRoot":"","sources":["../../../src/client/Connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAGjC,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAEzC,cAAM,UAAW,YAAW,UAAU,CAAC,cAAc,EAAE,UAAU,CAAC,aAAa,EAAE,UAAU,CAAC,kBAAkB;IAE5G,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,WAAW;aACZ,YAAY,EAAE,YAAY;gBAFzB,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,UAAU,CAAC,WAAW,EACpC,YAAY,GAAE,YAAqC;WAStD,MAAM,CACnB,EAAE,IAAS,EAAE,GAAG,MAAM,EAAE,EAAE,cAAc,GAAG;QAAE,IAAI,CAAC,EAAE,UAAU,CAAA;KAAE,EAChE,WAAW,GAAE,UAAU,CAAC,WAAgB,GACtC,UAAU;IAIN,YAAY,CAAC,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AAQ,CAAC,MAAM,CAAC,EAAE;YACjB,aAAa,EAAE,MAAM,CAAA;YACrB,YAAY,EAAE,MAAM,CAAA;SACpB,CAAA;KACD;IAEM,MAAM,eAAe,oDAAoD,CAAA;CAEhF;AAED,OAAO,EAAE,UAAU,EAAE,CAAA"}
|
|
@@ -1,70 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Connection = void 0;
|
|
4
|
-
const pg_1 = require("pg");
|
|
5
4
|
const EventManager_1 = require("./EventManager");
|
|
6
5
|
const Client_1 = require("./Client");
|
|
7
6
|
const Transaction_1 = require("./Transaction");
|
|
8
7
|
const execution_1 = require("./execution");
|
|
8
|
+
const Pool_1 = require("./Pool");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
9
10
|
class Connection {
|
|
10
|
-
constructor(
|
|
11
|
-
this.
|
|
11
|
+
constructor(pool, queryConfig, eventManager = new EventManager_1.EventManager(null)) {
|
|
12
|
+
this.pool = pool;
|
|
12
13
|
this.queryConfig = queryConfig;
|
|
13
14
|
this.eventManager = eventManager;
|
|
14
|
-
this.pool = new pg_1.Pool(config);
|
|
15
15
|
this.pool.on('error', err => {
|
|
16
16
|
// eslint-disable-next-line no-console
|
|
17
17
|
console.error(err);
|
|
18
18
|
this.eventManager.fire(EventManager_1.EventManager.Event.clientError, err);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
static create({ pool = {}, ...config }, queryConfig = {}) {
|
|
22
|
+
return new Connection(new Pool_1.Pool((0, utils_1.createPgClientFactory)(config), pool), queryConfig);
|
|
23
|
+
}
|
|
21
24
|
createClient(schema, queryMeta) {
|
|
22
25
|
return new Client_1.Client(this, schema, queryMeta, new EventManager_1.EventManager(this.eventManager));
|
|
23
26
|
}
|
|
27
|
+
async clearPool() {
|
|
28
|
+
await this.pool.closeIdle();
|
|
29
|
+
}
|
|
24
30
|
async transaction(callback, options = {}) {
|
|
25
31
|
var _a;
|
|
26
|
-
const
|
|
32
|
+
const acquired = await this.pool.acquire();
|
|
27
33
|
const eventManager = new EventManager_1.EventManager((_a = options.eventManager) !== null && _a !== void 0 ? _a : this.eventManager);
|
|
28
|
-
await (0, execution_1.executeQuery)(client, eventManager, {
|
|
34
|
+
await (0, execution_1.executeQuery)(acquired.client, eventManager, {
|
|
29
35
|
sql: 'BEGIN',
|
|
30
36
|
...this.queryConfig,
|
|
31
37
|
});
|
|
32
|
-
const transaction = new Transaction_1.Transaction(client, eventManager, this.queryConfig);
|
|
38
|
+
const transaction = new Transaction_1.Transaction(acquired.client, eventManager, this.queryConfig);
|
|
33
39
|
try {
|
|
34
40
|
const result = await callback(transaction);
|
|
35
41
|
await transaction.commitUnclosed();
|
|
36
|
-
|
|
42
|
+
this.pool.release(acquired);
|
|
37
43
|
return result;
|
|
38
44
|
}
|
|
39
45
|
catch (e) {
|
|
40
46
|
await transaction.rollbackUnclosed();
|
|
41
|
-
|
|
47
|
+
this.pool.dispose(acquired);
|
|
42
48
|
throw e;
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
async end() {
|
|
46
|
-
await
|
|
52
|
+
await this.pool.end();
|
|
47
53
|
}
|
|
48
54
|
async query(sql, parameters = [], meta = {}, { eventManager, ...config } = {}) {
|
|
49
|
-
const client = await
|
|
55
|
+
const client = await this.pool.acquire();
|
|
50
56
|
const query = { sql, parameters, meta, ...this.queryConfig, ...config };
|
|
51
57
|
try {
|
|
52
|
-
const result = await (0, execution_1.executeQuery)(client, eventManager !== null && eventManager !== void 0 ? eventManager : this.eventManager, query, {});
|
|
53
|
-
|
|
58
|
+
const result = await (0, execution_1.executeQuery)(client.client, eventManager !== null && eventManager !== void 0 ? eventManager : this.eventManager, query, {});
|
|
59
|
+
this.pool.release(client);
|
|
54
60
|
return result;
|
|
55
61
|
}
|
|
56
62
|
catch (e) {
|
|
57
|
-
|
|
63
|
+
this.pool.dispose(client);
|
|
58
64
|
throw e;
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
getPoolStatus() {
|
|
62
|
-
return
|
|
63
|
-
idleCount: this.pool.idleCount,
|
|
64
|
-
totalCount: this.pool.totalCount,
|
|
65
|
-
waitingCount: this.pool.waitingCount,
|
|
66
|
-
maxCount: this.config.max || 10,
|
|
67
|
-
};
|
|
68
|
+
return this.pool.getPoolStatus();
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
exports.Connection = Connection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Connection.js","sourceRoot":"","sources":["../../../src/client/Connection.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"Connection.js","sourceRoot":"","sources":["../../../src/client/Connection.ts"],"names":[],"mappings":";;;AAAA,iDAA6C;AAC7C,qCAAiC;AACjC,+CAA2C;AAC3C,2CAA0C;AAC1C,iCAAqD;AACrD,oCAAgD;AAGhD,MAAM,UAAU;IACf,YACkB,IAAU,EACV,WAAmC,EACpC,eAA6B,IAAI,2BAAY,CAAC,IAAI,CAAC;QAFlD,SAAI,GAAJ,IAAI,CAAM;QACV,gBAAW,GAAX,WAAW,CAAwB;QACpC,iBAAY,GAAZ,YAAY,CAAuC;QAEnE,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE;YAC3B,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YAClB,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,KAAK,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;QAC5D,CAAC,CAAC,CAAA;IACH,CAAC;IAEM,MAAM,CAAC,MAAM,CACnB,EAAE,IAAI,GAAG,EAAE,EAAE,GAAG,MAAM,EAA0C,EAChE,cAAsC,EAAE;QAExC,OAAO,IAAI,UAAU,CAAC,IAAI,WAAI,CAAC,IAAA,6BAAqB,EAAC,MAAM,CAAC,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,C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IAED,KAAK,CAAC,KAAK,CACV,GAAW,EACX,aAAoB,EAAE,EACtB,OAA4B,EAAE,EAC9B,EAAE,YAAY,EAAE,GAAG,MAAM,KAA6B,EAAE;QAExD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAA;QACxC,MAAM,KAAK,GAAqB,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,WAAW,EAAE,GAAG,MAAM,EAAE,CAAA;QACzF,IAAI;YACH,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAY,EAAM,MAAM,CAAC,MAAM,EAAE,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,IAAI,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA;YACnG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;YACzB,OAAO,MAAM,CAAA;SACb;QAAC,OAAO,CAAC,EAAE;YACX,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;YACzB,MAAM,CAAC,CAAA;SACP;IACF,CAAC;IAED,aAAa;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAA;IACjC,CAAC;CACD;AAwEQ,gCAAU;AAtEnB,WAAU,UAAU;IAkEN,0BAAe,GAAG,iDAAiD,CAAA;AAEjF,CAAC,EApES,UAAU,KAAV,UAAU,QAoEnB;AAEQ,gCAAU"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Client as PgClient } from 'pg';
|
|
3
|
+
import EventEmitter from 'events';
|
|
4
|
+
import { DatabaseError } from './errors';
|
|
5
|
+
import { PgClientFactory } from '../utils';
|
|
6
|
+
export declare type PoolLogger = (message: string, status: PoolStatus) => void;
|
|
7
|
+
export declare type PoolConfig = Partial<PoolConfigInternal>;
|
|
8
|
+
interface PoolConfigInternal {
|
|
9
|
+
/** maximum number of connections in a pool */
|
|
10
|
+
maxConnections: number;
|
|
11
|
+
/** maximum number of connections concurrently establishing*/
|
|
12
|
+
maxConnecting: number;
|
|
13
|
+
/** maximum number of idle connections. when reached, will be disposed immediately */
|
|
14
|
+
maxIdle: number;
|
|
15
|
+
/** interval between retries of recoverable errors */
|
|
16
|
+
reconnectIntervalMs: number;
|
|
17
|
+
/** timeout to dispose idle connections */
|
|
18
|
+
idleTimeoutMs: number;
|
|
19
|
+
/** timeout of pending item waiting for a connection. also used for retries of recoverable errors */
|
|
20
|
+
acquireTimeoutMs: number;
|
|
21
|
+
/** how many times can a single connection be acquired */
|
|
22
|
+
maxUses?: number;
|
|
23
|
+
/** max age of connection. when reached, connection will be disposed after returning to a pool */
|
|
24
|
+
maxAgeMs?: number;
|
|
25
|
+
/** internal logging, mainly for debugging and testing */
|
|
26
|
+
log?: PoolLogger;
|
|
27
|
+
}
|
|
28
|
+
export declare type PoolStats = {
|
|
29
|
+
[K in keyof typeof poolStatsDescription]: number;
|
|
30
|
+
};
|
|
31
|
+
export interface PoolStatus {
|
|
32
|
+
/** maximum number of connections in a pool */
|
|
33
|
+
max: number;
|
|
34
|
+
/** number of items waiting for a connections */
|
|
35
|
+
pending: number;
|
|
36
|
+
/** number of active (acquired) connections */
|
|
37
|
+
active: number;
|
|
38
|
+
/** number of available idle connections */
|
|
39
|
+
idle: number;
|
|
40
|
+
/** number of currently establishing connections */
|
|
41
|
+
connecting: number;
|
|
42
|
+
stats: PoolStats;
|
|
43
|
+
}
|
|
44
|
+
export declare const poolStatsDescription: {
|
|
45
|
+
connection_established_count: string;
|
|
46
|
+
connection_recoverable_error_count: string;
|
|
47
|
+
connection_error_count: string;
|
|
48
|
+
connection_disposed_idle_timeout_count: string;
|
|
49
|
+
connection_disposed_idle_max_count: string;
|
|
50
|
+
connection_disposed_old_count: string;
|
|
51
|
+
connection_disposed_manual_count: string;
|
|
52
|
+
connection_released_count: string;
|
|
53
|
+
connection_reused_immediately_count: string;
|
|
54
|
+
connection_reused_later_count: string;
|
|
55
|
+
connection_established_idle_count: string;
|
|
56
|
+
item_queued_count: string;
|
|
57
|
+
item_timeout_count: string;
|
|
58
|
+
item_resolved_new_ms_count: string;
|
|
59
|
+
item_resolved_new_ms_sum: string;
|
|
60
|
+
item_resolved_released_ms_count: string;
|
|
61
|
+
item_resolved_released_ms_sum: string;
|
|
62
|
+
};
|
|
63
|
+
declare class PoolConnection {
|
|
64
|
+
readonly client: PgClient;
|
|
65
|
+
disposed: boolean;
|
|
66
|
+
uses: number;
|
|
67
|
+
createdAt: number;
|
|
68
|
+
timerId: NodeJS.Timeout | undefined;
|
|
69
|
+
constructor(client: PgClient);
|
|
70
|
+
}
|
|
71
|
+
interface Pool {
|
|
72
|
+
on(event: 'error', listener: (err: Error) => void): this;
|
|
73
|
+
on(event: 'recoverableError', listener: (err: Error) => void): this;
|
|
74
|
+
}
|
|
75
|
+
declare class Pool extends EventEmitter {
|
|
76
|
+
private clientFactory;
|
|
77
|
+
/**
|
|
78
|
+
* pool was closed by calling end().
|
|
79
|
+
* All connection was disposed and it is not possible to acquire new one
|
|
80
|
+
*/
|
|
81
|
+
private ended;
|
|
82
|
+
/**
|
|
83
|
+
* current count of connection attempts
|
|
84
|
+
* also includes timeout between recoverable retries
|
|
85
|
+
*/
|
|
86
|
+
private connectingCount;
|
|
87
|
+
/**
|
|
88
|
+
* available idle connections
|
|
89
|
+
*/
|
|
90
|
+
private idle;
|
|
91
|
+
/**
|
|
92
|
+
* currently acquired connections
|
|
93
|
+
*/
|
|
94
|
+
private active;
|
|
95
|
+
/**
|
|
96
|
+
* items waiting for released or newly established connection
|
|
97
|
+
*/
|
|
98
|
+
private queue;
|
|
99
|
+
private poolConfig;
|
|
100
|
+
private poolStats;
|
|
101
|
+
private lastRecoverableError;
|
|
102
|
+
constructor(clientFactory: PgClientFactory, poolConfig: PoolConfig);
|
|
103
|
+
acquire(): Promise<PoolConnection>;
|
|
104
|
+
release(poolConnection: PoolConnection): void;
|
|
105
|
+
dispose(poolConnection: PoolConnection): void;
|
|
106
|
+
end(): Promise<void>;
|
|
107
|
+
closeIdle(): Promise<void>;
|
|
108
|
+
getPoolStatus(): PoolStatus;
|
|
109
|
+
private maybeCreateNew;
|
|
110
|
+
private createPendingItem;
|
|
111
|
+
private handleAvailableConnection;
|
|
112
|
+
private disposeConnection;
|
|
113
|
+
private log;
|
|
114
|
+
}
|
|
115
|
+
export { Pool };
|
|
116
|
+
export declare class PoolError extends DatabaseError {
|
|
117
|
+
}
|
|
118
|
+
export declare class AcquireTimeoutError extends PoolError {
|
|
119
|
+
constructor(previous: any);
|
|
120
|
+
}
|
|
121
|
+
export declare class PoolClosedError extends PoolError {
|
|
122
|
+
constructor();
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=Pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Pool.d.ts","sourceRoot":"","sources":["../../../src/client/Pool.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,IAAI,CAAA;AAEvC,OAAO,YAAY,MAAM,QAAQ,CAAA;AACjC,OAAO,EAAe,aAAa,EAAE,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAE1C,oBAAY,UAAU,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,KAAK,IAAI,CAAA;AAEtE,oBAAY,UAAU,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAA;AAEpD,UAAU,kBAAkB;IAC3B,8CAA8C;IAC9C,cAAc,EAAE,MAAM,CAAA;IACtB,6DAA6D;IAC7D,aAAa,EAAE,MAAM,CAAA;IACrB,qFAAqF;IACrF,OAAO,EAAE,MAAM,CAAA;IACf,qDAAqD;IACrD,mBAAmB,EAAE,MAAM,CAAA;IAC3B,0CAA0C;IAC1C,aAAa,EAAE,MAAM,CAAA;IACrB,oGAAoG;IACpG,gBAAgB,EA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