@agentuity/postgres 0.1.41

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.
Files changed (48) hide show
  1. package/AGENTS.md +124 -0
  2. package/README.md +297 -0
  3. package/dist/client.d.ts +224 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +670 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/errors.d.ts +109 -0
  8. package/dist/errors.d.ts.map +1 -0
  9. package/dist/errors.js +115 -0
  10. package/dist/errors.js.map +1 -0
  11. package/dist/index.d.ts +41 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +47 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/patch.d.ts +65 -0
  16. package/dist/patch.d.ts.map +1 -0
  17. package/dist/patch.js +111 -0
  18. package/dist/patch.js.map +1 -0
  19. package/dist/postgres.d.ts +62 -0
  20. package/dist/postgres.d.ts.map +1 -0
  21. package/dist/postgres.js +63 -0
  22. package/dist/postgres.js.map +1 -0
  23. package/dist/reconnect.d.ts +31 -0
  24. package/dist/reconnect.d.ts.map +1 -0
  25. package/dist/reconnect.js +60 -0
  26. package/dist/reconnect.js.map +1 -0
  27. package/dist/registry.d.ts +71 -0
  28. package/dist/registry.d.ts.map +1 -0
  29. package/dist/registry.js +175 -0
  30. package/dist/registry.js.map +1 -0
  31. package/dist/transaction.d.ts +147 -0
  32. package/dist/transaction.d.ts.map +1 -0
  33. package/dist/transaction.js +287 -0
  34. package/dist/transaction.js.map +1 -0
  35. package/dist/types.d.ts +213 -0
  36. package/dist/types.d.ts.map +1 -0
  37. package/dist/types.js +2 -0
  38. package/dist/types.js.map +1 -0
  39. package/package.json +55 -0
  40. package/src/client.ts +776 -0
  41. package/src/errors.ts +154 -0
  42. package/src/index.ts +71 -0
  43. package/src/patch.ts +123 -0
  44. package/src/postgres.ts +65 -0
  45. package/src/reconnect.ts +74 -0
  46. package/src/registry.ts +194 -0
  47. package/src/transaction.ts +312 -0
  48. package/src/types.ts +250 -0
@@ -0,0 +1,31 @@
1
+ import type { ReconnectConfig } from './types';
2
+ /**
3
+ * Default reconnection configuration values.
4
+ */
5
+ export declare const DEFAULT_RECONNECT_CONFIG: Required<ReconnectConfig>;
6
+ /**
7
+ * Computes the backoff delay for a reconnection attempt using exponential backoff with jitter.
8
+ *
9
+ * The delay is calculated as:
10
+ * `min(maxDelayMs, initialDelayMs * (multiplier ^ attempt)) + random(0, jitterMs)`
11
+ *
12
+ * @param attempt - The current attempt number (0-indexed)
13
+ * @param config - Reconnection configuration
14
+ * @returns The delay in milliseconds before the next reconnection attempt
15
+ */
16
+ export declare function computeBackoff(attempt: number, config?: Partial<ReconnectConfig>): number;
17
+ /**
18
+ * Sleeps for the specified number of milliseconds.
19
+ *
20
+ * @param ms - The number of milliseconds to sleep
21
+ * @returns A promise that resolves after the specified delay
22
+ */
23
+ export declare function sleep(ms: number): Promise<void>;
24
+ /**
25
+ * Merges user-provided reconnect config with defaults.
26
+ *
27
+ * @param config - User-provided reconnect configuration
28
+ * @returns Complete reconnect configuration with all values filled in
29
+ */
30
+ export declare function mergeReconnectConfig(config?: Partial<ReconnectConfig>): Required<ReconnectConfig>;
31
+ //# sourceMappingURL=reconnect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconnect.d.ts","sourceRoot":"","sources":["../src/reconnect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,QAAQ,CAAC,eAAe,CAO9D,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,CAAC,eAAe,CAAM,GAAG,MAAM,CAkB7F;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,QAAQ,CAAC,eAAe,CAAC,CAajG"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Default reconnection configuration values.
3
+ */
4
+ export const DEFAULT_RECONNECT_CONFIG = {
5
+ maxAttempts: 10,
6
+ initialDelayMs: 100,
7
+ maxDelayMs: 30000,
8
+ multiplier: 2,
9
+ jitterMs: 1000,
10
+ enabled: true,
11
+ };
12
+ /**
13
+ * Computes the backoff delay for a reconnection attempt using exponential backoff with jitter.
14
+ *
15
+ * The delay is calculated as:
16
+ * `min(maxDelayMs, initialDelayMs * (multiplier ^ attempt)) + random(0, jitterMs)`
17
+ *
18
+ * @param attempt - The current attempt number (0-indexed)
19
+ * @param config - Reconnection configuration
20
+ * @returns The delay in milliseconds before the next reconnection attempt
21
+ */
22
+ export function computeBackoff(attempt, config = {}) {
23
+ const { initialDelayMs = DEFAULT_RECONNECT_CONFIG.initialDelayMs, maxDelayMs = DEFAULT_RECONNECT_CONFIG.maxDelayMs, multiplier = DEFAULT_RECONNECT_CONFIG.multiplier, jitterMs = DEFAULT_RECONNECT_CONFIG.jitterMs, } = config;
24
+ // Calculate exponential delay
25
+ const exponentialDelay = initialDelayMs * Math.pow(multiplier, attempt);
26
+ // Cap at maximum delay
27
+ const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
28
+ // Add random jitter to prevent thundering herd
29
+ const jitter = Math.random() * jitterMs;
30
+ return Math.floor(cappedDelay + jitter);
31
+ }
32
+ /**
33
+ * Sleeps for the specified number of milliseconds.
34
+ *
35
+ * @param ms - The number of milliseconds to sleep
36
+ * @returns A promise that resolves after the specified delay
37
+ */
38
+ export function sleep(ms) {
39
+ return new Promise((resolve) => setTimeout(resolve, ms));
40
+ }
41
+ /**
42
+ * Merges user-provided reconnect config with defaults.
43
+ *
44
+ * @param config - User-provided reconnect configuration
45
+ * @returns Complete reconnect configuration with all values filled in
46
+ */
47
+ export function mergeReconnectConfig(config) {
48
+ if (!config) {
49
+ return { ...DEFAULT_RECONNECT_CONFIG };
50
+ }
51
+ return {
52
+ maxAttempts: config.maxAttempts ?? DEFAULT_RECONNECT_CONFIG.maxAttempts,
53
+ initialDelayMs: config.initialDelayMs ?? DEFAULT_RECONNECT_CONFIG.initialDelayMs,
54
+ maxDelayMs: config.maxDelayMs ?? DEFAULT_RECONNECT_CONFIG.maxDelayMs,
55
+ multiplier: config.multiplier ?? DEFAULT_RECONNECT_CONFIG.multiplier,
56
+ jitterMs: config.jitterMs ?? DEFAULT_RECONNECT_CONFIG.jitterMs,
57
+ enabled: config.enabled ?? DEFAULT_RECONNECT_CONFIG.enabled,
58
+ };
59
+ }
60
+ //# sourceMappingURL=reconnect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconnect.js","sourceRoot":"","sources":["../src/reconnect.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAA8B;IAClE,WAAW,EAAE,EAAE;IACf,cAAc,EAAE,GAAG;IACnB,UAAU,EAAE,KAAK;IACjB,UAAU,EAAE,CAAC;IACb,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE,IAAI;CACb,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,SAAmC,EAAE;IACpF,MAAM,EACL,cAAc,GAAG,wBAAwB,CAAC,cAAc,EACxD,UAAU,GAAG,wBAAwB,CAAC,UAAU,EAChD,UAAU,GAAG,wBAAwB,CAAC,UAAU,EAChD,QAAQ,GAAG,wBAAwB,CAAC,QAAQ,GAC5C,GAAG,MAAM,CAAC;IAEX,8BAA8B;IAC9B,MAAM,gBAAgB,GAAG,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAExE,uBAAuB;IACvB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,UAAU,CAAC,CAAC;IAE3D,+CAA+C;IAC/C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,QAAQ,CAAC;IAExC,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,EAAU;IAC/B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAiC;IACrE,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,OAAO,EAAE,GAAG,wBAAwB,EAAE,CAAC;IACxC,CAAC;IAED,OAAO;QACN,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,wBAAwB,CAAC,WAAW;QACvE,cAAc,EAAE,MAAM,CAAC,cAAc,IAAI,wBAAwB,CAAC,cAAc;QAChF,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,wBAAwB,CAAC,UAAU;QACpE,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,wBAAwB,CAAC,UAAU;QACpE,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,wBAAwB,CAAC,QAAQ;QAC9D,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,wBAAwB,CAAC,OAAO;KAC3D,CAAC;AACH,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Global registry for PostgreSQL clients.
3
+ *
4
+ * This module provides a way to track all active PostgreSQL clients
5
+ * so they can be gracefully shut down together (e.g., on process exit).
6
+ *
7
+ * The runtime can use `shutdownAll()` to close all registered clients
8
+ * during graceful shutdown.
9
+ *
10
+ * When @agentuity/runtime is available, this module automatically registers
11
+ * a shutdown hook so all postgres clients are closed during graceful shutdown.
12
+ */
13
+ import type { PostgresClient } from './client';
14
+ /**
15
+ * Registers a client in the global registry.
16
+ * Called automatically when a client is created.
17
+ *
18
+ * @param client - The client to register
19
+ * @internal
20
+ */
21
+ export declare function registerClient(client: PostgresClient): void;
22
+ /**
23
+ * Unregisters a client from the global registry.
24
+ * Called automatically when a client is closed.
25
+ *
26
+ * @param client - The client to unregister
27
+ * @internal
28
+ */
29
+ export declare function unregisterClient(client: PostgresClient): void;
30
+ /**
31
+ * Returns the number of registered clients.
32
+ * Useful for debugging and testing.
33
+ */
34
+ export declare function getClientCount(): number;
35
+ /**
36
+ * Returns all registered clients.
37
+ * Useful for debugging and monitoring.
38
+ */
39
+ export declare function getClients(): ReadonlySet<PostgresClient>;
40
+ /**
41
+ * Shuts down all registered PostgreSQL clients gracefully.
42
+ *
43
+ * This function:
44
+ * 1. Signals shutdown to all clients (prevents reconnection)
45
+ * 2. Closes all clients in parallel
46
+ * 3. Clears the registry
47
+ *
48
+ * This is intended to be called by the runtime during graceful shutdown.
49
+ *
50
+ * @param timeoutMs - Optional timeout in milliseconds. If provided, the function
51
+ * will resolve after the timeout even if some clients haven't
52
+ * finished closing. Default: no timeout.
53
+ * @returns A promise that resolves when all clients are closed (or timeout)
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * import { shutdownAll } from '@agentuity/postgres';
58
+ *
59
+ * process.on('SIGTERM', async () => {
60
+ * await shutdownAll(5000); // 5 second timeout
61
+ * process.exit(0);
62
+ * });
63
+ * ```
64
+ */
65
+ export declare function shutdownAll(timeoutMs?: number): Promise<void>;
66
+ /**
67
+ * Checks if there are any active (non-shutdown) clients.
68
+ * Useful for health checks.
69
+ */
70
+ export declare function hasActiveClients(): boolean;
71
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAuB/C;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAE7D;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED;;;GAGG;AACH,wBAAgB,UAAU,IAAI,WAAW,CAAC,cAAc,CAAC,CAExD;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,WAAW,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAyCnE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,OAAO,CAQ1C"}
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Global registry for PostgreSQL clients.
3
+ *
4
+ * This module provides a way to track all active PostgreSQL clients
5
+ * so they can be gracefully shut down together (e.g., on process exit).
6
+ *
7
+ * The runtime can use `shutdownAll()` to close all registered clients
8
+ * during graceful shutdown.
9
+ *
10
+ * When @agentuity/runtime is available, this module automatically registers
11
+ * a shutdown hook so all postgres clients are closed during graceful shutdown.
12
+ */
13
+ /**
14
+ * Symbol used to store the registry in globalThis to avoid conflicts.
15
+ */
16
+ const REGISTRY_KEY = Symbol.for('@agentuity/postgres:registry');
17
+ /**
18
+ * Symbol used to track if we've registered with the runtime.
19
+ */
20
+ const RUNTIME_HOOK_REGISTERED = Symbol.for('@agentuity/postgres:runtime-hook-registered');
21
+ /**
22
+ * Gets the global client registry, creating it if it doesn't exist.
23
+ */
24
+ function getRegistry() {
25
+ const global = globalThis;
26
+ if (!global[REGISTRY_KEY]) {
27
+ global[REGISTRY_KEY] = new Set();
28
+ }
29
+ return global[REGISTRY_KEY];
30
+ }
31
+ /**
32
+ * Registers a client in the global registry.
33
+ * Called automatically when a client is created.
34
+ *
35
+ * @param client - The client to register
36
+ * @internal
37
+ */
38
+ export function registerClient(client) {
39
+ getRegistry().add(client);
40
+ }
41
+ /**
42
+ * Unregisters a client from the global registry.
43
+ * Called automatically when a client is closed.
44
+ *
45
+ * @param client - The client to unregister
46
+ * @internal
47
+ */
48
+ export function unregisterClient(client) {
49
+ getRegistry().delete(client);
50
+ }
51
+ /**
52
+ * Returns the number of registered clients.
53
+ * Useful for debugging and testing.
54
+ */
55
+ export function getClientCount() {
56
+ return getRegistry().size;
57
+ }
58
+ /**
59
+ * Returns all registered clients.
60
+ * Useful for debugging and monitoring.
61
+ */
62
+ export function getClients() {
63
+ return getRegistry();
64
+ }
65
+ /**
66
+ * Shuts down all registered PostgreSQL clients gracefully.
67
+ *
68
+ * This function:
69
+ * 1. Signals shutdown to all clients (prevents reconnection)
70
+ * 2. Closes all clients in parallel
71
+ * 3. Clears the registry
72
+ *
73
+ * This is intended to be called by the runtime during graceful shutdown.
74
+ *
75
+ * @param timeoutMs - Optional timeout in milliseconds. If provided, the function
76
+ * will resolve after the timeout even if some clients haven't
77
+ * finished closing. Default: no timeout.
78
+ * @returns A promise that resolves when all clients are closed (or timeout)
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { shutdownAll } from '@agentuity/postgres';
83
+ *
84
+ * process.on('SIGTERM', async () => {
85
+ * await shutdownAll(5000); // 5 second timeout
86
+ * process.exit(0);
87
+ * });
88
+ * ```
89
+ */
90
+ export async function shutdownAll(timeoutMs) {
91
+ const registry = getRegistry();
92
+ const clients = Array.from(registry);
93
+ if (clients.length === 0) {
94
+ return;
95
+ }
96
+ // Signal shutdown to all clients first (prevents reconnection attempts)
97
+ for (const client of clients) {
98
+ client.shutdown();
99
+ }
100
+ // Close all clients in parallel
101
+ const closePromises = clients.map(async (client) => {
102
+ try {
103
+ await client.close();
104
+ }
105
+ catch {
106
+ // Ignore close errors during shutdown
107
+ }
108
+ });
109
+ // Wait for all to close, with optional timeout
110
+ if (timeoutMs !== undefined) {
111
+ let timer;
112
+ const timeout = new Promise((resolve) => {
113
+ timer = setTimeout(resolve, timeoutMs);
114
+ });
115
+ try {
116
+ await Promise.race([Promise.all(closePromises), timeout]);
117
+ }
118
+ finally {
119
+ if (timer !== undefined) {
120
+ clearTimeout(timer);
121
+ }
122
+ }
123
+ }
124
+ else {
125
+ await Promise.all(closePromises);
126
+ }
127
+ // Clear the registry
128
+ registry.clear();
129
+ }
130
+ /**
131
+ * Checks if there are any active (non-shutdown) clients.
132
+ * Useful for health checks.
133
+ */
134
+ export function hasActiveClients() {
135
+ const registry = getRegistry();
136
+ for (const client of registry) {
137
+ if (!client.shuttingDown) {
138
+ return true;
139
+ }
140
+ }
141
+ return false;
142
+ }
143
+ /**
144
+ * Attempts to register a shutdown hook with @agentuity/runtime if available.
145
+ * This is called automatically when the first client is registered.
146
+ *
147
+ * @internal
148
+ */
149
+ function tryRegisterRuntimeHook() {
150
+ const global = globalThis;
151
+ // Only try once
152
+ if (global[RUNTIME_HOOK_REGISTERED]) {
153
+ return;
154
+ }
155
+ global[RUNTIME_HOOK_REGISTERED] = true;
156
+ // Try to dynamically import the runtime and register our shutdown hook
157
+ // This is done asynchronously to avoid blocking client creation
158
+ // and to handle the case where runtime is not available
159
+ // Using Function constructor to avoid TypeScript trying to resolve the module at build time
160
+ const dynamicImport = new Function('specifier', 'return import(specifier)');
161
+ dynamicImport('@agentuity/runtime')
162
+ .then((runtime) => {
163
+ if (typeof runtime.registerShutdownHook === 'function') {
164
+ runtime.registerShutdownHook(async () => {
165
+ await shutdownAll(5000); // 5 second timeout for graceful shutdown
166
+ });
167
+ }
168
+ })
169
+ .catch(() => {
170
+ // Runtime not available - that's fine, user can call shutdownAll manually
171
+ });
172
+ }
173
+ // Try to register with runtime when this module is first loaded
174
+ tryRegisterRuntimeHook();
175
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH;;GAEG;AACH,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;AAEhE;;GAEG;AAC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IAEvC,uEAAuE;IACvE,gEAAgE;IAChE,wDAAwD;IACxD,4FAA4F;IAC5F,MAAM,aAAa,GAAG,IAAI,QAAQ,CAAC,WAAW,EAAE,0BAA0B,CAES,CAAC;IAEpF,aAAa,CAAC,oBAAoB,CAAC;SACjC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;QACjB,IAAI,OAAO,OAAO,CAAC,oBAAoB,KAAK,UAAU,EAAE,CAAC;YACxD,OAAO,CAAC,oBAAoB,CAAC,KAAK,IAAI,EAAE;gBACvC,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,yCAAyC;YACnE,CAAC,CAAC,CAAC;QACJ,CAAC;IACF,CAAC,CAAC;SACD,KAAK,CAAC,GAAG,EAAE;QACX,0EAA0E;IAC3E,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gEAAgE;AAChE,sBAAsB,EAAE,CAAC"}
@@ -0,0 +1,147 @@
1
+ import type { SQL, SQLQuery } from 'bun';
2
+ /**
3
+ * Represents a PostgreSQL transaction with support for savepoints.
4
+ *
5
+ * Transactions are created via `PostgresClient.begin()` and support
6
+ * tagged template literal syntax for queries.
7
+ */
8
+ export declare class Transaction {
9
+ private _sql;
10
+ private _connection;
11
+ private _committed;
12
+ private _rolledBack;
13
+ private _savepointCounter;
14
+ constructor(sql: SQL, connection: SQLQuery);
15
+ /**
16
+ * Whether the transaction has been committed.
17
+ */
18
+ get committed(): boolean;
19
+ /**
20
+ * Whether the transaction has been rolled back.
21
+ */
22
+ get rolledBack(): boolean;
23
+ /**
24
+ * Whether the transaction is still active (not committed or rolled back).
25
+ */
26
+ get active(): boolean;
27
+ /**
28
+ * Execute a query within this transaction using tagged template literal syntax.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const tx = await client.begin();
33
+ * const result = await tx`SELECT * FROM users WHERE id = ${userId}`;
34
+ * await tx.commit();
35
+ * ```
36
+ */
37
+ query(strings: TemplateStringsArray, ...values: unknown[]): SQLQuery;
38
+ /**
39
+ * Create a savepoint within this transaction.
40
+ *
41
+ * @param name - Optional name for the savepoint. If not provided, a unique name is generated.
42
+ * If provided, must be a valid SQL identifier (alphanumeric and underscores only,
43
+ * starting with a letter or underscore).
44
+ * @returns A Savepoint object that can be used to rollback to this point.
45
+ * @throws {TransactionError} If the provided name is not a valid SQL identifier.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const tx = await client.begin();
50
+ * await tx`INSERT INTO users (name) VALUES ('Alice')`;
51
+ *
52
+ * const sp = await tx.savepoint();
53
+ * await tx`INSERT INTO users (name) VALUES ('Bob')`;
54
+ *
55
+ * // Oops, rollback Bob but keep Alice
56
+ * await sp.rollback();
57
+ *
58
+ * await tx.commit(); // Only Alice is committed
59
+ * ```
60
+ */
61
+ savepoint(name?: string): Promise<Savepoint>;
62
+ /**
63
+ * Commit the transaction.
64
+ *
65
+ * @throws {TransactionError} If the transaction is not active or commit fails.
66
+ */
67
+ commit(): Promise<void>;
68
+ /**
69
+ * Rollback the transaction.
70
+ *
71
+ * @throws {TransactionError} If the transaction is not active or rollback fails.
72
+ */
73
+ rollback(): Promise<void>;
74
+ /**
75
+ * Ensures the transaction is still active.
76
+ */
77
+ private _ensureActive;
78
+ }
79
+ /**
80
+ * Represents a savepoint within a transaction.
81
+ */
82
+ export declare class Savepoint {
83
+ private _sql;
84
+ private _name;
85
+ private _released;
86
+ private _rolledBack;
87
+ constructor(sql: SQL, name: string);
88
+ /**
89
+ * The name of this savepoint.
90
+ */
91
+ get name(): string;
92
+ /**
93
+ * Whether the savepoint has been released.
94
+ */
95
+ get released(): boolean;
96
+ /**
97
+ * Whether the savepoint has been rolled back to.
98
+ */
99
+ get rolledBack(): boolean;
100
+ /**
101
+ * Rollback to this savepoint.
102
+ * All changes made after this savepoint was created will be undone.
103
+ *
104
+ * @throws {TransactionError} If the savepoint has been released or already rolled back.
105
+ */
106
+ rollback(): Promise<void>;
107
+ /**
108
+ * Release this savepoint.
109
+ * The savepoint is destroyed but changes are kept.
110
+ */
111
+ release(): Promise<void>;
112
+ }
113
+ /**
114
+ * Represents a reserved (exclusive) connection from the pool.
115
+ *
116
+ * Reserved connections are created via `PostgresClient.reserve()` and support
117
+ * tagged template literal syntax for queries.
118
+ */
119
+ export declare class ReservedConnection {
120
+ private _sql;
121
+ private _released;
122
+ constructor(sql: SQL);
123
+ /**
124
+ * Whether the connection has been released back to the pool.
125
+ */
126
+ get released(): boolean;
127
+ /**
128
+ * Execute a query on this reserved connection using tagged template literal syntax.
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * const conn = await client.reserve();
133
+ * try {
134
+ * await conn`SET LOCAL timezone = 'UTC'`;
135
+ * const result = await conn`SELECT NOW()`;
136
+ * } finally {
137
+ * conn.release();
138
+ * }
139
+ * ```
140
+ */
141
+ query(strings: TemplateStringsArray, ...values: unknown[]): SQLQuery;
142
+ /**
143
+ * Release the connection back to the pool.
144
+ */
145
+ release(): void;
146
+ }
147
+ //# sourceMappingURL=transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAGzC;;;;;GAKG;AACH,qBAAa,WAAW;IACvB,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,iBAAiB,CAAK;gBAElB,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,QAAQ;IAK1C;;OAEG;IACH,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;OAEG;IACH,IAAI,UAAU,IAAI,OAAO,CAExB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,EAAE,oBAAoB,EAAE,GAAG,MAAM,EAAE,OAA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