@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,318 @@
1
+ import { Pool } from "pg";
2
+ import { DextoLogComponent, StorageError } from "@dexto/core";
3
+ class PostgresStore {
4
+ constructor(config, logger) {
5
+ this.config = config;
6
+ this.logger = logger.createChild(DextoLogComponent.STORAGE);
7
+ }
8
+ pool = null;
9
+ connected = false;
10
+ logger;
11
+ async connect() {
12
+ if (this.connected) return;
13
+ const connectionString = this.config.connectionString || this.config.url;
14
+ if (connectionString?.startsWith("$")) {
15
+ throw StorageError.connectionFailed(
16
+ `PostgreSQL: Connection string contains unexpanded environment variable: ${connectionString}. Ensure the environment variable is set in your .env file.`
17
+ );
18
+ }
19
+ if (!connectionString) {
20
+ throw StorageError.connectionFailed(
21
+ "PostgreSQL: No connection string provided. Set url or connectionString in database config."
22
+ );
23
+ }
24
+ const { schema, ...pgOptions } = this.config.options || {};
25
+ this.logger.info("Connecting to PostgreSQL database...");
26
+ this.pool = new Pool({
27
+ connectionString,
28
+ max: this.config.maxConnections || 20,
29
+ // Shorter idle timeout for serverless DBs (Neon) - connections go stale quickly
30
+ idleTimeoutMillis: this.config.idleTimeoutMillis || 1e4,
31
+ connectionTimeoutMillis: this.config.connectionTimeoutMillis || 1e4,
32
+ // Enable TCP keepalive to detect dead connections
33
+ keepAlive: true,
34
+ keepAliveInitialDelayMillis: 1e4,
35
+ ...pgOptions
36
+ });
37
+ this.pool.on("error", (err) => {
38
+ this.logger.warn(`PostgreSQL pool error (will retry on next query): ${err.message}`);
39
+ });
40
+ if (schema) {
41
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(schema)) {
42
+ throw StorageError.connectionFailed(
43
+ `PostgreSQL: Invalid schema name "${schema}". Schema names must start with a letter or underscore and contain only alphanumeric characters and underscores.`
44
+ );
45
+ }
46
+ this.pool.on("connect", async (client2) => {
47
+ try {
48
+ await client2.query(`SET search_path TO "${schema}", public`);
49
+ } catch (err) {
50
+ this.logger.error(`Failed to set search_path to "${schema}": ${err}`);
51
+ }
52
+ });
53
+ this.logger.info(`Using custom schema: "${schema}"`);
54
+ }
55
+ let client;
56
+ try {
57
+ client = await this.pool.connect();
58
+ await client.query("SELECT NOW()");
59
+ if (schema) {
60
+ await this.createSchema(client, schema);
61
+ }
62
+ await this.createTables(client);
63
+ this.connected = true;
64
+ this.logger.info("PostgreSQL database connected successfully");
65
+ } catch (error) {
66
+ const errorMessage = error instanceof Error ? error.message : String(error);
67
+ this.logger.error(`PostgreSQL connection failed: ${errorMessage}`);
68
+ if (this.pool) {
69
+ await this.pool.end().catch(() => {
70
+ });
71
+ this.pool = null;
72
+ }
73
+ throw StorageError.connectionFailed(`PostgreSQL: ${errorMessage}`);
74
+ } finally {
75
+ if (client) {
76
+ client.release();
77
+ }
78
+ }
79
+ }
80
+ /**
81
+ * Creates a PostgreSQL schema if it doesn't exist.
82
+ */
83
+ async createSchema(client, schemaName) {
84
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(schemaName)) {
85
+ throw StorageError.connectionFailed(
86
+ `PostgreSQL: Invalid schema name "${schemaName}". Schema names must start with a letter or underscore and contain only alphanumeric characters and underscores.`
87
+ );
88
+ }
89
+ try {
90
+ await client.query(`CREATE SCHEMA IF NOT EXISTS "${schemaName}"`);
91
+ this.logger.debug(`Schema "${schemaName}" ready`);
92
+ } catch (error) {
93
+ this.logger.warn(
94
+ `Could not create schema "${schemaName}": ${error}. Assuming it exists.`
95
+ );
96
+ }
97
+ }
98
+ async disconnect() {
99
+ if (this.pool) {
100
+ await this.pool.end();
101
+ this.pool = null;
102
+ }
103
+ this.connected = false;
104
+ }
105
+ isConnected() {
106
+ return this.connected && this.pool !== null;
107
+ }
108
+ getStoreType() {
109
+ return "postgres";
110
+ }
111
+ // Core operations - all use withRetry for serverless DB resilience
112
+ async get(key) {
113
+ try {
114
+ return await this.withRetry("get", async (client) => {
115
+ const result = await client.query("SELECT value FROM kv WHERE key = $1", [key]);
116
+ return result.rows[0] ? result.rows[0].value : void 0;
117
+ });
118
+ } catch (error) {
119
+ throw StorageError.readFailed(
120
+ "get",
121
+ error instanceof Error ? error.message : String(error),
122
+ { key }
123
+ );
124
+ }
125
+ }
126
+ async set(key, value) {
127
+ try {
128
+ await this.withRetry("set", async (client) => {
129
+ const jsonValue = JSON.stringify(value);
130
+ await client.query(
131
+ "INSERT INTO kv (key, value, updated_at) VALUES ($1, $2::jsonb, $3) ON CONFLICT (key) DO UPDATE SET value = $2::jsonb, updated_at = $3",
132
+ [key, jsonValue, /* @__PURE__ */ new Date()]
133
+ );
134
+ });
135
+ } catch (error) {
136
+ throw StorageError.writeFailed(
137
+ "set",
138
+ error instanceof Error ? error.message : String(error),
139
+ { key }
140
+ );
141
+ }
142
+ }
143
+ async delete(key) {
144
+ await this.withRetry("delete", async (client) => {
145
+ await client.query("BEGIN");
146
+ try {
147
+ await client.query("DELETE FROM kv WHERE key = $1", [key]);
148
+ await client.query("DELETE FROM lists WHERE key = $1", [key]);
149
+ await client.query("COMMIT");
150
+ } catch (error) {
151
+ await client.query("ROLLBACK");
152
+ throw error;
153
+ }
154
+ });
155
+ }
156
+ // List operations
157
+ async list(prefix) {
158
+ return await this.withRetry("list", async (client) => {
159
+ const kvResult = await client.query("SELECT key FROM kv WHERE key LIKE $1", [
160
+ `${prefix}%`
161
+ ]);
162
+ const listResult = await client.query(
163
+ "SELECT DISTINCT key FROM lists WHERE key LIKE $1",
164
+ [`${prefix}%`]
165
+ );
166
+ const allKeys = /* @__PURE__ */ new Set([
167
+ ...kvResult.rows.map((row) => row.key),
168
+ ...listResult.rows.map((row) => row.key)
169
+ ]);
170
+ return Array.from(allKeys).sort();
171
+ });
172
+ }
173
+ async append(key, item) {
174
+ try {
175
+ await this.withRetry("append", async (client) => {
176
+ const jsonItem = JSON.stringify(item);
177
+ await client.query(
178
+ "INSERT INTO lists (key, item, created_at) VALUES ($1, $2::jsonb, $3)",
179
+ [key, jsonItem, /* @__PURE__ */ new Date()]
180
+ );
181
+ });
182
+ } catch (error) {
183
+ throw StorageError.writeFailed(
184
+ "append",
185
+ error instanceof Error ? error.message : String(error),
186
+ { key }
187
+ );
188
+ }
189
+ }
190
+ async getRange(key, start, count) {
191
+ return await this.withRetry("getRange", async (client) => {
192
+ const result = await client.query(
193
+ "SELECT item FROM lists WHERE key = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
194
+ [key, count, start]
195
+ );
196
+ return result.rows.map((row) => row.item);
197
+ });
198
+ }
199
+ // Schema management
200
+ async createTables(client) {
201
+ await client.query(`
202
+ CREATE TABLE IF NOT EXISTS kv (
203
+ key VARCHAR(255) PRIMARY KEY,
204
+ value JSONB NOT NULL,
205
+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
206
+ )
207
+ `);
208
+ await client.query(`
209
+ CREATE TABLE IF NOT EXISTS lists (
210
+ id BIGSERIAL PRIMARY KEY,
211
+ key VARCHAR(255) NOT NULL,
212
+ item JSONB NOT NULL,
213
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
214
+ )
215
+ `);
216
+ await client.query("CREATE INDEX IF NOT EXISTS idx_kv_key ON kv(key)");
217
+ await client.query("CREATE INDEX IF NOT EXISTS idx_lists_key ON lists(key)");
218
+ await client.query(
219
+ "CREATE INDEX IF NOT EXISTS idx_lists_created_at ON lists(key, created_at DESC)"
220
+ );
221
+ }
222
+ checkConnection() {
223
+ if (!this.connected || !this.pool) {
224
+ throw StorageError.notConnected("PostgresStore");
225
+ }
226
+ }
227
+ /**
228
+ * Check if an error is a connection error that should trigger a retry.
229
+ * Common with serverless databases (Neon) where connections go stale.
230
+ */
231
+ isConnectionError(error) {
232
+ if (!(error instanceof Error)) return false;
233
+ const code = error.code;
234
+ const codeString = typeof code === "string" ? code : void 0;
235
+ return codeString === "ETIMEDOUT" || codeString === "ECONNRESET" || codeString === "ECONNREFUSED" || codeString === "EPIPE" || codeString === "57P01" || // admin_shutdown
236
+ codeString === "57P02" || // crash_shutdown
237
+ codeString === "57P03" || // cannot_connect_now
238
+ error.message.includes("Connection terminated") || error.message.includes("connection lost");
239
+ }
240
+ /**
241
+ * Execute a database operation with automatic retry on connection errors.
242
+ * Handles serverless DB connection issues (Neon cold starts, stale connections).
243
+ */
244
+ async withRetry(operation, fn, maxRetries = 2) {
245
+ this.checkConnection();
246
+ let lastError;
247
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
248
+ let client;
249
+ try {
250
+ client = await this.pool.connect();
251
+ const result = await fn(client);
252
+ return result;
253
+ } catch (error) {
254
+ lastError = error instanceof Error ? error : new Error(String(error));
255
+ if (client) {
256
+ client.release(true);
257
+ client = void 0;
258
+ }
259
+ if (this.isConnectionError(error) && attempt < maxRetries) {
260
+ this.logger.warn(
261
+ `PostgreSQL ${operation} failed with connection error (attempt ${attempt + 1}/${maxRetries + 1}): ${lastError.message}. Retrying...`
262
+ );
263
+ await new Promise((resolve) => setTimeout(resolve, 100 * (attempt + 1)));
264
+ continue;
265
+ }
266
+ throw error;
267
+ } finally {
268
+ if (client) {
269
+ client.release();
270
+ }
271
+ }
272
+ }
273
+ throw lastError;
274
+ }
275
+ // Advanced operations
276
+ /**
277
+ * Execute a callback within a database transaction.
278
+ * Note: On connection failure, the entire callback will be retried on a new connection.
279
+ * Ensure callback operations are idempotent or use this only for read operations.
280
+ */
281
+ async transaction(callback) {
282
+ return await this.withRetry("transaction", async (client) => {
283
+ await client.query("BEGIN");
284
+ try {
285
+ const result = await callback(client);
286
+ await client.query("COMMIT");
287
+ return result;
288
+ } catch (error) {
289
+ await client.query("ROLLBACK");
290
+ throw error;
291
+ }
292
+ });
293
+ }
294
+ async getStats() {
295
+ return await this.withRetry("getStats", async (client) => {
296
+ const kvResult = await client.query("SELECT COUNT(*) as count FROM kv");
297
+ const listResult = await client.query("SELECT COUNT(*) as count FROM lists");
298
+ const sizeResult = await client.query(
299
+ "SELECT pg_size_pretty(pg_total_relation_size($1)) as size",
300
+ ["kv"]
301
+ );
302
+ return {
303
+ kvCount: parseInt(kvResult.rows[0].count),
304
+ listCount: parseInt(listResult.rows[0].count),
305
+ totalSize: sizeResult.rows[0].size
306
+ };
307
+ });
308
+ }
309
+ // Maintenance operations
310
+ async vacuum() {
311
+ await this.withRetry("vacuum", async (client) => {
312
+ await client.query("VACUUM ANALYZE kv, lists");
313
+ });
314
+ }
315
+ }
316
+ export {
317
+ PostgresStore
318
+ };
@@ -0,0 +1,82 @@
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 schemas_exports = {};
20
+ __export(schemas_exports, {
21
+ DATABASE_TYPES: () => DATABASE_TYPES,
22
+ DatabaseConfigSchema: () => DatabaseConfigSchema,
23
+ InMemoryDatabaseSchema: () => InMemoryDatabaseSchema,
24
+ PostgresDatabaseSchema: () => PostgresDatabaseSchema,
25
+ SqliteDatabaseSchema: () => SqliteDatabaseSchema
26
+ });
27
+ module.exports = __toCommonJS(schemas_exports);
28
+ var import_zod = require("zod");
29
+ var import_core = require("@dexto/core");
30
+ const DATABASE_TYPES = ["in-memory", "sqlite", "postgres"];
31
+ const BaseDatabaseSchema = import_zod.z.object({
32
+ maxConnections: import_zod.z.number().int().positive().optional().describe("Maximum connections"),
33
+ idleTimeoutMillis: import_zod.z.number().int().positive().optional().describe("Idle timeout in milliseconds"),
34
+ connectionTimeoutMillis: import_zod.z.number().int().positive().optional().describe("Connection timeout in milliseconds"),
35
+ options: import_zod.z.record(import_zod.z.unknown()).optional().describe("Backend-specific options")
36
+ });
37
+ const InMemoryDatabaseSchema = BaseDatabaseSchema.extend({
38
+ type: import_zod.z.literal("in-memory")
39
+ // In-memory database doesn't need connection options, but inherits pool options for consistency
40
+ }).strict();
41
+ const SqliteDatabaseSchema = BaseDatabaseSchema.extend({
42
+ type: import_zod.z.literal("sqlite"),
43
+ path: import_zod.z.string().describe(
44
+ "SQLite database file path (required for SQLite - CLI enrichment provides per-agent path)"
45
+ )
46
+ }).strict();
47
+ const PostgresDatabaseSchema = BaseDatabaseSchema.extend({
48
+ type: import_zod.z.literal("postgres"),
49
+ url: (0, import_core.EnvExpandedString)().optional().describe("PostgreSQL connection URL (postgresql://...)"),
50
+ connectionString: (0, import_core.EnvExpandedString)().optional().describe("PostgreSQL connection string"),
51
+ host: import_zod.z.string().optional().describe("PostgreSQL host"),
52
+ port: import_zod.z.number().int().positive().optional().describe("PostgreSQL port"),
53
+ database: import_zod.z.string().optional().describe("PostgreSQL database name"),
54
+ password: import_zod.z.string().optional().describe("PostgreSQL password"),
55
+ // TODO: keyPrefix is reserved for future use - allows namespacing keys when multiple
56
+ // agents or environments share the same database (e.g., "dev:agent1:" vs "prod:agent2:")
57
+ keyPrefix: import_zod.z.string().optional().describe('Optional key prefix for namespacing (e.g., "dev:myagent:")')
58
+ }).strict().superRefine((data, ctx) => {
59
+ if (!data.url && !data.connectionString && !data.host) {
60
+ ctx.addIssue({
61
+ code: import_zod.z.ZodIssueCode.custom,
62
+ message: "PostgreSQL database requires one of 'url', 'connectionString', or 'host' to be specified",
63
+ path: ["url"],
64
+ params: {
65
+ code: import_core.StorageErrorCode.CONNECTION_CONFIG_MISSING,
66
+ scope: import_core.ErrorScope.STORAGE,
67
+ type: import_core.ErrorType.USER
68
+ }
69
+ });
70
+ }
71
+ });
72
+ const DatabaseConfigSchema = import_zod.z.object({
73
+ type: import_zod.z.string().describe("Database backend type identifier")
74
+ }).passthrough().describe("Database configuration (validated by image factory)");
75
+ // Annotate the CommonJS export names for ESM import in node:
76
+ 0 && (module.exports = {
77
+ DATABASE_TYPES,
78
+ DatabaseConfigSchema,
79
+ InMemoryDatabaseSchema,
80
+ PostgresDatabaseSchema,
81
+ SqliteDatabaseSchema
82
+ });
@@ -0,0 +1,127 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const DATABASE_TYPES: readonly ["in-memory", "sqlite", "postgres"];
4
+ type DatabaseType = (typeof DATABASE_TYPES)[number];
5
+ declare const InMemoryDatabaseSchema: z.ZodObject<{
6
+ maxConnections: z.ZodOptional<z.ZodNumber>;
7
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
8
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
9
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
10
+ } & {
11
+ type: z.ZodLiteral<"in-memory">;
12
+ }, "strict", z.ZodTypeAny, {
13
+ type: "in-memory";
14
+ maxConnections?: number | undefined;
15
+ idleTimeoutMillis?: number | undefined;
16
+ connectionTimeoutMillis?: number | undefined;
17
+ options?: Record<string, unknown> | undefined;
18
+ }, {
19
+ type: "in-memory";
20
+ maxConnections?: number | undefined;
21
+ idleTimeoutMillis?: number | undefined;
22
+ connectionTimeoutMillis?: number | undefined;
23
+ options?: Record<string, unknown> | undefined;
24
+ }>;
25
+ type InMemoryDatabaseConfig = z.output<typeof InMemoryDatabaseSchema>;
26
+ declare const SqliteDatabaseSchema: z.ZodObject<{
27
+ maxConnections: z.ZodOptional<z.ZodNumber>;
28
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
29
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
30
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
31
+ } & {
32
+ type: z.ZodLiteral<"sqlite">;
33
+ path: z.ZodString;
34
+ }, "strict", z.ZodTypeAny, {
35
+ path: string;
36
+ type: "sqlite";
37
+ maxConnections?: number | undefined;
38
+ idleTimeoutMillis?: number | undefined;
39
+ connectionTimeoutMillis?: number | undefined;
40
+ options?: Record<string, unknown> | undefined;
41
+ }, {
42
+ path: string;
43
+ type: "sqlite";
44
+ maxConnections?: number | undefined;
45
+ idleTimeoutMillis?: number | undefined;
46
+ connectionTimeoutMillis?: number | undefined;
47
+ options?: Record<string, unknown> | undefined;
48
+ }>;
49
+ type SqliteDatabaseConfig = z.output<typeof SqliteDatabaseSchema>;
50
+ declare const PostgresDatabaseSchema: z.ZodEffects<z.ZodObject<{
51
+ maxConnections: z.ZodOptional<z.ZodNumber>;
52
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
53
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
54
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
55
+ } & {
56
+ type: z.ZodLiteral<"postgres">;
57
+ url: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
58
+ connectionString: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
59
+ host: z.ZodOptional<z.ZodString>;
60
+ port: z.ZodOptional<z.ZodNumber>;
61
+ database: z.ZodOptional<z.ZodString>;
62
+ password: z.ZodOptional<z.ZodString>;
63
+ keyPrefix: z.ZodOptional<z.ZodString>;
64
+ }, "strict", z.ZodTypeAny, {
65
+ type: "postgres";
66
+ maxConnections?: number | undefined;
67
+ idleTimeoutMillis?: number | undefined;
68
+ connectionTimeoutMillis?: number | undefined;
69
+ options?: Record<string, unknown> | undefined;
70
+ url?: string | undefined;
71
+ host?: string | undefined;
72
+ port?: number | undefined;
73
+ password?: string | undefined;
74
+ database?: string | undefined;
75
+ connectionString?: string | undefined;
76
+ keyPrefix?: string | undefined;
77
+ }, {
78
+ type: "postgres";
79
+ maxConnections?: number | undefined;
80
+ idleTimeoutMillis?: number | undefined;
81
+ connectionTimeoutMillis?: number | undefined;
82
+ options?: Record<string, unknown> | undefined;
83
+ url?: string | undefined;
84
+ host?: string | undefined;
85
+ port?: number | undefined;
86
+ password?: string | undefined;
87
+ database?: string | undefined;
88
+ connectionString?: string | undefined;
89
+ keyPrefix?: string | undefined;
90
+ }>, {
91
+ type: "postgres";
92
+ maxConnections?: number | undefined;
93
+ idleTimeoutMillis?: number | undefined;
94
+ connectionTimeoutMillis?: number | undefined;
95
+ options?: Record<string, unknown> | undefined;
96
+ url?: string | undefined;
97
+ host?: string | undefined;
98
+ port?: number | undefined;
99
+ password?: string | undefined;
100
+ database?: string | undefined;
101
+ connectionString?: string | undefined;
102
+ keyPrefix?: string | undefined;
103
+ }, {
104
+ type: "postgres";
105
+ maxConnections?: number | undefined;
106
+ idleTimeoutMillis?: number | undefined;
107
+ connectionTimeoutMillis?: number | undefined;
108
+ options?: Record<string, unknown> | undefined;
109
+ url?: string | undefined;
110
+ host?: string | undefined;
111
+ port?: number | undefined;
112
+ password?: string | undefined;
113
+ database?: string | undefined;
114
+ connectionString?: string | undefined;
115
+ keyPrefix?: string | undefined;
116
+ }>;
117
+ type PostgresDatabaseConfig = z.output<typeof PostgresDatabaseSchema>;
118
+ declare const DatabaseConfigSchema: z.ZodObject<{
119
+ type: z.ZodString;
120
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
121
+ type: z.ZodString;
122
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
123
+ type: z.ZodString;
124
+ }, z.ZodTypeAny, "passthrough">>;
125
+ type DatabaseConfig = z.output<typeof DatabaseConfigSchema>;
126
+
127
+ export { DATABASE_TYPES, type DatabaseConfig, DatabaseConfigSchema, type DatabaseType, type InMemoryDatabaseConfig, InMemoryDatabaseSchema, type PostgresDatabaseConfig, PostgresDatabaseSchema, type SqliteDatabaseConfig, SqliteDatabaseSchema };
@@ -0,0 +1,125 @@
1
+ import { z } from 'zod';
2
+ export declare const DATABASE_TYPES: readonly ["in-memory", "sqlite", "postgres"];
3
+ export type DatabaseType = (typeof DATABASE_TYPES)[number];
4
+ export declare const InMemoryDatabaseSchema: z.ZodObject<{
5
+ maxConnections: z.ZodOptional<z.ZodNumber>;
6
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
7
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
8
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
9
+ } & {
10
+ type: z.ZodLiteral<"in-memory">;
11
+ }, "strict", z.ZodTypeAny, {
12
+ type: "in-memory";
13
+ maxConnections?: number | undefined;
14
+ idleTimeoutMillis?: number | undefined;
15
+ connectionTimeoutMillis?: number | undefined;
16
+ options?: Record<string, unknown> | undefined;
17
+ }, {
18
+ type: "in-memory";
19
+ maxConnections?: number | undefined;
20
+ idleTimeoutMillis?: number | undefined;
21
+ connectionTimeoutMillis?: number | undefined;
22
+ options?: Record<string, unknown> | undefined;
23
+ }>;
24
+ export type InMemoryDatabaseConfig = z.output<typeof InMemoryDatabaseSchema>;
25
+ export declare const SqliteDatabaseSchema: z.ZodObject<{
26
+ maxConnections: z.ZodOptional<z.ZodNumber>;
27
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
28
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
29
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
30
+ } & {
31
+ type: z.ZodLiteral<"sqlite">;
32
+ path: z.ZodString;
33
+ }, "strict", z.ZodTypeAny, {
34
+ path: string;
35
+ type: "sqlite";
36
+ maxConnections?: number | undefined;
37
+ idleTimeoutMillis?: number | undefined;
38
+ connectionTimeoutMillis?: number | undefined;
39
+ options?: Record<string, unknown> | undefined;
40
+ }, {
41
+ path: string;
42
+ type: "sqlite";
43
+ maxConnections?: number | undefined;
44
+ idleTimeoutMillis?: number | undefined;
45
+ connectionTimeoutMillis?: number | undefined;
46
+ options?: Record<string, unknown> | undefined;
47
+ }>;
48
+ export type SqliteDatabaseConfig = z.output<typeof SqliteDatabaseSchema>;
49
+ export declare const PostgresDatabaseSchema: z.ZodEffects<z.ZodObject<{
50
+ maxConnections: z.ZodOptional<z.ZodNumber>;
51
+ idleTimeoutMillis: z.ZodOptional<z.ZodNumber>;
52
+ connectionTimeoutMillis: z.ZodOptional<z.ZodNumber>;
53
+ options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
54
+ } & {
55
+ type: z.ZodLiteral<"postgres">;
56
+ url: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
57
+ connectionString: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
58
+ host: z.ZodOptional<z.ZodString>;
59
+ port: z.ZodOptional<z.ZodNumber>;
60
+ database: z.ZodOptional<z.ZodString>;
61
+ password: z.ZodOptional<z.ZodString>;
62
+ keyPrefix: z.ZodOptional<z.ZodString>;
63
+ }, "strict", z.ZodTypeAny, {
64
+ type: "postgres";
65
+ maxConnections?: number | undefined;
66
+ idleTimeoutMillis?: number | undefined;
67
+ connectionTimeoutMillis?: number | undefined;
68
+ options?: Record<string, unknown> | undefined;
69
+ url?: string | undefined;
70
+ host?: string | undefined;
71
+ port?: number | undefined;
72
+ password?: string | undefined;
73
+ database?: string | undefined;
74
+ connectionString?: string | undefined;
75
+ keyPrefix?: string | undefined;
76
+ }, {
77
+ type: "postgres";
78
+ maxConnections?: number | undefined;
79
+ idleTimeoutMillis?: number | undefined;
80
+ connectionTimeoutMillis?: number | undefined;
81
+ options?: Record<string, unknown> | undefined;
82
+ url?: string | undefined;
83
+ host?: string | undefined;
84
+ port?: number | undefined;
85
+ password?: string | undefined;
86
+ database?: string | undefined;
87
+ connectionString?: string | undefined;
88
+ keyPrefix?: string | undefined;
89
+ }>, {
90
+ type: "postgres";
91
+ maxConnections?: number | undefined;
92
+ idleTimeoutMillis?: number | undefined;
93
+ connectionTimeoutMillis?: number | undefined;
94
+ options?: Record<string, unknown> | undefined;
95
+ url?: string | undefined;
96
+ host?: string | undefined;
97
+ port?: number | undefined;
98
+ password?: string | undefined;
99
+ database?: string | undefined;
100
+ connectionString?: string | undefined;
101
+ keyPrefix?: string | undefined;
102
+ }, {
103
+ type: "postgres";
104
+ maxConnections?: number | undefined;
105
+ idleTimeoutMillis?: number | undefined;
106
+ connectionTimeoutMillis?: number | undefined;
107
+ options?: Record<string, unknown> | undefined;
108
+ url?: string | undefined;
109
+ host?: string | undefined;
110
+ port?: number | undefined;
111
+ password?: string | undefined;
112
+ database?: string | undefined;
113
+ connectionString?: string | undefined;
114
+ keyPrefix?: string | undefined;
115
+ }>;
116
+ export type PostgresDatabaseConfig = z.output<typeof PostgresDatabaseSchema>;
117
+ export declare const DatabaseConfigSchema: z.ZodObject<{
118
+ type: z.ZodString;
119
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
120
+ type: z.ZodString;
121
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
122
+ type: z.ZodString;
123
+ }, z.ZodTypeAny, "passthrough">>;
124
+ export type DatabaseConfig = z.output<typeof DatabaseConfigSchema>;
125
+ //# sourceMappingURL=schemas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../../src/database/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,eAAO,MAAM,cAAc,8CAA+C,CAAC;AAC3E,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAoB3D,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;EAGxB,CAAC;AAEZ,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAG7E,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;EAOtB,CAAC;AAEZ,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAGzE,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA8B7B,CAAC;AAEP,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAG7E,eAAO,MAAM,oBAAoB;;;;;;gCAKmC,CAAC;AAErE,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,oBAAoB,CAAC,CAAC"}