postgrejs 2.12.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 +814 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cjs/connection/bind-param.js +10 -0
- package/cjs/connection/connection.js +279 -0
- package/cjs/connection/cursor.js +93 -0
- package/cjs/connection/intl-connection.js +243 -0
- package/cjs/connection/pool.js +226 -0
- package/cjs/connection/portal.js +148 -0
- package/cjs/connection/prepared-statement.js +217 -0
- package/cjs/constants.js +250 -0
- package/cjs/data-type-map.js +119 -0
- package/cjs/data-types/bool-type.js +27 -0
- package/cjs/data-types/box-type.js +52 -0
- package/cjs/data-types/bytea-type.js +29 -0
- package/cjs/data-types/char-type.js +27 -0
- package/cjs/data-types/circle-type.js +48 -0
- package/cjs/data-types/date-type.js +67 -0
- package/cjs/data-types/float4-type.js +25 -0
- package/cjs/data-types/float8-type.js +25 -0
- package/cjs/data-types/int2-type.js +26 -0
- package/cjs/data-types/int2-vector-type.js +38 -0
- package/cjs/data-types/int4-type.js +26 -0
- package/cjs/data-types/int8-type.js +31 -0
- package/cjs/data-types/json-type.js +38 -0
- package/cjs/data-types/jsonb-type.js +42 -0
- package/cjs/data-types/lseg-type.js +52 -0
- package/cjs/data-types/numeric-type.js +125 -0
- package/cjs/data-types/oid-type.js +32 -0
- package/cjs/data-types/point-type.js +39 -0
- package/cjs/data-types/time-type.js +54 -0
- package/cjs/data-types/timestamp-type.js +81 -0
- package/cjs/data-types/timestamptz-type.js +73 -0
- package/cjs/data-types/uuid-type.js +39 -0
- package/cjs/data-types/varchar-type.js +27 -0
- package/cjs/index.js +41 -0
- package/cjs/interfaces/command-result.js +2 -0
- package/cjs/interfaces/data-mapping-options.js +2 -0
- package/cjs/interfaces/data-type.js +2 -0
- package/cjs/interfaces/database-connection-params.js +2 -0
- package/cjs/interfaces/field-info.js +2 -0
- package/cjs/interfaces/query-options.js +2 -0
- package/cjs/interfaces/query-result.js +2 -0
- package/cjs/interfaces/script-execute-options.js +2 -0
- package/cjs/interfaces/script-result.js +2 -0
- package/cjs/interfaces/statement-prepare-options.js +2 -0
- package/cjs/package.json +3 -0
- package/cjs/protocol/backend.js +256 -0
- package/cjs/protocol/buffer-reader.js +78 -0
- package/cjs/protocol/database-error.js +17 -0
- package/cjs/protocol/frontend.js +218 -0
- package/cjs/protocol/pg-socket.js +345 -0
- package/cjs/protocol/protocol.js +71 -0
- package/cjs/protocol/sasl.js +108 -0
- package/cjs/protocol/smart-buffer.js +195 -0
- package/cjs/safe-event-emitter.js +17 -0
- package/cjs/types.js +2 -0
- package/cjs/util/array-calculatedim.js +19 -0
- package/cjs/util/bigint-methods.js +42 -0
- package/cjs/util/config-from-env.js +30 -0
- package/cjs/util/connection-config.js +65 -0
- package/cjs/util/convert-row-to-object.js +13 -0
- package/cjs/util/decode-binaryarray.js +39 -0
- package/cjs/util/encode-binaryarray.js +45 -0
- package/cjs/util/escape-literal.js +27 -0
- package/cjs/util/fast-parseint.js +32 -0
- package/cjs/util/get-parsers.js +42 -0
- package/cjs/util/parse-array.js +61 -0
- package/cjs/util/parse-datetime.js +38 -0
- package/cjs/util/parse-row.js +11 -0
- package/cjs/util/parse-time.js +29 -0
- package/cjs/util/stringify-arrayliteral.js +38 -0
- package/cjs/util/stringify-for-sql.js +30 -0
- package/cjs/util/wrap-row-description.js +35 -0
- package/esm/connection/bind-param.js +6 -0
- package/esm/connection/connection.js +275 -0
- package/esm/connection/cursor.js +86 -0
- package/esm/connection/intl-connection.js +238 -0
- package/esm/connection/pool.js +222 -0
- package/esm/connection/portal.js +144 -0
- package/esm/connection/prepared-statement.js +213 -0
- package/esm/constants.js +247 -0
- package/esm/data-type-map.js +115 -0
- package/esm/data-types/bool-type.js +24 -0
- package/esm/data-types/box-type.js +49 -0
- package/esm/data-types/bytea-type.js +23 -0
- package/esm/data-types/char-type.js +24 -0
- package/esm/data-types/circle-type.js +45 -0
- package/esm/data-types/date-type.js +64 -0
- package/esm/data-types/float4-type.js +22 -0
- package/esm/data-types/float8-type.js +22 -0
- package/esm/data-types/int2-type.js +23 -0
- package/esm/data-types/int2-vector-type.js +35 -0
- package/esm/data-types/int4-type.js +23 -0
- package/esm/data-types/int8-type.js +28 -0
- package/esm/data-types/json-type.js +35 -0
- package/esm/data-types/jsonb-type.js +39 -0
- package/esm/data-types/lseg-type.js +49 -0
- package/esm/data-types/numeric-type.js +121 -0
- package/esm/data-types/oid-type.js +29 -0
- package/esm/data-types/point-type.js +36 -0
- package/esm/data-types/time-type.js +51 -0
- package/esm/data-types/timestamp-type.js +78 -0
- package/esm/data-types/timestamptz-type.js +70 -0
- package/esm/data-types/uuid-type.js +36 -0
- package/esm/data-types/varchar-type.js +24 -0
- package/esm/index.js +25 -0
- package/esm/interfaces/command-result.js +1 -0
- package/esm/interfaces/data-mapping-options.js +1 -0
- package/esm/interfaces/data-type.js +1 -0
- package/esm/interfaces/database-connection-params.js +1 -0
- package/esm/interfaces/field-info.js +1 -0
- package/esm/interfaces/query-options.js +1 -0
- package/esm/interfaces/query-result.js +1 -0
- package/esm/interfaces/script-execute-options.js +1 -0
- package/esm/interfaces/script-result.js +1 -0
- package/esm/interfaces/statement-prepare-options.js +1 -0
- package/esm/protocol/backend.js +252 -0
- package/esm/protocol/buffer-reader.js +74 -0
- package/esm/protocol/database-error.js +13 -0
- package/esm/protocol/frontend.js +214 -0
- package/esm/protocol/pg-socket.js +338 -0
- package/esm/protocol/protocol.js +68 -0
- package/esm/protocol/sasl.js +102 -0
- package/esm/protocol/smart-buffer.js +168 -0
- package/esm/safe-event-emitter.js +13 -0
- package/esm/types.js +1 -0
- package/esm/util/array-calculatedim.js +15 -0
- package/esm/util/bigint-methods.js +37 -0
- package/esm/util/config-from-env.js +26 -0
- package/esm/util/connection-config.js +57 -0
- package/esm/util/convert-row-to-object.js +9 -0
- package/esm/util/decode-binaryarray.js +35 -0
- package/esm/util/encode-binaryarray.js +41 -0
- package/esm/util/escape-literal.js +23 -0
- package/esm/util/fast-parseint.js +28 -0
- package/esm/util/get-parsers.js +38 -0
- package/esm/util/parse-array.js +57 -0
- package/esm/util/parse-datetime.js +34 -0
- package/esm/util/parse-row.js +7 -0
- package/esm/util/parse-time.js +25 -0
- package/esm/util/stringify-arrayliteral.js +34 -0
- package/esm/util/stringify-for-sql.js +25 -0
- package/esm/util/wrap-row-description.js +31 -0
- package/package.json +54 -0
- package/typings/connection/bind-param.d.ts +6 -0
- package/typings/connection/connection.d.ts +110 -0
- package/typings/connection/cursor.d.ts +25 -0
- package/typings/connection/intl-connection.d.ts +40 -0
- package/typings/connection/pool.d.ts +52 -0
- package/typings/connection/portal.d.ts +23 -0
- package/typings/connection/prepared-statement.d.ts +27 -0
- package/typings/constants.d.ts +130 -0
- package/typings/data-type-map.d.ts +11 -0
- package/typings/data-types/bool-type.d.ts +3 -0
- package/typings/data-types/box-type.d.ts +3 -0
- package/typings/data-types/bytea-type.d.ts +3 -0
- package/typings/data-types/char-type.d.ts +3 -0
- package/typings/data-types/circle-type.d.ts +3 -0
- package/typings/data-types/date-type.d.ts +3 -0
- package/typings/data-types/float4-type.d.ts +3 -0
- package/typings/data-types/float8-type.d.ts +3 -0
- package/typings/data-types/int2-type.d.ts +3 -0
- package/typings/data-types/int2-vector-type.d.ts +3 -0
- package/typings/data-types/int4-type.d.ts +3 -0
- package/typings/data-types/int8-type.d.ts +3 -0
- package/typings/data-types/json-type.d.ts +3 -0
- package/typings/data-types/jsonb-type.d.ts +3 -0
- package/typings/data-types/lseg-type.d.ts +3 -0
- package/typings/data-types/numeric-type.d.ts +12 -0
- package/typings/data-types/oid-type.d.ts +4 -0
- package/typings/data-types/point-type.d.ts +3 -0
- package/typings/data-types/time-type.d.ts +3 -0
- package/typings/data-types/timestamp-type.d.ts +3 -0
- package/typings/data-types/timestamptz-type.d.ts +3 -0
- package/typings/data-types/uuid-type.d.ts +3 -0
- package/typings/data-types/varchar-type.d.ts +3 -0
- package/typings/index.d.ts +25 -0
- package/typings/interfaces/command-result.d.ts +28 -0
- package/typings/interfaces/data-mapping-options.d.ts +9 -0
- package/typings/interfaces/data-type.d.ts +31 -0
- package/typings/interfaces/database-connection-params.d.ts +38 -0
- package/typings/interfaces/field-info.d.ts +46 -0
- package/typings/interfaces/query-options.d.ts +46 -0
- package/typings/interfaces/query-result.d.ts +8 -0
- package/typings/interfaces/script-execute-options.d.ts +26 -0
- package/typings/interfaces/script-result.d.ts +15 -0
- package/typings/interfaces/statement-prepare-options.d.ts +13 -0
- package/typings/protocol/backend.d.ts +9 -0
- package/typings/protocol/buffer-reader.d.ts +18 -0
- package/typings/protocol/database-error.d.ts +20 -0
- package/typings/protocol/frontend.d.ts +59 -0
- package/typings/protocol/pg-socket.d.ts +51 -0
- package/typings/protocol/protocol.d.ts +180 -0
- package/typings/protocol/sasl.d.ts +13 -0
- package/typings/protocol/smart-buffer.d.ts +39 -0
- package/typings/safe-event-emitter.d.ts +5 -0
- package/typings/types.d.ts +15 -0
- package/typings/util/array-calculatedim.d.ts +1 -0
- package/typings/util/bigint-methods.d.ts +3 -0
- package/typings/util/config-from-env.d.ts +2 -0
- package/typings/util/connection-config.d.ts +3 -0
- package/typings/util/convert-row-to-object.d.ts +2 -0
- package/typings/util/decode-binaryarray.d.ts +4 -0
- package/typings/util/encode-binaryarray.d.ts +5 -0
- package/typings/util/escape-literal.d.ts +1 -0
- package/typings/util/fast-parseint.d.ts +1 -0
- package/typings/util/get-parsers.d.ts +4 -0
- package/typings/util/parse-array.d.ts +5 -0
- package/typings/util/parse-datetime.d.ts +1 -0
- package/typings/util/parse-row.d.ts +3 -0
- package/typings/util/parse-time.d.ts +3 -0
- package/typings/util/stringify-arrayliteral.d.ts +3 -0
- package/typings/util/stringify-for-sql.d.ts +4 -0
- package/typings/util/wrap-row-description.d.ts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Panates, Eray Hanoglu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
## postgresql-client
|
|
2
|
+
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
+
[![CircleCI][circleci-image]][circleci-url]
|
|
6
|
+
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Enterprise level PostgreSQL client for NodeJs
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Library
|
|
13
|
+
|
|
14
|
+
- Pure JavaScript library completely written in TypeScript
|
|
15
|
+
- Works with both CommonJS and ESM module systems
|
|
16
|
+
- Well tested
|
|
17
|
+
- Strictly typed
|
|
18
|
+
- Asynchronous Promise based api
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- Both single connection and advanced pooling support
|
|
23
|
+
- Full binary wire protocol support for all data types
|
|
24
|
+
- Named Prepared Statements
|
|
25
|
+
- Cursors with fast double-link cache
|
|
26
|
+
- High level implementation for notifications (LISTEN/NOTIFY)
|
|
27
|
+
- Extensible data-types and type mapping
|
|
28
|
+
- Bind parameters with OID mappings
|
|
29
|
+
- Multidimensional arrays with fast binary encoding/decoding
|
|
30
|
+
- Low memory utilization and boosted performance with Shared Buffers
|
|
31
|
+
- Supports Clear text, MD5 and SASL password algorithms
|
|
32
|
+
- Can return both array and object rows
|
|
33
|
+
- Auto disposal with "using" syntax ([TC30 Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
$ npm install postgresql-client --save
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Documentation
|
|
43
|
+
Please read :small_orange_diamond: [DOCUMENTATION](DOCUMENTATION.md) :small_orange_diamond: for detailed usage.
|
|
44
|
+
|
|
45
|
+
## Example usage
|
|
46
|
+
|
|
47
|
+
### Establish a single connection, execute a simple query
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import {Connection} from 'postgresql-client';
|
|
51
|
+
// Create connection
|
|
52
|
+
const connection = new Connection('postgres://localhost');
|
|
53
|
+
// Connect to database server
|
|
54
|
+
await connection.connect();
|
|
55
|
+
|
|
56
|
+
// Execute query and fetch rows
|
|
57
|
+
const result = await connection.query(
|
|
58
|
+
'select * from cities where name like $1',
|
|
59
|
+
{params: ['%york%']});
|
|
60
|
+
const rows: any[] = result.rows;
|
|
61
|
+
// Do what ever you want with rows
|
|
62
|
+
|
|
63
|
+
// Disconnect from server
|
|
64
|
+
await connection.close();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Establish a pooled connection, create a cursor
|
|
68
|
+
```ts
|
|
69
|
+
import {Pool} from 'postgresql-client';
|
|
70
|
+
|
|
71
|
+
// Create connection pool
|
|
72
|
+
const db = new Pool({
|
|
73
|
+
host: 'postgres://localhost',
|
|
74
|
+
pool: {
|
|
75
|
+
min: 1,
|
|
76
|
+
max: 10,
|
|
77
|
+
idleTimeoutMillis: 5000
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Execute query and fetch cursor
|
|
82
|
+
const result = await db.query(
|
|
83
|
+
'select * from cities where name like $1',
|
|
84
|
+
{params: ['%york%'], cursor: true});
|
|
85
|
+
|
|
86
|
+
// Walk through the cursor, and do whatever you want with fetched rows
|
|
87
|
+
const cursor = result.cursor;
|
|
88
|
+
let row;
|
|
89
|
+
while ((row = await cursor.next())) {
|
|
90
|
+
console.log(row);
|
|
91
|
+
}
|
|
92
|
+
// Close cursor, (Send connection back to the pool)
|
|
93
|
+
await cursor.close();
|
|
94
|
+
|
|
95
|
+
// Disconnect all connections and shutdown pool
|
|
96
|
+
await db.close();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Using prepared statements
|
|
100
|
+
```ts
|
|
101
|
+
import {DataTypeOIDs} from 'postgresql-client';
|
|
102
|
+
|
|
103
|
+
// .....
|
|
104
|
+
const statement = await connection.prepare(
|
|
105
|
+
'insert into my_table(id, name) values ($1, $2)', {
|
|
106
|
+
paramTypes: [DataTypeOIDs.Int4, DataTypeOIDs.Varchar]
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < 100; i++) {
|
|
110
|
+
await statement.execute({params: [i, ('name' + i)]});
|
|
111
|
+
}
|
|
112
|
+
await statement.close(); // When you done, close the statement to relase resources
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Check [DOCUMENTATION](DOCUMENTATION.md) for other examples.
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
## Type mappings
|
|
120
|
+
The table below lists builtin data type mappings.
|
|
121
|
+
|
|
122
|
+
| Posgtres type | JS type | Receive | Send |
|
|
123
|
+
|---------------|:------------|-------------|--------|
|
|
124
|
+
| bool | boolean | text,binary | binary |
|
|
125
|
+
| int2 | number | text,binary | binary |
|
|
126
|
+
| int4 | number | text,binary | binary |
|
|
127
|
+
| int8 | BigInt | text,binary | binary |
|
|
128
|
+
| float4 | number | text,binary | binary |
|
|
129
|
+
| float8 | number | text,binary | binary |
|
|
130
|
+
| char | string | text,binary | binary |
|
|
131
|
+
| bpchar | string | text,binary | binary |
|
|
132
|
+
| varchar | string | text,binary | binary |
|
|
133
|
+
| date | Date | text,binary | binary |
|
|
134
|
+
| time | Date | text,binary | binary |
|
|
135
|
+
| timestamp | Date | text,binary | binary |
|
|
136
|
+
| timestamptz | Date | text,binary | binary |
|
|
137
|
+
| oid | number | text,binary | binary |
|
|
138
|
+
| bytea | Buffer | text,binary | binary |
|
|
139
|
+
| uuid | string | text,binary | binary |
|
|
140
|
+
| json | object | text,binary | binary |
|
|
141
|
+
| jsonb | object | text,binary | binary |
|
|
142
|
+
| xml | string | text,binary | binary |
|
|
143
|
+
| point | Point | text,binary | binary |
|
|
144
|
+
| circle | Circle | text,binary | binary |
|
|
145
|
+
| lseg | Rectangle | text,binary | binary |
|
|
146
|
+
| box | Rectangle | text,binary | binary |
|
|
147
|
+
| int2Vector | number[] | text,binary | binary |
|
|
148
|
+
| _bool | boolean[] | text,binary | binary |
|
|
149
|
+
| _int2 | number[] | text,binary | binary |
|
|
150
|
+
| _int4 | number[] | text,binary | binary |
|
|
151
|
+
| _int8 | BigInt[] | text,binary | binary |
|
|
152
|
+
| _float4 | number[] | text,binary | binary |
|
|
153
|
+
| _float8 | number[] | text,binary | binary |
|
|
154
|
+
| _char | string[] | text,binary | binary |
|
|
155
|
+
| _bpchar | string[] | text,binary | binary |
|
|
156
|
+
| _varchar | string[] | text,binary | binary |
|
|
157
|
+
| _date | Date[] | text,binary | binary |
|
|
158
|
+
| _time | Date[] | text,binary | binary |
|
|
159
|
+
| _timestamp | Date[] | text,binary | binary |
|
|
160
|
+
| _timestamptz | Date[] | text,binary | binary |
|
|
161
|
+
| _uuid | string[] | text,binary | binary |
|
|
162
|
+
| _oid | number[] | text,binary | binary |
|
|
163
|
+
| _bytea | Buffer[] | text,binary | binary |
|
|
164
|
+
| _json | object[] | text,binary | binary |
|
|
165
|
+
| _jsonb | object[] | text,binary | binary |
|
|
166
|
+
| _xml | string[] | text,binary | binary |
|
|
167
|
+
| _point | Point[] | text,binary | binary |
|
|
168
|
+
| _circle | Circle[] | text,binary | binary |
|
|
169
|
+
| _lseg | Rectangle[] | text,binary | binary |
|
|
170
|
+
| _box | Rectangle[] | text,binary | binary |
|
|
171
|
+
| _int2Vector | number[][] | text,binary | binary |
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
## Support
|
|
175
|
+
You can report bugs and discuss features on the [GitHub issues](https://github.com/panates/postgresql-client/issues) page
|
|
176
|
+
When you open an issue please provide version of NodeJS and PostgreSQL server.
|
|
177
|
+
|
|
178
|
+
## Node Compatibility
|
|
179
|
+
- node >= 14.x
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
postgresql-client is available under [MIT](LICENSE) license.
|
|
184
|
+
|
|
185
|
+
[npm-image]: https://img.shields.io/npm/v/postgresql-client
|
|
186
|
+
[npm-url]: https://npmjs.org/package/postgresql-client
|
|
187
|
+
[circleci-image]: https://circleci.com/gh/panates/postgresql-client/tree/master.svg?style=shield
|
|
188
|
+
[circleci-url]: https://circleci.com/gh/panates/postgresql-client/tree/master
|
|
189
|
+
[coveralls-image]: https://img.shields.io/coveralls/panates/postgresql-client/master.svg
|
|
190
|
+
[coveralls-url]: https://coveralls.io/r/panates/postgresql-client
|
|
191
|
+
[downloads-image]: https://img.shields.io/npm/dm/postgresql-client.svg
|
|
192
|
+
[downloads-url]: https://npmjs.org/package/postgresql-client
|
|
193
|
+
[gitter-image]: https://badges.gitter.im/panates/postgresql-client.svg
|
|
194
|
+
[gitter-url]: https://gitter.im/panates/postgresql-client?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
|
|
195
|
+
[dependencies-image]: https://david-dm.org/panates/postgresql-client/status.svg
|
|
196
|
+
[dependencies-url]:https://david-dm.org/panates/postgresql-client
|
|
197
|
+
[devdependencies-image]: https://david-dm.org/panates/postgresql-client/dev-status.svg
|
|
198
|
+
[devdependencies-url]:https://david-dm.org/panates/postgresql-client?type=dev
|
|
199
|
+
[quality-image]: http://npm.packagequality.com/shield/postgresql-client.png
|
|
200
|
+
[quality-url]: http://packagequality.com/#?package=postgresql-client
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Connection = void 0;
|
|
4
|
+
const constants_js_1 = require("../constants.js");
|
|
5
|
+
const data_type_map_js_1 = require("../data-type-map.js");
|
|
6
|
+
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
|
|
7
|
+
const bind_param_js_1 = require("./bind-param.js");
|
|
8
|
+
const intl_connection_js_1 = require("./intl-connection.js");
|
|
9
|
+
const prepared_statement_js_1 = require("./prepared-statement.js");
|
|
10
|
+
class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
11
|
+
constructor(arg0, arg1) {
|
|
12
|
+
super();
|
|
13
|
+
this._notificationListeners = new safe_event_emitter_js_1.SafeEventEmitter();
|
|
14
|
+
this._closing = false;
|
|
15
|
+
if (arg0 && typeof arg0 === 'object' && typeof arg0.acquire === 'function') {
|
|
16
|
+
if (!(arg1 instanceof intl_connection_js_1.IntlConnection))
|
|
17
|
+
throw new TypeError('Invalid argument');
|
|
18
|
+
this._pool = arg0;
|
|
19
|
+
this._intlCon = arg1;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this._intlCon = new intl_connection_js_1.IntlConnection(arg0);
|
|
23
|
+
}
|
|
24
|
+
this._intlCon.on('ready', (...args) => this.emit('ready', ...args));
|
|
25
|
+
this._intlCon.on('error', (...args) => this.emit('error', ...args));
|
|
26
|
+
this._intlCon.on('close', (...args) => this.emit('close', ...args));
|
|
27
|
+
this._intlCon.on('connecting', (...args) => this.emit('connecting', ...args));
|
|
28
|
+
this._intlCon.on('ready', (...args) => this.emit('ready', ...args));
|
|
29
|
+
this._intlCon.on('terminate', (...args) => this.emit('terminate', ...args));
|
|
30
|
+
this._intlCon.on('notification', (msg) => this._handleNotification(msg));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns configuration object
|
|
34
|
+
*/
|
|
35
|
+
get config() {
|
|
36
|
+
return this._intlCon.config;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns true if connection is in a transaction
|
|
40
|
+
*/
|
|
41
|
+
get inTransaction() {
|
|
42
|
+
return this._intlCon.inTransaction;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns current state of the connection
|
|
46
|
+
*/
|
|
47
|
+
get state() {
|
|
48
|
+
return this._intlCon.state;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns processId of current session
|
|
52
|
+
*/
|
|
53
|
+
get processID() {
|
|
54
|
+
return this._intlCon.processID;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns information parameters for current session
|
|
58
|
+
*/
|
|
59
|
+
get sessionParameters() {
|
|
60
|
+
return this._intlCon.sessionParameters;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns secret key of current session
|
|
64
|
+
*/
|
|
65
|
+
get secretKey() {
|
|
66
|
+
return this._intlCon.secretKey;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Connects to the server
|
|
70
|
+
*/
|
|
71
|
+
async connect() {
|
|
72
|
+
await this._captureErrorStack(this._intlCon.connect());
|
|
73
|
+
if (this.state === constants_js_1.ConnectionState.READY)
|
|
74
|
+
this._closing = false;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Closes connection. You can define how long time the connection will
|
|
78
|
+
* wait for active queries before terminating the connection.
|
|
79
|
+
* On the end of the given time, it forces to close the socket and than emits `terminate` event.
|
|
80
|
+
*
|
|
81
|
+
* @param terminateWait {number} - Determines how long the connection will wait for active queries before terminating.
|
|
82
|
+
*/
|
|
83
|
+
async close(terminateWait) {
|
|
84
|
+
this._notificationListeners.removeAllListeners();
|
|
85
|
+
this._intlCon.statementQueue.clearQueue();
|
|
86
|
+
if (this.state === constants_js_1.ConnectionState.CLOSED || this._closing)
|
|
87
|
+
return;
|
|
88
|
+
/* istanbul ignore next */
|
|
89
|
+
if (this.listenerCount('debug'))
|
|
90
|
+
this.emit('debug', {
|
|
91
|
+
location: 'Connection.close',
|
|
92
|
+
connection: this,
|
|
93
|
+
message: `[${this.processID}] closing`,
|
|
94
|
+
});
|
|
95
|
+
this._closing = true;
|
|
96
|
+
if (this._intlCon.refCount > 0 && typeof terminateWait === 'number' && terminateWait > 0) {
|
|
97
|
+
const startTime = Date.now();
|
|
98
|
+
return this._captureErrorStack(new Promise((resolve, reject) => {
|
|
99
|
+
/* istanbul ignore next */
|
|
100
|
+
if (this.listenerCount('debug'))
|
|
101
|
+
this.emit('debug', {
|
|
102
|
+
location: 'Connection.close',
|
|
103
|
+
connection: this,
|
|
104
|
+
message: `[${this.processID}] waiting active queries`,
|
|
105
|
+
});
|
|
106
|
+
const timer = setInterval(() => {
|
|
107
|
+
if (this._intlCon.refCount <= 0 || Date.now() > startTime + terminateWait) {
|
|
108
|
+
clearInterval(timer);
|
|
109
|
+
if (this._intlCon.refCount > 0) {
|
|
110
|
+
/* istanbul ignore next */
|
|
111
|
+
if (this.listenerCount('debug'))
|
|
112
|
+
this.emit('debug', {
|
|
113
|
+
location: 'Connection.close',
|
|
114
|
+
connection: this,
|
|
115
|
+
message: `[${this.processID}] terminate`,
|
|
116
|
+
});
|
|
117
|
+
this.emit('terminate');
|
|
118
|
+
}
|
|
119
|
+
this._close().then(resolve).catch(reject);
|
|
120
|
+
}
|
|
121
|
+
}, 50);
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
await this._close();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Executes single or multiple SQL scripts using Simple Query protocol.
|
|
128
|
+
*
|
|
129
|
+
* @param sql {string} - SQL script that will be executed
|
|
130
|
+
* @param options {ScriptExecuteOptions} - Execute options
|
|
131
|
+
*/
|
|
132
|
+
async execute(sql, options) {
|
|
133
|
+
return this._captureErrorStack(this._intlCon.execute(sql, options)).catch((e) => {
|
|
134
|
+
throw this._handleError(e, sql);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async query(sql, options) {
|
|
138
|
+
this._intlCon.assertConnected();
|
|
139
|
+
/* istanbul ignore next */
|
|
140
|
+
if (this.listenerCount('debug'))
|
|
141
|
+
this.emit('debug', {
|
|
142
|
+
location: 'Connection.query',
|
|
143
|
+
connection: this,
|
|
144
|
+
message: `[${this.processID}] query | ${sql}`,
|
|
145
|
+
sql,
|
|
146
|
+
});
|
|
147
|
+
const typeMap = options?.typeMap || data_type_map_js_1.GlobalTypeMap;
|
|
148
|
+
const paramTypes = options?.params?.map(prm => prm instanceof bind_param_js_1.BindParam ? prm.oid : typeMap.determine(prm));
|
|
149
|
+
const statement = await this.prepare(sql, { paramTypes, typeMap }).catch((e) => {
|
|
150
|
+
throw this._handleError(e, sql);
|
|
151
|
+
});
|
|
152
|
+
try {
|
|
153
|
+
const params = options?.params?.map(prm => (prm instanceof bind_param_js_1.BindParam ? prm.value : prm));
|
|
154
|
+
return await this._captureErrorStack(statement.execute({ ...options, params }));
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
await statement.close();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Creates a PreparedStatement instance
|
|
162
|
+
* @param sql {string} - SQL script that will be executed
|
|
163
|
+
* @param options {StatementPrepareOptions} - Options
|
|
164
|
+
*/
|
|
165
|
+
async prepare(sql, options) {
|
|
166
|
+
/* istanbul ignore next */
|
|
167
|
+
if (this.listenerCount('debug'))
|
|
168
|
+
this.emit('debug', {
|
|
169
|
+
location: 'Connection.prepare',
|
|
170
|
+
connection: this,
|
|
171
|
+
message: `[${this.processID}] prepare | ${sql}`,
|
|
172
|
+
sql,
|
|
173
|
+
});
|
|
174
|
+
return await this._captureErrorStack(prepared_statement_js_1.PreparedStatement.prepare(this, sql, options));
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Starts a transaction
|
|
178
|
+
*/
|
|
179
|
+
startTransaction() {
|
|
180
|
+
return this._captureErrorStack(this._intlCon.startTransaction());
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Commits current transaction
|
|
184
|
+
*/
|
|
185
|
+
commit() {
|
|
186
|
+
return this._captureErrorStack(this._intlCon.commit());
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Rolls back current transaction
|
|
190
|
+
*/
|
|
191
|
+
rollback() {
|
|
192
|
+
return this._captureErrorStack(this._intlCon.rollback());
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Starts transaction and creates a savepoint
|
|
196
|
+
* @param name {string} - Name of the savepoint
|
|
197
|
+
*/
|
|
198
|
+
async savepoint(name) {
|
|
199
|
+
if (!this._intlCon.inTransaction)
|
|
200
|
+
await this._intlCon.startTransaction();
|
|
201
|
+
return this._captureErrorStack(this._intlCon.savepoint(name));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Rolls back current transaction to given savepoint
|
|
205
|
+
* @param name {string} - Name of the savepoint
|
|
206
|
+
*/
|
|
207
|
+
rollbackToSavepoint(name) {
|
|
208
|
+
return this._captureErrorStack(this._intlCon.rollbackToSavepoint(name));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Releases savepoint
|
|
212
|
+
* @param name {string} - Name of the savepoint
|
|
213
|
+
*/
|
|
214
|
+
releaseSavepoint(name) {
|
|
215
|
+
return this._captureErrorStack(this._intlCon.releaseSavepoint(name));
|
|
216
|
+
}
|
|
217
|
+
async listen(channel, callback) {
|
|
218
|
+
if (!/^[A-Z]\w+$/i.test(channel))
|
|
219
|
+
throw new TypeError(`Invalid channel name`);
|
|
220
|
+
const registered = !!this._notificationListeners.eventNames().length;
|
|
221
|
+
this._notificationListeners.on(channel, callback);
|
|
222
|
+
if (!registered)
|
|
223
|
+
await this._captureErrorStack(this.query('LISTEN ' + channel));
|
|
224
|
+
}
|
|
225
|
+
async unListen(channel) {
|
|
226
|
+
if (!/^[A-Z]\w+$/i.test(channel))
|
|
227
|
+
throw new TypeError(`Invalid channel name`);
|
|
228
|
+
this._notificationListeners.removeAllListeners(channel);
|
|
229
|
+
await this._captureErrorStack(this.query('UNLISTEN ' + channel));
|
|
230
|
+
}
|
|
231
|
+
async unListenAll() {
|
|
232
|
+
this._notificationListeners.removeAllListeners();
|
|
233
|
+
await this._captureErrorStack(this.query('UNLISTEN *'));
|
|
234
|
+
}
|
|
235
|
+
_handleNotification(msg) {
|
|
236
|
+
this.emit('notification', msg);
|
|
237
|
+
this._notificationListeners.emit(msg.channel, msg);
|
|
238
|
+
}
|
|
239
|
+
async _close() {
|
|
240
|
+
if (this._pool) {
|
|
241
|
+
await this._captureErrorStack(this._pool.release(this));
|
|
242
|
+
this.emit('release');
|
|
243
|
+
}
|
|
244
|
+
else
|
|
245
|
+
await this._captureErrorStack(this._intlCon.close());
|
|
246
|
+
this._closing = false;
|
|
247
|
+
}
|
|
248
|
+
_handleError(err, script) {
|
|
249
|
+
if (err.position) {
|
|
250
|
+
const i1 = script.lastIndexOf('\n', err.position) + 1;
|
|
251
|
+
let i2 = script.indexOf('\n', err.position);
|
|
252
|
+
if (i2 < 0)
|
|
253
|
+
i2 = Number.MAX_SAFE_INTEGER;
|
|
254
|
+
err.line = script.substring(i1, i2);
|
|
255
|
+
err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length;
|
|
256
|
+
err.colNr = err.position - i1;
|
|
257
|
+
err.message +=
|
|
258
|
+
`\nAt line ${err.lineNr} column ${err.colNr}` +
|
|
259
|
+
`\n | ${err.line}\n | ${' '.repeat(Math.max(err.colNr - 2, 0))}-^-`;
|
|
260
|
+
}
|
|
261
|
+
return err;
|
|
262
|
+
}
|
|
263
|
+
async _captureErrorStack(promise) {
|
|
264
|
+
const stack = new Error().stack;
|
|
265
|
+
return promise.catch(e => {
|
|
266
|
+
if (e instanceof Error && stack) {
|
|
267
|
+
e.stack = stack
|
|
268
|
+
.split('\n')
|
|
269
|
+
.filter(x => !x.includes('Connection._captureErrorStack'))
|
|
270
|
+
.join('\n');
|
|
271
|
+
}
|
|
272
|
+
throw e;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
[Symbol.asyncDispose]() {
|
|
276
|
+
return this.close();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
exports.Connection = Connection;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Cursor = void 0;
|
|
7
|
+
const doublylinked_1 = __importDefault(require("doublylinked"));
|
|
8
|
+
const power_tasks_1 = require("power-tasks");
|
|
9
|
+
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
|
|
10
|
+
const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
|
|
11
|
+
const parse_row_js_1 = require("../util/parse-row.js");
|
|
12
|
+
class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
13
|
+
constructor(statement, portal, fields, parsers, queryOptions) {
|
|
14
|
+
super();
|
|
15
|
+
this._taskQueue = new power_tasks_1.TaskQueue({ concurrency: 1 });
|
|
16
|
+
this._rows = new doublylinked_1.default();
|
|
17
|
+
this._closed = false;
|
|
18
|
+
this._statement = statement;
|
|
19
|
+
this._portal = portal;
|
|
20
|
+
this._parsers = parsers;
|
|
21
|
+
this._queryOptions = queryOptions;
|
|
22
|
+
this.fields = fields;
|
|
23
|
+
}
|
|
24
|
+
get rowType() {
|
|
25
|
+
return this._queryOptions.objectRows ? 'object' : 'array';
|
|
26
|
+
}
|
|
27
|
+
get isClosed() {
|
|
28
|
+
return this._closed;
|
|
29
|
+
}
|
|
30
|
+
async next() {
|
|
31
|
+
if (!this._rows.length) {
|
|
32
|
+
if (this._closed)
|
|
33
|
+
return;
|
|
34
|
+
await this._fetchRows();
|
|
35
|
+
}
|
|
36
|
+
return this._rows.shift();
|
|
37
|
+
}
|
|
38
|
+
async fetch(nRows) {
|
|
39
|
+
const out = [];
|
|
40
|
+
if (this._closed)
|
|
41
|
+
return out;
|
|
42
|
+
for (let i = 0; i < nRows; i++) {
|
|
43
|
+
if (!this._rows.length)
|
|
44
|
+
await this._fetchRows();
|
|
45
|
+
if (this._rows.length)
|
|
46
|
+
out.push(this._rows.shift());
|
|
47
|
+
else
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
async close() {
|
|
53
|
+
if (this._closed)
|
|
54
|
+
return;
|
|
55
|
+
await this._portal.close();
|
|
56
|
+
await this._statement.close();
|
|
57
|
+
this.emit('close');
|
|
58
|
+
this._closed = true;
|
|
59
|
+
}
|
|
60
|
+
async _fetchRows() {
|
|
61
|
+
if (this._closed)
|
|
62
|
+
return;
|
|
63
|
+
const portal = this._portal;
|
|
64
|
+
await this._taskQueue
|
|
65
|
+
.enqueue(async () => {
|
|
66
|
+
const queryOptions = this._queryOptions;
|
|
67
|
+
const r = await portal.execute(queryOptions.fetchCount || 100);
|
|
68
|
+
if (r && r.rows && r.rows.length) {
|
|
69
|
+
if (this._parsers) {
|
|
70
|
+
const objectRows = queryOptions.objectRows;
|
|
71
|
+
const fields = this.fields;
|
|
72
|
+
const rows = r.rows;
|
|
73
|
+
for (let i = 0; i < rows.length; i++) {
|
|
74
|
+
const row = rows[i];
|
|
75
|
+
(0, parse_row_js_1.parseRow)(this._parsers, row, this._queryOptions);
|
|
76
|
+
if (objectRows)
|
|
77
|
+
rows[i] = (0, convert_row_to_object_js_1.convertRowToObject)(fields, row);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
this._rows.push(...r.rows);
|
|
81
|
+
this.emit('fetch', r.rows);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await this.close();
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
.toPromise();
|
|
88
|
+
}
|
|
89
|
+
[Symbol.asyncDispose]() {
|
|
90
|
+
return this.close();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.Cursor = Cursor;
|