-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
pg_v7.x.x.js
296 lines (269 loc) · 10 KB
/
pg_v7.x.x.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
declare module pg {
// Note: Currently There are some issues in Function overloading.
// https://github.com/facebook/flow/issues/2423
// So i temporarily remove the
// `((event: string, listener: Function) => EventEmitter );`
// from all overloading for EventEmitter.on().
// `any` types exised in this file, cause of currently `mixed` did not work well
// in Function Overloading.
// `Function` types exised in this file, cause of they come from another
// untyped npm lib.
/* Cause of <flow 0.36 did not support export type very well,
// so copy the types from pg-pool
// https://github.com/flowtype/flow-typed/issues/16
// https://github.com/facebook/flow/commit/843389f89c69516506213e298096a14867a45061
const Pool = require('pg-pool');
import type {
PgPoolConfig,
PoolConnectCallback,
DoneCallback,
PoolClient
} from 'pg-pool';
*/
// ------------- copy from 'pg-pool' ------------>>
/*
* PgPoolConfig's properties are passed unchanged to both
* the node-postgres Client constructor and the node-pool constructor
* allowing you to fully configure the behavior of both
* node-pool (https://github.com/coopernurse/node-pool)
*/
declare type PgPoolConfig = {
// node-pool ----------------
name: string,
create: Function,
destroy: Function,
max: number,
min: number,
refreshIdle: boolean,
idleTimeoutMillis: number,
connectionTimeoutMillis: number,
reapIntervalMillis: number,
returnToHead: boolean,
priorityRange: number,
validate: Function,
validateAsync: Function,
log: Function,
// node-postgres Client ------
//database user's name
user: string,
//name of database to connect
database: string,
//database user's password
password: string,
//database port
port: number,
// database host. defaults to localhost
host?: string,
// whether to try SSL/TLS to connect to server. default value: false
ssl?: boolean,
// name displayed in the pg_stat_activity view and included in CSV log entries
// default value: process.env.PGAPPNAME
application_name?: string,
// fallback value for the application_name configuration parameter
// default value: false
fallback_application_name?: string,
// max milliseconds any query using this connection will execute for before timing out in error. false=unlimited
// default value: false
statement_timeout?: boolean | number,
// pg-pool
Client: mixed,
Promise: mixed,
onCreate: Function,
};
/*
* Not extends from Client, cause some of Client's functions(ex: connect and end)
* should not be used by PoolClient (which returned from Pool.connect).
*/
declare type PoolClient = {
release(error?: mixed): void,
query:
( (query: QueryConfig|string, callback?: QueryCallback) => Query ) &
( (text: string, values: Array<any>, callback?: QueryCallback) => Query ),
on:
((event: 'drain', listener: () => void) => events$EventEmitter )&
((event: 'error', listener: (err: PG_ERROR) => void) => events$EventEmitter )&
((event: 'notification', listener: (message: any) => void) => events$EventEmitter )&
((event: 'notice', listener: (message: any) => void) => events$EventEmitter )&
((event: 'end', listener: () => void) => events$EventEmitter ),
}
declare type PoolConnectCallback = (error: PG_ERROR|null,
client: PoolClient|null, done: DoneCallback) => void;
declare type DoneCallback = (error?: mixed) => void;
// https://github.com/facebook/flow/blob/master/lib/node.js#L581
// on() returns a events$EventEmitter
declare class Pool extends events$EventEmitter {
constructor(options: $Shape<PgPoolConfig>, Client?: Class<Client>): void;
connect(cb?: PoolConnectCallback): Promise<PoolClient>;
take(cb?: PoolConnectCallback): Promise<PoolClient>;
end(cb?: DoneCallback): Promise<void>;
// Note: not like the pg's Client, the Pool.query return a Promise,
// not a Thenable Query which Client returned.
// And there is a flow(<0.34) issue here, when Array<mixed>,
// the overloading will not work
query:
( (query: QueryConfig|string, callback?: QueryCallback) => Promise<ResultSet> ) &
( (text: string, values: Array<any>, callback?: QueryCallback) => Promise<ResultSet>);
/* flow issue: https://github.com/facebook/flow/issues/2423
* When this fixed, this overloading can be used.
*/
/*
on:
((event: 'connect', listener: (client: PoolClient) => void) => events$EventEmitter )&
((event: 'acquire', listener: (client: PoolClient) => void) => events$EventEmitter )&
((event: "error", listener: (err: PG_ERROR) => void) => events$EventEmitter )&
((event: string, listener: Function) => events$EventEmitter);
*/
}
// <<------------- copy from 'pg-pool' ------------------------------
// error
declare type PG_ERROR = {
name: string,
length: number,
severity: string,
code: string,
detail: string|void,
hint: string|void,
position: string|void,
internalPosition: string|void,
internalQuery: string|void,
where: string|void,
schema: string|void,
table: string|void,
column: string|void,
dataType: string|void,
constraint: string|void,
file: string|void,
line: string|void,
routine: string|void
};
declare type ClientConfig = {
//database user's name
user?: string,
//name of database to connect
database?: string,
//database user's password
password?: string,
//database port
port?: number,
// database host. defaults to localhost
host?: string,
// whether to try SSL/TLS to connect to server. default value: false
ssl?: boolean,
// name displayed in the pg_stat_activity view and included in CSV log entries
// default value: process.env.PGAPPNAME
application_name?: string,
// fallback value for the application_name configuration parameter
// default value: false
fallback_application_name?: string,
}
declare type Row = {
[key: string]: mixed,
};
declare type ResultSet = {
command: string,
rowCount: number,
oid: number,
rows: Array<Row>,
};
declare type ResultBuilder = {
command: string,
rowCount: number,
oid: number,
rows: Array<Row>,
addRow: (row: Row) => void,
};
declare type QueryConfig = {
name?: string,
text: string,
values?: any[],
};
declare type QueryCallback = (err: PG_ERROR|null, result: ResultSet|void) => void;
declare type ClientConnectCallback = (err: PG_ERROR|null, client: Client|void) => void;
/*
* lib/query.js
* Query extends from EventEmitter in source code.
* but in Flow there is no multiple extends.
* And in Flow await is a `declare function $await<T>(p: Promise<T> | T): T;`
* seems can not resolve a Thenable's value type directly
* so `Query extends Promise` to make thing temporarily work.
* like this:
* const q = client.query('select * from some');
* q.on('row',cb); // Event
* const result = await q; // or await
*
* ToDo: should find a better way.
*/
declare class Query extends Promise<ResultSet> {
then<U>( onFulfill?: (value: ResultSet) => Promise<U> | U,
onReject?: (error: PG_ERROR) => Promise<U> | U
): Promise<U>;
// Because then and catch return a Promise,
// .then.catch will lose catch's type information PG_ERROR.
catch<U>( onReject?: (error: PG_ERROR) => ?Promise<U> | U ): Promise<U>;
on :
((event: 'row', listener: (row: Row, result: ResultBuilder) => void) => events$EventEmitter )&
((event: 'end', listener: (result: ResultBuilder) => void) => events$EventEmitter )&
((event: 'error', listener: (err: PG_ERROR) => void) => events$EventEmitter );
}
/*
* lib/client.js
* Note: not extends from EventEmitter, for This Type returned by on().
* Flow's EventEmitter force return a EventEmitter in on().
* ToDo: Not sure in on() if return events$EventEmitter or this will be more suitable
* return this will restrict event to given literial when chain on().on().on().
* return a events$EventEmitter will fallback to raw EventEmitter, when chains
*/
declare class Client {
constructor(config?: string | ClientConfig): void;
connect(callback?: ClientConnectCallback):void;
end(): void;
escapeLiteral(str: string): string;
escapeIdentifier(str: string): string;
query:
( (query: QueryConfig|string, callback?: QueryCallback) => Query ) &
( (text: string, values: Array<any>, callback?: QueryCallback) => Query );
on:
((event: 'drain', listener: () => void) => this )&
((event: 'error', listener: (err: PG_ERROR) => void) => this )&
((event: 'notification', listener: (message: any) => void) => this )&
((event: 'notice', listener: (message: any) => void) => this )&
((event: 'end', listener: () => void) => this );
}
/*
* require('pg-types')
*/
declare type TypeParserText = (value: string) => any;
declare type TypeParserBinary = (value: Buffer) => any;
declare type Types = {
getTypeParser:
((oid: number, format?: 'text') => TypeParserText )&
((oid: number, format: 'binary') => TypeParserBinary );
setTypeParser:
((oid: number, format?: 'text', parseFn: TypeParserText) => void )&
((oid: number, format: 'binary', parseFn: TypeParserBinary) => void)&
((oid: number, parseFn: TypeParserText) => void),
}
/*
* lib/index.js ( class PG)
*/
declare class PG extends events$EventEmitter {
types: Types;
Client: Class<Client>;
Pool: Class<Pool>;
Connection: mixed; //Connection is used internally by the Client.
constructor(client: Client): void;
native: { // native binding, have the same capability like PG
types: Types;
Client: Class<Client>;
Pool: Class<Pool>;
Connection: mixed;
};
// The end(),connect(),cancel() in PG is abandoned ?
}
// These class are not exposed by pg.
declare type PoolType = Pool;
declare type PGType = PG;
declare type QueryType = Query;
// module export, keep same structure with index.js
declare module.exports: PG;
}