@elizaos/plugin-sql 1.0.7 → 1.0.8
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/dist/index.d.ts +2431 -5
- package/dist/index.js +1420 -233
- package/dist/index.js.map +1 -1
- package/package.json +16 -8
- package/dist/chunk-74FVWTXP.js +0 -401
- package/dist/chunk-74FVWTXP.js.map +0 -1
- package/dist/migrate.d.ts +0 -2
- package/dist/migrate.js +0 -57
- package/dist/migrate.js.map +0 -1
- package/drizzle/extension.sql +0 -4
- package/drizzle/migrations/0000_thin_madame_web.sql +0 -256
- package/drizzle/migrations/meta/0000_snapshot.json +0 -1629
- package/drizzle/migrations/meta/_journal.json +0 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-sql",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -27,17 +27,22 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@electric-sql/pglite": "^0.2.17",
|
|
30
|
-
"@elizaos/core": "1.0.
|
|
30
|
+
"@elizaos/core": "1.0.8",
|
|
31
31
|
"@types/pg": "8.11.10",
|
|
32
32
|
"drizzle-kit": "^0.30.4",
|
|
33
|
-
"drizzle-orm": "^0.
|
|
33
|
+
"drizzle-orm": "^0.44.2",
|
|
34
34
|
"pg": "^8.13.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
+
"@eslint/js": "^9.28.0",
|
|
37
38
|
"@types/node": "^22.15.3",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^8.26.0",
|
|
40
|
+
"@typescript-eslint/parser": "^8.26.0",
|
|
38
41
|
"dockerode": "^4.0.4",
|
|
42
|
+
"eslint": "^9.28.0",
|
|
39
43
|
"prettier": "3.5.3",
|
|
40
|
-
"tsup": "8.
|
|
44
|
+
"tsup": "8.5.0",
|
|
45
|
+
"typescript-eslint": "^8.26.0",
|
|
41
46
|
"vitest": "3.1.4",
|
|
42
47
|
"zod": "^3.22.4"
|
|
43
48
|
},
|
|
@@ -46,18 +51,21 @@
|
|
|
46
51
|
"dev": "tsup --watch",
|
|
47
52
|
"migrate:generate": "drizzle-kit generate",
|
|
48
53
|
"migrate": "tsup src/migrate.ts --format esm --clean=false --onSuccess=\"node dist/migrate.js\"",
|
|
49
|
-
"lint": "
|
|
54
|
+
"lint": "eslint .",
|
|
50
55
|
"clean": "rm -rf dist .turbo node_modules .turbo-tsconfig.json tsconfig.tsbuildinfo",
|
|
51
|
-
"format": "prettier --
|
|
56
|
+
"format": "prettier --check .",
|
|
52
57
|
"format:check": "prettier --check ./src",
|
|
53
58
|
"test": "vitest run",
|
|
54
59
|
"test:watch": "vitest",
|
|
55
60
|
"test:coverage": "vitest run --coverage",
|
|
56
|
-
"test:integration": "vitest run __tests__/integration"
|
|
61
|
+
"test:integration": "vitest run __tests__/integration",
|
|
62
|
+
"test:postgres": "POSTGRES_URL=postgresql://postgres:cT5nfCeebBS8R0q3@db.mwyntrazuakaieibjuvo.supabase.co:5432/postgres vitest run",
|
|
63
|
+
"build:clean": "rm -rf dist",
|
|
64
|
+
"lint:fix": "eslint . --fix"
|
|
57
65
|
},
|
|
58
66
|
"peerDependencies": {
|
|
59
67
|
"typescript": "5.8.2",
|
|
60
68
|
"whatwg-url": "7.1.0"
|
|
61
69
|
},
|
|
62
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "928b3cadaf4fb6bd4337932f63c759fb34fc6964"
|
|
63
71
|
}
|
package/dist/chunk-74FVWTXP.js
DELETED
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/pglite/manager.ts
|
|
5
|
-
import { dirname as pathDirname, resolve as pathResolve } from "node:path";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { PGlite } from "@electric-sql/pglite";
|
|
8
|
-
import { fuzzystrmatch } from "@electric-sql/pglite/contrib/fuzzystrmatch";
|
|
9
|
-
import { vector } from "@electric-sql/pglite/vector";
|
|
10
|
-
import { logger } from "@elizaos/core";
|
|
11
|
-
import { drizzle } from "drizzle-orm/pglite";
|
|
12
|
-
import { migrate } from "drizzle-orm/pglite/migrator";
|
|
13
|
-
var PGliteClientManager = class {
|
|
14
|
-
static {
|
|
15
|
-
__name(this, "PGliteClientManager");
|
|
16
|
-
}
|
|
17
|
-
client;
|
|
18
|
-
shuttingDown = false;
|
|
19
|
-
shutdownTimeout = 500;
|
|
20
|
-
/**
|
|
21
|
-
* Constructor for creating a new instance of PGlite with the provided options.
|
|
22
|
-
* Initializes the PGlite client with additional extensions.
|
|
23
|
-
* @param {PGliteOptions} options - The options to configure the PGlite client.
|
|
24
|
-
*/
|
|
25
|
-
constructor(options) {
|
|
26
|
-
this.client = new PGlite({
|
|
27
|
-
...options,
|
|
28
|
-
extensions: {
|
|
29
|
-
vector,
|
|
30
|
-
fuzzystrmatch
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
this.setupShutdownHandlers();
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Retrieves the PostgreSQL lite connection.
|
|
37
|
-
*
|
|
38
|
-
* @returns {PGlite} The PostgreSQL lite connection.
|
|
39
|
-
* @throws {Error} If the client manager is currently shutting down.
|
|
40
|
-
*/
|
|
41
|
-
getConnection() {
|
|
42
|
-
if (this.shuttingDown) {
|
|
43
|
-
throw new Error("Client manager is shutting down");
|
|
44
|
-
}
|
|
45
|
-
return this.client;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Initiates a graceful shutdown of the PGlite client.
|
|
49
|
-
* Checks if the client is already in the process of shutting down.
|
|
50
|
-
* Logs the start of shutdown process and sets shuttingDown flag to true.
|
|
51
|
-
* Sets a timeout for the shutdown process and forces closure of database connection if timeout is reached.
|
|
52
|
-
* Handles the shutdown process, closes the client connection, clears the timeout, and logs the completion of shutdown.
|
|
53
|
-
* Logs any errors that occur during the shutdown process.
|
|
54
|
-
*/
|
|
55
|
-
async gracefulShutdown() {
|
|
56
|
-
if (this.shuttingDown) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
this.shuttingDown = true;
|
|
60
|
-
logger.info("Starting graceful shutdown of PGlite client...");
|
|
61
|
-
const timeout = setTimeout(() => {
|
|
62
|
-
logger.warn("Shutdown timeout reached, forcing database connection closure...");
|
|
63
|
-
this.client.close().finally(() => {
|
|
64
|
-
logger.warn("Forced database connection closure complete.");
|
|
65
|
-
if (process.env.NODE_ENV !== "test") {
|
|
66
|
-
process.exit(1);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
}, this.shutdownTimeout);
|
|
70
|
-
try {
|
|
71
|
-
await this.client.close();
|
|
72
|
-
clearTimeout(timeout);
|
|
73
|
-
logger.info("PGlite client shutdown completed successfully");
|
|
74
|
-
if (process.env.NODE_ENV !== "test") {
|
|
75
|
-
process.exit(0);
|
|
76
|
-
}
|
|
77
|
-
} catch (error) {
|
|
78
|
-
logger.error("Error during graceful shutdown:", error);
|
|
79
|
-
if (process.env.NODE_ENV !== "test") {
|
|
80
|
-
process.exit(1);
|
|
81
|
-
} else {
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Sets up shutdown handlers for SIGINT, SIGTERM, and beforeExit events to gracefully shutdown the application.
|
|
88
|
-
* @private
|
|
89
|
-
*/
|
|
90
|
-
setupShutdownHandlers() {
|
|
91
|
-
process.on("SIGINT", async () => {
|
|
92
|
-
await this.gracefulShutdown();
|
|
93
|
-
});
|
|
94
|
-
process.on("SIGTERM", async () => {
|
|
95
|
-
await this.gracefulShutdown();
|
|
96
|
-
});
|
|
97
|
-
process.on("beforeExit", async () => {
|
|
98
|
-
await this.gracefulShutdown();
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Initializes the client for PGlite.
|
|
103
|
-
*
|
|
104
|
-
* @returns {Promise<void>} A Promise that resolves when the client is initialized successfully
|
|
105
|
-
*/
|
|
106
|
-
async initialize() {
|
|
107
|
-
try {
|
|
108
|
-
await this.client.waitReady;
|
|
109
|
-
await this.client.query("CREATE EXTENSION IF NOT EXISTS vector;");
|
|
110
|
-
await this.client.query("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;");
|
|
111
|
-
logger.info("PGlite client initialized and extensions ensured successfully");
|
|
112
|
-
} catch (error) {
|
|
113
|
-
logger.error("Failed to initialize PGlite client:", error);
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Asynchronously closes the resource. If the resource is not already shutting down,
|
|
119
|
-
* it performs a graceful shutdown before closing.
|
|
120
|
-
*
|
|
121
|
-
* @returns A promise that resolves once the resource has been closed.
|
|
122
|
-
*/
|
|
123
|
-
async close() {
|
|
124
|
-
if (!this.shuttingDown) {
|
|
125
|
-
await this.gracefulShutdown();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Check if the system is currently shutting down.
|
|
130
|
-
*
|
|
131
|
-
* @returns {boolean} True if the system is shutting down, false otherwise.
|
|
132
|
-
*/
|
|
133
|
-
isShuttingDown() {
|
|
134
|
-
return this.shuttingDown;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Asynchronously runs database migrations using Drizzle.
|
|
138
|
-
*
|
|
139
|
-
* Drizzle will first check if the migrations are already applied.
|
|
140
|
-
* If there is a diff between database schema and migrations, it will apply the migrations.
|
|
141
|
-
* If they are already applied, it will skip them.
|
|
142
|
-
*
|
|
143
|
-
* @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.
|
|
144
|
-
*/
|
|
145
|
-
async runMigrations() {
|
|
146
|
-
try {
|
|
147
|
-
const db = drizzle(this.client);
|
|
148
|
-
const packageJsonUrl = await import.meta.resolve("@elizaos/plugin-sql/package.json");
|
|
149
|
-
const packageJsonPath = fileURLToPath(packageJsonUrl);
|
|
150
|
-
const packageRoot = pathDirname(packageJsonPath);
|
|
151
|
-
const migrationsPath = pathResolve(packageRoot, "drizzle/migrations");
|
|
152
|
-
logger.debug(
|
|
153
|
-
`Resolved migrations path (pglite) using import.meta.resolve: ${migrationsPath}`
|
|
154
|
-
);
|
|
155
|
-
await migrate(db, {
|
|
156
|
-
migrationsFolder: migrationsPath,
|
|
157
|
-
migrationsSchema: "public"
|
|
158
|
-
});
|
|
159
|
-
} catch (error) {
|
|
160
|
-
logger.error("Failed to run database migrations (pglite):", error);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
// src/pg/manager.ts
|
|
166
|
-
import path from "node:path";
|
|
167
|
-
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
168
|
-
import { logger as logger2 } from "@elizaos/core";
|
|
169
|
-
import { drizzle as drizzle2 } from "drizzle-orm/node-postgres";
|
|
170
|
-
import { migrate as migrate2 } from "drizzle-orm/node-postgres/migrator";
|
|
171
|
-
import pkg from "pg";
|
|
172
|
-
var { Pool } = pkg;
|
|
173
|
-
var PostgresConnectionManager = class {
|
|
174
|
-
static {
|
|
175
|
-
__name(this, "PostgresConnectionManager");
|
|
176
|
-
}
|
|
177
|
-
pool;
|
|
178
|
-
isShuttingDown = false;
|
|
179
|
-
connectionTimeout = 5e3;
|
|
180
|
-
/**
|
|
181
|
-
* Constructor for creating a connection pool.
|
|
182
|
-
* @param {string} connectionString - The connection string used to connect to the database.
|
|
183
|
-
*/
|
|
184
|
-
constructor(connectionString) {
|
|
185
|
-
const defaultConfig = {
|
|
186
|
-
max: 20,
|
|
187
|
-
idleTimeoutMillis: 3e4,
|
|
188
|
-
connectionTimeoutMillis: this.connectionTimeout
|
|
189
|
-
};
|
|
190
|
-
this.pool = new Pool({
|
|
191
|
-
...defaultConfig,
|
|
192
|
-
connectionString
|
|
193
|
-
});
|
|
194
|
-
this.pool.on("error", (err) => {
|
|
195
|
-
logger2.error("Unexpected pool error", err);
|
|
196
|
-
this.handlePoolError(err);
|
|
197
|
-
});
|
|
198
|
-
this.setupPoolErrorHandling();
|
|
199
|
-
this.testConnection();
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Handles a pool error by attempting to reconnect the pool.
|
|
203
|
-
*
|
|
204
|
-
* @param {Error} error The error that occurred in the pool.
|
|
205
|
-
* @throws {Error} If failed to reconnect the pool.
|
|
206
|
-
*/
|
|
207
|
-
async handlePoolError(error) {
|
|
208
|
-
logger2.error("Pool error occurred, attempting to reconnect", {
|
|
209
|
-
error: error.message
|
|
210
|
-
});
|
|
211
|
-
try {
|
|
212
|
-
await this.pool.end();
|
|
213
|
-
this.pool = new Pool({
|
|
214
|
-
...this.pool.options,
|
|
215
|
-
connectionTimeoutMillis: this.connectionTimeout
|
|
216
|
-
});
|
|
217
|
-
await this.testConnection();
|
|
218
|
-
logger2.success("Pool reconnection successful");
|
|
219
|
-
} catch (reconnectError) {
|
|
220
|
-
logger2.error("Failed to reconnect pool", {
|
|
221
|
-
error: reconnectError instanceof Error ? reconnectError.message : String(reconnectError)
|
|
222
|
-
});
|
|
223
|
-
throw reconnectError;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Asynchronously tests the database connection by executing a query to get the current timestamp.
|
|
228
|
-
*
|
|
229
|
-
* @returns {Promise<boolean>} - A Promise that resolves to true if the database connection test is successful.
|
|
230
|
-
*/
|
|
231
|
-
async testConnection() {
|
|
232
|
-
let client = null;
|
|
233
|
-
try {
|
|
234
|
-
client = await this.pool.connect();
|
|
235
|
-
const result = await client.query("SELECT NOW()");
|
|
236
|
-
logger2.success("Database connection test successful:", result.rows[0]);
|
|
237
|
-
return true;
|
|
238
|
-
} catch (error) {
|
|
239
|
-
logger2.error("Database connection test failed:", error);
|
|
240
|
-
throw new Error(`Failed to connect to database: ${error.message}`);
|
|
241
|
-
} finally {
|
|
242
|
-
if (client) client.release();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Sets up event listeners to handle pool cleanup on SIGINT, SIGTERM, and beforeExit events.
|
|
247
|
-
*/
|
|
248
|
-
setupPoolErrorHandling() {
|
|
249
|
-
process.on("SIGINT", async () => {
|
|
250
|
-
await this.cleanup();
|
|
251
|
-
process.exit(0);
|
|
252
|
-
});
|
|
253
|
-
process.on("SIGTERM", async () => {
|
|
254
|
-
await this.cleanup();
|
|
255
|
-
process.exit(0);
|
|
256
|
-
});
|
|
257
|
-
process.on("beforeExit", async () => {
|
|
258
|
-
await this.cleanup();
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Get the connection pool.
|
|
263
|
-
* @returns {PgPool} The connection pool
|
|
264
|
-
* @throws {Error} If the connection manager is shutting down or an error occurs when trying to get the connection from the pool
|
|
265
|
-
*/
|
|
266
|
-
getConnection() {
|
|
267
|
-
if (this.isShuttingDown) {
|
|
268
|
-
throw new Error("Connection manager is shutting down");
|
|
269
|
-
}
|
|
270
|
-
try {
|
|
271
|
-
return this.pool;
|
|
272
|
-
} catch (error) {
|
|
273
|
-
logger2.error("Failed to get connection from pool:", error);
|
|
274
|
-
throw error;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Asynchronously acquires a database client from the connection pool.
|
|
279
|
-
*
|
|
280
|
-
* @returns {Promise<pkg.PoolClient>} A Promise that resolves with the acquired database client.
|
|
281
|
-
* @throws {Error} If an error occurs while acquiring the database client.
|
|
282
|
-
*/
|
|
283
|
-
async getClient() {
|
|
284
|
-
try {
|
|
285
|
-
return await this.pool.connect();
|
|
286
|
-
} catch (error) {
|
|
287
|
-
logger2.error("Failed to acquire a database client:", error);
|
|
288
|
-
throw error;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Initializes the PostgreSQL connection manager by testing the connection and logging the result.
|
|
293
|
-
*
|
|
294
|
-
* @returns {Promise<void>} A Promise that resolves once the manager is successfully initialized
|
|
295
|
-
* @throws {Error} If there is an error initializing the connection manager
|
|
296
|
-
*/
|
|
297
|
-
async initialize() {
|
|
298
|
-
try {
|
|
299
|
-
await this.testConnection();
|
|
300
|
-
logger2.debug("PostgreSQL connection manager initialized successfully");
|
|
301
|
-
} catch (error) {
|
|
302
|
-
logger2.error("Failed to initialize connection manager:", error);
|
|
303
|
-
throw error;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Asynchronously close the current process by executing a cleanup function.
|
|
308
|
-
* @returns A promise that resolves once the cleanup is complete.
|
|
309
|
-
*/
|
|
310
|
-
async close() {
|
|
311
|
-
await this.cleanup();
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Cleans up and closes the database pool.
|
|
315
|
-
* @returns {Promise<void>} A Promise that resolves when the database pool is closed.
|
|
316
|
-
*/
|
|
317
|
-
async cleanup() {
|
|
318
|
-
try {
|
|
319
|
-
await this.pool.end();
|
|
320
|
-
logger2.info("Database pool closed");
|
|
321
|
-
} catch (error) {
|
|
322
|
-
logger2.error("Error closing database pool:", error);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Asynchronously runs database migrations using the Drizzle library.
|
|
327
|
-
*
|
|
328
|
-
* Drizzle will first check if the migrations are already applied.
|
|
329
|
-
* If there is a diff between database schema and migrations, it will apply the migrations.
|
|
330
|
-
* If they are already applied, it will skip them.
|
|
331
|
-
*
|
|
332
|
-
* @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.
|
|
333
|
-
*/
|
|
334
|
-
async runMigrations() {
|
|
335
|
-
try {
|
|
336
|
-
const db = drizzle2(this.pool);
|
|
337
|
-
const packageJsonUrl = await import.meta.resolve("@elizaos/plugin-sql/package.json");
|
|
338
|
-
const packageJsonPath = fileURLToPath2(packageJsonUrl);
|
|
339
|
-
const packageRoot = path.dirname(packageJsonPath);
|
|
340
|
-
const migrationsPath = path.resolve(packageRoot, "drizzle/migrations");
|
|
341
|
-
logger2.debug(`Resolved migrations path (pg) using import.meta.resolve: ${migrationsPath}`);
|
|
342
|
-
await migrate2(db, {
|
|
343
|
-
migrationsFolder: migrationsPath,
|
|
344
|
-
migrationsSchema: "public"
|
|
345
|
-
});
|
|
346
|
-
} catch (error) {
|
|
347
|
-
if (error instanceof Error && error.message.includes("already exists")) {
|
|
348
|
-
logger2.info(
|
|
349
|
-
"Database migrations already applied - if you need to upgrade, you will need to manually migrate or reset your database"
|
|
350
|
-
);
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
logger2.error("Failed to run database migrations (pg):", error);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
// src/utils.ts
|
|
359
|
-
import dotenv from "dotenv";
|
|
360
|
-
import { existsSync } from "node:fs";
|
|
361
|
-
import path2 from "node:path";
|
|
362
|
-
function expandTildePath(filepath) {
|
|
363
|
-
if (filepath && filepath.startsWith("~")) {
|
|
364
|
-
return path2.join(process.cwd(), filepath.slice(1));
|
|
365
|
-
}
|
|
366
|
-
return filepath;
|
|
367
|
-
}
|
|
368
|
-
__name(expandTildePath, "expandTildePath");
|
|
369
|
-
function resolveEnvFile(startDir = process.cwd()) {
|
|
370
|
-
let currentDir = startDir;
|
|
371
|
-
while (true) {
|
|
372
|
-
const candidate = path2.join(currentDir, ".env");
|
|
373
|
-
if (existsSync(candidate)) {
|
|
374
|
-
return candidate;
|
|
375
|
-
}
|
|
376
|
-
const parentDir = path2.dirname(currentDir);
|
|
377
|
-
if (parentDir === currentDir) {
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
currentDir = parentDir;
|
|
381
|
-
}
|
|
382
|
-
return path2.join(startDir, ".env");
|
|
383
|
-
}
|
|
384
|
-
__name(resolveEnvFile, "resolveEnvFile");
|
|
385
|
-
function resolvePgliteDir(dir, fallbackDir) {
|
|
386
|
-
const envPath = resolveEnvFile();
|
|
387
|
-
if (existsSync(envPath)) {
|
|
388
|
-
dotenv.config({ path: envPath });
|
|
389
|
-
}
|
|
390
|
-
const base = dir ?? process.env.PGLITE_DATA_DIR ?? fallbackDir ?? path2.join(process.cwd(), ".elizadb");
|
|
391
|
-
return expandTildePath(base);
|
|
392
|
-
}
|
|
393
|
-
__name(resolvePgliteDir, "resolvePgliteDir");
|
|
394
|
-
|
|
395
|
-
export {
|
|
396
|
-
__name,
|
|
397
|
-
PGliteClientManager,
|
|
398
|
-
PostgresConnectionManager,
|
|
399
|
-
resolvePgliteDir
|
|
400
|
-
};
|
|
401
|
-
//# sourceMappingURL=chunk-74FVWTXP.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pglite/manager.ts","../src/pg/manager.ts","../src/utils.ts"],"sourceRoot":"./","sourcesContent":["import { dirname as pathDirname, resolve as pathResolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { PGlite, type PGliteOptions } from '@electric-sql/pglite';\nimport { fuzzystrmatch } from '@electric-sql/pglite/contrib/fuzzystrmatch';\nimport { vector } from '@electric-sql/pglite/vector';\nimport { logger } from '@elizaos/core';\nimport { drizzle } from 'drizzle-orm/pglite';\nimport { migrate } from 'drizzle-orm/pglite/migrator';\nimport type { IDatabaseClientManager } from '../types';\n\n/**\n * Class representing a database client manager for PGlite.\n * @implements { IDatabaseClientManager }\n */\nexport class PGliteClientManager implements IDatabaseClientManager<PGlite> {\n private client: PGlite;\n private shuttingDown = false;\n private readonly shutdownTimeout = 500;\n\n /**\n * Constructor for creating a new instance of PGlite with the provided options.\n * Initializes the PGlite client with additional extensions.\n * @param {PGliteOptions} options - The options to configure the PGlite client.\n */\n constructor(options: PGliteOptions) {\n this.client = new PGlite({\n ...options,\n extensions: {\n vector,\n fuzzystrmatch,\n },\n });\n this.setupShutdownHandlers();\n }\n\n /**\n * Retrieves the PostgreSQL lite connection.\n *\n * @returns {PGlite} The PostgreSQL lite connection.\n * @throws {Error} If the client manager is currently shutting down.\n */\n public getConnection(): PGlite {\n if (this.shuttingDown) {\n throw new Error('Client manager is shutting down');\n }\n return this.client;\n }\n\n /**\n * Initiates a graceful shutdown of the PGlite client.\n * Checks if the client is already in the process of shutting down.\n * Logs the start of shutdown process and sets shuttingDown flag to true.\n * Sets a timeout for the shutdown process and forces closure of database connection if timeout is reached.\n * Handles the shutdown process, closes the client connection, clears the timeout, and logs the completion of shutdown.\n * Logs any errors that occur during the shutdown process.\n */\n private async gracefulShutdown() {\n if (this.shuttingDown) {\n return;\n }\n\n this.shuttingDown = true;\n logger.info('Starting graceful shutdown of PGlite client...');\n\n const timeout = setTimeout(() => {\n logger.warn('Shutdown timeout reached, forcing database connection closure...');\n this.client.close().finally(() => {\n logger.warn('Forced database connection closure complete.');\n if (process.env.NODE_ENV !== 'test') {\n process.exit(1);\n }\n });\n }, this.shutdownTimeout);\n\n try {\n await this.client.close();\n clearTimeout(timeout);\n logger.info('PGlite client shutdown completed successfully');\n if (process.env.NODE_ENV !== 'test') {\n process.exit(0);\n }\n } catch (error) {\n logger.error('Error during graceful shutdown:', error);\n if (process.env.NODE_ENV !== 'test') {\n process.exit(1);\n } else {\n throw error;\n }\n }\n }\n\n /**\n * Sets up shutdown handlers for SIGINT, SIGTERM, and beforeExit events to gracefully shutdown the application.\n * @private\n */\n private setupShutdownHandlers() {\n process.on('SIGINT', async () => {\n await this.gracefulShutdown();\n });\n\n process.on('SIGTERM', async () => {\n await this.gracefulShutdown();\n });\n\n process.on('beforeExit', async () => {\n await this.gracefulShutdown();\n });\n }\n\n /**\n * Initializes the client for PGlite.\n *\n * @returns {Promise<void>} A Promise that resolves when the client is initialized successfully\n */\n public async initialize(): Promise<void> {\n try {\n await this.client.waitReady;\n // Explicitly create extensions\n await this.client.query('CREATE EXTENSION IF NOT EXISTS vector;');\n await this.client.query('CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;');\n logger.info('PGlite client initialized and extensions ensured successfully');\n } catch (error) {\n logger.error('Failed to initialize PGlite client:', error);\n throw error;\n }\n }\n\n /**\n * Asynchronously closes the resource. If the resource is not already shutting down,\n * it performs a graceful shutdown before closing.\n *\n * @returns A promise that resolves once the resource has been closed.\n */\n public async close(): Promise<void> {\n if (!this.shuttingDown) {\n await this.gracefulShutdown();\n }\n }\n\n /**\n * Check if the system is currently shutting down.\n *\n * @returns {boolean} True if the system is shutting down, false otherwise.\n */\n public isShuttingDown(): boolean {\n return this.shuttingDown;\n }\n\n /**\n * Asynchronously runs database migrations using Drizzle.\n *\n * Drizzle will first check if the migrations are already applied.\n * If there is a diff between database schema and migrations, it will apply the migrations.\n * If they are already applied, it will skip them.\n *\n * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n */\n async runMigrations(): Promise<void> {\n try {\n const db = drizzle(this.client);\n\n const packageJsonUrl = await import.meta.resolve('@elizaos/plugin-sql/package.json');\n const packageJsonPath = fileURLToPath(packageJsonUrl);\n const packageRoot = pathDirname(packageJsonPath);\n const migrationsPath = pathResolve(packageRoot, 'drizzle/migrations');\n logger.debug(\n `Resolved migrations path (pglite) using import.meta.resolve: ${migrationsPath}`\n );\n\n await migrate(db, {\n migrationsFolder: migrationsPath,\n migrationsSchema: 'public',\n });\n } catch (error) {\n logger.error('Failed to run database migrations (pglite):', error);\n }\n }\n}\n","import path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { logger } from '@elizaos/core';\nimport { drizzle } from 'drizzle-orm/node-postgres';\nimport { migrate } from 'drizzle-orm/node-postgres/migrator';\nimport pkg, { type Pool as PgPool } from 'pg';\nimport type { IDatabaseClientManager } from '../types';\n\nconst { Pool } = pkg;\n\n/**\n * Manages connections to a PostgreSQL database using a connection pool.\n * Implements IDatabaseClientManager interface.\n */\n\nexport class PostgresConnectionManager implements IDatabaseClientManager<PgPool> {\n private pool: PgPool;\n private isShuttingDown = false;\n private readonly connectionTimeout: number = 5000;\n\n /**\n * Constructor for creating a connection pool.\n * @param {string} connectionString - The connection string used to connect to the database.\n */\n constructor(connectionString: string) {\n const defaultConfig = {\n max: 20,\n idleTimeoutMillis: 30000,\n connectionTimeoutMillis: this.connectionTimeout,\n };\n\n this.pool = new Pool({\n ...defaultConfig,\n connectionString,\n });\n\n this.pool.on('error', (err) => {\n logger.error('Unexpected pool error', err);\n this.handlePoolError(err);\n });\n\n this.setupPoolErrorHandling();\n this.testConnection();\n }\n\n /**\n * Handles a pool error by attempting to reconnect the pool.\n *\n * @param {Error} error The error that occurred in the pool.\n * @throws {Error} If failed to reconnect the pool.\n */\n private async handlePoolError(error: Error) {\n logger.error('Pool error occurred, attempting to reconnect', {\n error: error.message,\n });\n\n try {\n await this.pool.end();\n\n this.pool = new Pool({\n ...this.pool.options,\n connectionTimeoutMillis: this.connectionTimeout,\n });\n\n await this.testConnection();\n logger.success('Pool reconnection successful');\n } catch (reconnectError) {\n logger.error('Failed to reconnect pool', {\n error: reconnectError instanceof Error ? reconnectError.message : String(reconnectError),\n });\n throw reconnectError;\n }\n }\n\n /**\n * Asynchronously tests the database connection by executing a query to get the current timestamp.\n *\n * @returns {Promise<boolean>} - A Promise that resolves to true if the database connection test is successful.\n */\n async testConnection(): Promise<boolean> {\n let client: pkg.PoolClient | null = null;\n try {\n client = await this.pool.connect();\n const result = await client.query('SELECT NOW()');\n logger.success('Database connection test successful:', result.rows[0]);\n return true;\n } catch (error) {\n logger.error('Database connection test failed:', error);\n throw new Error(`Failed to connect to database: ${(error as Error).message}`);\n } finally {\n if (client) client.release();\n }\n }\n\n /**\n * Sets up event listeners to handle pool cleanup on SIGINT, SIGTERM, and beforeExit events.\n */\n private setupPoolErrorHandling() {\n process.on('SIGINT', async () => {\n await this.cleanup();\n process.exit(0);\n });\n\n process.on('SIGTERM', async () => {\n await this.cleanup();\n process.exit(0);\n });\n\n process.on('beforeExit', async () => {\n await this.cleanup();\n });\n }\n\n /**\n * Get the connection pool.\n * @returns {PgPool} The connection pool\n * @throws {Error} If the connection manager is shutting down or an error occurs when trying to get the connection from the pool\n */\n public getConnection(): PgPool {\n if (this.isShuttingDown) {\n throw new Error('Connection manager is shutting down');\n }\n\n try {\n return this.pool;\n } catch (error) {\n logger.error('Failed to get connection from pool:', error);\n throw error;\n }\n }\n\n /**\n * Asynchronously acquires a database client from the connection pool.\n *\n * @returns {Promise<pkg.PoolClient>} A Promise that resolves with the acquired database client.\n * @throws {Error} If an error occurs while acquiring the database client.\n */\n public async getClient(): Promise<pkg.PoolClient> {\n try {\n return await this.pool.connect();\n } catch (error) {\n logger.error('Failed to acquire a database client:', error);\n throw error;\n }\n }\n\n /**\n * Initializes the PostgreSQL connection manager by testing the connection and logging the result.\n *\n * @returns {Promise<void>} A Promise that resolves once the manager is successfully initialized\n * @throws {Error} If there is an error initializing the connection manager\n */\n public async initialize(): Promise<void> {\n try {\n await this.testConnection();\n logger.debug('PostgreSQL connection manager initialized successfully');\n } catch (error) {\n logger.error('Failed to initialize connection manager:', error);\n throw error;\n }\n }\n\n /**\n * Asynchronously close the current process by executing a cleanup function.\n * @returns A promise that resolves once the cleanup is complete.\n */\n public async close(): Promise<void> {\n await this.cleanup();\n }\n\n /**\n * Cleans up and closes the database pool.\n * @returns {Promise<void>} A Promise that resolves when the database pool is closed.\n */\n async cleanup(): Promise<void> {\n try {\n await this.pool.end();\n logger.info('Database pool closed');\n } catch (error) {\n logger.error('Error closing database pool:', error);\n }\n }\n\n /**\n * Asynchronously runs database migrations using the Drizzle library.\n *\n * Drizzle will first check if the migrations are already applied.\n * If there is a diff between database schema and migrations, it will apply the migrations.\n * If they are already applied, it will skip them.\n *\n * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n */\n async runMigrations(): Promise<void> {\n try {\n const db = drizzle(this.pool);\n\n const packageJsonUrl = await import.meta.resolve('@elizaos/plugin-sql/package.json');\n const packageJsonPath = fileURLToPath(packageJsonUrl);\n const packageRoot = path.dirname(packageJsonPath);\n const migrationsPath = path.resolve(packageRoot, 'drizzle/migrations');\n logger.debug(`Resolved migrations path (pg) using import.meta.resolve: ${migrationsPath}`);\n\n await migrate(db, {\n migrationsFolder: migrationsPath,\n migrationsSchema: 'public',\n });\n } catch (error) {\n if (error instanceof Error && error.message.includes('already exists')) {\n logger.info(\n 'Database migrations already applied - if you need to upgrade, you will need to manually migrate or reset your database'\n );\n return;\n }\n logger.error('Failed to run database migrations (pg):', error);\n }\n }\n}\n","import dotenv from 'dotenv';\nimport { existsSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Expands a file path starting with `~` to the project directory.\n *\n * @param filepath - The path to expand.\n * @returns The expanded path.\n */\nexport function expandTildePath(filepath: string): string {\n if (filepath && filepath.startsWith('~')) {\n return path.join(process.cwd(), filepath.slice(1));\n }\n return filepath;\n}\n\n/**\n * Resolves the path to the nearest `.env` file.\n *\n * If no `.env` file is found when traversing up from the starting directory,\n * a path to `.env` in the starting directory is returned.\n *\n * @param startDir - The directory to start searching from. Defaults to the\n * current working directory.\n * @returns The resolved path to the `.env` file.\n */\nexport function resolveEnvFile(startDir: string = process.cwd()): string {\n let currentDir = startDir;\n\n while (true) {\n const candidate = path.join(currentDir, '.env');\n if (existsSync(candidate)) {\n return candidate;\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n break;\n }\n currentDir = parentDir;\n }\n\n return path.join(startDir, '.env');\n}\n\n/**\n * Resolves the directory used for PGlite database storage.\n *\n * Resolution order:\n * 1. The `dir` argument if provided.\n * 2. The `PGLITE_DATA_DIR` environment variable.\n * 3. The `fallbackDir` argument if provided.\n * 4. `./.elizadb` relative to the current working directory.\n *\n * @param dir - Optional directory preference.\n * @param fallbackDir - Optional fallback directory when env var is not set.\n * @returns The resolved data directory with any tilde expanded.\n */\nexport function resolvePgliteDir(dir?: string, fallbackDir?: string): string {\n const envPath = resolveEnvFile();\n if (existsSync(envPath)) {\n dotenv.config({ path: envPath });\n }\n\n const base =\n dir ?? process.env.PGLITE_DATA_DIR ?? fallbackDir ?? path.join(process.cwd(), '.elizadb');\n return expandTildePath(base);\n}\n"],"mappings":";;;;AAAA,SAAS,WAAW,aAAa,WAAW,mBAAmB;AAC/D,SAAS,qBAAqB;AAC9B,SAAS,cAAkC;AAC3C,SAAS,qBAAqB;AAC9B,SAAS,cAAc;AACvB,SAAS,cAAc;AACvB,SAAS,eAAe;AACxB,SAAS,eAAe;AAOjB,IAAM,sBAAN,MAAoE;AAAA,EAd3E,OAc2E;AAAA;AAAA;AAAA,EACjE;AAAA,EACA,eAAe;AAAA,EACN,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOnC,YAAY,SAAwB;AAClC,SAAK,SAAS,IAAI,OAAO;AAAA,MACvB,GAAG;AAAA,MACH,YAAY;AAAA,QACV;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AACD,SAAK,sBAAsB;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,gBAAwB;AAC7B,QAAI,KAAK,cAAc;AACrB,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACnD;AACA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAc,mBAAmB;AAC/B,QAAI,KAAK,cAAc;AACrB;AAAA,IACF;AAEA,SAAK,eAAe;AACpB,WAAO,KAAK,gDAAgD;AAE5D,UAAM,UAAU,WAAW,MAAM;AAC/B,aAAO,KAAK,kEAAkE;AAC9E,WAAK,OAAO,MAAM,EAAE,QAAQ,MAAM;AAChC,eAAO,KAAK,8CAA8C;AAC1D,YAAI,QAAQ,IAAI,aAAa,QAAQ;AACnC,kBAAQ,KAAK,CAAC;AAAA,QAChB;AAAA,MACF,CAAC;AAAA,IACH,GAAG,KAAK,eAAe;AAEvB,Q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,SAAQ,IAAI;AAAA,QAChB,kBAAkB;AAAA,QAClB,kBAAkB;AAAA,MACpB,CAAC;AAAA,IACH,SAAS,OAAO;AACd,UAAI,iBAAiB,SAAS,MAAM,QAAQ,SAAS,gBAAgB,GAAG;AACtE,QAAAH,QAAO;AAAA,UACL;AAAA,QACF;AACA;AAAA,MACF;AACA,MAAAA,QAAO,MAAM,2CAA2C,KAAK;AAAA,IAC/D;AAAA,EACF;AACF;;;ACxNA,OAAO,YAAY;AACnB,SAAS,kBAAkB;AAC3B,OAAOI,WAAU;AAQV,SAAS,gBAAgB,UAA0B;AACxD,MAAI,YAAY,SAAS,WAAW,GAAG,GAAG;AACxC,WAAOC,MAAK,KAAK,QAAQ,IAAI,GAAG,SAAS,MAAM,CAAC,CAAC;AAAA,EACnD;AACA,SAAO;AACT;AALgB;AAiBT,SAAS,eAAe,WAAmB,QAAQ,IAAI,GAAW;AACvE,MAAI,aAAa;AAEjB,SAAO,MAAM;AACX,UAAM,YAAYA,MAAK,KAAK,YAAY,MAAM;AAC9C,QAAI,WAAW,SAAS,GAAG;AACzB,aAAO;AAAA,IACT;AAEA,UAAM,YAAYA,MAAK,QAAQ,UAAU;AACzC,QAAI,cAAc,YAAY;AAC5B;AAAA,IACF;AACA,iBAAa;AAAA,EACf;AAEA,SAAOA,MAAK,KAAK,UAAU,MAAM;AACnC;AAjBgB;AAgCT,SAAS,iBAAiB,KAAc,aAA8B;AAC3E,QAAM,UAAU,eAAe;AAC/B,MAAI,WAAW,OAAO,GAAG;AACvB,WAAO,OAAO,EAAE,MAAM,QAAQ,CAAC;AAAA,EACjC;AAEA,QAAM,OACJ,OAAO,QAAQ,IAAI,mBAAmB,eAAeA,MAAK,KAAK,QAAQ,IAAI,GAAG,UAAU;AAC1F,SAAO,gBAAgB,IAAI;AAC7B;AATgB;","names":["fileURLToPath","logger","drizzle","migrate","logger","drizzle","fileURLToPath","migrate","path","path"]}
|
package/dist/migrate.d.ts
DELETED
package/dist/migrate.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
PGliteClientManager,
|
|
3
|
-
PostgresConnectionManager,
|
|
4
|
-
__name,
|
|
5
|
-
resolvePgliteDir
|
|
6
|
-
} from "./chunk-74FVWTXP.js";
|
|
7
|
-
|
|
8
|
-
// src/migrate.ts
|
|
9
|
-
import { logger } from "@elizaos/core";
|
|
10
|
-
import { config } from "dotenv";
|
|
11
|
-
config({ path: "../../.env" });
|
|
12
|
-
async function runMigrations() {
|
|
13
|
-
if (process.env.POSTGRES_URL) {
|
|
14
|
-
try {
|
|
15
|
-
const connectionManager = new PostgresConnectionManager(process.env.POSTGRES_URL);
|
|
16
|
-
await connectionManager.initialize();
|
|
17
|
-
await connectionManager.runMigrations();
|
|
18
|
-
logger.success("PostgreSQL migrations completed successfully");
|
|
19
|
-
process.exit(0);
|
|
20
|
-
} catch (error) {
|
|
21
|
-
logger.warn("PostgreSQL migration failed:", error);
|
|
22
|
-
process.exit(1);
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
const elizaDbDir = resolvePgliteDir();
|
|
26
|
-
if (!process.env.PGLITE_DATA_DIR) {
|
|
27
|
-
logger.info(`PGLITE_DATA_DIR not set, defaulting to project path: ${elizaDbDir}`);
|
|
28
|
-
} else {
|
|
29
|
-
logger.info(`Using PGLITE_DATA_DIR: ${elizaDbDir}`);
|
|
30
|
-
}
|
|
31
|
-
logger.info("Using PGlite database at:", elizaDbDir);
|
|
32
|
-
const clientManager = new PGliteClientManager({
|
|
33
|
-
dataDir: elizaDbDir
|
|
34
|
-
});
|
|
35
|
-
try {
|
|
36
|
-
await clientManager.initialize();
|
|
37
|
-
await clientManager.runMigrations();
|
|
38
|
-
logger.success("PGlite migrations completed successfully");
|
|
39
|
-
await clientManager.close();
|
|
40
|
-
process.exit(0);
|
|
41
|
-
} catch (error) {
|
|
42
|
-
logger.error("PGlite migration failed:", error);
|
|
43
|
-
try {
|
|
44
|
-
await clientManager.close();
|
|
45
|
-
} catch (closeError) {
|
|
46
|
-
logger.error("Failed to close PGlite connection:", closeError);
|
|
47
|
-
}
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
__name(runMigrations, "runMigrations");
|
|
53
|
-
runMigrations().catch((error) => {
|
|
54
|
-
logger.error("Unhandled error in migrations:", error);
|
|
55
|
-
process.exit(1);
|
|
56
|
-
});
|
|
57
|
-
//# sourceMappingURL=migrate.js.map
|
package/dist/migrate.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/migrate.ts"],"sourceRoot":"./","sourcesContent":["import { logger } from '@elizaos/core';\nimport { config } from 'dotenv';\nimport { PostgresConnectionManager } from './pg/manager';\nimport { PGliteClientManager } from './pglite/manager';\nimport { resolvePgliteDir } from './utils';\n\nconfig({ path: '../../.env' });\n\n/**\n * Executes database migrations using either PostgreSQL or PGlite, depending on environment configuration.\n *\n * If the `POSTGRES_URL` environment variable is set, migrations are run against the specified PostgreSQL database. Otherwise, migrations are run using a PGlite database, with the data directory determined by the `PGLITE_DATA_DIR` environment variable or a project-specific default path.\n *\n * @remark This function terminates the Node.js process upon completion or failure.\n */\nasync function runMigrations() {\n if (process.env.POSTGRES_URL) {\n try {\n const connectionManager = new PostgresConnectionManager(process.env.POSTGRES_URL);\n await connectionManager.initialize();\n await connectionManager.runMigrations();\n // await connectionManager.close();\n logger.success('PostgreSQL migrations completed successfully');\n process.exit(0);\n } catch (error) {\n logger.warn('PostgreSQL migration failed:', error);\n process.exit(1);\n }\n } else {\n const elizaDbDir = resolvePgliteDir();\n\n if (!process.env.PGLITE_DATA_DIR) {\n logger.info(`PGLITE_DATA_DIR not set, defaulting to project path: ${elizaDbDir}`);\n } else {\n logger.info(`Using PGLITE_DATA_DIR: ${elizaDbDir}`);\n }\n\n logger.info('Using PGlite database at:', elizaDbDir);\n const clientManager = new PGliteClientManager({\n dataDir: elizaDbDir,\n });\n\n try {\n await clientManager.initialize();\n await clientManager.runMigrations();\n logger.success('PGlite migrations completed successfully');\n await clientManager.close();\n process.exit(0);\n } catch (error) {\n logger.error('PGlite migration failed:', error);\n try {\n await clientManager.close();\n } catch (closeError) {\n logger.error('Failed to close PGlite connection:', closeError);\n }\n process.exit(1);\n }\n }\n}\n\nrunMigrations().catch((error) => {\n logger.error('Unhandled error in migrations:', error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,cAAc;AAKvB,OAAO,EAAE,MAAM,aAAa,CAAC;AAS7B,eAAe,gBAAgB;AAC7B,MAAI,QAAQ,IAAI,cAAc;AAC5B,QAAI;AACF,YAAM,oBAAoB,IAAI,0BAA0B,QAAQ,IAAI,YAAY;AAChF,YAAM,kBAAkB,WAAW;AACnC,YAAM,kBAAkB,cAAc;AAEtC,aAAO,QAAQ,8CAA8C;AAC7D,cAAQ,KAAK,CAAC;AAAA,IAChB,SAAS,OAAO;AACd,aAAO,KAAK,gCAAgC,KAAK;AACjD,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF,OAAO;AACL,UAAM,aAAa,iBAAiB;AAEpC,QAAI,CAAC,QAAQ,IAAI,iBAAiB;AAChC,aAAO,KAAK,wDAAwD,UAAU,EAAE;AAAA,IAClF,OAAO;AACL,aAAO,KAAK,0BAA0B,UAAU,EAAE;AAAA,IACpD;AAEA,WAAO,KAAK,6BAA6B,UAAU;AACnD,UAAM,gBAAgB,IAAI,oBAAoB;AAAA,MAC5C,SAAS;AAAA,IACX,CAAC;AAED,QAAI;AACF,YAAM,cAAc,WAAW;AAC/B,YAAM,cAAc,cAAc;AAClC,aAAO,QAAQ,0CAA0C;AACzD,YAAM,cAAc,MAAM;AAC1B,cAAQ,KAAK,CAAC;AAAA,IAChB,SAAS,OAAO;AACd,aAAO,MAAM,4BAA4B,KAAK;AAC9C,UAAI;AACF,cAAM,cAAc,MAAM;AAAA,MAC5B,SAAS,YAAY;AACnB,eAAO,MA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