@elizaos/plugin-sql 2.0.0-beta.1 → 2.0.11-beta.7
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 +21 -0
- package/README.md +87 -304
- package/package.json +24 -12
- package/src/dist/base.d.ts +0 -1152
- package/src/dist/browser/index.browser.d.ts +0 -2
- package/src/dist/browser/index.browser.js +0 -26781
- package/src/dist/browser/index.browser.js.map +0 -72
- package/src/dist/browser/index.d.ts +0 -2
- package/src/dist/cjs/index.d.ts +0 -2
- package/src/dist/cjs/index.node.cjs +0 -9154
- package/src/dist/cjs/index.node.cjs.map +0 -70
- package/src/dist/cjs/index.node.d.cts +0 -2
- package/src/dist/connector-credential-store.d.ts +0 -48
- package/src/dist/drizzle/index.d.ts +0 -1
- package/src/dist/drizzle/index.js +0 -1
- package/src/dist/index.d.ts +0 -4
- package/src/dist/index.js +0 -2
- package/src/dist/index.node.d.ts +0 -20
- package/src/dist/migration-service.d.ts +0 -17
- package/src/dist/migrations.d.ts +0 -15
- package/src/dist/node/index.d.ts +0 -2
- package/src/dist/node/index.node.d.ts +0 -2
- package/src/dist/node/index.node.js +0 -9175
- package/src/dist/node/index.node.js.map +0 -70
- package/src/dist/pg/adapter.d.ts +0 -42
- package/src/dist/pg/manager.d.ts +0 -17
- package/src/dist/pglite/adapter.d.ts +0 -24
- package/src/dist/pglite/errors.d.ts +0 -20
- package/src/dist/pglite/manager.d.ts +0 -32
- package/src/dist/rls.d.ts +0 -13
- package/src/dist/runtime-migrator/crypto-utils.d.ts +0 -25
- package/src/dist/runtime-migrator/drizzle-adapters/database-introspector.d.ts +0 -58
- package/src/dist/runtime-migrator/drizzle-adapters/diff-calculator.d.ts +0 -77
- package/src/dist/runtime-migrator/drizzle-adapters/snapshot-generator.d.ts +0 -21
- package/src/dist/runtime-migrator/drizzle-adapters/sql-generator.d.ts +0 -38
- package/src/dist/runtime-migrator/extension-manager.d.ts +0 -6
- package/src/dist/runtime-migrator/index.d.ts +0 -8
- package/src/dist/runtime-migrator/runtime-migrator.d.ts +0 -95
- package/src/dist/runtime-migrator/schema-transformer.d.ts +0 -18
- package/src/dist/runtime-migrator/storage/journal-storage.d.ts +0 -10
- package/src/dist/runtime-migrator/storage/migration-tracker.d.ts +0 -13
- package/src/dist/runtime-migrator/storage/snapshot-storage.d.ts +0 -9
- package/src/dist/runtime-migrator/types.d.ts +0 -157
- package/src/dist/schema/agent.d.ts +0 -344
- package/src/dist/schema/approvalRequests.d.ts +0 -277
- package/src/dist/schema/authAuditEvent.d.ts +0 -153
- package/src/dist/schema/authBootstrapJti.d.ts +0 -49
- package/src/dist/schema/authIdentity.d.ts +0 -121
- package/src/dist/schema/authOwnerBinding.d.ts +0 -168
- package/src/dist/schema/authOwnerLoginToken.d.ts +0 -122
- package/src/dist/schema/authSession.d.ts +0 -225
- package/src/dist/schema/cache.d.ts +0 -97
- package/src/dist/schema/channel.d.ts +0 -177
- package/src/dist/schema/channelParticipant.d.ts +0 -41
- package/src/dist/schema/component.d.ts +0 -163
- package/src/dist/schema/connectorAccounts.d.ts +0 -981
- package/src/dist/schema/embedding.d.ts +0 -204
- package/src/dist/schema/entity.d.ts +0 -125
- package/src/dist/schema/entityIdentity.d.ts +0 -577
- package/src/dist/schema/index.d.ts +0 -35
- package/src/dist/schema/index.js +0 -1
- package/src/dist/schema/log.d.ts +0 -114
- package/src/dist/schema/longTermMemories.d.ts +0 -254
- package/src/dist/schema/memory.d.ts +0 -185
- package/src/dist/schema/memoryAccessLogs.d.ts +0 -109
- package/src/dist/schema/message.d.ts +0 -194
- package/src/dist/schema/messageServer.d.ts +0 -126
- package/src/dist/schema/messageServerAgent.d.ts +0 -41
- package/src/dist/schema/pairingAllowlist.d.ts +0 -113
- package/src/dist/schema/pairingRequest.d.ts +0 -147
- package/src/dist/schema/participant.d.ts +0 -114
- package/src/dist/schema/relationship.d.ts +0 -156
- package/src/dist/schema/room.d.ts +0 -195
- package/src/dist/schema/server.d.ts +0 -64
- package/src/dist/schema/sessionSummaries.d.ts +0 -273
- package/src/dist/schema/tasks.d.ts +0 -225
- package/src/dist/schema/types.d.ts +0 -68
- package/src/dist/schema/world.d.ts +0 -114
- package/src/dist/services/advanced-memory-storage.d.ts +0 -36
- package/src/dist/stores/connectorAccount.store.d.ts +0 -64
- package/src/dist/stores/types.d.ts +0 -25
- package/src/dist/types.d.ts +0 -13
- package/src/dist/utils/string-to-uuid.d.ts +0 -2
- package/src/dist/utils.d.ts +0 -4
- package/src/dist/utils.node.d.ts +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shaw Walters and elizaOS Contributors
|
|
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
CHANGED
|
@@ -1,359 +1,142 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @elizaos/plugin-sql
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
SQL database adapter plugin for elizaOS — provides persistent storage via PostgreSQL or embedded PGlite (WASM), with Drizzle ORM, automatic schema migrations, and optional Row Level Security.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
# Using bun
|
|
9
8
|
bun add @elizaos/plugin-sql
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
The adapter supports the following vector dimensions:
|
|
15
|
-
|
|
16
|
-
```typescript
|
|
17
|
-
VECTOR_DIMS = {
|
|
18
|
-
SMALL: 384,
|
|
19
|
-
MEDIUM: 512,
|
|
20
|
-
LARGE: 768,
|
|
21
|
-
XL: 1024,
|
|
22
|
-
XXL: 1536,
|
|
23
|
-
XXXL: 3072,
|
|
24
|
-
};
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
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"
|
|
28
|
-
|
|
29
|
-
## Features
|
|
11
|
+
## Overview
|
|
30
12
|
|
|
31
|
-
|
|
32
|
-
- Automatic retries with exponential backoff
|
|
33
|
-
- Connection pooling
|
|
34
|
-
- Vector search capabilities
|
|
35
|
-
- Memory management
|
|
36
|
-
- Caching system
|
|
37
|
-
- Room and participant management
|
|
38
|
-
- Goal tracking system
|
|
13
|
+
This plugin registers a `DatabaseAdapter` with the elizaOS agent runtime so that all core runtime persistence (memories, entities, rooms, tasks, cache, logs, relationships, etc.) works against a real SQL backend. On Node/Bun it selects PostgreSQL when `POSTGRES_URL` is set, otherwise falls back to embedded PGlite. In the browser build it always uses PGlite (WASM).
|
|
39
14
|
|
|
40
15
|
## Database Schema
|
|
41
16
|
|
|
42
|
-
The plugin uses
|
|
17
|
+
The plugin uses the following main tables:
|
|
43
18
|
|
|
44
|
-
|
|
19
|
+
- **Agent**: Agent information and configurations
|
|
20
|
+
- **Room / Channel**: Conversation rooms and messaging channels
|
|
21
|
+
- **Participant / ChannelParticipant**: Participants in rooms and channels
|
|
22
|
+
- **Memory**: Agent memories with vector embeddings for semantic search
|
|
23
|
+
- **Embedding**: Vector embeddings for entities
|
|
24
|
+
- **Entity**: Entities agents interact with
|
|
25
|
+
- **Relationship**: Relationships between entities
|
|
26
|
+
- **Component**: Agent components and configurations
|
|
27
|
+
- **Tasks**: Tasks and goals
|
|
28
|
+
- **Log**: System logs
|
|
29
|
+
- **Cache**: Frequently accessed data cache
|
|
30
|
+
- **World**: World settings and configurations
|
|
45
31
|
|
|
46
|
-
|
|
47
|
-
- **Room**: Manages conversation rooms and their settings
|
|
48
|
-
- **Participant**: Tracks participants in rooms
|
|
49
|
-
- **Memory**: Stores agent memories with vector embeddings for semantic search
|
|
50
|
-
- **Embedding**: Manages vector embeddings for various entities
|
|
51
|
-
- **Entity**: Represents entities that agents can interact with
|
|
52
|
-
- **Relationship**: Tracks relationships between entities
|
|
53
|
-
- **Component**: Stores agent components and their configurations
|
|
54
|
-
- **Tasks**: Manages tasks and goals for agents
|
|
55
|
-
- **Log**: Stores system logs
|
|
56
|
-
- **Cache**: Provides a caching mechanism for frequently accessed data
|
|
57
|
-
- **World**: Manages world settings and configurations
|
|
32
|
+
Table definitions live in `src/schema/`.
|
|
58
33
|
|
|
59
|
-
|
|
34
|
+
## Electric Sync (PGlite ↔ Electric Cloud)
|
|
60
35
|
|
|
61
|
-
|
|
36
|
+
When `ELIZA_ELECTRIC_SYNC_URL` and `AGENT_ID` are set, PGlite connects to an Electric sync service and streams real-time updates for all core tables. Each agent syncs only its own rows (filtered by `agent_id` / `id`), preserving per-agent isolation in shared-Neon deployments.
|
|
62
37
|
|
|
63
|
-
|
|
38
|
+
### Local dev with Electric Cloud
|
|
64
39
|
|
|
65
|
-
|
|
66
|
-
async function findDatabaseAdapter(runtime: IAgentRuntime) {
|
|
67
|
-
let adapter = runtime;
|
|
68
|
-
|
|
69
|
-
if (!adapter) {
|
|
70
|
-
const drizzleAdapterPlugin = await import("@elizaos/plugin-sql");
|
|
71
|
-
const drizzleAdapterPluginDefault = drizzleAdapterPlugin.default;
|
|
72
|
-
adapter = drizzleAdapterPluginDefault.adapter;
|
|
73
|
-
if (!adapter) {
|
|
74
|
-
throw new Error(
|
|
75
|
-
"Internal error: No database adapter found for default plugin-sql",
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
} else if (!adapter) {
|
|
79
|
-
throw new Error(
|
|
80
|
-
"Multiple database adapters found. You must have no more than one. Adjust your plugins configuration.",
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const adapterInterface = await adapter?.init(runtime);
|
|
85
|
-
return adapterInterface;
|
|
86
|
-
}
|
|
87
|
-
```
|
|
40
|
+
The e2e write-back test (`__tests__/integration/electric-write-back.test.ts`) uses a [Caddy](https://caddyserver.com) reverse proxy to forward shape requests to Electric Cloud with auth. Caddy is the proxy [recommended by Electric](https://electric.ax/docs/sync/guides/troubleshooting#missing-headers).
|
|
88
41
|
|
|
89
|
-
|
|
42
|
+
```bash
|
|
43
|
+
# 1. Set your Electric Cloud credentials (get these from dashboard.electric-sql.cloud)
|
|
44
|
+
export ELECTRIC_CLOUD_SOURCE_ID=svc-xxxxxxxxxxxx
|
|
45
|
+
export ELECTRIC_CLOUD_SECRET=eyJ...
|
|
90
46
|
|
|
91
|
-
|
|
47
|
+
# 2. Start the Caddy proxy
|
|
48
|
+
caddy run --config plugins/plugin-sql/caddy/electric-proxy.Caddyfile
|
|
92
49
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
resetTimeout: 60000,
|
|
97
|
-
halfOpenMaxAttempts: 3,
|
|
98
|
-
maxRetries: 3,
|
|
99
|
-
baseDelay: 1000, // 1 second
|
|
100
|
-
maxDelay: 10000, // 10 seconds
|
|
101
|
-
jitterMax: 1000, // 1 second
|
|
102
|
-
connectionTimeout: 5000 // 5 seconds
|
|
103
|
-
}
|
|
50
|
+
# 3. Run the e2e test
|
|
51
|
+
bun run --cwd plugins/plugin-sql test -- \
|
|
52
|
+
__tests__/integration/electric-write-back.test.ts
|
|
104
53
|
```
|
|
105
54
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
- PostgreSQL with vector extension installed
|
|
109
|
-
- Node.js or Bun (≥1.2.2)
|
|
55
|
+
The Caddyfile at `plugins/plugin-sql/caddy/electric-proxy.Caddyfile` forwards every incoming request to `api.electric-sql.cloud` with auth query params appended.
|
|
110
56
|
|
|
111
57
|
## Environment Variables
|
|
112
58
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
59
|
+
| Variable | Required | Default | Effect |
|
|
60
|
+
|----------|----------|---------|--------|
|
|
61
|
+
| `POSTGRES_URL` | No | — | PostgreSQL connection string. When absent, PGlite is used. |
|
|
62
|
+
| `PGLITE_DATA_DIR` | No | `.eliza/.elizadb` | Directory (or `idb://` URL) for PGlite data storage. |
|
|
63
|
+
| `ELIZA_ELECTRIC_SYNC_URL` | No | — | Base URL of Electric sync service (e.g. `http://localhost:3001` via Caddy). |
|
|
64
|
+
| `AGENT_ID` | Conditional | — | UUID of the agent. Required when `ELIZA_ELECTRIC_SYNC_URL` is set (per-agent WHERE filter). |
|
|
65
|
+
| `ELIZA_CLOUD_WRITE_BASE_URL` | No | — | Write-back cloud endpoint for forwarding local PGlite writes to Postgres. |
|
|
66
|
+
| `ELIZA_CLOUD_SERVICE_KEY` | No | — | Service key for authenticating write-back requests. |
|
|
67
|
+
| `ELECTRIC_CLOUD_SOURCE_ID` | Test-only | — | Electric Cloud source ID (consumed by Caddy, not the runtime). |
|
|
68
|
+
| `ELECTRIC_CLOUD_SECRET` | Test-only | — | Electric Cloud JWT secret (consumed by Caddy, not the runtime). |
|
|
69
|
+
| `ELIZA_PGLITE_DISABLE_EXTENSIONS` | No | `false` | Set to `1` to disable PGlite extensions (vector, live, fuzzystrmatch, Electric sync). |
|
|
70
|
+
| `ENABLE_DATA_ISOLATION` | No | `false` | When `true`, enables PostgreSQL Row Level Security per-server isolation. |
|
|
71
|
+
| `ELIZA_SERVER_ID` | Conditional | — | Required when `ENABLE_DATA_ISOLATION=true`; becomes the RLS server UUID. |
|
|
72
|
+
| `ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS` | No | `false` | Allow column drops and other destructive schema changes at startup. |
|
|
73
|
+
| `NODE_ENV` | No | `development` | `production` disables verbose migration logging and tightens safety checks. |
|
|
74
|
+
|
|
75
|
+
Settings are read via `runtime.getSetting(key)` inside `plugin.init`.
|
|
120
76
|
|
|
121
|
-
##
|
|
122
|
-
|
|
123
|
-
Default pool configuration:
|
|
77
|
+
## Vector Dimensions
|
|
124
78
|
|
|
125
79
|
```typescript
|
|
126
|
-
{
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
80
|
+
VECTOR_DIMS = {
|
|
81
|
+
SMALL: 384,
|
|
82
|
+
MEDIUM: 512,
|
|
83
|
+
LARGE: 768,
|
|
84
|
+
XL: 1024,
|
|
85
|
+
XXL: 1536,
|
|
86
|
+
XXXL: 3072,
|
|
87
|
+
};
|
|
131
88
|
```
|
|
132
89
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
elizaOS v1.0.0 introduces **dynamic runtime migrations** - automatic schema management that runs at startup without manual intervention. Plugins can define their schemas and the system handles all migrations automatically.
|
|
136
|
-
|
|
137
|
-
### TLDR: What Changed?
|
|
138
|
-
|
|
139
|
-
**Before (v0.x):** Manual migrations with `drizzle-kit generate` → `drizzle-kit push` → restart
|
|
140
|
-
**Now (v1.0.0):** Define schema in plugin → Start agent → Migrations run automatically ✨
|
|
90
|
+
Once an agent is initialized with a specific embedding dimension, it cannot be changed without a new agent or manual DB surgery.
|
|
141
91
|
|
|
142
|
-
|
|
92
|
+
## Runtime Migrations
|
|
143
93
|
|
|
144
|
-
-
|
|
145
|
-
- **Plugin Isolation**: Each plugin gets its own schema namespace
|
|
146
|
-
- **Safety First**: Destructive changes blocked by default in production
|
|
147
|
-
- **Concurrent Safety**: Built-in locks prevent race conditions
|
|
148
|
-
- **Rollback Protection**: All migrations run in transactions
|
|
149
|
-
|
|
150
|
-
### How It Works
|
|
151
|
-
|
|
152
|
-
1. **Plugin defines schema** using Drizzle ORM:
|
|
94
|
+
Plugins export a `schema` object; `DatabaseMigrationService` diffs the schema against the live DB at startup and runs migrations automatically. No manual `drizzle-kit generate` / `drizzle-kit push` step is needed in normal development.
|
|
153
95
|
|
|
154
96
|
```typescript
|
|
155
|
-
// In your plugin
|
|
156
|
-
import { pgTable, text, uuid } from "drizzle-orm/pg-core";
|
|
157
|
-
|
|
158
|
-
export const myTable = pgTable("my_table", {
|
|
159
|
-
id: uuid("id").primaryKey(),
|
|
160
|
-
name: text("name").notNull(),
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Export schema in your plugin
|
|
97
|
+
// In your plugin
|
|
164
98
|
export const plugin = {
|
|
165
99
|
name: "@your-org/plugin-name",
|
|
166
|
-
schema: schema, //
|
|
167
|
-
// ...
|
|
100
|
+
schema: schema, // Drizzle schema object
|
|
101
|
+
// ...
|
|
168
102
|
};
|
|
169
103
|
```
|
|
170
104
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
```bash
|
|
174
|
-
[RuntimeMigrator] Starting migration for plugin: @your-org/plugin-name
|
|
175
|
-
[RuntimeMigrator] Executing 2 SQL statements...
|
|
176
|
-
[RuntimeMigrator] Migration completed successfully
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
3. **Automatic safety checks**:
|
|
180
|
-
|
|
181
|
-
```bash
|
|
182
|
-
# Destructive changes are blocked
|
|
183
|
-
[RuntimeMigrator] Destructive migration blocked
|
|
184
|
-
[RuntimeMigrator] Destructive operations detected:
|
|
185
|
-
[RuntimeMigrator] - Column "email" will be dropped from table "users"
|
|
186
|
-
[RuntimeMigrator] To proceed:
|
|
187
|
-
[RuntimeMigrator] 1. Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true
|
|
188
|
-
[RuntimeMigrator] 2. Or use { force: true } option
|
|
189
|
-
```
|
|
105
|
+
Destructive changes (column drops, type changes) are blocked by default. Set `ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true` to allow them.
|
|
190
106
|
|
|
191
|
-
|
|
107
|
+
## Connection Management
|
|
192
108
|
|
|
193
|
-
|
|
109
|
+
Both `PostgresConnectionManager` and `PGliteClientManager` are stored under `Symbol.for("elizaos.plugin-sql.global-singletons")` on `globalThis`. This prevents multiple pools when the module is imported from multiple paths in the same process. Do not construct manager instances directly — always go through `createDatabaseAdapter()`.
|
|
194
110
|
|
|
195
|
-
|
|
196
|
-
# Allow destructive migrations (drops, type changes)
|
|
197
|
-
ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true
|
|
198
|
-
|
|
199
|
-
# Development vs Production
|
|
200
|
-
NODE_ENV=production # Stricter checks, verbose off by default
|
|
201
|
-
NODE_ENV=development # More permissive, verbose on
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
Or programmatically:
|
|
205
|
-
|
|
206
|
-
```typescript
|
|
207
|
-
await databaseAdapter.runPluginMigrations(plugins, {
|
|
208
|
-
verbose: true, // Show SQL statements
|
|
209
|
-
force: true, // Allow destructive changes
|
|
210
|
-
dryRun: true, // Preview without applying
|
|
211
|
-
});
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### Transitioning from Manual Migrations
|
|
215
|
-
|
|
216
|
-
If you have existing manual Drizzle migrations:
|
|
217
|
-
|
|
218
|
-
1. **Keep existing migrations** - They remain compatible
|
|
219
|
-
2. **Add schema to plugin** - Export your Drizzle schema
|
|
220
|
-
3. **First run** - Runtime migrator detects current state
|
|
221
|
-
4. **Future changes** - Just update schema and restart
|
|
222
|
-
|
|
223
|
-
Example transition:
|
|
224
|
-
|
|
225
|
-
```typescript
|
|
226
|
-
// Before: Manual migrations
|
|
227
|
-
// 1. Edit schema
|
|
228
|
-
// 2. Run: bunx drizzle-kit generate
|
|
229
|
-
// 3. Run: bunx drizzle-kit push
|
|
230
|
-
// 4. Restart agent
|
|
231
|
-
|
|
232
|
-
// After: Runtime migrations
|
|
233
|
-
// 1. Edit schema in plugin
|
|
234
|
-
// 2. Restart agent (migrations run automatically)
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### Schema Namespacing
|
|
238
|
-
|
|
239
|
-
Plugins automatically get namespaced schemas for isolation:
|
|
240
|
-
|
|
241
|
-
- `@elizaos/plugin-sql` → Uses `public` schema (core tables)
|
|
242
|
-
- `@your-org/plugin-name` → Uses `your_org_plugin_name` schema
|
|
243
|
-
- Prevents table name conflicts between plugins
|
|
244
|
-
- Clean separation of concerns
|
|
245
|
-
|
|
246
|
-
To use a custom schema:
|
|
247
|
-
|
|
248
|
-
```typescript
|
|
249
|
-
import { pgSchema } from "drizzle-orm/pg-core";
|
|
250
|
-
|
|
251
|
-
const mySchema = pgSchema("my_custom_schema");
|
|
252
|
-
export const myTable = mySchema.table("my_table", {
|
|
253
|
-
// ... columns
|
|
254
|
-
});
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
### Debugging Migrations
|
|
258
|
-
|
|
259
|
-
Check migration status:
|
|
260
|
-
|
|
261
|
-
```typescript
|
|
262
|
-
const migrator = migrationService.getMigrator();
|
|
263
|
-
const status = await migrator.getStatus("@your-org/plugin-name");
|
|
264
|
-
console.log(status);
|
|
265
|
-
// {
|
|
266
|
-
// hasRun: true,
|
|
267
|
-
// lastMigration: { hash: "...", timestamp: ... },
|
|
268
|
-
// journal: [...],
|
|
269
|
-
// snapshots: 3
|
|
270
|
-
// }
|
|
271
|
-
```
|
|
111
|
+
## Database Pool Configuration
|
|
272
112
|
|
|
273
|
-
|
|
113
|
+
Default Postgres pool configuration (`src/pg/manager.ts`):
|
|
274
114
|
|
|
275
115
|
```typescript
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
116
|
+
{
|
|
117
|
+
max: 20,
|
|
118
|
+
min: 2,
|
|
119
|
+
idleTimeoutMillis: 30000,
|
|
120
|
+
connectionTimeoutMillis: 5000,
|
|
121
|
+
keepAlive: true,
|
|
122
|
+
keepAliveInitialDelayMillis: 10000
|
|
279
123
|
}
|
|
280
124
|
```
|
|
281
125
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
The plugin supports two database backends with automatic migration support:
|
|
126
|
+
## Retry Configuration
|
|
285
127
|
|
|
286
|
-
|
|
287
|
-
2. **PGlite**: Embedded database for development/testing
|
|
288
|
-
|
|
289
|
-
Both use identical migration systems - develop locally with PGlite, deploy to PostgreSQL.
|
|
290
|
-
|
|
291
|
-
### Troubleshooting
|
|
292
|
-
|
|
293
|
-
**"Destructive migration blocked"**
|
|
294
|
-
|
|
295
|
-
- Set `ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true` for development
|
|
296
|
-
- For production, review changes carefully before enabling
|
|
297
|
-
|
|
298
|
-
**"Migration already in progress"**
|
|
299
|
-
|
|
300
|
-
- Another instance is running migrations
|
|
301
|
-
- System will wait for lock automatically
|
|
302
|
-
|
|
303
|
-
**"No changes detected"**
|
|
304
|
-
|
|
305
|
-
- Schema matches database state
|
|
306
|
-
- No migration needed
|
|
307
|
-
|
|
308
|
-
**Manual migration needed?**
|
|
309
|
-
|
|
310
|
-
- Use standard Drizzle Kit for complex scenarios:
|
|
311
|
-
```bash
|
|
312
|
-
bunx drizzle-kit generate
|
|
313
|
-
bunx drizzle-kit migrate
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
## Clean Shutdown
|
|
317
|
-
|
|
318
|
-
The adapter implements cleanup handlers for:
|
|
319
|
-
|
|
320
|
-
- SIGINT
|
|
321
|
-
- SIGTERM
|
|
322
|
-
- beforeExit
|
|
323
|
-
|
|
324
|
-
These ensure proper closing of database connections when the application shuts down.
|
|
325
|
-
|
|
326
|
-
## Implementation Details
|
|
327
|
-
|
|
328
|
-
### Connection Management
|
|
329
|
-
|
|
330
|
-
The plugin uses a global singleton pattern to manage database connections. This approach ensures that:
|
|
331
|
-
|
|
332
|
-
1. **Single Connection Per Process**: Only one connection manager instance exists per Node.js process, regardless of how many times the package is imported or initialized.
|
|
333
|
-
|
|
334
|
-
2. **Resource Efficiency**: Prevents multiple connection pools to the same database, which could lead to resource exhaustion.
|
|
335
|
-
|
|
336
|
-
3. **Consistent State**: Ensures all parts of the application share the same database connection state.
|
|
337
|
-
|
|
338
|
-
4. **Proper Cleanup**: Facilitates proper cleanup of database connections during application shutdown, preventing connection leaks.
|
|
339
|
-
|
|
340
|
-
This pattern is particularly important in monorepo setups or when the package is used by multiple modules within the same process. The implementation uses JavaScript Symbols to create a global registry that persists across module boundaries.
|
|
128
|
+
`BaseDrizzleAdapter` retries failed operations with exponential backoff and jitter (`src/base.ts`):
|
|
341
129
|
|
|
342
130
|
```typescript
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
globalSymbols[GLOBAL_SINGLETONS] = {};
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// Reuse existing managers or create new ones when needed
|
|
352
|
-
if (!globalSingletons.postgresConnectionManager) {
|
|
353
|
-
globalSingletons.postgresConnectionManager = new PostgresConnectionManager(
|
|
354
|
-
config.postgresUrl,
|
|
355
|
-
);
|
|
131
|
+
{
|
|
132
|
+
maxRetries: 3,
|
|
133
|
+
baseDelay: 1000,
|
|
134
|
+
maxDelay: 10000,
|
|
135
|
+
jitterMax: 1000
|
|
356
136
|
}
|
|
357
137
|
```
|
|
358
138
|
|
|
359
|
-
|
|
139
|
+
## Requirements
|
|
140
|
+
|
|
141
|
+
- Node.js or Bun
|
|
142
|
+
- PostgreSQL with vector extension (for Postgres mode)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-sql",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.11-beta.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/dist/index.js",
|
|
@@ -9,10 +9,15 @@
|
|
|
9
9
|
"./package.json": "./package.json",
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./src/dist/index.node.d.ts",
|
|
12
|
+
"eliza-source": {
|
|
13
|
+
"types": "./src/index.node.ts",
|
|
14
|
+
"import": "./src/index.node.ts",
|
|
15
|
+
"default": "./src/index.node.ts"
|
|
16
|
+
},
|
|
12
17
|
"bun": {
|
|
13
|
-
"types": "./src/index.ts",
|
|
14
|
-
"import": "./src/index.
|
|
15
|
-
"default": "./src/index.
|
|
18
|
+
"types": "./src/dist/index.node.d.ts",
|
|
19
|
+
"import": "./src/dist/node/index.node.js",
|
|
20
|
+
"default": "./src/dist/node/index.node.js"
|
|
16
21
|
},
|
|
17
22
|
"browser": {
|
|
18
23
|
"types": "./src/dist/index.d.ts",
|
|
@@ -30,9 +35,9 @@
|
|
|
30
35
|
"./drizzle": {
|
|
31
36
|
"types": "./src/dist/drizzle/index.d.ts",
|
|
32
37
|
"bun": {
|
|
33
|
-
"types": "./src/drizzle/index.ts",
|
|
34
|
-
"import": "./src/drizzle/index.
|
|
35
|
-
"default": "./src/drizzle/index.
|
|
38
|
+
"types": "./src/dist/drizzle/index.d.ts",
|
|
39
|
+
"import": "./src/dist/drizzle/index.js",
|
|
40
|
+
"default": "./src/dist/drizzle/index.js"
|
|
36
41
|
},
|
|
37
42
|
"import": "./src/dist/drizzle/index.js",
|
|
38
43
|
"default": "./src/dist/drizzle/index.js"
|
|
@@ -40,12 +45,18 @@
|
|
|
40
45
|
"./schema": {
|
|
41
46
|
"types": "./src/dist/schema/index.d.ts",
|
|
42
47
|
"bun": {
|
|
43
|
-
"types": "./src/schema/index.ts",
|
|
44
|
-
"import": "./src/schema/index.
|
|
45
|
-
"default": "./src/schema/index.
|
|
48
|
+
"types": "./src/dist/schema/index.d.ts",
|
|
49
|
+
"import": "./src/dist/schema/index.js",
|
|
50
|
+
"default": "./src/dist/schema/index.js"
|
|
46
51
|
},
|
|
47
52
|
"import": "./src/dist/schema/index.js",
|
|
48
53
|
"default": "./src/dist/schema/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./*.css": "./dist/*.css",
|
|
56
|
+
"./*": {
|
|
57
|
+
"types": "./dist/*.d.ts",
|
|
58
|
+
"import": "./dist/*.js",
|
|
59
|
+
"default": "./dist/*.js"
|
|
49
60
|
}
|
|
50
61
|
},
|
|
51
62
|
"files": [
|
|
@@ -75,6 +86,7 @@
|
|
|
75
86
|
},
|
|
76
87
|
"dependencies": {
|
|
77
88
|
"@electric-sql/pglite": "^0.4.0",
|
|
89
|
+
"@electric-sql/pglite-sync": "0.5.6",
|
|
78
90
|
"@neondatabase/serverless": "^1.1.0",
|
|
79
91
|
"drizzle-orm": "0.45.2",
|
|
80
92
|
"pg": "^8.16.3",
|
|
@@ -93,9 +105,9 @@
|
|
|
93
105
|
"vitest": "^4.0.0"
|
|
94
106
|
},
|
|
95
107
|
"peerDependencies": {
|
|
96
|
-
"@elizaos/core": "2.0.
|
|
108
|
+
"@elizaos/core": "2.0.11-beta.7"
|
|
97
109
|
},
|
|
98
|
-
"gitHead": "
|
|
110
|
+
"gitHead": "cdbc876f793d96073d7eb0d09715a031ce0cd32e",
|
|
99
111
|
"publishConfig": {
|
|
100
112
|
"access": "public"
|
|
101
113
|
}
|