@keyv/postgres 2.2.3 → 6.0.0-alpha.2
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/README.md +434 -23
- package/dist/index.cjs +572 -101
- package/dist/index.d.cts +275 -15
- package/dist/index.d.ts +275 -15
- package/dist/index.js +572 -101
- package/package.json +4 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,37 +1,297 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ConnectionOptions } from 'node:tls';
|
|
2
|
+
import { Hookified } from 'hookified';
|
|
2
3
|
import Keyv, { KeyvStoreAdapter, KeyvEntry } from 'keyv';
|
|
3
4
|
import { PoolConfig } from 'pg';
|
|
4
5
|
|
|
5
6
|
type KeyvPostgresOptions = {
|
|
6
7
|
uri?: string;
|
|
7
8
|
table?: string;
|
|
8
|
-
|
|
9
|
+
keyLength?: number;
|
|
10
|
+
namespaceLength?: number;
|
|
9
11
|
schema?: string;
|
|
10
|
-
ssl?:
|
|
11
|
-
dialect?: string;
|
|
12
|
+
ssl?: boolean | ConnectionOptions;
|
|
12
13
|
iterationLimit?: number;
|
|
13
14
|
useUnloggedTable?: boolean;
|
|
15
|
+
clearExpiredInterval?: number;
|
|
14
16
|
} & PoolConfig;
|
|
15
|
-
type Query = (sqlString: string, values?: any) => Promise<any[]>;
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
/**
|
|
19
|
+
* PostgreSQL storage adapter for Keyv.
|
|
20
|
+
* Uses the `pg` library for connection pooling and parameterized queries.
|
|
21
|
+
*/
|
|
22
|
+
declare class KeyvPostgres extends Hookified implements KeyvStoreAdapter {
|
|
23
|
+
/** Function for executing SQL queries against the PostgreSQL database. */
|
|
24
|
+
private query;
|
|
25
|
+
/** Promise that resolves to the query function once initialization completes. */
|
|
26
|
+
private _connected;
|
|
27
|
+
/** The namespace used to prefix keys for multi-tenant separation. */
|
|
28
|
+
private _namespace?;
|
|
29
|
+
/**
|
|
30
|
+
* The PostgreSQL connection URI.
|
|
31
|
+
* @default 'postgresql://localhost:5432'
|
|
32
|
+
*/
|
|
33
|
+
private _uri;
|
|
34
|
+
/**
|
|
35
|
+
* The table name used for storage.
|
|
36
|
+
* @default 'keyv'
|
|
37
|
+
*/
|
|
38
|
+
private _table;
|
|
39
|
+
/**
|
|
40
|
+
* The maximum key length (VARCHAR length) for the key column.
|
|
41
|
+
* @default 255
|
|
42
|
+
*/
|
|
43
|
+
private _keyLength;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum namespace length (VARCHAR length) for the namespace column.
|
|
46
|
+
* @default 255
|
|
47
|
+
*/
|
|
48
|
+
private _namespaceLength;
|
|
49
|
+
/**
|
|
50
|
+
* The PostgreSQL schema name.
|
|
51
|
+
* @default 'public'
|
|
52
|
+
*/
|
|
53
|
+
private _schema;
|
|
54
|
+
/**
|
|
55
|
+
* The SSL configuration for the PostgreSQL connection.
|
|
56
|
+
* @default undefined
|
|
57
|
+
*/
|
|
58
|
+
private _ssl?;
|
|
59
|
+
/**
|
|
60
|
+
* The number of rows to fetch per iteration batch.
|
|
61
|
+
* @default 10
|
|
62
|
+
*/
|
|
63
|
+
private _iterationLimit;
|
|
64
|
+
/**
|
|
65
|
+
* Whether to use a PostgreSQL unlogged table (faster writes, no WAL, data lost on crash).
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
private _useUnloggedTable;
|
|
69
|
+
/**
|
|
70
|
+
* The interval in milliseconds between automatic expired-entry cleanup runs.
|
|
71
|
+
* A value of 0 (default) disables the automatic cleanup.
|
|
72
|
+
* @default 0
|
|
73
|
+
*/
|
|
74
|
+
private _clearExpiredInterval;
|
|
75
|
+
/**
|
|
76
|
+
* The timer reference for the automatic expired-entry cleanup interval.
|
|
77
|
+
*/
|
|
78
|
+
private _clearExpiredTimer?;
|
|
79
|
+
/**
|
|
80
|
+
* Additional PoolConfig properties passed through to the pg connection pool.
|
|
81
|
+
*/
|
|
82
|
+
private _poolConfig;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new KeyvPostgres instance.
|
|
85
|
+
* @param options - A PostgreSQL connection URI string or a {@link KeyvPostgresOptions} configuration object.
|
|
86
|
+
*/
|
|
22
87
|
constructor(options?: KeyvPostgresOptions | string);
|
|
23
|
-
|
|
24
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Initializes the database connection and ensures the table schema exists.
|
|
90
|
+
* Called from the constructor; errors are emitted rather than thrown.
|
|
91
|
+
*/
|
|
92
|
+
private init;
|
|
93
|
+
/**
|
|
94
|
+
* Get the namespace for the adapter. If undefined, no namespace prefix is applied.
|
|
95
|
+
*/
|
|
96
|
+
get namespace(): string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Set the namespace for the adapter. Used for key prefixing and scoping operations like `clear()`.
|
|
99
|
+
*/
|
|
100
|
+
set namespace(value: string | undefined);
|
|
101
|
+
/**
|
|
102
|
+
* Get the PostgreSQL connection URI.
|
|
103
|
+
* @default 'postgresql://localhost:5432'
|
|
104
|
+
*/
|
|
105
|
+
get uri(): string;
|
|
106
|
+
/**
|
|
107
|
+
* Set the PostgreSQL connection URI.
|
|
108
|
+
*/
|
|
109
|
+
set uri(value: string);
|
|
110
|
+
/**
|
|
111
|
+
* Get the table name used for storage.
|
|
112
|
+
* @default 'keyv'
|
|
113
|
+
*/
|
|
114
|
+
get table(): string;
|
|
115
|
+
/**
|
|
116
|
+
* Set the table name used for storage.
|
|
117
|
+
*/
|
|
118
|
+
set table(value: string);
|
|
119
|
+
/**
|
|
120
|
+
* Get the maximum key length (VARCHAR length) for the key column.
|
|
121
|
+
* @default 255
|
|
122
|
+
*/
|
|
123
|
+
get keyLength(): number;
|
|
124
|
+
/**
|
|
125
|
+
* Set the maximum key length (VARCHAR length) for the key column.
|
|
126
|
+
*/
|
|
127
|
+
set keyLength(value: number);
|
|
128
|
+
/**
|
|
129
|
+
* Get the maximum namespace length (VARCHAR length) for the namespace column.
|
|
130
|
+
* @default 255
|
|
131
|
+
*/
|
|
132
|
+
get namespaceLength(): number;
|
|
133
|
+
/**
|
|
134
|
+
* Set the maximum namespace length (VARCHAR length) for the namespace column.
|
|
135
|
+
*/
|
|
136
|
+
set namespaceLength(value: number);
|
|
137
|
+
/**
|
|
138
|
+
* Get the PostgreSQL schema name.
|
|
139
|
+
* @default 'public'
|
|
140
|
+
*/
|
|
141
|
+
get schema(): string;
|
|
142
|
+
/**
|
|
143
|
+
* Set the PostgreSQL schema name.
|
|
144
|
+
*/
|
|
145
|
+
set schema(value: string);
|
|
146
|
+
/**
|
|
147
|
+
* Get the SSL configuration for the PostgreSQL connection.
|
|
148
|
+
* @default undefined
|
|
149
|
+
*/
|
|
150
|
+
get ssl(): boolean | ConnectionOptions | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* Set the SSL configuration for the PostgreSQL connection.
|
|
153
|
+
*/
|
|
154
|
+
set ssl(value: boolean | ConnectionOptions | undefined);
|
|
155
|
+
/**
|
|
156
|
+
* Get the number of rows to fetch per iteration batch.
|
|
157
|
+
* @default 10
|
|
158
|
+
*/
|
|
159
|
+
get iterationLimit(): number;
|
|
160
|
+
/**
|
|
161
|
+
* Set the number of rows to fetch per iteration batch.
|
|
162
|
+
*/
|
|
163
|
+
set iterationLimit(value: number);
|
|
164
|
+
/**
|
|
165
|
+
* Get whether to use a PostgreSQL unlogged table (faster writes, no WAL, data lost on crash).
|
|
166
|
+
* @default false
|
|
167
|
+
*/
|
|
168
|
+
get useUnloggedTable(): boolean;
|
|
169
|
+
/**
|
|
170
|
+
* Set whether to use a PostgreSQL unlogged table.
|
|
171
|
+
*/
|
|
172
|
+
set useUnloggedTable(value: boolean);
|
|
173
|
+
/**
|
|
174
|
+
* Get the interval in milliseconds between automatic expired-entry cleanup runs.
|
|
175
|
+
* A value of 0 means the automatic cleanup is disabled.
|
|
176
|
+
* @default 0
|
|
177
|
+
*/
|
|
178
|
+
get clearExpiredInterval(): number;
|
|
179
|
+
/**
|
|
180
|
+
* Set the interval in milliseconds between automatic expired-entry cleanup runs.
|
|
181
|
+
* Setting to 0 disables the automatic cleanup.
|
|
182
|
+
*/
|
|
183
|
+
set clearExpiredInterval(value: number);
|
|
184
|
+
/**
|
|
185
|
+
* Get the options for the adapter. This is required by the KeyvStoreAdapter interface.
|
|
186
|
+
*/
|
|
187
|
+
get opts(): any;
|
|
188
|
+
/**
|
|
189
|
+
* Set the options for the adapter.
|
|
190
|
+
*/
|
|
191
|
+
set opts(options: KeyvPostgresOptions);
|
|
192
|
+
/**
|
|
193
|
+
* Gets a value by key.
|
|
194
|
+
* @param key - The key to retrieve.
|
|
195
|
+
* @returns The value associated with the key, or `undefined` if not found.
|
|
196
|
+
*/
|
|
197
|
+
get<Value>(key: string): Promise<Value | undefined>;
|
|
198
|
+
/**
|
|
199
|
+
* Gets multiple values by their keys.
|
|
200
|
+
* @param keys - An array of keys to retrieve.
|
|
201
|
+
* @returns An array of values in the same order as the keys, with `undefined` for missing keys.
|
|
202
|
+
*/
|
|
203
|
+
getMany<Value>(keys: string[]): Promise<Array<Value | undefined>>;
|
|
204
|
+
/**
|
|
205
|
+
* Sets a key-value pair. Uses an upsert operation via `ON CONFLICT` to insert or update.
|
|
206
|
+
* @param key - The key to set.
|
|
207
|
+
* @param value - The value to store.
|
|
208
|
+
*/
|
|
25
209
|
set(key: string, value: any): Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* Sets multiple key-value pairs at once using PostgreSQL `UNNEST` for efficient bulk operations.
|
|
212
|
+
* @param entries - An array of key-value entry objects.
|
|
213
|
+
*/
|
|
26
214
|
setMany(entries: KeyvEntry[]): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Deletes a key from the store.
|
|
217
|
+
* @param key - The key to delete.
|
|
218
|
+
* @returns `true` if the key existed and was deleted, `false` otherwise.
|
|
219
|
+
*/
|
|
27
220
|
delete(key: string): Promise<boolean>;
|
|
221
|
+
/**
|
|
222
|
+
* Deletes multiple keys from the store at once.
|
|
223
|
+
* @param keys - An array of keys to delete.
|
|
224
|
+
* @returns `true` if any of the keys existed and were deleted, `false` otherwise.
|
|
225
|
+
*/
|
|
28
226
|
deleteMany(keys: string[]): Promise<boolean>;
|
|
227
|
+
/**
|
|
228
|
+
* Clears all keys in the current namespace. If no namespace is set, all keys are removed.
|
|
229
|
+
*/
|
|
29
230
|
clear(): Promise<void>;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Utility helper method to delete all expired entries from the store where the `expires` column is less than the current timestamp.
|
|
233
|
+
*/
|
|
234
|
+
clearExpired(): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Iterates over all key-value pairs, optionally filtered by namespace.
|
|
237
|
+
* Uses cursor-based (keyset) pagination with batch size controlled by `iterationLimit`.
|
|
238
|
+
* @param namespace - Optional namespace to filter keys by.
|
|
239
|
+
* @yields A `[key, value]` tuple for each entry.
|
|
240
|
+
*/
|
|
241
|
+
iterator(namespace?: string): AsyncGenerator<[string, string], void, unknown>;
|
|
242
|
+
/**
|
|
243
|
+
* Checks whether a key exists in the store.
|
|
244
|
+
* @param key - The key to check.
|
|
245
|
+
* @returns `true` if the key exists, `false` otherwise.
|
|
246
|
+
*/
|
|
247
|
+
has(key: string): Promise<boolean>;
|
|
248
|
+
/**
|
|
249
|
+
* Checks whether multiple keys exist in the store.
|
|
250
|
+
* @param keys - An array of keys to check.
|
|
251
|
+
* @returns An array of booleans in the same order as the input keys.
|
|
252
|
+
*/
|
|
253
|
+
hasMany(keys: string[]): Promise<boolean[]>;
|
|
254
|
+
/**
|
|
255
|
+
* Establishes a connection to the PostgreSQL database via the connection pool.
|
|
256
|
+
* @returns A query function that executes SQL statements and returns result rows.
|
|
257
|
+
*/
|
|
258
|
+
private connect;
|
|
259
|
+
/**
|
|
260
|
+
* Disconnects from the PostgreSQL database and releases the connection pool.
|
|
261
|
+
* Also stops the automatic expired-entry cleanup interval if running.
|
|
262
|
+
*/
|
|
33
263
|
disconnect(): Promise<void>;
|
|
264
|
+
/**
|
|
265
|
+
* Strips the namespace prefix from a key that was added by the Keyv core.
|
|
266
|
+
* For example, if namespace is "ns" and key is "ns:foo", returns "foo".
|
|
267
|
+
*/
|
|
268
|
+
private removeKeyPrefix;
|
|
269
|
+
/**
|
|
270
|
+
* Returns the namespace value for SQL parameters. Returns null when no namespace is set.
|
|
271
|
+
*/
|
|
272
|
+
private getNamespaceValue;
|
|
273
|
+
/**
|
|
274
|
+
* Extracts the `expires` timestamp from a serialized value.
|
|
275
|
+
* The Keyv core serializes data as JSON like `{"value":"...","expires":1234567890}`.
|
|
276
|
+
* Returns the expires value as a number, or null if not present or not parseable.
|
|
277
|
+
*/
|
|
278
|
+
private getExpiresFromValue;
|
|
279
|
+
/**
|
|
280
|
+
* Starts (or restarts) the automatic expired-entry cleanup interval.
|
|
281
|
+
* If the interval is 0 or negative, any existing timer is stopped.
|
|
282
|
+
*/
|
|
283
|
+
private startClearExpiredTimer;
|
|
284
|
+
/**
|
|
285
|
+
* Stops the automatic expired-entry cleanup interval if running.
|
|
286
|
+
*/
|
|
287
|
+
private stopClearExpiredTimer;
|
|
288
|
+
private setOptions;
|
|
34
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Helper function to create a Keyv instance with KeyvPostgres as the storage adapter.
|
|
292
|
+
* @param options - Optional {@link KeyvPostgresOptions} configuration object.
|
|
293
|
+
* @returns A new Keyv instance backed by PostgreSQL.
|
|
294
|
+
*/
|
|
35
295
|
declare const createKeyv: (options?: KeyvPostgresOptions) => Keyv<any>;
|
|
36
296
|
|
|
37
297
|
export { KeyvPostgres, type KeyvPostgresOptions, createKeyv, KeyvPostgres as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,297 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ConnectionOptions } from 'node:tls';
|
|
2
|
+
import { Hookified } from 'hookified';
|
|
2
3
|
import Keyv, { KeyvStoreAdapter, KeyvEntry } from 'keyv';
|
|
3
4
|
import { PoolConfig } from 'pg';
|
|
4
5
|
|
|
5
6
|
type KeyvPostgresOptions = {
|
|
6
7
|
uri?: string;
|
|
7
8
|
table?: string;
|
|
8
|
-
|
|
9
|
+
keyLength?: number;
|
|
10
|
+
namespaceLength?: number;
|
|
9
11
|
schema?: string;
|
|
10
|
-
ssl?:
|
|
11
|
-
dialect?: string;
|
|
12
|
+
ssl?: boolean | ConnectionOptions;
|
|
12
13
|
iterationLimit?: number;
|
|
13
14
|
useUnloggedTable?: boolean;
|
|
15
|
+
clearExpiredInterval?: number;
|
|
14
16
|
} & PoolConfig;
|
|
15
|
-
type Query = (sqlString: string, values?: any) => Promise<any[]>;
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
/**
|
|
19
|
+
* PostgreSQL storage adapter for Keyv.
|
|
20
|
+
* Uses the `pg` library for connection pooling and parameterized queries.
|
|
21
|
+
*/
|
|
22
|
+
declare class KeyvPostgres extends Hookified implements KeyvStoreAdapter {
|
|
23
|
+
/** Function for executing SQL queries against the PostgreSQL database. */
|
|
24
|
+
private query;
|
|
25
|
+
/** Promise that resolves to the query function once initialization completes. */
|
|
26
|
+
private _connected;
|
|
27
|
+
/** The namespace used to prefix keys for multi-tenant separation. */
|
|
28
|
+
private _namespace?;
|
|
29
|
+
/**
|
|
30
|
+
* The PostgreSQL connection URI.
|
|
31
|
+
* @default 'postgresql://localhost:5432'
|
|
32
|
+
*/
|
|
33
|
+
private _uri;
|
|
34
|
+
/**
|
|
35
|
+
* The table name used for storage.
|
|
36
|
+
* @default 'keyv'
|
|
37
|
+
*/
|
|
38
|
+
private _table;
|
|
39
|
+
/**
|
|
40
|
+
* The maximum key length (VARCHAR length) for the key column.
|
|
41
|
+
* @default 255
|
|
42
|
+
*/
|
|
43
|
+
private _keyLength;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum namespace length (VARCHAR length) for the namespace column.
|
|
46
|
+
* @default 255
|
|
47
|
+
*/
|
|
48
|
+
private _namespaceLength;
|
|
49
|
+
/**
|
|
50
|
+
* The PostgreSQL schema name.
|
|
51
|
+
* @default 'public'
|
|
52
|
+
*/
|
|
53
|
+
private _schema;
|
|
54
|
+
/**
|
|
55
|
+
* The SSL configuration for the PostgreSQL connection.
|
|
56
|
+
* @default undefined
|
|
57
|
+
*/
|
|
58
|
+
private _ssl?;
|
|
59
|
+
/**
|
|
60
|
+
* The number of rows to fetch per iteration batch.
|
|
61
|
+
* @default 10
|
|
62
|
+
*/
|
|
63
|
+
private _iterationLimit;
|
|
64
|
+
/**
|
|
65
|
+
* Whether to use a PostgreSQL unlogged table (faster writes, no WAL, data lost on crash).
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
private _useUnloggedTable;
|
|
69
|
+
/**
|
|
70
|
+
* The interval in milliseconds between automatic expired-entry cleanup runs.
|
|
71
|
+
* A value of 0 (default) disables the automatic cleanup.
|
|
72
|
+
* @default 0
|
|
73
|
+
*/
|
|
74
|
+
private _clearExpiredInterval;
|
|
75
|
+
/**
|
|
76
|
+
* The timer reference for the automatic expired-entry cleanup interval.
|
|
77
|
+
*/
|
|
78
|
+
private _clearExpiredTimer?;
|
|
79
|
+
/**
|
|
80
|
+
* Additional PoolConfig properties passed through to the pg connection pool.
|
|
81
|
+
*/
|
|
82
|
+
private _poolConfig;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new KeyvPostgres instance.
|
|
85
|
+
* @param options - A PostgreSQL connection URI string or a {@link KeyvPostgresOptions} configuration object.
|
|
86
|
+
*/
|
|
22
87
|
constructor(options?: KeyvPostgresOptions | string);
|
|
23
|
-
|
|
24
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Initializes the database connection and ensures the table schema exists.
|
|
90
|
+
* Called from the constructor; errors are emitted rather than thrown.
|
|
91
|
+
*/
|
|
92
|
+
private init;
|
|
93
|
+
/**
|
|
94
|
+
* Get the namespace for the adapter. If undefined, no namespace prefix is applied.
|
|
95
|
+
*/
|
|
96
|
+
get namespace(): string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Set the namespace for the adapter. Used for key prefixing and scoping operations like `clear()`.
|
|
99
|
+
*/
|
|
100
|
+
set namespace(value: string | undefined);
|
|
101
|
+
/**
|
|
102
|
+
* Get the PostgreSQL connection URI.
|
|
103
|
+
* @default 'postgresql://localhost:5432'
|
|
104
|
+
*/
|
|
105
|
+
get uri(): string;
|
|
106
|
+
/**
|
|
107
|
+
* Set the PostgreSQL connection URI.
|
|
108
|
+
*/
|
|
109
|
+
set uri(value: string);
|
|
110
|
+
/**
|
|
111
|
+
* Get the table name used for storage.
|
|
112
|
+
* @default 'keyv'
|
|
113
|
+
*/
|
|
114
|
+
get table(): string;
|
|
115
|
+
/**
|
|
116
|
+
* Set the table name used for storage.
|
|
117
|
+
*/
|
|
118
|
+
set table(value: string);
|
|
119
|
+
/**
|
|
120
|
+
* Get the maximum key length (VARCHAR length) for the key column.
|
|
121
|
+
* @default 255
|
|
122
|
+
*/
|
|
123
|
+
get keyLength(): number;
|
|
124
|
+
/**
|
|
125
|
+
* Set the maximum key length (VARCHAR length) for the key column.
|
|
126
|
+
*/
|
|
127
|
+
set keyLength(value: number);
|
|
128
|
+
/**
|
|
129
|
+
* Get the maximum namespace length (VARCHAR length) for the namespace column.
|
|
130
|
+
* @default 255
|
|
131
|
+
*/
|
|
132
|
+
get namespaceLength(): number;
|
|
133
|
+
/**
|
|
134
|
+
* Set the maximum namespace length (VARCHAR length) for the namespace column.
|
|
135
|
+
*/
|
|
136
|
+
set namespaceLength(value: number);
|
|
137
|
+
/**
|
|
138
|
+
* Get the PostgreSQL schema name.
|
|
139
|
+
* @default 'public'
|
|
140
|
+
*/
|
|
141
|
+
get schema(): string;
|
|
142
|
+
/**
|
|
143
|
+
* Set the PostgreSQL schema name.
|
|
144
|
+
*/
|
|
145
|
+
set schema(value: string);
|
|
146
|
+
/**
|
|
147
|
+
* Get the SSL configuration for the PostgreSQL connection.
|
|
148
|
+
* @default undefined
|
|
149
|
+
*/
|
|
150
|
+
get ssl(): boolean | ConnectionOptions | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* Set the SSL configuration for the PostgreSQL connection.
|
|
153
|
+
*/
|
|
154
|
+
set ssl(value: boolean | ConnectionOptions | undefined);
|
|
155
|
+
/**
|
|
156
|
+
* Get the number of rows to fetch per iteration batch.
|
|
157
|
+
* @default 10
|
|
158
|
+
*/
|
|
159
|
+
get iterationLimit(): number;
|
|
160
|
+
/**
|
|
161
|
+
* Set the number of rows to fetch per iteration batch.
|
|
162
|
+
*/
|
|
163
|
+
set iterationLimit(value: number);
|
|
164
|
+
/**
|
|
165
|
+
* Get whether to use a PostgreSQL unlogged table (faster writes, no WAL, data lost on crash).
|
|
166
|
+
* @default false
|
|
167
|
+
*/
|
|
168
|
+
get useUnloggedTable(): boolean;
|
|
169
|
+
/**
|
|
170
|
+
* Set whether to use a PostgreSQL unlogged table.
|
|
171
|
+
*/
|
|
172
|
+
set useUnloggedTable(value: boolean);
|
|
173
|
+
/**
|
|
174
|
+
* Get the interval in milliseconds between automatic expired-entry cleanup runs.
|
|
175
|
+
* A value of 0 means the automatic cleanup is disabled.
|
|
176
|
+
* @default 0
|
|
177
|
+
*/
|
|
178
|
+
get clearExpiredInterval(): number;
|
|
179
|
+
/**
|
|
180
|
+
* Set the interval in milliseconds between automatic expired-entry cleanup runs.
|
|
181
|
+
* Setting to 0 disables the automatic cleanup.
|
|
182
|
+
*/
|
|
183
|
+
set clearExpiredInterval(value: number);
|
|
184
|
+
/**
|
|
185
|
+
* Get the options for the adapter. This is required by the KeyvStoreAdapter interface.
|
|
186
|
+
*/
|
|
187
|
+
get opts(): any;
|
|
188
|
+
/**
|
|
189
|
+
* Set the options for the adapter.
|
|
190
|
+
*/
|
|
191
|
+
set opts(options: KeyvPostgresOptions);
|
|
192
|
+
/**
|
|
193
|
+
* Gets a value by key.
|
|
194
|
+
* @param key - The key to retrieve.
|
|
195
|
+
* @returns The value associated with the key, or `undefined` if not found.
|
|
196
|
+
*/
|
|
197
|
+
get<Value>(key: string): Promise<Value | undefined>;
|
|
198
|
+
/**
|
|
199
|
+
* Gets multiple values by their keys.
|
|
200
|
+
* @param keys - An array of keys to retrieve.
|
|
201
|
+
* @returns An array of values in the same order as the keys, with `undefined` for missing keys.
|
|
202
|
+
*/
|
|
203
|
+
getMany<Value>(keys: string[]): Promise<Array<Value | undefined>>;
|
|
204
|
+
/**
|
|
205
|
+
* Sets a key-value pair. Uses an upsert operation via `ON CONFLICT` to insert or update.
|
|
206
|
+
* @param key - The key to set.
|
|
207
|
+
* @param value - The value to store.
|
|
208
|
+
*/
|
|
25
209
|
set(key: string, value: any): Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* Sets multiple key-value pairs at once using PostgreSQL `UNNEST` for efficient bulk operations.
|
|
212
|
+
* @param entries - An array of key-value entry objects.
|
|
213
|
+
*/
|
|
26
214
|
setMany(entries: KeyvEntry[]): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Deletes a key from the store.
|
|
217
|
+
* @param key - The key to delete.
|
|
218
|
+
* @returns `true` if the key existed and was deleted, `false` otherwise.
|
|
219
|
+
*/
|
|
27
220
|
delete(key: string): Promise<boolean>;
|
|
221
|
+
/**
|
|
222
|
+
* Deletes multiple keys from the store at once.
|
|
223
|
+
* @param keys - An array of keys to delete.
|
|
224
|
+
* @returns `true` if any of the keys existed and were deleted, `false` otherwise.
|
|
225
|
+
*/
|
|
28
226
|
deleteMany(keys: string[]): Promise<boolean>;
|
|
227
|
+
/**
|
|
228
|
+
* Clears all keys in the current namespace. If no namespace is set, all keys are removed.
|
|
229
|
+
*/
|
|
29
230
|
clear(): Promise<void>;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Utility helper method to delete all expired entries from the store where the `expires` column is less than the current timestamp.
|
|
233
|
+
*/
|
|
234
|
+
clearExpired(): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Iterates over all key-value pairs, optionally filtered by namespace.
|
|
237
|
+
* Uses cursor-based (keyset) pagination with batch size controlled by `iterationLimit`.
|
|
238
|
+
* @param namespace - Optional namespace to filter keys by.
|
|
239
|
+
* @yields A `[key, value]` tuple for each entry.
|
|
240
|
+
*/
|
|
241
|
+
iterator(namespace?: string): AsyncGenerator<[string, string], void, unknown>;
|
|
242
|
+
/**
|
|
243
|
+
* Checks whether a key exists in the store.
|
|
244
|
+
* @param key - The key to check.
|
|
245
|
+
* @returns `true` if the key exists, `false` otherwise.
|
|
246
|
+
*/
|
|
247
|
+
has(key: string): Promise<boolean>;
|
|
248
|
+
/**
|
|
249
|
+
* Checks whether multiple keys exist in the store.
|
|
250
|
+
* @param keys - An array of keys to check.
|
|
251
|
+
* @returns An array of booleans in the same order as the input keys.
|
|
252
|
+
*/
|
|
253
|
+
hasMany(keys: string[]): Promise<boolean[]>;
|
|
254
|
+
/**
|
|
255
|
+
* Establishes a connection to the PostgreSQL database via the connection pool.
|
|
256
|
+
* @returns A query function that executes SQL statements and returns result rows.
|
|
257
|
+
*/
|
|
258
|
+
private connect;
|
|
259
|
+
/**
|
|
260
|
+
* Disconnects from the PostgreSQL database and releases the connection pool.
|
|
261
|
+
* Also stops the automatic expired-entry cleanup interval if running.
|
|
262
|
+
*/
|
|
33
263
|
disconnect(): Promise<void>;
|
|
264
|
+
/**
|
|
265
|
+
* Strips the namespace prefix from a key that was added by the Keyv core.
|
|
266
|
+
* For example, if namespace is "ns" and key is "ns:foo", returns "foo".
|
|
267
|
+
*/
|
|
268
|
+
private removeKeyPrefix;
|
|
269
|
+
/**
|
|
270
|
+
* Returns the namespace value for SQL parameters. Returns null when no namespace is set.
|
|
271
|
+
*/
|
|
272
|
+
private getNamespaceValue;
|
|
273
|
+
/**
|
|
274
|
+
* Extracts the `expires` timestamp from a serialized value.
|
|
275
|
+
* The Keyv core serializes data as JSON like `{"value":"...","expires":1234567890}`.
|
|
276
|
+
* Returns the expires value as a number, or null if not present or not parseable.
|
|
277
|
+
*/
|
|
278
|
+
private getExpiresFromValue;
|
|
279
|
+
/**
|
|
280
|
+
* Starts (or restarts) the automatic expired-entry cleanup interval.
|
|
281
|
+
* If the interval is 0 or negative, any existing timer is stopped.
|
|
282
|
+
*/
|
|
283
|
+
private startClearExpiredTimer;
|
|
284
|
+
/**
|
|
285
|
+
* Stops the automatic expired-entry cleanup interval if running.
|
|
286
|
+
*/
|
|
287
|
+
private stopClearExpiredTimer;
|
|
288
|
+
private setOptions;
|
|
34
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Helper function to create a Keyv instance with KeyvPostgres as the storage adapter.
|
|
292
|
+
* @param options - Optional {@link KeyvPostgresOptions} configuration object.
|
|
293
|
+
* @returns A new Keyv instance backed by PostgreSQL.
|
|
294
|
+
*/
|
|
35
295
|
declare const createKeyv: (options?: KeyvPostgresOptions) => Keyv<any>;
|
|
36
296
|
|
|
37
297
|
export { KeyvPostgres, type KeyvPostgresOptions, createKeyv, KeyvPostgres as default };
|