@dexto/storage 1.6.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.
Files changed (158) hide show
  1. package/LICENSE +44 -0
  2. package/README.md +80 -0
  3. package/dist/blob/factories/index.cjs +31 -0
  4. package/dist/blob/factories/index.d.cts +6 -0
  5. package/dist/blob/factories/index.d.ts +6 -0
  6. package/dist/blob/factories/index.d.ts.map +1 -0
  7. package/dist/blob/factories/index.js +6 -0
  8. package/dist/blob/factories/local.cjs +38 -0
  9. package/dist/blob/factories/local.d.cts +21 -0
  10. package/dist/blob/factories/local.d.ts +17 -0
  11. package/dist/blob/factories/local.d.ts.map +1 -0
  12. package/dist/blob/factories/local.js +14 -0
  13. package/dist/blob/factories/memory.cjs +44 -0
  14. package/dist/blob/factories/memory.d.cts +21 -0
  15. package/dist/blob/factories/memory.d.ts +17 -0
  16. package/dist/blob/factories/memory.d.ts.map +1 -0
  17. package/dist/blob/factories/memory.js +20 -0
  18. package/dist/blob/factory.cjs +16 -0
  19. package/dist/blob/factory.d.cts +36 -0
  20. package/dist/blob/factory.d.ts +35 -0
  21. package/dist/blob/factory.d.ts.map +1 -0
  22. package/dist/blob/factory.js +0 -0
  23. package/dist/blob/index.cjs +45 -0
  24. package/dist/blob/index.d.cts +8 -0
  25. package/dist/blob/index.d.ts +26 -0
  26. package/dist/blob/index.d.ts.map +1 -0
  27. package/dist/blob/index.js +19 -0
  28. package/dist/blob/local-blob-store.cjs +532 -0
  29. package/dist/blob/local-blob-store.d.cts +56 -0
  30. package/dist/blob/local-blob-store.d.ts +54 -0
  31. package/dist/blob/local-blob-store.d.ts.map +1 -0
  32. package/dist/blob/local-blob-store.js +498 -0
  33. package/dist/blob/memory-blob-store.cjs +327 -0
  34. package/dist/blob/memory-blob-store.d.cts +69 -0
  35. package/dist/blob/memory-blob-store.d.ts +67 -0
  36. package/dist/blob/memory-blob-store.d.ts.map +1 -0
  37. package/dist/blob/memory-blob-store.js +303 -0
  38. package/dist/blob/schemas.cjs +52 -0
  39. package/dist/blob/schemas.d.cts +87 -0
  40. package/dist/blob/schemas.d.ts +86 -0
  41. package/dist/blob/schemas.d.ts.map +1 -0
  42. package/dist/blob/schemas.js +25 -0
  43. package/dist/blob/types.cjs +16 -0
  44. package/dist/blob/types.d.cts +1 -0
  45. package/dist/blob/types.d.ts +2 -0
  46. package/dist/blob/types.d.ts.map +1 -0
  47. package/dist/blob/types.js +0 -0
  48. package/dist/cache/factories/index.cjs +31 -0
  49. package/dist/cache/factories/index.d.cts +6 -0
  50. package/dist/cache/factories/index.d.ts +6 -0
  51. package/dist/cache/factories/index.d.ts.map +1 -0
  52. package/dist/cache/factories/index.js +6 -0
  53. package/dist/cache/factories/memory.cjs +39 -0
  54. package/dist/cache/factories/memory.d.cts +21 -0
  55. package/dist/cache/factories/memory.d.ts +17 -0
  56. package/dist/cache/factories/memory.d.ts.map +1 -0
  57. package/dist/cache/factories/memory.js +15 -0
  58. package/dist/cache/factories/redis.cjs +65 -0
  59. package/dist/cache/factories/redis.d.cts +24 -0
  60. package/dist/cache/factories/redis.d.ts +20 -0
  61. package/dist/cache/factories/redis.d.ts.map +1 -0
  62. package/dist/cache/factories/redis.js +31 -0
  63. package/dist/cache/factory.cjs +16 -0
  64. package/dist/cache/factory.d.cts +42 -0
  65. package/dist/cache/factory.d.ts +41 -0
  66. package/dist/cache/factory.d.ts.map +1 -0
  67. package/dist/cache/factory.js +0 -0
  68. package/dist/cache/index.cjs +42 -0
  69. package/dist/cache/index.d.cts +7 -0
  70. package/dist/cache/index.d.ts +25 -0
  71. package/dist/cache/index.d.ts.map +1 -0
  72. package/dist/cache/index.js +17 -0
  73. package/dist/cache/memory-cache-store.cjs +106 -0
  74. package/dist/cache/memory-cache-store.d.cts +27 -0
  75. package/dist/cache/memory-cache-store.d.ts +25 -0
  76. package/dist/cache/memory-cache-store.d.ts.map +1 -0
  77. package/dist/cache/memory-cache-store.js +82 -0
  78. package/dist/cache/redis-store.cjs +176 -0
  79. package/dist/cache/redis-store.d.cts +34 -0
  80. package/dist/cache/redis-store.d.ts +32 -0
  81. package/dist/cache/redis-store.d.ts.map +1 -0
  82. package/dist/cache/redis-store.js +152 -0
  83. package/dist/cache/schemas.cjs +70 -0
  84. package/dist/cache/schemas.d.cts +93 -0
  85. package/dist/cache/schemas.d.ts +91 -0
  86. package/dist/cache/schemas.d.ts.map +1 -0
  87. package/dist/cache/schemas.js +43 -0
  88. package/dist/cache/types.cjs +16 -0
  89. package/dist/cache/types.d.cts +1 -0
  90. package/dist/cache/types.d.ts +2 -0
  91. package/dist/cache/types.d.ts.map +1 -0
  92. package/dist/cache/types.js +0 -0
  93. package/dist/database/factories/index.cjs +34 -0
  94. package/dist/database/factories/index.d.cts +7 -0
  95. package/dist/database/factories/index.d.ts +7 -0
  96. package/dist/database/factories/index.d.ts.map +1 -0
  97. package/dist/database/factories/index.js +8 -0
  98. package/dist/database/factories/memory.cjs +39 -0
  99. package/dist/database/factories/memory.d.cts +20 -0
  100. package/dist/database/factories/memory.d.ts +16 -0
  101. package/dist/database/factories/memory.d.ts.map +1 -0
  102. package/dist/database/factories/memory.js +15 -0
  103. package/dist/database/factories/postgres.cjs +61 -0
  104. package/dist/database/factories/postgres.d.cts +23 -0
  105. package/dist/database/factories/postgres.d.ts +19 -0
  106. package/dist/database/factories/postgres.d.ts.map +1 -0
  107. package/dist/database/factories/postgres.js +27 -0
  108. package/dist/database/factories/sqlite.cjs +65 -0
  109. package/dist/database/factories/sqlite.d.cts +24 -0
  110. package/dist/database/factories/sqlite.d.ts +20 -0
  111. package/dist/database/factories/sqlite.d.ts.map +1 -0
  112. package/dist/database/factories/sqlite.js +31 -0
  113. package/dist/database/factory.cjs +16 -0
  114. package/dist/database/factory.d.cts +42 -0
  115. package/dist/database/factory.d.ts +41 -0
  116. package/dist/database/factory.d.ts.map +1 -0
  117. package/dist/database/factory.js +0 -0
  118. package/dist/database/index.cjs +46 -0
  119. package/dist/database/index.d.cts +8 -0
  120. package/dist/database/index.d.ts +26 -0
  121. package/dist/database/index.d.ts.map +1 -0
  122. package/dist/database/index.js +24 -0
  123. package/dist/database/memory-database-store.cjs +121 -0
  124. package/dist/database/memory-database-store.d.cts +30 -0
  125. package/dist/database/memory-database-store.d.ts +28 -0
  126. package/dist/database/memory-database-store.d.ts.map +1 -0
  127. package/dist/database/memory-database-store.js +97 -0
  128. package/dist/database/postgres-store.cjs +342 -0
  129. package/dist/database/postgres-store.d.cts +57 -0
  130. package/dist/database/postgres-store.d.ts +55 -0
  131. package/dist/database/postgres-store.d.ts.map +1 -0
  132. package/dist/database/postgres-store.js +318 -0
  133. package/dist/database/schemas.cjs +82 -0
  134. package/dist/database/schemas.d.cts +127 -0
  135. package/dist/database/schemas.d.ts +125 -0
  136. package/dist/database/schemas.d.ts.map +1 -0
  137. package/dist/database/schemas.js +54 -0
  138. package/dist/database/sqlite-store.cjs +270 -0
  139. package/dist/database/sqlite-store.d.cts +35 -0
  140. package/dist/database/sqlite-store.d.ts +33 -0
  141. package/dist/database/sqlite-store.d.ts.map +1 -0
  142. package/dist/database/sqlite-store.js +236 -0
  143. package/dist/database/types.cjs +16 -0
  144. package/dist/database/types.d.cts +1 -0
  145. package/dist/database/types.d.ts +2 -0
  146. package/dist/database/types.d.ts.map +1 -0
  147. package/dist/database/types.js +0 -0
  148. package/dist/index.cjs +82 -0
  149. package/dist/index.d.cts +24 -0
  150. package/dist/index.d.ts +25 -0
  151. package/dist/index.d.ts.map +1 -0
  152. package/dist/index.js +50 -0
  153. package/dist/schemas.cjs +67 -0
  154. package/dist/schemas.d.cts +72 -0
  155. package/dist/schemas.d.ts +70 -0
  156. package/dist/schemas.d.ts.map +1 -0
  157. package/dist/schemas.js +46 -0
  158. package/package.json +55 -0
@@ -0,0 +1,342 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var postgres_store_exports = {};
20
+ __export(postgres_store_exports, {
21
+ PostgresStore: () => PostgresStore
22
+ });
23
+ module.exports = __toCommonJS(postgres_store_exports);
24
+ var import_pg = require("pg");
25
+ var import_core = require("@dexto/core");
26
+ class PostgresStore {
27
+ constructor(config, logger) {
28
+ this.config = config;
29
+ this.logger = logger.createChild(import_core.DextoLogComponent.STORAGE);
30
+ }
31
+ pool = null;
32
+ connected = false;
33
+ logger;
34
+ async connect() {
35
+ if (this.connected) return;
36
+ const connectionString = this.config.connectionString || this.config.url;
37
+ if (connectionString?.startsWith("$")) {
38
+ throw import_core.StorageError.connectionFailed(
39
+ `PostgreSQL: Connection string contains unexpanded environment variable: ${connectionString}. Ensure the environment variable is set in your .env file.`
40
+ );
41
+ }
42
+ if (!connectionString) {
43
+ throw import_core.StorageError.connectionFailed(
44
+ "PostgreSQL: No connection string provided. Set url or connectionString in database config."
45
+ );
46
+ }
47
+ const { schema, ...pgOptions } = this.config.options || {};
48
+ this.logger.info("Connecting to PostgreSQL database...");
49
+ this.pool = new import_pg.Pool({
50
+ connectionString,
51
+ max: this.config.maxConnections || 20,
52
+ // Shorter idle timeout for serverless DBs (Neon) - connections go stale quickly
53
+ idleTimeoutMillis: this.config.idleTimeoutMillis || 1e4,
54
+ connectionTimeoutMillis: this.config.connectionTimeoutMillis || 1e4,
55
+ // Enable TCP keepalive to detect dead connections
56
+ keepAlive: true,
57
+ keepAliveInitialDelayMillis: 1e4,
58
+ ...pgOptions
59
+ });
60
+ this.pool.on("error", (err) => {
61
+ this.logger.warn(`PostgreSQL pool error (will retry on next query): ${err.message}`);
62
+ });
63
+ if (schema) {
64
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(schema)) {
65
+ throw import_core.StorageError.connectionFailed(
66
+ `PostgreSQL: Invalid schema name "${schema}". Schema names must start with a letter or underscore and contain only alphanumeric characters and underscores.`
67
+ );
68
+ }
69
+ this.pool.on("connect", async (client2) => {
70
+ try {
71
+ await client2.query(`SET search_path TO "${schema}", public`);
72
+ } catch (err) {
73
+ this.logger.error(`Failed to set search_path to "${schema}": ${err}`);
74
+ }
75
+ });
76
+ this.logger.info(`Using custom schema: "${schema}"`);
77
+ }
78
+ let client;
79
+ try {
80
+ client = await this.pool.connect();
81
+ await client.query("SELECT NOW()");
82
+ if (schema) {
83
+ await this.createSchema(client, schema);
84
+ }
85
+ await this.createTables(client);
86
+ this.connected = true;
87
+ this.logger.info("PostgreSQL database connected successfully");
88
+ } catch (error) {
89
+ const errorMessage = error instanceof Error ? error.message : String(error);
90
+ this.logger.error(`PostgreSQL connection failed: ${errorMessage}`);
91
+ if (this.pool) {
92
+ await this.pool.end().catch(() => {
93
+ });
94
+ this.pool = null;
95
+ }
96
+ throw import_core.StorageError.connectionFailed(`PostgreSQL: ${errorMessage}`);
97
+ } finally {
98
+ if (client) {
99
+ client.release();
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Creates a PostgreSQL schema if it doesn't exist.
105
+ */
106
+ async createSchema(client, schemaName) {
107
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(schemaName)) {
108
+ throw import_core.StorageError.connectionFailed(
109
+ `PostgreSQL: Invalid schema name "${schemaName}". Schema names must start with a letter or underscore and contain only alphanumeric characters and underscores.`
110
+ );
111
+ }
112
+ try {
113
+ await client.query(`CREATE SCHEMA IF NOT EXISTS "${schemaName}"`);
114
+ this.logger.debug(`Schema "${schemaName}" ready`);
115
+ } catch (error) {
116
+ this.logger.warn(
117
+ `Could not create schema "${schemaName}": ${error}. Assuming it exists.`
118
+ );
119
+ }
120
+ }
121
+ async disconnect() {
122
+ if (this.pool) {
123
+ await this.pool.end();
124
+ this.pool = null;
125
+ }
126
+ this.connected = false;
127
+ }
128
+ isConnected() {
129
+ return this.connected && this.pool !== null;
130
+ }
131
+ getStoreType() {
132
+ return "postgres";
133
+ }
134
+ // Core operations - all use withRetry for serverless DB resilience
135
+ async get(key) {
136
+ try {
137
+ return await this.withRetry("get", async (client) => {
138
+ const result = await client.query("SELECT value FROM kv WHERE key = $1", [key]);
139
+ return result.rows[0] ? result.rows[0].value : void 0;
140
+ });
141
+ } catch (error) {
142
+ throw import_core.StorageError.readFailed(
143
+ "get",
144
+ error instanceof Error ? error.message : String(error),
145
+ { key }
146
+ );
147
+ }
148
+ }
149
+ async set(key, value) {
150
+ try {
151
+ await this.withRetry("set", async (client) => {
152
+ const jsonValue = JSON.stringify(value);
153
+ await client.query(
154
+ "INSERT INTO kv (key, value, updated_at) VALUES ($1, $2::jsonb, $3) ON CONFLICT (key) DO UPDATE SET value = $2::jsonb, updated_at = $3",
155
+ [key, jsonValue, /* @__PURE__ */ new Date()]
156
+ );
157
+ });
158
+ } catch (error) {
159
+ throw import_core.StorageError.writeFailed(
160
+ "set",
161
+ error instanceof Error ? error.message : String(error),
162
+ { key }
163
+ );
164
+ }
165
+ }
166
+ async delete(key) {
167
+ await this.withRetry("delete", async (client) => {
168
+ await client.query("BEGIN");
169
+ try {
170
+ await client.query("DELETE FROM kv WHERE key = $1", [key]);
171
+ await client.query("DELETE FROM lists WHERE key = $1", [key]);
172
+ await client.query("COMMIT");
173
+ } catch (error) {
174
+ await client.query("ROLLBACK");
175
+ throw error;
176
+ }
177
+ });
178
+ }
179
+ // List operations
180
+ async list(prefix) {
181
+ return await this.withRetry("list", async (client) => {
182
+ const kvResult = await client.query("SELECT key FROM kv WHERE key LIKE $1", [
183
+ `${prefix}%`
184
+ ]);
185
+ const listResult = await client.query(
186
+ "SELECT DISTINCT key FROM lists WHERE key LIKE $1",
187
+ [`${prefix}%`]
188
+ );
189
+ const allKeys = /* @__PURE__ */ new Set([
190
+ ...kvResult.rows.map((row) => row.key),
191
+ ...listResult.rows.map((row) => row.key)
192
+ ]);
193
+ return Array.from(allKeys).sort();
194
+ });
195
+ }
196
+ async append(key, item) {
197
+ try {
198
+ await this.withRetry("append", async (client) => {
199
+ const jsonItem = JSON.stringify(item);
200
+ await client.query(
201
+ "INSERT INTO lists (key, item, created_at) VALUES ($1, $2::jsonb, $3)",
202
+ [key, jsonItem, /* @__PURE__ */ new Date()]
203
+ );
204
+ });
205
+ } catch (error) {
206
+ throw import_core.StorageError.writeFailed(
207
+ "append",
208
+ error instanceof Error ? error.message : String(error),
209
+ { key }
210
+ );
211
+ }
212
+ }
213
+ async getRange(key, start, count) {
214
+ return await this.withRetry("getRange", async (client) => {
215
+ const result = await client.query(
216
+ "SELECT item FROM lists WHERE key = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
217
+ [key, count, start]
218
+ );
219
+ return result.rows.map((row) => row.item);
220
+ });
221
+ }
222
+ // Schema management
223
+ async createTables(client) {
224
+ await client.query(`
225
+ CREATE TABLE IF NOT EXISTS kv (
226
+ key VARCHAR(255) PRIMARY KEY,
227
+ value JSONB NOT NULL,
228
+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
229
+ )
230
+ `);
231
+ await client.query(`
232
+ CREATE TABLE IF NOT EXISTS lists (
233
+ id BIGSERIAL PRIMARY KEY,
234
+ key VARCHAR(255) NOT NULL,
235
+ item JSONB NOT NULL,
236
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
237
+ )
238
+ `);
239
+ await client.query("CREATE INDEX IF NOT EXISTS idx_kv_key ON kv(key)");
240
+ await client.query("CREATE INDEX IF NOT EXISTS idx_lists_key ON lists(key)");
241
+ await client.query(
242
+ "CREATE INDEX IF NOT EXISTS idx_lists_created_at ON lists(key, created_at DESC)"
243
+ );
244
+ }
245
+ checkConnection() {
246
+ if (!this.connected || !this.pool) {
247
+ throw import_core.StorageError.notConnected("PostgresStore");
248
+ }
249
+ }
250
+ /**
251
+ * Check if an error is a connection error that should trigger a retry.
252
+ * Common with serverless databases (Neon) where connections go stale.
253
+ */
254
+ isConnectionError(error) {
255
+ if (!(error instanceof Error)) return false;
256
+ const code = error.code;
257
+ const codeString = typeof code === "string" ? code : void 0;
258
+ return codeString === "ETIMEDOUT" || codeString === "ECONNRESET" || codeString === "ECONNREFUSED" || codeString === "EPIPE" || codeString === "57P01" || // admin_shutdown
259
+ codeString === "57P02" || // crash_shutdown
260
+ codeString === "57P03" || // cannot_connect_now
261
+ error.message.includes("Connection terminated") || error.message.includes("connection lost");
262
+ }
263
+ /**
264
+ * Execute a database operation with automatic retry on connection errors.
265
+ * Handles serverless DB connection issues (Neon cold starts, stale connections).
266
+ */
267
+ async withRetry(operation, fn, maxRetries = 2) {
268
+ this.checkConnection();
269
+ let lastError;
270
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
271
+ let client;
272
+ try {
273
+ client = await this.pool.connect();
274
+ const result = await fn(client);
275
+ return result;
276
+ } catch (error) {
277
+ lastError = error instanceof Error ? error : new Error(String(error));
278
+ if (client) {
279
+ client.release(true);
280
+ client = void 0;
281
+ }
282
+ if (this.isConnectionError(error) && attempt < maxRetries) {
283
+ this.logger.warn(
284
+ `PostgreSQL ${operation} failed with connection error (attempt ${attempt + 1}/${maxRetries + 1}): ${lastError.message}. Retrying...`
285
+ );
286
+ await new Promise((resolve) => setTimeout(resolve, 100 * (attempt + 1)));
287
+ continue;
288
+ }
289
+ throw error;
290
+ } finally {
291
+ if (client) {
292
+ client.release();
293
+ }
294
+ }
295
+ }
296
+ throw lastError;
297
+ }
298
+ // Advanced operations
299
+ /**
300
+ * Execute a callback within a database transaction.
301
+ * Note: On connection failure, the entire callback will be retried on a new connection.
302
+ * Ensure callback operations are idempotent or use this only for read operations.
303
+ */
304
+ async transaction(callback) {
305
+ return await this.withRetry("transaction", async (client) => {
306
+ await client.query("BEGIN");
307
+ try {
308
+ const result = await callback(client);
309
+ await client.query("COMMIT");
310
+ return result;
311
+ } catch (error) {
312
+ await client.query("ROLLBACK");
313
+ throw error;
314
+ }
315
+ });
316
+ }
317
+ async getStats() {
318
+ return await this.withRetry("getStats", async (client) => {
319
+ const kvResult = await client.query("SELECT COUNT(*) as count FROM kv");
320
+ const listResult = await client.query("SELECT COUNT(*) as count FROM lists");
321
+ const sizeResult = await client.query(
322
+ "SELECT pg_size_pretty(pg_total_relation_size($1)) as size",
323
+ ["kv"]
324
+ );
325
+ return {
326
+ kvCount: parseInt(kvResult.rows[0].count),
327
+ listCount: parseInt(listResult.rows[0].count),
328
+ totalSize: sizeResult.rows[0].size
329
+ };
330
+ });
331
+ }
332
+ // Maintenance operations
333
+ async vacuum() {
334
+ await this.withRetry("vacuum", async (client) => {
335
+ await client.query("VACUUM ANALYZE kv, lists");
336
+ });
337
+ }
338
+ }
339
+ // Annotate the CommonJS export names for ESM import in node:
340
+ 0 && (module.exports = {
341
+ PostgresStore
342
+ });
@@ -0,0 +1,57 @@
1
+ import { PoolClient } from 'pg';
2
+ import { Database, Logger } from '@dexto/core';
3
+ import { PostgresDatabaseConfig } from './schemas.cjs';
4
+ import 'zod';
5
+
6
+ /**
7
+ * PostgreSQL database store for production database operations.
8
+ * Implements the Database interface with connection pooling and JSONB support.
9
+ * EXPERIMENTAL - NOT FULLY TESTED YET
10
+ */
11
+ declare class PostgresStore implements Database {
12
+ private config;
13
+ private pool;
14
+ private connected;
15
+ private logger;
16
+ constructor(config: PostgresDatabaseConfig, logger: Logger);
17
+ connect(): Promise<void>;
18
+ /**
19
+ * Creates a PostgreSQL schema if it doesn't exist.
20
+ */
21
+ private createSchema;
22
+ disconnect(): Promise<void>;
23
+ isConnected(): boolean;
24
+ getStoreType(): string;
25
+ get<T>(key: string): Promise<T | undefined>;
26
+ set<T>(key: string, value: T): Promise<void>;
27
+ delete(key: string): Promise<void>;
28
+ list(prefix: string): Promise<string[]>;
29
+ append<T>(key: string, item: T): Promise<void>;
30
+ getRange<T>(key: string, start: number, count: number): Promise<T[]>;
31
+ private createTables;
32
+ private checkConnection;
33
+ /**
34
+ * Check if an error is a connection error that should trigger a retry.
35
+ * Common with serverless databases (Neon) where connections go stale.
36
+ */
37
+ private isConnectionError;
38
+ /**
39
+ * Execute a database operation with automatic retry on connection errors.
40
+ * Handles serverless DB connection issues (Neon cold starts, stale connections).
41
+ */
42
+ private withRetry;
43
+ /**
44
+ * Execute a callback within a database transaction.
45
+ * Note: On connection failure, the entire callback will be retried on a new connection.
46
+ * Ensure callback operations are idempotent or use this only for read operations.
47
+ */
48
+ transaction<T>(callback: (client: PoolClient) => Promise<T>): Promise<T>;
49
+ getStats(): Promise<{
50
+ kvCount: number;
51
+ listCount: number;
52
+ totalSize: string;
53
+ }>;
54
+ vacuum(): Promise<void>;
55
+ }
56
+
57
+ export { PostgresStore };
@@ -0,0 +1,55 @@
1
+ import { PoolClient } from 'pg';
2
+ import type { Database } from './types.js';
3
+ import type { PostgresDatabaseConfig } from './schemas.js';
4
+ import type { Logger } from '@dexto/core';
5
+ /**
6
+ * PostgreSQL database store for production database operations.
7
+ * Implements the Database interface with connection pooling and JSONB support.
8
+ * EXPERIMENTAL - NOT FULLY TESTED YET
9
+ */
10
+ export declare class PostgresStore implements Database {
11
+ private config;
12
+ private pool;
13
+ private connected;
14
+ private logger;
15
+ constructor(config: PostgresDatabaseConfig, logger: Logger);
16
+ connect(): Promise<void>;
17
+ /**
18
+ * Creates a PostgreSQL schema if it doesn't exist.
19
+ */
20
+ private createSchema;
21
+ disconnect(): Promise<void>;
22
+ isConnected(): boolean;
23
+ getStoreType(): string;
24
+ get<T>(key: string): Promise<T | undefined>;
25
+ set<T>(key: string, value: T): Promise<void>;
26
+ delete(key: string): Promise<void>;
27
+ list(prefix: string): Promise<string[]>;
28
+ append<T>(key: string, item: T): Promise<void>;
29
+ getRange<T>(key: string, start: number, count: number): Promise<T[]>;
30
+ private createTables;
31
+ private checkConnection;
32
+ /**
33
+ * Check if an error is a connection error that should trigger a retry.
34
+ * Common with serverless databases (Neon) where connections go stale.
35
+ */
36
+ private isConnectionError;
37
+ /**
38
+ * Execute a database operation with automatic retry on connection errors.
39
+ * Handles serverless DB connection issues (Neon cold starts, stale connections).
40
+ */
41
+ private withRetry;
42
+ /**
43
+ * Execute a callback within a database transaction.
44
+ * Note: On connection failure, the entire callback will be retried on a new connection.
45
+ * Ensure callback operations are idempotent or use this only for read operations.
46
+ */
47
+ transaction<T>(callback: (client: PoolClient) => Promise<T>): Promise<T>;
48
+ getStats(): Promise<{
49
+ kvCount: number;
50
+ listCount: number;
51
+ totalSize: string;
52
+ }>;
53
+ vacuum(): Promise<void>;
54
+ }
55
+ //# sourceMappingURL=postgres-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-store.d.ts","sourceRoot":"","sources":["../../src/database/postgres-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,UAAU,EAAE,MAAM,IAAI,CAAC;AACtC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C;;;;GAIG;AACH,qBAAa,aAAc,YAAW,QAAQ;IAMtC,OAAO,CAAC,MAAM;IALlB,OAAO,CAAC,IAAI,CAAqB;IACjC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAS;gBAGX,MAAM,EAAE,sBAAsB,EACtC,MAAM,EAAE,MAAM;IAKZ,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA+F9B;;OAEG;YACW,YAAY;IAmBpB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAQjC,WAAW,IAAI,OAAO;IAItB,YAAY,IAAI,MAAM;IAKhB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAe3C,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB5C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAelC,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAmBvC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB9C,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;YAW5D,YAAY;IA4B1B,OAAO,CAAC,eAAe;IAMvB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAiBzB;;;OAGG;YACW,SAAS;IA8CvB;;;;OAIG;IACG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAcxE,QAAQ,IAAI,OAAO,CAAC;QACtB,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACrB,CAAC;IAkBI,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAKhC"}