-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.d.ts
More file actions
144 lines (128 loc) · 4.75 KB
/
Copy pathclient.d.ts
File metadata and controls
144 lines (128 loc) · 4.75 KB
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
export type Mode = 'read-write' | 'read-only' | 'prefer-standby' | 'any'
export type IsolationLevel = 'read-committed' | 'repeatable-read' | 'serializable'
export interface PoolOptions {
/** One entry per cluster node. The pool races them per `mode`. */
hosts: string[]
/** Applied to every host. Defaults to 5432. */
port?: number
user: string
password?: string
database: string
/** Routing. Defaults to `'read-write'` (follow the primary). */
mode?: Mode
/** Max pooled connections. Defaults to 10. */
poolSize?: number
/** Per-connection TCP/handshake timeout. */
connectTimeoutMs?: number
/** How long `query`/`begin` retry for a writable primary before throwing. Defaults to 5000. */
acquireTimeoutMs?: number
/** Server-enforced `statement_timeout` (ms) set on every pooled connection. */
statementTimeoutMs?: number
validationIntervalMs?: number
applicationName?: string
/** Called once per query with timing and outcome. Errors thrown here are ignored. */
logger?: (event: QueryEvent) => void
/** Retry policy for retryable reads, `transaction(cb)`, and idempotent `insert`. */
retry?: RetryOptions
}
export interface RetryOptions {
/** Max total attempts (1 = no retry). Defaults to 3. */
maxAttempts?: number
/** First backoff delay in ms (doubles per attempt, jittered). Defaults to 50. */
baseDelayMs?: number
/** Backoff cap in ms. Defaults to 1000. */
maxDelayMs?: number
}
export interface QueryEvent {
sql: string
durationMs: number
/** Present on success. */
rowCount?: number
/** Present on failure (carries `.code` for DB errors). */
error?: DbError
}
export interface PoolStatus {
maxSize: number
size: number
available: number
inUse: number
waiting: number
}
/** A result row: column name → decoded value. */
export type Row = Record<string, unknown>
/** A value accepted as a bound query parameter. */
export type Param =
| null
| boolean
| number
| bigint
| string
| Uint8Array
| Date
| Param[]
| { [key: string]: unknown }
export interface QueryOptions {
/** Cancel the in-flight query server-side after this many milliseconds. */
timeoutMs?: number
/** Abort the query (server-side cancel) when the signal fires. */
signal?: AbortSignal
/**
* Retry on transient errors (serialization/deadlock, connection loss). Only safe for
* reads or idempotent statements. Defaults to true on `read-only`/`prefer-standby` pools,
* false otherwise — set explicitly to opt a specific query in or out.
*/
retry?: boolean
}
export interface InsertOptions {
/** Add `ON CONFLICT (<conflictTarget>) DO NOTHING`, making the insert safely retryable. */
idempotency?: boolean
/** Conflict key for idempotent inserts. Defaults to `'_id'`. */
conflictTarget?: string | string[]
timeoutMs?: number
signal?: AbortSignal
}
export interface TransactionOptions {
/** Max total attempts for the retry loop. Defaults to the pool's `retry.maxAttempts`. */
maxAttempts?: number
isolation?: IsolationLevel
}
export interface BeginOptions {
isolation?: IsolationLevel
}
/**
* A PostgreSQL `Error` carries the 5-character SQLSTATE on `.code`. A `transaction(cb)`
* whose `COMMIT` is lost to a connection failure (outcome unknown) throws with
* `code === 'IN_DOUBT'` and is never auto-retried.
*/
export interface DbError extends Error {
code?: string
cause?: unknown
}
export interface Transaction {
query<T = Row>(sql: string, params?: Param[], opts?: QueryOptions): Promise<T[]>
commit(): Promise<void>
rollback(): Promise<void>
}
export interface Pool {
query<T = Row>(sql: string, params?: Param[], opts?: QueryOptions): Promise<T[]>
/**
* Insert `row` into `table`, returning the inserted row(s). With `idempotency: true` a
* re-applied row collapses to a no-op (`ON CONFLICT (<conflictTarget>) DO NOTHING`),
* making the write safe to retry through the in-doubt window — a duplicate returns `[]`.
*/
insert<T = Row>(table: string, row: Record<string, Param>, opts?: InsertOptions): Promise<T[]>
/** Begin a transaction on a dedicated connection. Remember to `commit()` or `rollback()`. */
begin(opts?: BeginOptions): Promise<Transaction>
/**
* Run `fn` inside a transaction, auto `COMMIT` on resolve and `ROLLBACK` on throw, retrying
* the whole callback on serialization/deadlock and pre-commit connection failures. A failure
* during `COMMIT` is surfaced as a `code === 'IN_DOUBT'` error and never auto-retried.
*/
transaction<T>(fn: (tx: Transaction) => Promise<T>, opts?: TransactionOptions): Promise<T>
/** Live pool counters: size, idle, in-use, and waiters. */
status(): PoolStatus
/** Drain and close the pool. */
end(): Promise<void>
}
export function createPool(options: PoolOptions): Pool
export function hello(name: string): string