postgrejs 2.22.7 → 2.23.0
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/CHANGELOG.md +20 -1
- package/{esm/connection → connection}/bind-param.js +2 -0
- package/{esm/connection → connection}/connection.js +4 -2
- package/{esm/connection → connection}/cursor.js +8 -3
- package/{esm/connection → connection}/intl-connection.js +6 -3
- package/{esm/connection → connection}/pool.js +4 -1
- package/{esm/connection → connection}/portal.js +3 -1
- package/{esm/connection → connection}/prepared-statement.js +6 -3
- package/{esm/constants.js → constants.js} +1 -1
- package/{esm/data-type-map.js → data-type-map.js} +2 -2
- package/package.json +13 -32
- package/{esm/protocol → protocol}/backend.js +1 -0
- package/{esm/protocol → protocol}/buffer-reader.js +2 -1
- package/{esm/protocol → protocol}/database-error.js +16 -0
- package/{esm/protocol → protocol}/frontend.js +1 -0
- package/{esm/protocol → protocol}/pg-socket.js +9 -3
- package/{types/protocol → protocol}/smart-buffer.d.ts +1 -0
- package/{esm/protocol → protocol}/smart-buffer.js +29 -19
- package/{esm/util → util}/bigint-methods.js +1 -0
- package/cjs/connection/bind-param.js +0 -10
- package/cjs/connection/connection.js +0 -295
- package/cjs/connection/cursor.js +0 -91
- package/cjs/connection/intl-connection.js +0 -258
- package/cjs/connection/pool.js +0 -236
- package/cjs/connection/portal.js +0 -157
- package/cjs/connection/prepared-statement.js +0 -226
- package/cjs/constants.js +0 -249
- package/cjs/data-type-map.js +0 -131
- package/cjs/data-types/bool-type.js +0 -33
- package/cjs/data-types/box-type.js +0 -52
- package/cjs/data-types/bytea-type.js +0 -28
- package/cjs/data-types/char-type.js +0 -27
- package/cjs/data-types/circle-type.js +0 -51
- package/cjs/data-types/date-type.js +0 -75
- package/cjs/data-types/float4-type.js +0 -25
- package/cjs/data-types/float8-type.js +0 -25
- package/cjs/data-types/int2-type.js +0 -26
- package/cjs/data-types/int2-vector-type.js +0 -42
- package/cjs/data-types/int4-type.js +0 -28
- package/cjs/data-types/int8-type.js +0 -36
- package/cjs/data-types/json-type.js +0 -40
- package/cjs/data-types/jsonb-type.js +0 -44
- package/cjs/data-types/lseg-type.js +0 -55
- package/cjs/data-types/numeric-type.js +0 -125
- package/cjs/data-types/oid-type.js +0 -32
- package/cjs/data-types/point-type.js +0 -42
- package/cjs/data-types/time-type.js +0 -66
- package/cjs/data-types/timestamp-type.js +0 -89
- package/cjs/data-types/timestamptz-type.js +0 -76
- package/cjs/data-types/uuid-type.js +0 -39
- package/cjs/data-types/varchar-type.js +0 -27
- package/cjs/index.js +0 -28
- package/cjs/interfaces/command-result.js +0 -2
- package/cjs/interfaces/data-mapping-options.js +0 -2
- package/cjs/interfaces/data-type.js +0 -2
- package/cjs/interfaces/database-connection-params.js +0 -2
- package/cjs/interfaces/field-info.js +0 -2
- package/cjs/interfaces/query-options.js +0 -2
- package/cjs/interfaces/query-result.js +0 -2
- package/cjs/interfaces/script-execute-options.js +0 -2
- package/cjs/interfaces/script-result.js +0 -2
- package/cjs/interfaces/statement-prepare-options.js +0 -2
- package/cjs/package.json +0 -3
- package/cjs/protocol/backend.js +0 -262
- package/cjs/protocol/buffer-reader.js +0 -78
- package/cjs/protocol/database-error.js +0 -17
- package/cjs/protocol/frontend.js +0 -255
- package/cjs/protocol/pg-socket.js +0 -367
- package/cjs/protocol/protocol.js +0 -71
- package/cjs/protocol/sasl.js +0 -108
- package/cjs/protocol/smart-buffer.js +0 -172
- package/cjs/safe-event-emitter.js +0 -17
- package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
- package/cjs/types.js +0 -2
- package/cjs/util/array-calculatedim.js +0 -18
- package/cjs/util/bigint-methods.js +0 -44
- package/cjs/util/config-from-env.js +0 -29
- package/cjs/util/connection-config.js +0 -61
- package/cjs/util/convert-row-to-object.js +0 -12
- package/cjs/util/decode-binaryarray.js +0 -38
- package/cjs/util/encode-binaryarray.js +0 -44
- package/cjs/util/escape-literal.js +0 -26
- package/cjs/util/fast-parseint.js +0 -31
- package/cjs/util/get-parsers.js +0 -41
- package/cjs/util/parse-array.js +0 -60
- package/cjs/util/parse-datetime.js +0 -37
- package/cjs/util/parse-row.js +0 -11
- package/cjs/util/parse-time.js +0 -30
- package/cjs/util/stringify-arrayliteral.js +0 -37
- package/cjs/util/stringify-for-sql.js +0 -29
- package/cjs/util/wrap-row-description.js +0 -34
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -25
- /package/{types/connection → connection}/bind-param.d.ts +0 -0
- /package/{types/connection → connection}/connection.d.ts +0 -0
- /package/{types/connection → connection}/cursor.d.ts +0 -0
- /package/{types/connection → connection}/intl-connection.d.ts +0 -0
- /package/{types/connection → connection}/pool.d.ts +0 -0
- /package/{types/connection → connection}/portal.d.ts +0 -0
- /package/{types/connection → connection}/prepared-statement.d.ts +0 -0
- /package/{types/constants.d.ts → constants.d.ts} +0 -0
- /package/{types/data-type-map.d.ts → data-type-map.d.ts} +0 -0
- /package/{types/data-types → data-types}/bool-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/bool-type.js +0 -0
- /package/{types/data-types → data-types}/box-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/box-type.js +0 -0
- /package/{types/data-types → data-types}/bytea-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/bytea-type.js +0 -0
- /package/{types/data-types → data-types}/char-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/char-type.js +0 -0
- /package/{types/data-types → data-types}/circle-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/circle-type.js +0 -0
- /package/{types/data-types → data-types}/date-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/date-type.js +0 -0
- /package/{types/data-types → data-types}/float4-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/float4-type.js +0 -0
- /package/{types/data-types → data-types}/float8-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/float8-type.js +0 -0
- /package/{types/data-types → data-types}/int2-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/int2-type.js +0 -0
- /package/{types/data-types → data-types}/int2-vector-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/int2-vector-type.js +0 -0
- /package/{types/data-types → data-types}/int4-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/int4-type.js +0 -0
- /package/{types/data-types → data-types}/int8-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/int8-type.js +0 -0
- /package/{types/data-types → data-types}/json-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/json-type.js +0 -0
- /package/{types/data-types → data-types}/jsonb-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/jsonb-type.js +0 -0
- /package/{types/data-types → data-types}/lseg-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/lseg-type.js +0 -0
- /package/{types/data-types → data-types}/numeric-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/numeric-type.js +0 -0
- /package/{types/data-types → data-types}/oid-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/oid-type.js +0 -0
- /package/{types/data-types → data-types}/point-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/point-type.js +0 -0
- /package/{types/data-types → data-types}/time-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/time-type.js +0 -0
- /package/{types/data-types → data-types}/timestamp-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/timestamp-type.js +0 -0
- /package/{types/data-types → data-types}/timestamptz-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/timestamptz-type.js +0 -0
- /package/{types/data-types → data-types}/uuid-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/uuid-type.js +0 -0
- /package/{types/data-types → data-types}/varchar-type.d.ts +0 -0
- /package/{esm/data-types → data-types}/varchar-type.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/interfaces → interfaces}/command-result.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/command-result.js +0 -0
- /package/{types/interfaces → interfaces}/data-mapping-options.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/data-mapping-options.js +0 -0
- /package/{types/interfaces → interfaces}/data-type.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/data-type.js +0 -0
- /package/{types/interfaces → interfaces}/database-connection-params.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/database-connection-params.js +0 -0
- /package/{types/interfaces → interfaces}/field-info.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/field-info.js +0 -0
- /package/{types/interfaces → interfaces}/query-options.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/query-options.js +0 -0
- /package/{types/interfaces → interfaces}/query-result.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/query-result.js +0 -0
- /package/{types/interfaces → interfaces}/script-execute-options.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/script-execute-options.js +0 -0
- /package/{types/interfaces → interfaces}/script-result.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/script-result.js +0 -0
- /package/{types/interfaces → interfaces}/statement-prepare-options.d.ts +0 -0
- /package/{esm/interfaces → interfaces}/statement-prepare-options.js +0 -0
- /package/{types/protocol → protocol}/backend.d.ts +0 -0
- /package/{types/protocol → protocol}/buffer-reader.d.ts +0 -0
- /package/{types/protocol → protocol}/database-error.d.ts +0 -0
- /package/{types/protocol → protocol}/frontend.d.ts +0 -0
- /package/{types/protocol → protocol}/pg-socket.d.ts +0 -0
- /package/{types/protocol → protocol}/protocol.d.ts +0 -0
- /package/{esm/protocol → protocol}/protocol.js +0 -0
- /package/{types/protocol → protocol}/sasl.d.ts +0 -0
- /package/{esm/protocol → protocol}/sasl.js +0 -0
- /package/{types/safe-event-emitter.d.ts → safe-event-emitter.d.ts} +0 -0
- /package/{esm/safe-event-emitter.js → safe-event-emitter.js} +0 -0
- /package/{types/types.d.ts → types.d.ts} +0 -0
- /package/{esm/types.js → types.js} +0 -0
- /package/{types/util → util}/array-calculatedim.d.ts +0 -0
- /package/{esm/util → util}/array-calculatedim.js +0 -0
- /package/{types/util → util}/bigint-methods.d.ts +0 -0
- /package/{types/util → util}/config-from-env.d.ts +0 -0
- /package/{esm/util → util}/config-from-env.js +0 -0
- /package/{types/util → util}/connection-config.d.ts +0 -0
- /package/{esm/util → util}/connection-config.js +0 -0
- /package/{types/util → util}/convert-row-to-object.d.ts +0 -0
- /package/{esm/util → util}/convert-row-to-object.js +0 -0
- /package/{types/util → util}/decode-binaryarray.d.ts +0 -0
- /package/{esm/util → util}/decode-binaryarray.js +0 -0
- /package/{types/util → util}/encode-binaryarray.d.ts +0 -0
- /package/{esm/util → util}/encode-binaryarray.js +0 -0
- /package/{types/util → util}/escape-literal.d.ts +0 -0
- /package/{esm/util → util}/escape-literal.js +0 -0
- /package/{types/util → util}/fast-parseint.d.ts +0 -0
- /package/{esm/util → util}/fast-parseint.js +0 -0
- /package/{types/util → util}/get-parsers.d.ts +0 -0
- /package/{esm/util → util}/get-parsers.js +0 -0
- /package/{types/util → util}/parse-array.d.ts +0 -0
- /package/{esm/util → util}/parse-array.js +0 -0
- /package/{types/util → util}/parse-datetime.d.ts +0 -0
- /package/{esm/util → util}/parse-datetime.js +0 -0
- /package/{types/util → util}/parse-row.d.ts +0 -0
- /package/{esm/util → util}/parse-row.js +0 -0
- /package/{types/util → util}/parse-time.d.ts +0 -0
- /package/{esm/util → util}/parse-time.js +0 -0
- /package/{types/util → util}/stringify-arrayliteral.d.ts +0 -0
- /package/{esm/util → util}/stringify-arrayliteral.js +0 -0
- /package/{types/util → util}/stringify-for-sql.d.ts +0 -0
- /package/{esm/util → util}/stringify-for-sql.js +0 -0
- /package/{types/util → util}/wrap-row-description.d.ts +0 -0
- /package/{esm/util → util}/wrap-row-description.js +0 -0
package/cjs/connection/pool.js
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Pool = void 0;
|
|
4
|
-
const lightning_pool_1 = require("lightning-pool");
|
|
5
|
-
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
6
|
-
const constants_js_1 = require("../constants.js");
|
|
7
|
-
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
|
|
8
|
-
const connection_config_js_1 = require("../util/connection-config.js");
|
|
9
|
-
const connection_js_1 = require("./connection.js");
|
|
10
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
11
|
-
class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
12
|
-
constructor(config) {
|
|
13
|
-
super();
|
|
14
|
-
this._notificationListeners = new safe_event_emitter_js_1.SafeEventEmitter();
|
|
15
|
-
const cfg = (0, connection_config_js_1.getConnectionConfig)(config);
|
|
16
|
-
this.config = Object.freeze(cfg);
|
|
17
|
-
const poolOptions = {};
|
|
18
|
-
poolOptions.acquireMaxRetries = (0, putil_varhelpers_1.coerceToInt)(cfg.acquireMaxRetries, 0);
|
|
19
|
-
poolOptions.acquireRetryWait = (0, putil_varhelpers_1.coerceToInt)(cfg.acquireRetryWait, 2000);
|
|
20
|
-
poolOptions.acquireTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(cfg.acquireTimeoutMillis, 0);
|
|
21
|
-
poolOptions.idleTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(cfg.idleTimeoutMillis, 30000);
|
|
22
|
-
poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(cfg.max, 10);
|
|
23
|
-
poolOptions.maxQueue = (0, putil_varhelpers_1.coerceToInt)(cfg.maxQueue, 1000);
|
|
24
|
-
poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(cfg.max, 10);
|
|
25
|
-
poolOptions.min = (0, putil_varhelpers_1.coerceToInt)(cfg.min, 0);
|
|
26
|
-
poolOptions.minIdle = (0, putil_varhelpers_1.coerceToInt)(cfg.minIdle, 0);
|
|
27
|
-
poolOptions.validation = (0, putil_varhelpers_1.coerceToBoolean)(cfg.validation, false);
|
|
28
|
-
const poolFactory = {
|
|
29
|
-
create: async () => {
|
|
30
|
-
/* istanbul ignore next */
|
|
31
|
-
if (this.listenerCount('debug')) {
|
|
32
|
-
this.emit('debug', {
|
|
33
|
-
location: 'Pool.factory.create',
|
|
34
|
-
pool: this,
|
|
35
|
-
message: `new connection creating`,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
const intlCon = new intl_connection_js_1.IntlConnection(cfg);
|
|
39
|
-
await intlCon.connect();
|
|
40
|
-
intlCon.on('close', () => this._pool.destroy(intlCon));
|
|
41
|
-
/* istanbul ignore next */
|
|
42
|
-
if (this.listenerCount('debug')) {
|
|
43
|
-
this.emit('debug', {
|
|
44
|
-
location: 'Pool.factory.create',
|
|
45
|
-
pool: this,
|
|
46
|
-
message: `[${intlCon.processID}] connection created`,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
return intlCon;
|
|
50
|
-
},
|
|
51
|
-
destroy: intlCon => {
|
|
52
|
-
/* istanbul ignore next */
|
|
53
|
-
if (this.listenerCount('debug')) {
|
|
54
|
-
this.emit('debug', {
|
|
55
|
-
location: 'Pool.factory.destroy',
|
|
56
|
-
pool: this,
|
|
57
|
-
message: `[${intlCon.processID}] connection destroy`,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
return intlCon.close();
|
|
61
|
-
},
|
|
62
|
-
reset: async (intlCon) => {
|
|
63
|
-
/* istanbul ignore next */
|
|
64
|
-
if (this.listenerCount('debug')) {
|
|
65
|
-
this.emit('debug', {
|
|
66
|
-
location: 'Pool.factory.reset',
|
|
67
|
-
pool: this,
|
|
68
|
-
message: `[${intlCon.processID}] connection reset`,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
if (intlCon.state === constants_js_1.ConnectionState.READY) {
|
|
73
|
-
await intlCon.execute('ROLLBACK;UNLISTEN *');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
finally {
|
|
77
|
-
intlCon.removeAllListeners();
|
|
78
|
-
intlCon.once('close', () => this._pool.destroy(intlCon));
|
|
79
|
-
intlCon._refCount = 0;
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
validate: async (intlCon) => {
|
|
83
|
-
/* istanbul ignore next */
|
|
84
|
-
if (this.listenerCount('debug')) {
|
|
85
|
-
this.emit('debug', {
|
|
86
|
-
location: 'Pool.factory.validate',
|
|
87
|
-
pool: this,
|
|
88
|
-
message: `[${intlCon.processID}] connection validate`,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (intlCon.state !== constants_js_1.ConnectionState.READY)
|
|
92
|
-
throw new Error('Connection is not active');
|
|
93
|
-
await intlCon.execute('select 1;');
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
this._pool = new lightning_pool_1.Pool(poolFactory, poolOptions);
|
|
97
|
-
this._pool.on('return', (...args) => this.emit('release', ...args));
|
|
98
|
-
this._pool.on('error', (...args) => this.emit('error', ...args));
|
|
99
|
-
this._pool.on('acquire', (...args) => this.emit('acquire', ...args));
|
|
100
|
-
this._pool.on('destroy', (...args) => this.emit('destroy', ...args));
|
|
101
|
-
this._pool.start();
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Returns number of connections that are currently acquired
|
|
105
|
-
*/
|
|
106
|
-
get acquiredConnections() {
|
|
107
|
-
return this._pool.acquired;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Returns number of unused connections in the pool
|
|
111
|
-
*/
|
|
112
|
-
get idleConnections() {
|
|
113
|
-
return this._pool.available;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Returns total number of connections in the pool regardless of whether they are idle or in use
|
|
117
|
-
*/
|
|
118
|
-
get totalConnections() {
|
|
119
|
-
return this._pool.size;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Obtains a connection from the connection pool
|
|
123
|
-
*/
|
|
124
|
-
async acquire() {
|
|
125
|
-
const intlCon = await this._pool.acquire();
|
|
126
|
-
/* istanbul ignore next */
|
|
127
|
-
if (this.listenerCount('debug')) {
|
|
128
|
-
this.emit('debug', {
|
|
129
|
-
location: 'Pool.acquire',
|
|
130
|
-
pool: this,
|
|
131
|
-
message: `[${intlCon.processID}] acquired`,
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
const connection = new connection_js_1.Connection(this, intlCon);
|
|
135
|
-
/* istanbul ignore next */
|
|
136
|
-
if (this.listenerCount('debug'))
|
|
137
|
-
connection.on('debug', (...args) => this.emit('debug', ...args));
|
|
138
|
-
if (this.listenerCount('execute'))
|
|
139
|
-
connection.on('execute', (...args) => this.emit('execute', ...args));
|
|
140
|
-
if (this.listenerCount('query'))
|
|
141
|
-
connection.on('query', (...args) => this.emit('query', ...args));
|
|
142
|
-
return connection;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Shuts down the pool and destroys all resources.
|
|
146
|
-
*/
|
|
147
|
-
async close(terminateWait) {
|
|
148
|
-
this._notificationListeners.removeAllListeners();
|
|
149
|
-
await this._notificationConnection?.close(terminateWait);
|
|
150
|
-
const ms = terminateWait == null ? 10000 : terminateWait;
|
|
151
|
-
return this._pool.closeAsync(ms);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Executes a script
|
|
155
|
-
*/
|
|
156
|
-
async execute(sql, options) {
|
|
157
|
-
const connection = await this.acquire();
|
|
158
|
-
try {
|
|
159
|
-
return await connection.execute(sql, options);
|
|
160
|
-
}
|
|
161
|
-
finally {
|
|
162
|
-
await this.release(connection);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Executes a query
|
|
167
|
-
*/
|
|
168
|
-
async query(sql, options) {
|
|
169
|
-
const connection = await this.acquire();
|
|
170
|
-
try {
|
|
171
|
-
return await connection.query(sql, options);
|
|
172
|
-
}
|
|
173
|
-
finally {
|
|
174
|
-
await this.release(connection);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
async prepare(sql, options) {
|
|
178
|
-
const connection = await this.acquire();
|
|
179
|
-
const statement = await connection.prepare(sql, options);
|
|
180
|
-
statement.once('close', () => this._pool.release((0, intl_connection_js_1.getIntlConnection)(connection)));
|
|
181
|
-
return statement;
|
|
182
|
-
}
|
|
183
|
-
release(connection) {
|
|
184
|
-
return this._pool.releaseAsync((0, intl_connection_js_1.getIntlConnection)(connection));
|
|
185
|
-
}
|
|
186
|
-
async listen(channel, callback) {
|
|
187
|
-
if (!/^[A-Z]\w+$/i.test(channel))
|
|
188
|
-
throw new TypeError(`Invalid channel name`);
|
|
189
|
-
this._notificationListeners.on(channel, callback);
|
|
190
|
-
await this._initNotificationConnection();
|
|
191
|
-
}
|
|
192
|
-
async unListen(channel) {
|
|
193
|
-
if (!/^[A-Z]\w+$/i.test(channel))
|
|
194
|
-
throw new TypeError(`Invalid channel name`);
|
|
195
|
-
this._notificationListeners.removeAllListeners(channel);
|
|
196
|
-
if (!this._notificationListeners.eventNames().length) {
|
|
197
|
-
await this.unListenAll();
|
|
198
|
-
}
|
|
199
|
-
else if (this._notificationConnection)
|
|
200
|
-
await this._notificationConnection.unListen(channel);
|
|
201
|
-
}
|
|
202
|
-
async unListenAll() {
|
|
203
|
-
this._notificationListeners.removeAllListeners();
|
|
204
|
-
if (this._notificationConnection) {
|
|
205
|
-
const conn = this._notificationConnection;
|
|
206
|
-
this._notificationConnection = undefined;
|
|
207
|
-
await conn.close();
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
async _initNotificationConnection() {
|
|
211
|
-
if (this._notificationConnection)
|
|
212
|
-
return;
|
|
213
|
-
const conn = (this._notificationConnection = new connection_js_1.Connection(this.config));
|
|
214
|
-
// Reconnect on connection lost
|
|
215
|
-
conn.on('close', () => reConnect());
|
|
216
|
-
const registerEvents = async () => {
|
|
217
|
-
const channels = this._notificationListeners.eventNames();
|
|
218
|
-
for (const channel of channels) {
|
|
219
|
-
const fns = this._notificationListeners.listeners(channel);
|
|
220
|
-
for (const fn of fns) {
|
|
221
|
-
await conn.listen(channel, fn);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
const reConnect = async () => {
|
|
226
|
-
setTimeout(() => {
|
|
227
|
-
if (!this._notificationListeners.eventNames().length)
|
|
228
|
-
return;
|
|
229
|
-
conn.connect().catch(() => reConnect());
|
|
230
|
-
}, 500).unref();
|
|
231
|
-
};
|
|
232
|
-
await conn.connect();
|
|
233
|
-
await registerEvents();
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
exports.Pool = Pool;
|
package/cjs/connection/portal.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Portal = void 0;
|
|
4
|
-
const constants_js_1 = require("../constants.js");
|
|
5
|
-
const data_type_map_js_1 = require("../data-type-map.js");
|
|
6
|
-
const protocol_js_1 = require("../protocol/protocol.js");
|
|
7
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
8
|
-
class Portal {
|
|
9
|
-
constructor(statement, name) {
|
|
10
|
-
this._columnFormat = constants_js_1.DEFAULT_COLUMN_FORMAT;
|
|
11
|
-
this._statement = statement;
|
|
12
|
-
this._name = name;
|
|
13
|
-
}
|
|
14
|
-
get connection() {
|
|
15
|
-
return this._statement.connection;
|
|
16
|
-
}
|
|
17
|
-
get name() {
|
|
18
|
-
return this._name;
|
|
19
|
-
}
|
|
20
|
-
async bind(params, queryOptions) {
|
|
21
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
22
|
-
intoCon.ref();
|
|
23
|
-
try {
|
|
24
|
-
const socket = intoCon.socket;
|
|
25
|
-
this._columnFormat =
|
|
26
|
-
queryOptions.columnFormat != null
|
|
27
|
-
? queryOptions.columnFormat
|
|
28
|
-
: protocol_js_1.Protocol.DataFormat.binary;
|
|
29
|
-
socket.sendBindMessage({
|
|
30
|
-
typeMap: queryOptions.typeMap || data_type_map_js_1.GlobalTypeMap,
|
|
31
|
-
statement: this._statement.name,
|
|
32
|
-
portal: this.name,
|
|
33
|
-
paramTypes: this._statement.paramTypes,
|
|
34
|
-
params,
|
|
35
|
-
queryOptions,
|
|
36
|
-
});
|
|
37
|
-
socket.sendFlushMessage();
|
|
38
|
-
return await socket.capture(async (code, msg, done) => {
|
|
39
|
-
switch (code) {
|
|
40
|
-
case protocol_js_1.Protocol.BackendMessageCode.BindComplete:
|
|
41
|
-
done();
|
|
42
|
-
break;
|
|
43
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
|
|
44
|
-
break;
|
|
45
|
-
default:
|
|
46
|
-
done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`));
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
finally {
|
|
51
|
-
intoCon.unref();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async retrieveFields() {
|
|
55
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
56
|
-
intoCon.ref();
|
|
57
|
-
try {
|
|
58
|
-
const socket = intoCon.socket;
|
|
59
|
-
socket.sendDescribeMessage({ type: 'P', name: this.name });
|
|
60
|
-
socket.sendFlushMessage();
|
|
61
|
-
return await socket.capture(async (code, msg, done) => {
|
|
62
|
-
switch (code) {
|
|
63
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
|
|
64
|
-
break;
|
|
65
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoData:
|
|
66
|
-
done();
|
|
67
|
-
break;
|
|
68
|
-
case protocol_js_1.Protocol.BackendMessageCode.RowDescription:
|
|
69
|
-
done(undefined, msg.fields);
|
|
70
|
-
break;
|
|
71
|
-
default:
|
|
72
|
-
done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`));
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
finally {
|
|
77
|
-
intoCon.unref();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
async execute(fetchCount) {
|
|
81
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
82
|
-
intoCon.ref();
|
|
83
|
-
try {
|
|
84
|
-
const socket = intoCon.socket;
|
|
85
|
-
socket.sendExecuteMessage({
|
|
86
|
-
portal: this.name,
|
|
87
|
-
fetchCount: fetchCount || 100,
|
|
88
|
-
});
|
|
89
|
-
socket.sendFlushMessage();
|
|
90
|
-
const rows = [];
|
|
91
|
-
return await socket.capture(async (code, msg, done) => {
|
|
92
|
-
switch (code) {
|
|
93
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
|
|
94
|
-
break;
|
|
95
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoData:
|
|
96
|
-
done(undefined, { code });
|
|
97
|
-
break;
|
|
98
|
-
case protocol_js_1.Protocol.BackendMessageCode.DataRow:
|
|
99
|
-
if (Array.isArray(this._columnFormat)) {
|
|
100
|
-
rows.push(msg.columns.map((buf, i) => this._columnFormat[i] ===
|
|
101
|
-
protocol_js_1.Protocol.DataFormat.text
|
|
102
|
-
? buf.toString('utf8')
|
|
103
|
-
: buf));
|
|
104
|
-
}
|
|
105
|
-
else if (this._columnFormat === protocol_js_1.Protocol.DataFormat.binary)
|
|
106
|
-
rows.push(msg.columns);
|
|
107
|
-
else
|
|
108
|
-
rows.push(msg.columns.map((buf) => buf.toString('utf8')));
|
|
109
|
-
break;
|
|
110
|
-
case protocol_js_1.Protocol.BackendMessageCode.PortalSuspended:
|
|
111
|
-
done(undefined, { code, rows });
|
|
112
|
-
break;
|
|
113
|
-
case protocol_js_1.Protocol.BackendMessageCode.CommandComplete:
|
|
114
|
-
done(undefined, {
|
|
115
|
-
code,
|
|
116
|
-
rows,
|
|
117
|
-
command: msg.command,
|
|
118
|
-
rowCount: msg.rowCount,
|
|
119
|
-
});
|
|
120
|
-
break;
|
|
121
|
-
default:
|
|
122
|
-
done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`));
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
finally {
|
|
127
|
-
intoCon.unref();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
async close() {
|
|
131
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
132
|
-
intoCon.ref();
|
|
133
|
-
try {
|
|
134
|
-
const socket = intoCon.socket;
|
|
135
|
-
socket.sendCloseMessage({ type: 'P', name: this.name });
|
|
136
|
-
socket.sendSyncMessage();
|
|
137
|
-
return await socket.capture(async (code, msg, done) => {
|
|
138
|
-
switch (code) {
|
|
139
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
|
|
140
|
-
break;
|
|
141
|
-
case protocol_js_1.Protocol.BackendMessageCode.CloseComplete:
|
|
142
|
-
break;
|
|
143
|
-
case protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery:
|
|
144
|
-
intoCon.transactionStatus = msg.status;
|
|
145
|
-
done();
|
|
146
|
-
break;
|
|
147
|
-
default:
|
|
148
|
-
done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`));
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
153
|
-
intoCon.unref();
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.Portal = Portal;
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PreparedStatement = void 0;
|
|
4
|
-
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
5
|
-
const constants_js_1 = require("../constants.js");
|
|
6
|
-
const data_type_map_js_1 = require("../data-type-map.js");
|
|
7
|
-
const protocol_js_1 = require("../protocol/protocol.js");
|
|
8
|
-
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
|
|
9
|
-
const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
|
|
10
|
-
const get_parsers_js_1 = require("../util/get-parsers.js");
|
|
11
|
-
const parse_row_js_1 = require("../util/parse-row.js");
|
|
12
|
-
const wrap_row_description_js_1 = require("../util/wrap-row-description.js");
|
|
13
|
-
const cursor_js_1 = require("./cursor.js");
|
|
14
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
15
|
-
const portal_js_1 = require("./portal.js");
|
|
16
|
-
let statementCounter = 0;
|
|
17
|
-
let portalCounter = 0;
|
|
18
|
-
class PreparedStatement extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
19
|
-
constructor(connection, sql, paramTypes) {
|
|
20
|
-
super();
|
|
21
|
-
this._sql = '';
|
|
22
|
-
this._name = '';
|
|
23
|
-
this._refCount = 0;
|
|
24
|
-
this._connection = connection;
|
|
25
|
-
this._name = 'S_' + ++statementCounter;
|
|
26
|
-
this._sql = sql;
|
|
27
|
-
this._paramTypes = paramTypes;
|
|
28
|
-
this._onErrorSavePoint = 'SP_' + Math.round(Math.random() * 100000000);
|
|
29
|
-
}
|
|
30
|
-
static async prepare(connection, sql, options) {
|
|
31
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(connection);
|
|
32
|
-
intoCon.assertConnected();
|
|
33
|
-
const socket = intoCon.socket;
|
|
34
|
-
const statement = new PreparedStatement(connection, sql, options?.paramTypes);
|
|
35
|
-
await intoCon.statementQueue
|
|
36
|
-
.enqueue(async () => {
|
|
37
|
-
intoCon.ref();
|
|
38
|
-
try {
|
|
39
|
-
socket.sendParseMessage({
|
|
40
|
-
statement: statement.name,
|
|
41
|
-
sql: statement.sql,
|
|
42
|
-
paramTypes: statement.paramTypes,
|
|
43
|
-
});
|
|
44
|
-
socket.sendFlushMessage();
|
|
45
|
-
try {
|
|
46
|
-
await socket.capture(async (code, msg, done) => {
|
|
47
|
-
if (code === protocol_js_1.Protocol.BackendMessageCode.ParseComplete)
|
|
48
|
-
done();
|
|
49
|
-
// May be Protocol.BackendMessageCode.NoticeResponse
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
finally {
|
|
53
|
-
socket.sendSyncMessage();
|
|
54
|
-
await socket.capture(async (code, msg, done) => {
|
|
55
|
-
if (code === protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery)
|
|
56
|
-
done();
|
|
57
|
-
// May be Protocol.BackendMessageCode.NoticeResponse
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
finally {
|
|
62
|
-
intoCon.unref();
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
.toPromise();
|
|
66
|
-
statement._refCount = 1;
|
|
67
|
-
return statement;
|
|
68
|
-
}
|
|
69
|
-
get connection() {
|
|
70
|
-
return this._connection;
|
|
71
|
-
}
|
|
72
|
-
get name() {
|
|
73
|
-
return this._name;
|
|
74
|
-
}
|
|
75
|
-
get sql() {
|
|
76
|
-
return this._sql;
|
|
77
|
-
}
|
|
78
|
-
get paramTypes() {
|
|
79
|
-
return this._paramTypes;
|
|
80
|
-
}
|
|
81
|
-
async execute(options = {}) {
|
|
82
|
-
const intlCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
83
|
-
const transactionCommand = this.sql.match(/^(\bBEGIN\b|\bCOMMIT\b|\bSTART\b|\bROLLBACK|SAVEPOINT|RELEASE\b)/i);
|
|
84
|
-
let beginFirst = false;
|
|
85
|
-
let commitLast = false;
|
|
86
|
-
if (!transactionCommand) {
|
|
87
|
-
if (!intlCon.inTransaction &&
|
|
88
|
-
(options?.autoCommit != null
|
|
89
|
-
? options?.autoCommit
|
|
90
|
-
: intlCon.config.autoCommit) === false) {
|
|
91
|
-
beginFirst = true;
|
|
92
|
-
}
|
|
93
|
-
if (intlCon.inTransaction && options?.autoCommit)
|
|
94
|
-
commitLast = true;
|
|
95
|
-
}
|
|
96
|
-
if (beginFirst)
|
|
97
|
-
await intlCon.execute('BEGIN');
|
|
98
|
-
const rollbackOnError = !transactionCommand &&
|
|
99
|
-
(options?.rollbackOnError != null
|
|
100
|
-
? options.rollbackOnError
|
|
101
|
-
: (0, putil_varhelpers_1.coerceToBoolean)(intlCon.config.rollbackOnError, true));
|
|
102
|
-
if (intlCon.inTransaction && rollbackOnError)
|
|
103
|
-
await intlCon.execute('SAVEPOINT ' + this._onErrorSavePoint);
|
|
104
|
-
try {
|
|
105
|
-
const result = await intlCon.statementQueue
|
|
106
|
-
.enqueue(() => this._execute(options))
|
|
107
|
-
.toPromise();
|
|
108
|
-
if (commitLast)
|
|
109
|
-
await intlCon.execute('COMMIT');
|
|
110
|
-
else if (intlCon.inTransaction && rollbackOnError) {
|
|
111
|
-
await intlCon.execute('RELEASE ' + this._onErrorSavePoint + ';');
|
|
112
|
-
}
|
|
113
|
-
return result;
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
if (intlCon.inTransaction && rollbackOnError) {
|
|
117
|
-
await intlCon.execute('ROLLBACK TO ' + this._onErrorSavePoint + ';');
|
|
118
|
-
}
|
|
119
|
-
throw e;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
async close() {
|
|
123
|
-
--this._refCount;
|
|
124
|
-
if (this._refCount > 0)
|
|
125
|
-
return;
|
|
126
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
127
|
-
await intoCon.statementQueue.enqueue(() => this._close()).toPromise();
|
|
128
|
-
}
|
|
129
|
-
async cancel() {
|
|
130
|
-
throw new Error('Not implemented yet');
|
|
131
|
-
}
|
|
132
|
-
async _execute(options = {}) {
|
|
133
|
-
let portal;
|
|
134
|
-
const intlCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
135
|
-
intlCon.ref();
|
|
136
|
-
try {
|
|
137
|
-
const result = { command: undefined };
|
|
138
|
-
const startTime = Date.now();
|
|
139
|
-
const t = Date.now();
|
|
140
|
-
// Create portal
|
|
141
|
-
const portalName = 'P_' + ++portalCounter;
|
|
142
|
-
portal = new portal_js_1.Portal(this, portalName);
|
|
143
|
-
await portal.bind(options.params, options);
|
|
144
|
-
const fields = await portal.retrieveFields();
|
|
145
|
-
const typeMap = options.typeMap || data_type_map_js_1.GlobalTypeMap;
|
|
146
|
-
let parsers;
|
|
147
|
-
let resultFields;
|
|
148
|
-
if (fields) {
|
|
149
|
-
parsers = (0, get_parsers_js_1.getParsers)(typeMap, fields);
|
|
150
|
-
resultFields = (0, wrap_row_description_js_1.wrapRowDescription)(typeMap, fields, options.columnFormat || constants_js_1.DEFAULT_COLUMN_FORMAT);
|
|
151
|
-
result.fields = resultFields;
|
|
152
|
-
result.rowType = options.objectRows ? 'object' : 'array';
|
|
153
|
-
if (options.cursor) {
|
|
154
|
-
result.cursor = new cursor_js_1.Cursor(this, portal, resultFields, parsers, options);
|
|
155
|
-
this._refCount++;
|
|
156
|
-
portal = undefined;
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const executeResult = await portal.execute(options.fetchCount);
|
|
161
|
-
result.executeTime = Date.now() - t;
|
|
162
|
-
if (executeResult.command)
|
|
163
|
-
result.command = executeResult.command;
|
|
164
|
-
if (resultFields && parsers && executeResult.rows) {
|
|
165
|
-
if (!result.command)
|
|
166
|
-
result.command = 'SELECT';
|
|
167
|
-
const rows = (result.rows = executeResult.rows);
|
|
168
|
-
const l = rows.length;
|
|
169
|
-
let row;
|
|
170
|
-
for (let i = 0; i < l; i++) {
|
|
171
|
-
row = rows[i];
|
|
172
|
-
(0, parse_row_js_1.parseRow)(parsers, row, options);
|
|
173
|
-
if (options.objectRows) {
|
|
174
|
-
rows[i] = (0, convert_row_to_object_js_1.convertRowToObject)(resultFields, row);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (result.command === 'DELETE' ||
|
|
179
|
-
result.command === 'INSERT' ||
|
|
180
|
-
result.command === 'UPDATE') {
|
|
181
|
-
result.rowsAffected = executeResult.rowCount;
|
|
182
|
-
}
|
|
183
|
-
result.executeTime = Date.now() - startTime;
|
|
184
|
-
return result;
|
|
185
|
-
}
|
|
186
|
-
finally {
|
|
187
|
-
intlCon.unref();
|
|
188
|
-
if (portal)
|
|
189
|
-
await portal.close();
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
async _close() {
|
|
193
|
-
if (--this._refCount > 0)
|
|
194
|
-
return;
|
|
195
|
-
const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection);
|
|
196
|
-
intoCon.ref();
|
|
197
|
-
try {
|
|
198
|
-
const socket = intoCon.socket;
|
|
199
|
-
socket.sendCloseMessage({ type: 'S', name: this.name });
|
|
200
|
-
socket.sendSyncMessage();
|
|
201
|
-
await socket.capture(async (code, msg, done) => {
|
|
202
|
-
switch (code) {
|
|
203
|
-
case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
|
|
204
|
-
this.emit('notice', msg);
|
|
205
|
-
break;
|
|
206
|
-
case protocol_js_1.Protocol.BackendMessageCode.CloseComplete:
|
|
207
|
-
break;
|
|
208
|
-
case protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery:
|
|
209
|
-
intoCon.transactionStatus = msg.status;
|
|
210
|
-
done();
|
|
211
|
-
break;
|
|
212
|
-
default:
|
|
213
|
-
done(new Error(`Server returned unexpected response message (0x${code.toString(16)})`));
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
finally {
|
|
218
|
-
intoCon.unref();
|
|
219
|
-
}
|
|
220
|
-
this.emit('close');
|
|
221
|
-
}
|
|
222
|
-
[Symbol.asyncDispose]() {
|
|
223
|
-
return this.close();
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
exports.PreparedStatement = PreparedStatement;
|