@elizaos/plugin-sql 1.0.0-alpha.51 → 1.0.0-alpha.53
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/{chunk-LXELBY3H.js → chunk-E7OUIX4R.js} +3 -3
- package/dist/{chunk-LXELBY3H.js.map → chunk-E7OUIX4R.js.map} +1 -1
- package/dist/index.js +1 -1
- package/dist/migrate.js +1 -2
- package/dist/migrate.js.map +1 -1
- package/drizzle/migrations/20250302132443_init.sql +198 -0
- package/drizzle/migrations/meta/20250302132443_snapshot.json +1418 -0
- package/drizzle/migrations/meta/_journal.json +13 -0
- package/package.json +5 -4
|
@@ -149,11 +149,11 @@ var PGliteClientManager = class {
|
|
|
149
149
|
};
|
|
150
150
|
|
|
151
151
|
// src/pg/manager.ts
|
|
152
|
+
import path from "node:path";
|
|
153
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
152
154
|
import { logger as logger2 } from "@elizaos/core";
|
|
153
155
|
import { drizzle as drizzle2 } from "drizzle-orm/node-postgres";
|
|
154
156
|
import { migrate as migrate2 } from "drizzle-orm/node-postgres/migrator";
|
|
155
|
-
import path from "node:path";
|
|
156
|
-
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
157
157
|
import pkg from "pg";
|
|
158
158
|
var { Pool } = pkg;
|
|
159
159
|
var PostgresConnectionManager = class {
|
|
@@ -339,4 +339,4 @@ export {
|
|
|
339
339
|
PGliteClientManager,
|
|
340
340
|
PostgresConnectionManager
|
|
341
341
|
};
|
|
342
|
-
//# sourceMappingURL=chunk-
|
|
342
|
+
//# sourceMappingURL=chunk-E7OUIX4R.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pg-lite/manager.ts","../src/pg/manager.ts"],"sourceRoot":"./","sourcesContent":["import { dirname, resolve } 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\tprivate client: PGlite;\n\tprivate shuttingDown = false;\n\tprivate readonly shutdownTimeout = 500;\n\n\t/**\n\t * Constructor for creating a new instance of PGlite with the provided options.\n\t * Initializes the PGlite client with additional extensions.\n\t * @param {PGliteOptions} options - The options to configure the PGlite client.\n\t */\n\tconstructor(options: PGliteOptions) {\n\t\tthis.client = new PGlite({\n\t\t\t...options,\n\t\t\textensions: {\n\t\t\t\tvector,\n\t\t\t\tfuzzystrmatch,\n\t\t\t},\n\t\t});\n\t\tthis.setupShutdownHandlers();\n\t}\n\n\t/**\n\t * Retrieves the PostgreSQL lite connection.\n\t *\n\t * @returns {PGlite} The PostgreSQL lite connection.\n\t * @throws {Error} If the client manager is currently shutting down.\n\t */\n\tpublic getConnection(): PGlite {\n\t\tif (this.shuttingDown) {\n\t\t\tthrow new Error(\"Client manager is shutting down\");\n\t\t}\n\t\treturn this.client;\n\t}\n\n\t/**\n\t * Initiates a graceful shutdown of the PGlite client.\n\t * Checks if the client is already in the process of shutting down.\n\t * Logs the start of shutdown process and sets shuttingDown flag to true.\n\t * Sets a timeout for the shutdown process and forces closure of database connection if timeout is reached.\n\t * Handles the shutdown process, closes the client connection, clears the timeout, and logs the completion of shutdown.\n\t * Logs any errors that occur during the shutdown process.\n\t */\n\tprivate async gracefulShutdown() {\n\t\tif (this.shuttingDown) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.shuttingDown = true;\n\t\tlogger.info(\"Starting graceful shutdown of PGlite client...\");\n\n\t\tconst timeout = setTimeout(() => {\n\t\t\tlogger.warn(\n\t\t\t\t\"Shutdown timeout reached, forcing database connection closure...\",\n\t\t\t);\n\t\t\tthis.client.close().finally(() => {\n\t\t\t\tprocess.exit(1);\n\t\t\t});\n\t\t}, this.shutdownTimeout);\n\n\t\ttry {\n\t\t\tawait this.client.close();\n\t\t\tclearTimeout(timeout);\n\t\t\tlogger.info(\"PGlite client shutdown completed successfully\");\n\t\t\tprocess.exit(0);\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Error during graceful shutdown:\", error);\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n\n\t/**\n\t * Sets up shutdown handlers for SIGINT, SIGTERM, and beforeExit events to gracefully shutdown the application.\n\t * @private\n\t */\n\tprivate setupShutdownHandlers() {\n\t\tprocess.on(\"SIGINT\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\n\t\tprocess.on(\"SIGTERM\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\n\t\tprocess.on(\"beforeExit\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\t}\n\n\t/**\n\t * Initializes the client for PGlite.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves when the client is initialized successfully\n\t */\n\tpublic async initialize(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.client.waitReady;\n\t\t\tlogger.info(\"PGlite client initialized successfully\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to initialize PGlite client:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously closes the resource. If the resource is not already shutting down,\n\t * it performs a graceful shutdown before closing.\n\t *\n\t * @returns A promise that resolves once the resource has been closed.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tif (!this.shuttingDown) {\n\t\t\tawait this.gracefulShutdown();\n\t\t}\n\t}\n\n\t/**\n\t * Check if the system is currently shutting down.\n\t *\n\t * @returns {boolean} True if the system is shutting down, false otherwise.\n\t */\n\tpublic isShuttingDown(): boolean {\n\t\treturn this.shuttingDown;\n\t}\n\n\t/**\n\t * Asynchronously runs database migrations using Drizzle.\n\t * \n\t * Drizzle will first check if the migrations are already applied.\n\t * If there is a diff between database schema and migrations, it will apply the migrations.\n\t * If they are already applied, it will skip them.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n\t */\n\tasync runMigrations(): Promise<void> {\n\t\ttry {\n\t\t\tconst db = drizzle(this.client);\n\n\t\t\tconst __filename = fileURLToPath(import.meta.url);\n\t\t\tconst __dirname = dirname(__filename);\n\n\t\t\tawait migrate(db, {\n\t\t\t\tmigrationsFolder: resolve(__dirname, \"../drizzle/migrations\"),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to run database migrations (pglite):\", error);\n\t\t\t// throw error;\n\t\t\tconsole.trace(error);\n\t\t}\n\t}\n}\n","import { logger } from \"@elizaos/core\";\nimport { drizzle } from \"drizzle-orm/node-postgres\";\nimport { migrate } from \"drizzle-orm/node-postgres/migrator\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\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\n\timplements IDatabaseClientManager<PgPool>\n{\n\tprivate pool: PgPool;\n\tprivate isShuttingDown = false;\n\tprivate readonly connectionTimeout: number = 5000;\n\n\t/**\n\t * Constructor for creating a connection pool.\n\t * @param {string} connectionString - The connection string used to connect to the database.\n\t */\n\tconstructor(connectionString: string) {\n\t\tconst defaultConfig = {\n\t\t\tmax: 20,\n\t\t\tidleTimeoutMillis: 30000,\n\t\t\tconnectionTimeoutMillis: this.connectionTimeout,\n\t\t};\n\n\t\tthis.pool = new Pool({\n\t\t\t...defaultConfig,\n\t\t\tconnectionString,\n\t\t});\n\n\t\tthis.pool.on(\"error\", (err) => {\n\t\t\tlogger.error(\"Unexpected pool error\", err);\n\t\t\tthis.handlePoolError(err);\n\t\t});\n\n\t\tthis.setupPoolErrorHandling();\n\t\tthis.testConnection();\n\t}\n\n\t/**\n\t * Handles a pool error by attempting to reconnect the pool.\n\t *\n\t * @param {Error} error The error that occurred in the pool.\n\t * @throws {Error} If failed to reconnect the pool.\n\t */\n\tprivate async handlePoolError(error: Error) {\n\t\tlogger.error(\"Pool error occurred, attempting to reconnect\", {\n\t\t\terror: error.message,\n\t\t});\n\n\t\ttry {\n\t\t\tawait this.pool.end();\n\n\t\t\tthis.pool = new Pool({\n\t\t\t\t...this.pool.options,\n\t\t\t\tconnectionTimeoutMillis: this.connectionTimeout,\n\t\t\t});\n\n\t\t\tawait this.testConnection();\n\t\t\tlogger.success(\"Pool reconnection successful\");\n\t\t} catch (reconnectError) {\n\t\t\tlogger.error(\"Failed to reconnect pool\", {\n\t\t\t\terror:\n\t\t\t\t\treconnectError instanceof Error\n\t\t\t\t\t\t? reconnectError.message\n\t\t\t\t\t\t: String(reconnectError),\n\t\t\t});\n\t\t\tthrow reconnectError;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously tests the database connection by executing a query to get the current timestamp.\n\t *\n\t * @returns {Promise<boolean>} - A Promise that resolves to true if the database connection test is successful.\n\t */\n\tasync testConnection(): Promise<boolean> {\n\t\tlet client: pkg.PoolClient | null = null;\n\t\ttry {\n\t\t\tclient = await this.pool.connect();\n\t\t\tconst result = await client.query(\"SELECT NOW()\");\n\t\t\tlogger.success(\"Database connection test successful:\", result.rows[0]);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Database connection test failed:\", error);\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to connect to database: ${(error as Error).message}`,\n\t\t\t);\n\t\t} finally {\n\t\t\tif (client) client.release();\n\t\t}\n\t}\n\n\t/**\n\t * Sets up event listeners to handle pool cleanup on SIGINT, SIGTERM, and beforeExit events.\n\t */\n\tprivate setupPoolErrorHandling() {\n\t\tprocess.on(\"SIGINT\", async () => {\n\t\t\tawait this.cleanup();\n\t\t\tprocess.exit(0);\n\t\t});\n\n\t\tprocess.on(\"SIGTERM\", async () => {\n\t\t\tawait this.cleanup();\n\t\t\tprocess.exit(0);\n\t\t});\n\n\t\tprocess.on(\"beforeExit\", async () => {\n\t\t\tawait this.cleanup();\n\t\t});\n\t}\n\n\t/**\n\t * Get the connection pool.\n\t * @returns {PgPool} The connection pool\n\t * @throws {Error} If the connection manager is shutting down or an error occurs when trying to get the connection from the pool\n\t */\n\tpublic getConnection(): PgPool {\n\t\tif (this.isShuttingDown) {\n\t\t\tthrow new Error(\"Connection manager is shutting down\");\n\t\t}\n\n\t\ttry {\n\t\t\treturn this.pool;\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to get connection from pool:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously acquires a database client from the connection pool.\n\t *\n\t * @returns {Promise<pkg.PoolClient>} A Promise that resolves with the acquired database client.\n\t * @throws {Error} If an error occurs while acquiring the database client.\n\t */\n\tpublic async getClient(): Promise<pkg.PoolClient> {\n\t\ttry {\n\t\t\treturn await this.pool.connect();\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to acquire a database client:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Initializes the PostgreSQL connection manager by testing the connection and logging the result.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the manager is successfully initialized\n\t * @throws {Error} If there is an error initializing the connection manager\n\t */\n\tpublic async initialize(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.testConnection();\n\t\t\tlogger.info(\"PostgreSQL connection manager initialized successfully\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to initialize connection manager:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously close the current process by executing a cleanup function.\n\t * @returns A promise that resolves once the cleanup is complete.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait this.cleanup();\n\t}\n\n\t/**\n\t * Cleans up and closes the database pool.\n\t * @returns {Promise<void>} A Promise that resolves when the database pool is closed.\n\t */\n\tasync cleanup(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.pool.end();\n\t\t\tlogger.info(\"Database pool closed\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Error closing database pool:\", error);\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously runs database migrations using the Drizzle library.\n\t *\n\t * Drizzle will first check if the migrations are already applied.\n\t * If there is a diff between database schema and migrations, it will apply the migrations.\n\t * If they are already applied, it will skip them.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n\t */\n\tasync runMigrations(): Promise<void> {\n\t\ttry {\n\t\t\tconst db = drizzle(this.pool);\n\n\t\t\tconst __filename = fileURLToPath(import.meta.url);\n\t\t\tconst __dirname = path.dirname(__filename);\n\n\t\t\tawait migrate(db, {\n\t\t\t\tmigrationsFolder: path.resolve(__dirname, \"../drizzle/migrations\"),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to run database migrations (pg):\", error);\n\t\t\tconsole.trace(error);\n\t\t}\n\t}\n}\n"],"mappings":";;;;AAAA,SAAS,SAAS,eAAe;AACjC,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,EAClE;AAAA,EACA,eAAe;AAAA,EACN,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOnC,YAAY,SAAwB;AACnC,SAAK,SAAS,IAAI,OAAO;AAAA,MACxB,GAAG;AAAA,MACH,YAAY;AAAA,QACX;AAAA,QACA;AAAA,MACD;AAAA,IACD,CAAC;AACD,SAAK,sBAAsB;AAAA,EAC5B;AAAA;AAAA;AAAA;AAA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QAAQ,UAAU;AAEzC,YAAMC,SAAQ,IAAI;AAAA,QACjB,kBAAkB,KAAK,QAAQ,WAAW,uBAAuB;AAAA,MAClE,CAAC;AAAA,IACF,SAAS,OAAO;AACf,MAAAH,QAAO,MAAM,2CAA2C,KAAK;AAC7D,cAAQ,MAAM,KAAK;AAAA,IACpB;AAAA,EACD;AACD;","names":["logger","drizzle","migrate","fileURLToPath","logger","drizzle","fileURLToPath","migrate"]}
|
|
1
|
+
{"version":3,"sources":["../src/pg-lite/manager.ts","../src/pg/manager.ts"],"sourceRoot":"./","sourcesContent":["import { dirname, resolve } 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\tprivate client: PGlite;\n\tprivate shuttingDown = false;\n\tprivate readonly shutdownTimeout = 500;\n\n\t/**\n\t * Constructor for creating a new instance of PGlite with the provided options.\n\t * Initializes the PGlite client with additional extensions.\n\t * @param {PGliteOptions} options - The options to configure the PGlite client.\n\t */\n\tconstructor(options: PGliteOptions) {\n\t\tthis.client = new PGlite({\n\t\t\t...options,\n\t\t\textensions: {\n\t\t\t\tvector,\n\t\t\t\tfuzzystrmatch,\n\t\t\t},\n\t\t});\n\t\tthis.setupShutdownHandlers();\n\t}\n\n\t/**\n\t * Retrieves the PostgreSQL lite connection.\n\t *\n\t * @returns {PGlite} The PostgreSQL lite connection.\n\t * @throws {Error} If the client manager is currently shutting down.\n\t */\n\tpublic getConnection(): PGlite {\n\t\tif (this.shuttingDown) {\n\t\t\tthrow new Error(\"Client manager is shutting down\");\n\t\t}\n\t\treturn this.client;\n\t}\n\n\t/**\n\t * Initiates a graceful shutdown of the PGlite client.\n\t * Checks if the client is already in the process of shutting down.\n\t * Logs the start of shutdown process and sets shuttingDown flag to true.\n\t * Sets a timeout for the shutdown process and forces closure of database connection if timeout is reached.\n\t * Handles the shutdown process, closes the client connection, clears the timeout, and logs the completion of shutdown.\n\t * Logs any errors that occur during the shutdown process.\n\t */\n\tprivate async gracefulShutdown() {\n\t\tif (this.shuttingDown) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.shuttingDown = true;\n\t\tlogger.info(\"Starting graceful shutdown of PGlite client...\");\n\n\t\tconst timeout = setTimeout(() => {\n\t\t\tlogger.warn(\n\t\t\t\t\"Shutdown timeout reached, forcing database connection closure...\",\n\t\t\t);\n\t\t\tthis.client.close().finally(() => {\n\t\t\t\tprocess.exit(1);\n\t\t\t});\n\t\t}, this.shutdownTimeout);\n\n\t\ttry {\n\t\t\tawait this.client.close();\n\t\t\tclearTimeout(timeout);\n\t\t\tlogger.info(\"PGlite client shutdown completed successfully\");\n\t\t\tprocess.exit(0);\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Error during graceful shutdown:\", error);\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n\n\t/**\n\t * Sets up shutdown handlers for SIGINT, SIGTERM, and beforeExit events to gracefully shutdown the application.\n\t * @private\n\t */\n\tprivate setupShutdownHandlers() {\n\t\tprocess.on(\"SIGINT\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\n\t\tprocess.on(\"SIGTERM\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\n\t\tprocess.on(\"beforeExit\", async () => {\n\t\t\tawait this.gracefulShutdown();\n\t\t});\n\t}\n\n\t/**\n\t * Initializes the client for PGlite.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves when the client is initialized successfully\n\t */\n\tpublic async initialize(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.client.waitReady;\n\t\t\tlogger.info(\"PGlite client initialized successfully\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to initialize PGlite client:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously closes the resource. If the resource is not already shutting down,\n\t * it performs a graceful shutdown before closing.\n\t *\n\t * @returns A promise that resolves once the resource has been closed.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tif (!this.shuttingDown) {\n\t\t\tawait this.gracefulShutdown();\n\t\t}\n\t}\n\n\t/**\n\t * Check if the system is currently shutting down.\n\t *\n\t * @returns {boolean} True if the system is shutting down, false otherwise.\n\t */\n\tpublic isShuttingDown(): boolean {\n\t\treturn this.shuttingDown;\n\t}\n\n\t/**\n\t * Asynchronously runs database migrations using Drizzle.\n\t * \n\t * Drizzle will first check if the migrations are already applied.\n\t * If there is a diff between database schema and migrations, it will apply the migrations.\n\t * If they are already applied, it will skip them.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n\t */\n\tasync runMigrations(): Promise<void> {\n\t\ttry {\n\t\t\tconst db = drizzle(this.client);\n\n\t\t\tconst __filename = fileURLToPath(import.meta.url);\n\t\t\tconst __dirname = dirname(__filename);\n\n\t\t\tawait migrate(db, {\n\t\t\t\tmigrationsFolder: resolve(__dirname, \"../drizzle/migrations\"),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to run database migrations (pglite):\", error);\n\t\t\t// throw error;\n\t\t\tconsole.trace(error);\n\t\t}\n\t}\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\n\timplements IDatabaseClientManager<PgPool>\n{\n\tprivate pool: PgPool;\n\tprivate isShuttingDown = false;\n\tprivate readonly connectionTimeout: number = 5000;\n\n\t/**\n\t * Constructor for creating a connection pool.\n\t * @param {string} connectionString - The connection string used to connect to the database.\n\t */\n\tconstructor(connectionString: string) {\n\t\tconst defaultConfig = {\n\t\t\tmax: 20,\n\t\t\tidleTimeoutMillis: 30000,\n\t\t\tconnectionTimeoutMillis: this.connectionTimeout,\n\t\t};\n\n\t\tthis.pool = new Pool({\n\t\t\t...defaultConfig,\n\t\t\tconnectionString,\n\t\t});\n\n\t\tthis.pool.on(\"error\", (err) => {\n\t\t\tlogger.error(\"Unexpected pool error\", err);\n\t\t\tthis.handlePoolError(err);\n\t\t});\n\n\t\tthis.setupPoolErrorHandling();\n\t\tthis.testConnection();\n\t}\n\n\t/**\n\t * Handles a pool error by attempting to reconnect the pool.\n\t *\n\t * @param {Error} error The error that occurred in the pool.\n\t * @throws {Error} If failed to reconnect the pool.\n\t */\n\tprivate async handlePoolError(error: Error) {\n\t\tlogger.error(\"Pool error occurred, attempting to reconnect\", {\n\t\t\terror: error.message,\n\t\t});\n\n\t\ttry {\n\t\t\tawait this.pool.end();\n\n\t\t\tthis.pool = new Pool({\n\t\t\t\t...this.pool.options,\n\t\t\t\tconnectionTimeoutMillis: this.connectionTimeout,\n\t\t\t});\n\n\t\t\tawait this.testConnection();\n\t\t\tlogger.success(\"Pool reconnection successful\");\n\t\t} catch (reconnectError) {\n\t\t\tlogger.error(\"Failed to reconnect pool\", {\n\t\t\t\terror:\n\t\t\t\t\treconnectError instanceof Error\n\t\t\t\t\t\t? reconnectError.message\n\t\t\t\t\t\t: String(reconnectError),\n\t\t\t});\n\t\t\tthrow reconnectError;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously tests the database connection by executing a query to get the current timestamp.\n\t *\n\t * @returns {Promise<boolean>} - A Promise that resolves to true if the database connection test is successful.\n\t */\n\tasync testConnection(): Promise<boolean> {\n\t\tlet client: pkg.PoolClient | null = null;\n\t\ttry {\n\t\t\tclient = await this.pool.connect();\n\t\t\tconst result = await client.query(\"SELECT NOW()\");\n\t\t\tlogger.success(\"Database connection test successful:\", result.rows[0]);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Database connection test failed:\", error);\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to connect to database: ${(error as Error).message}`,\n\t\t\t);\n\t\t} finally {\n\t\t\tif (client) client.release();\n\t\t}\n\t}\n\n\t/**\n\t * Sets up event listeners to handle pool cleanup on SIGINT, SIGTERM, and beforeExit events.\n\t */\n\tprivate setupPoolErrorHandling() {\n\t\tprocess.on(\"SIGINT\", async () => {\n\t\t\tawait this.cleanup();\n\t\t\tprocess.exit(0);\n\t\t});\n\n\t\tprocess.on(\"SIGTERM\", async () => {\n\t\t\tawait this.cleanup();\n\t\t\tprocess.exit(0);\n\t\t});\n\n\t\tprocess.on(\"beforeExit\", async () => {\n\t\t\tawait this.cleanup();\n\t\t});\n\t}\n\n\t/**\n\t * Get the connection pool.\n\t * @returns {PgPool} The connection pool\n\t * @throws {Error} If the connection manager is shutting down or an error occurs when trying to get the connection from the pool\n\t */\n\tpublic getConnection(): PgPool {\n\t\tif (this.isShuttingDown) {\n\t\t\tthrow new Error(\"Connection manager is shutting down\");\n\t\t}\n\n\t\ttry {\n\t\t\treturn this.pool;\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to get connection from pool:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously acquires a database client from the connection pool.\n\t *\n\t * @returns {Promise<pkg.PoolClient>} A Promise that resolves with the acquired database client.\n\t * @throws {Error} If an error occurs while acquiring the database client.\n\t */\n\tpublic async getClient(): Promise<pkg.PoolClient> {\n\t\ttry {\n\t\t\treturn await this.pool.connect();\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to acquire a database client:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Initializes the PostgreSQL connection manager by testing the connection and logging the result.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the manager is successfully initialized\n\t * @throws {Error} If there is an error initializing the connection manager\n\t */\n\tpublic async initialize(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.testConnection();\n\t\t\tlogger.info(\"PostgreSQL connection manager initialized successfully\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to initialize connection manager:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously close the current process by executing a cleanup function.\n\t * @returns A promise that resolves once the cleanup is complete.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait this.cleanup();\n\t}\n\n\t/**\n\t * Cleans up and closes the database pool.\n\t * @returns {Promise<void>} A Promise that resolves when the database pool is closed.\n\t */\n\tasync cleanup(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.pool.end();\n\t\t\tlogger.info(\"Database pool closed\");\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Error closing database pool:\", error);\n\t\t}\n\t}\n\n\t/**\n\t * Asynchronously runs database migrations using the Drizzle library.\n\t *\n\t * Drizzle will first check if the migrations are already applied.\n\t * If there is a diff between database schema and migrations, it will apply the migrations.\n\t * If they are already applied, it will skip them.\n\t *\n\t * @returns {Promise<void>} A Promise that resolves once the migrations are completed successfully.\n\t */\n\tasync runMigrations(): Promise<void> {\n\t\ttry {\n\t\t\tconst db = drizzle(this.pool);\n\n\t\t\tconst __filename = fileURLToPath(import.meta.url);\n\t\t\tconst __dirname = path.dirname(__filename);\n\n\t\t\tawait migrate(db, {\n\t\t\t\tmigrationsFolder: path.resolve(__dirname, \"../drizzle/migrations\"),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tlogger.error(\"Failed to run database migrations (pg):\", error);\n\t\t\tconsole.trace(error);\n\t\t}\n\t}\n}\n"],"mappings":";;;;AAAA,SAAS,SAAS,eAAe;AACjC,SAAS,qB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,MAAa,QAAuB;AACnC,UAAM,KAAK,QAAQ;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAyB;AAC9B,QAAI;AACH,YAAM,KAAK,KAAK,IAAI;AACpB,MAAAA,QAAO,KAAK,sBAAsB;AAAA,IACnC,SAAS,OAAO;AACf,MAAAA,QAAO,MAAM,gCAAgC,KAAK;AAAA,IACnD;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAM,gBAA+B;AACpC,QAAI;AACH,YAAM,KAAKC,SAAQ,KAAK,IAAI;AAE5B,YAAM,aAAaC,eAAc,YAAY,GAAG;AAChD,YAAM,YAAY,KAAK,QAAQ,UAAU;AAEzC,YAAMC,SAAQ,IAAI;AAAA,QACjB,kBAAkB,KAAK,QAAQ,WAAW,uBAAuB;AAAA,MAClE,CAAC;AAAA,IACF,SAAS,OAAO;AACf,MAAAH,QAAO,MAAM,2CAA2C,KAAK;AAC7D,cAAQ,MAAM,KAAK;AAAA,IACpB;AAAA,EACD;AACD;","names":["fileURLToPath","logger","drizzle","migrate","logger","drizzle","fileURLToPath","migrate"]}
|
package/dist/index.js
CHANGED
package/dist/migrate.js
CHANGED
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
PGliteClientManager,
|
|
3
3
|
PostgresConnectionManager,
|
|
4
4
|
__name
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-E7OUIX4R.js";
|
|
6
6
|
|
|
7
7
|
// src/migrate.ts
|
|
8
8
|
import { logger } from "@elizaos/core";
|
|
@@ -16,7 +16,6 @@ async function runMigrations() {
|
|
|
16
16
|
);
|
|
17
17
|
await connectionManager.initialize();
|
|
18
18
|
await connectionManager.runMigrations();
|
|
19
|
-
await connectionManager.close();
|
|
20
19
|
logger.success("PostgreSQL migrations completed successfully");
|
|
21
20
|
process.exit(0);
|
|
22
21
|
} catch (error) {
|
package/dist/migrate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/migrate.ts"],"sourceRoot":"./","sourcesContent":["import { logger } from \"@elizaos/core\";\nimport { config } from \"dotenv\";\nimport { PGliteClientManager } from \"./pg-lite/manager.js\";\nimport { PostgresConnectionManager } from \"./pg/manager.js\";\n\nconfig({ path: \"../../.env\" });\n\n/**\n * Runs the database migrations based on the environment variable POSTGRES_URL.\n * If the POSTGRES_URL is provided, it indicates the use of a PostgreSQL database and the corresponding migration logic needs to be implemented.\n * If POSTGRES_URL is not provided, it uses a PGlite database and runs the migrations on it.\n * @returns {Promise<void>} A promise that resolves once the migrations are completed successfully or rejects if an error occurs.\n */\nasync function runMigrations() {\n\tif (process.env.POSTGRES_URL) {\n\t\ttry {\n\t\t\tconst connectionManager = new PostgresConnectionManager(\n\t\t\t\tprocess.env.POSTGRES_URL,\n\t\t\t);\n\t\t\tawait connectionManager.initialize();\n\t\t\tawait connectionManager.runMigrations();\n\t\t\
|
|
1
|
+
{"version":3,"sources":["../src/migrate.ts"],"sourceRoot":"./","sourcesContent":["import { logger } from \"@elizaos/core\";\nimport { config } from \"dotenv\";\nimport { PGliteClientManager } from \"./pg-lite/manager.js\";\nimport { PostgresConnectionManager } from \"./pg/manager.js\";\n\nconfig({ path: \"../../.env\" });\n\n/**\n * Runs the database migrations based on the environment variable POSTGRES_URL.\n * If the POSTGRES_URL is provided, it indicates the use of a PostgreSQL database and the corresponding migration logic needs to be implemented.\n * If POSTGRES_URL is not provided, it uses a PGlite database and runs the migrations on it.\n * @returns {Promise<void>} A promise that resolves once the migrations are completed successfully or rejects if an error occurs.\n */\nasync function runMigrations() {\n\tif (process.env.POSTGRES_URL) {\n\t\ttry {\n\t\t\tconst connectionManager = new PostgresConnectionManager(\n\t\t\t\tprocess.env.POSTGRES_URL,\n\t\t\t);\n\t\t\tawait connectionManager.initialize();\n\t\t\tawait connectionManager.runMigrations();\n\t\t\t// await connectionManager.close();\n\t\t\tlogger.success(\"PostgreSQL migrations completed successfully\");\n\t\t\tprocess.exit(0);\n\t\t} catch (error) {\n\t\t\tlogger.warn(\"PostgreSQL migration failed:\", error);\n\t\t\tprocess.exit(1);\n\t\t}\n\t} else {\n\t\tlogger.info(\"Using PGlite database\");\n\t\tconst clientManager = new PGliteClientManager({\n\t\t\tdataDir: \"../../pglite\",\n\t\t});\n\n\t\ttry {\n\t\t\tawait clientManager.initialize();\n\t\t\tawait clientManager.runMigrations();\n\t\t\tlogger.success(\"PGlite migrations completed successfully\");\n\t\t\tawait clientManager.close();\n\t\t\tprocess.exit(0);\n\t\t} catch (error) {\n\t\t\tlogger.error(\"PGlite migration failed:\", error);\n\t\t\ttry {\n\t\t\t\tawait clientManager.close();\n\t\t\t} catch (closeError) {\n\t\t\t\tlogger.error(\"Failed to close PGlite connection:\", closeError);\n\t\t\t}\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n}\n\nrunMigrations().catch((error) => {\n\tlogger.error(\"Unhandled error in migrations:\", error);\n\tprocess.exit(1);\n});\n"],"mappings":";;;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,cAAc;AAIvB,OAAO,EAAE,MAAM,aAAa,CAAC;AAQ7B,eAAe,gBAAgB;AAC9B,MAAI,QAAQ,IAAI,cAAc;AAC7B,QAAI;AACH,YAAM,oBAAoB,IAAI;AAAA,QAC7B,QAAQ,IAAI;AAAA,MACb;AACA,YAAM,kBAAkB,WAAW;AACnC,YAAM,kBAAkB,cAAc;AAEtC,aAAO,QAAQ,8CAA8C;AAC7D,cAAQ,KAAK,CAAC;AAAA,IACf,SAAS,OAAO;AACf,aAAO,KAAK,gCAAgC,KAAK;AACjD,cAAQ,KAAK,CAAC;AAAA,IACf;AAAA,EACD,OAAO;AACN,WAAO,KAAK,uBAAuB;AACnC,UAAM,gBAAgB,IAAI,oBAAoB;AAAA,MAC7C,SAAS;AAAA,IACV,CAAC;AAED,QAAI;AACH,YAAM,cAAc,WAAW;AAC/B,YAAM,cAAc,cAAc;AAClC,aAAO,QAAQ,0CAA0C;AACzD,YAAM,cAAc,MAAM;AAC1B,cAAQ,KAAK,CAAC;AAAA,IACf,SAAS,OAAO;AACf,aAAO,MAAM,4BAA4B,KAAK;AAC9C,UAAI;AACH,cAAM,cAAc,MAAM;AAAA,MAC3B,SAAS,YAAY;AACpB,eAAO,MAAM,sCAAsC,UAAU;AAAA,MAC9D;AACA,cAAQ,KAAK,CAAC;AAAA,IACf;AAAA,EACD;AACD;AArCe;AAuCf,cAAc,EAAE,MAAM,CAAC,UAAU;AAChC,SAAO,MAAM,kCAAkC,KAAK;AACpD,UAAQ,KAAK,CAAC;AACf,CAAC;","names":[]}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
CREATE EXTENSION IF NOT EXISTS vector;
|
|
2
|
+
--> statement-breakpoint
|
|
3
|
+
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
|
|
4
|
+
--> statement-breakpoint
|
|
5
|
+
CREATE TABLE "agents" (
|
|
6
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
7
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
8
|
+
"updatedAt" timestamptz DEFAULT now() NOT NULL,
|
|
9
|
+
"name" text,
|
|
10
|
+
"username" text,
|
|
11
|
+
"system" text,
|
|
12
|
+
"bio" jsonb NOT NULL,
|
|
13
|
+
"message_examples" jsonb DEFAULT '[]'::jsonb,
|
|
14
|
+
"post_examples" jsonb DEFAULT '[]'::jsonb,
|
|
15
|
+
"topics" jsonb DEFAULT '[]'::jsonb,
|
|
16
|
+
"adjectives" jsonb DEFAULT '[]'::jsonb,
|
|
17
|
+
"knowledge" jsonb DEFAULT '[]'::jsonb,
|
|
18
|
+
"plugins" jsonb DEFAULT '[]'::jsonb,
|
|
19
|
+
"settings" jsonb DEFAULT '{}'::jsonb,
|
|
20
|
+
"style" jsonb DEFAULT '{}'::jsonb,
|
|
21
|
+
CONSTRAINT "name_unique" UNIQUE("name")
|
|
22
|
+
);
|
|
23
|
+
--> statement-breakpoint
|
|
24
|
+
CREATE TABLE "cache" (
|
|
25
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
26
|
+
"key" text NOT NULL,
|
|
27
|
+
"agentId" uuid NOT NULL,
|
|
28
|
+
"value" jsonb NOT NULL,
|
|
29
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
30
|
+
"expiresAt" timestamptz,
|
|
31
|
+
CONSTRAINT "cache_key_agent_unique" UNIQUE("key","agentId")
|
|
32
|
+
);
|
|
33
|
+
--> statement-breakpoint
|
|
34
|
+
CREATE TABLE "components" (
|
|
35
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
36
|
+
"entityId" uuid NOT NULL,
|
|
37
|
+
"agentId" uuid NOT NULL,
|
|
38
|
+
"roomId" uuid NOT NULL,
|
|
39
|
+
"worldId" uuid,
|
|
40
|
+
"sourceEntityId" uuid,
|
|
41
|
+
"type" text NOT NULL,
|
|
42
|
+
"data" jsonb DEFAULT '{}'::jsonb,
|
|
43
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL
|
|
44
|
+
);
|
|
45
|
+
--> statement-breakpoint
|
|
46
|
+
CREATE TABLE "embeddings" (
|
|
47
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
48
|
+
"memory_id" uuid,
|
|
49
|
+
"created_at" timestamptz DEFAULT now() NOT NULL,
|
|
50
|
+
"dim_384" vector(384),
|
|
51
|
+
"dim_512" vector(512),
|
|
52
|
+
"dim_768" vector(768),
|
|
53
|
+
"dim_1024" vector(1024),
|
|
54
|
+
"dim_1536" vector(1536),
|
|
55
|
+
"dim_3072" vector(3072),
|
|
56
|
+
CONSTRAINT "embedding_source_check" CHECK ("memory_id" IS NOT NULL)
|
|
57
|
+
);
|
|
58
|
+
--> statement-breakpoint
|
|
59
|
+
CREATE TABLE "entities" (
|
|
60
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
61
|
+
"agentId" uuid NOT NULL,
|
|
62
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
63
|
+
"names" text[] DEFAULT '{}'::text[],
|
|
64
|
+
"metadata" jsonb DEFAULT '{}'::jsonb,
|
|
65
|
+
CONSTRAINT "id_agent_id_unique" UNIQUE("id","agentId")
|
|
66
|
+
);
|
|
67
|
+
--> statement-breakpoint
|
|
68
|
+
CREATE TABLE "logs" (
|
|
69
|
+
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
70
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
71
|
+
"entityId" uuid NOT NULL,
|
|
72
|
+
"body" jsonb NOT NULL,
|
|
73
|
+
"type" text NOT NULL,
|
|
74
|
+
"roomId" uuid NOT NULL
|
|
75
|
+
);
|
|
76
|
+
--> statement-breakpoint
|
|
77
|
+
CREATE TABLE "memories" (
|
|
78
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
79
|
+
"type" text NOT NULL,
|
|
80
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
81
|
+
"content" jsonb NOT NULL,
|
|
82
|
+
"entityId" uuid,
|
|
83
|
+
"agentId" uuid,
|
|
84
|
+
"roomId" uuid,
|
|
85
|
+
"unique" boolean DEFAULT true NOT NULL,
|
|
86
|
+
"metadata" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
87
|
+
CONSTRAINT "fragment_metadata_check" CHECK (
|
|
88
|
+
CASE
|
|
89
|
+
WHEN metadata->>'type' = 'fragment' THEN
|
|
90
|
+
metadata ? 'documentId' AND
|
|
91
|
+
metadata ? 'position'
|
|
92
|
+
ELSE true
|
|
93
|
+
END
|
|
94
|
+
),
|
|
95
|
+
CONSTRAINT "document_metadata_check" CHECK (
|
|
96
|
+
CASE
|
|
97
|
+
WHEN metadata->>'type' = 'document' THEN
|
|
98
|
+
metadata ? 'timestamp'
|
|
99
|
+
ELSE true
|
|
100
|
+
END
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
--> statement-breakpoint
|
|
104
|
+
CREATE TABLE "participants" (
|
|
105
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
106
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
107
|
+
"entityId" uuid,
|
|
108
|
+
"roomId" uuid,
|
|
109
|
+
"agentId" uuid,
|
|
110
|
+
"roomState" text
|
|
111
|
+
);
|
|
112
|
+
--> statement-breakpoint
|
|
113
|
+
CREATE TABLE "relationships" (
|
|
114
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
115
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL,
|
|
116
|
+
"sourceEntityId" uuid NOT NULL,
|
|
117
|
+
"targetEntityId" uuid NOT NULL,
|
|
118
|
+
"agentId" uuid NOT NULL,
|
|
119
|
+
"tags" text[],
|
|
120
|
+
"metadata" jsonb,
|
|
121
|
+
CONSTRAINT "unique_relationship" UNIQUE("sourceEntityId","targetEntityId","agentId")
|
|
122
|
+
);
|
|
123
|
+
--> statement-breakpoint
|
|
124
|
+
CREATE TABLE "rooms" (
|
|
125
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
126
|
+
"agentId" uuid,
|
|
127
|
+
"source" text NOT NULL,
|
|
128
|
+
"type" text NOT NULL,
|
|
129
|
+
"serverId" text,
|
|
130
|
+
"worldId" uuid,
|
|
131
|
+
"name" text,
|
|
132
|
+
"metadata" jsonb,
|
|
133
|
+
"channelId" text,
|
|
134
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL
|
|
135
|
+
);
|
|
136
|
+
--> statement-breakpoint
|
|
137
|
+
CREATE TABLE "tasks" (
|
|
138
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
139
|
+
"name" text NOT NULL,
|
|
140
|
+
"description" text NOT NULL,
|
|
141
|
+
"room_id" uuid,
|
|
142
|
+
"world_id" uuid,
|
|
143
|
+
"agent_id" uuid NOT NULL,
|
|
144
|
+
"tags" text[],
|
|
145
|
+
"metadata" jsonb,
|
|
146
|
+
"created_at" timestamp DEFAULT now(),
|
|
147
|
+
"updated_at" timestamp DEFAULT now()
|
|
148
|
+
);
|
|
149
|
+
--> statement-breakpoint
|
|
150
|
+
CREATE TABLE "worlds" (
|
|
151
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
152
|
+
"agentId" uuid NOT NULL,
|
|
153
|
+
"name" text NOT NULL,
|
|
154
|
+
"metadata" jsonb,
|
|
155
|
+
"serverId" text NOT NULL,
|
|
156
|
+
"createdAt" timestamptz DEFAULT now() NOT NULL
|
|
157
|
+
);
|
|
158
|
+
--> statement-breakpoint
|
|
159
|
+
ALTER TABLE "cache" ADD CONSTRAINT "cache_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
160
|
+
ALTER TABLE "components" ADD CONSTRAINT "components_entityId_entities_id_fk" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
161
|
+
ALTER TABLE "components" ADD CONSTRAINT "components_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
162
|
+
ALTER TABLE "components" ADD CONSTRAINT "components_roomId_rooms_id_fk" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
163
|
+
ALTER TABLE "components" ADD CONSTRAINT "components_worldId_worlds_id_fk" FOREIGN KEY ("worldId") REFERENCES "public"."worlds"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
164
|
+
ALTER TABLE "components" ADD CONSTRAINT "components_sourceEntityId_entities_id_fk" FOREIGN KEY ("sourceEntityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
165
|
+
ALTER TABLE "embeddings" ADD CONSTRAINT "embeddings_memory_id_memories_id_fk" FOREIGN KEY ("memory_id") REFERENCES "public"."memories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
166
|
+
ALTER TABLE "embeddings" ADD CONSTRAINT "fk_embedding_memory" FOREIGN KEY ("memory_id") REFERENCES "public"."memories"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
167
|
+
ALTER TABLE "entities" ADD CONSTRAINT "entities_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
168
|
+
ALTER TABLE "logs" ADD CONSTRAINT "logs_entityId_entities_id_fk" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
169
|
+
ALTER TABLE "logs" ADD CONSTRAINT "logs_roomId_rooms_id_fk" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
170
|
+
ALTER TABLE "logs" ADD CONSTRAINT "fk_room" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
171
|
+
ALTER TABLE "logs" ADD CONSTRAINT "fk_user" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
172
|
+
ALTER TABLE "memories" ADD CONSTRAINT "memories_entityId_entities_id_fk" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
173
|
+
ALTER TABLE "memories" ADD CONSTRAINT "memories_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
174
|
+
ALTER TABLE "memories" ADD CONSTRAINT "memories_roomId_rooms_id_fk" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
175
|
+
ALTER TABLE "memories" ADD CONSTRAINT "fk_room" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
176
|
+
ALTER TABLE "memories" ADD CONSTRAINT "fk_user" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
177
|
+
ALTER TABLE "memories" ADD CONSTRAINT "fk_agent" FOREIGN KEY ("agentId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
178
|
+
ALTER TABLE "participants" ADD CONSTRAINT "participants_entityId_entities_id_fk" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
179
|
+
ALTER TABLE "participants" ADD CONSTRAINT "participants_roomId_rooms_id_fk" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
180
|
+
ALTER TABLE "participants" ADD CONSTRAINT "participants_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
181
|
+
ALTER TABLE "participants" ADD CONSTRAINT "fk_room" FOREIGN KEY ("roomId") REFERENCES "public"."rooms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
182
|
+
ALTER TABLE "participants" ADD CONSTRAINT "fk_user" FOREIGN KEY ("entityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
183
|
+
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_sourceEntityId_entities_id_fk" FOREIGN KEY ("sourceEntityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
184
|
+
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_targetEntityId_entities_id_fk" FOREIGN KEY ("targetEntityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
185
|
+
ALTER TABLE "relationships" ADD CONSTRAINT "relationships_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
186
|
+
ALTER TABLE "relationships" ADD CONSTRAINT "fk_user_a" FOREIGN KEY ("sourceEntityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
187
|
+
ALTER TABLE "relationships" ADD CONSTRAINT "fk_user_b" FOREIGN KEY ("targetEntityId") REFERENCES "public"."entities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
188
|
+
ALTER TABLE "rooms" ADD CONSTRAINT "rooms_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
189
|
+
ALTER TABLE "rooms" ADD CONSTRAINT "rooms_worldId_worlds_id_fk" FOREIGN KEY ("worldId") REFERENCES "public"."worlds"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
190
|
+
ALTER TABLE "worlds" ADD CONSTRAINT "worlds_agentId_agents_id_fk" FOREIGN KEY ("agentId") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
191
|
+
CREATE INDEX "idx_embedding_memory" ON "embeddings" USING btree ("memory_id");--> statement-breakpoint
|
|
192
|
+
CREATE INDEX "idx_memories_type_room" ON "memories" USING btree ("type","roomId");--> statement-breakpoint
|
|
193
|
+
CREATE INDEX "idx_memories_metadata_type" ON "memories" USING btree (((metadata->>'type')));--> statement-breakpoint
|
|
194
|
+
CREATE INDEX "idx_memories_document_id" ON "memories" USING btree (((metadata->>'documentId')));--> statement-breakpoint
|
|
195
|
+
CREATE INDEX "idx_fragments_order" ON "memories" USING btree (((metadata->>'documentId')),((metadata->>'position')));--> statement-breakpoint
|
|
196
|
+
CREATE INDEX "idx_participants_user" ON "participants" USING btree ("entityId");--> statement-breakpoint
|
|
197
|
+
CREATE INDEX "idx_participants_room" ON "participants" USING btree ("roomId");--> statement-breakpoint
|
|
198
|
+
CREATE INDEX "idx_relationships_users" ON "relationships" USING btree ("sourceEntityId","targetEntityId");
|