@elizaos/plugin-sql 1.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shaw Walters, aka Moon aka @lalalune
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # DrizzleDatabaseAdapter
2
+
3
+ A PostgreSQL database adapter built with Drizzle ORM for the ElizaOS ecosystem.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Using bun
9
+ bun add @elizaos/plugin-sql
10
+ ```
11
+
12
+ ## Database Schema
13
+
14
+ The adapter includes the following tables:
15
+ - accounts
16
+ - cache
17
+ - embeddings
18
+ - goals
19
+ - logs
20
+ - memories
21
+ - participants
22
+ - relationships
23
+ - rooms
24
+
25
+ ## Vector Dimensions
26
+
27
+ The adapter supports the following vector dimensions:
28
+ ```typescript
29
+ VECTOR_DIMS = {
30
+ SMALL: 384,
31
+ MEDIUM: 512,
32
+ LARGE: 768,
33
+ XL: 1024,
34
+ XXL: 1536,
35
+ XXXL: 3072
36
+ }
37
+ ```
38
+
39
+ Important Note: Once an agent is initialized with a specific embedding dimension, it cannot be changed. Attempting to change the dimension will result in an error: "Cannot change embedding dimension for agent"
40
+
41
+ ## Features
42
+
43
+ - Circuit breaker pattern for database failures
44
+ - Automatic retries with exponential backoff
45
+ - Connection pooling
46
+ - Vector search capabilities
47
+ - Memory management
48
+ - Caching system
49
+ - Room and participant management
50
+ - Goal tracking system
51
+
52
+ ## Usage
53
+
54
+ The adapter is typically used as part of the ElizaOS runtime:
55
+
56
+ ```typescript
57
+ async function findDatabaseAdapter(runtime: IAgentRuntime) {
58
+ const { adapters } = runtime;
59
+ let adapter: Adapter | undefined;
60
+
61
+ if (adapters.length === 0) {
62
+ const drizzleAdapterPlugin = await import('@elizaos/plugin-sql');
63
+ const drizzleAdapterPluginDefault = drizzleAdapterPlugin.default;
64
+ adapter = drizzleAdapterPluginDefault.adapters[0];
65
+ if (!adapter) {
66
+ throw new Error("Internal error: No database adapter found for default plugin-sql");
67
+ }
68
+ } else if (adapters.length === 1) {
69
+ adapter = adapters[0];
70
+ } else {
71
+ throw new Error("Multiple database adapters found. You must have no more than one. Adjust your plugins configuration.");
72
+ }
73
+
74
+ const adapterInterface = await adapter?.init(runtime);
75
+ return adapterInterface;
76
+ }
77
+ ```
78
+
79
+ ## Error Handling Configuration
80
+
81
+ The adapter implements the following error handling configurations:
82
+ ```typescript
83
+ {
84
+ failureThreshold: 5,
85
+ resetTimeout: 60000,
86
+ halfOpenMaxAttempts: 3,
87
+ maxRetries: 3,
88
+ baseDelay: 1000, // 1 second
89
+ maxDelay: 10000, // 10 seconds
90
+ jitterMax: 1000, // 1 second
91
+ connectionTimeout: 5000 // 5 seconds
92
+ }
93
+ ```
94
+
95
+ ## Requirements
96
+
97
+ - PostgreSQL with vector extension installed
98
+ - Node.js or Bun (≥1.2.2)
99
+
100
+ ## Database Pool Configuration
101
+
102
+ Default pool configuration:
103
+ ```typescript
104
+ {
105
+ max: 20,
106
+ idleTimeoutMillis: 30000,
107
+ connectionTimeoutMillis: 5000
108
+ }
109
+ ```
110
+
111
+ ## Migration Support
112
+
113
+ The adapter supports two approaches to managing database schema:
114
+
115
+ ### 1. Initial Setup
116
+ Migrations are automatically run during initialization if:
117
+ - Database tables don't exist
118
+ - Vector extension is not found
119
+
120
+ This is handled internally by:
121
+ ```typescript
122
+ await runMigrations(pgPool);
123
+ ```
124
+
125
+ ### 2. Schema Updates
126
+ To update the schema:
127
+
128
+ 1. Install drizzle-kit (if not already installed):
129
+ ```bash
130
+ pnpm add -D drizzle-kit
131
+ ```
132
+
133
+ 2. Create or update your schema files (e.g., `schema/account.ts`):
134
+ ```typescript
135
+ import { pgTable, text, uuid } from "drizzle-orm/pg-core";
136
+ import { sql } from "drizzle-orm";
137
+
138
+ export const accountTable = pgTable("accounts", {
139
+ id: uuid("id").primaryKey().notNull(),
140
+ name: text("name"),
141
+ email: text("email").notNull(),
142
+ // Add new fields here
143
+ newField: text("newField"),
144
+ });
145
+ ```
146
+
147
+ 3. Generate migrations:
148
+ ```bash
149
+ npx drizzle-kit generate:pg
150
+ ```
151
+
152
+ This will create SQL migration files in your migrations directory.
153
+
154
+ 4. Apply migrations using one of these methods:
155
+
156
+ a. Using drizzle-kit:
157
+ ```bash
158
+ npx drizzle-kit push:pg
159
+ ```
160
+
161
+ b. Through your application code:
162
+ ```typescript
163
+ import { migrate } from "drizzle-orm/node-postgres/migrator";
164
+
165
+ await migrate(db, { migrationsFolder: "./drizzle" });
166
+ ```
167
+
168
+ ### Note on Vector Support
169
+ Make sure the PostgreSQL vector extension is installed before running migrations. The adapter will validate vector setup during initialization.
170
+
171
+ ## Clean Shutdown
172
+
173
+ The adapter implements cleanup handlers for:
174
+ - SIGINT
175
+ - SIGTERM
176
+ - beforeExit
177
+
178
+ These ensure proper closing of database connections when the application shuts down.
@@ -0,0 +1,9 @@
1
+ import { Plugin, UUID, IDatabaseAdapter } from '@elizaos/core';
2
+
3
+ declare function createDatabaseAdapter(config: {
4
+ dataDir?: string;
5
+ postgresUrl?: string;
6
+ }, agentId: UUID): IDatabaseAdapter;
7
+ declare const drizzlePlugin: Plugin;
8
+
9
+ export { createDatabaseAdapter, drizzlePlugin as default };