@anvia/memory-drizzle ​
@anvia/memory-drizzle adds durable Anvia memory to applications that use Drizzle with PostgreSQL. The package exports its table definitions so the application can include memory in the same schema and migration workflow as the rest of its data.
Install ​
pnpm add @anvia/memory-drizzle @anvia/core drizzle-ormThe package is ESM-only and should use a version compatible with its declared @anvia/core dependency range and drizzle-orm >=0.45.2 <1.0.0. Supply a PostgreSQL Drizzle database instance; the exported tables use drizzle-orm/pg-core.
Add the schema ​
import { drizzleMemorySchema } from '@anvia/memory-drizzle'
export const schema = {
...applicationSchema,
...drizzleMemorySchema,
}Generate and apply the resulting migration with your normal Drizzle tooling. The adapter does not run DDL or migrations.
Create the store ​
import { Agent } from '@anvia/core/agent'
import { DrizzleMemoryStore } from '@anvia/memory-drizzle'
const memory = new DrizzleMemoryStore({
db,
scopeKey: {
metadataKeys: ['tenantId'],
},
})
const agent = new Agent({
id: 'support',
model: model,
memory: { store: memory, savePolicy: 'turn' },
})The default schema uses agent_memory_sessions, agent_memory_messages, and agent_memory_errors, with unique indexes for scope keys and ordered message positions.
Use a custom schema mapping ​
Pass a schema object with the same three roles when your application aliases the exported tables:
const memory = new DrizzleMemoryStore({
db,
schema: {
agentMemorySessions,
agentMemoryMessages,
agentMemoryErrors,
},
})The runtime expects compatible Drizzle table shapes. Changing column semantics is not an adapter customization point.
Runtime behavior ​
- Appends require a database exposing
transaction; the constructor throwsTypeErrorwithout it. - The default
lock: 'advisory'acquires a PostgreSQL advisory lock and requiresexecute; the constructor throwsTypeErrorwhen it is missing. - Messages and failed runs are validated and stored by default.
- The store exposes read-only inspection and compaction interfaces.
- Default scope contains
sessionIdanduserId; metadata paths or a custom function can extend it.
lock: 'none' removes the advisory lock and its execute requirement, and should be paired with an application-level serialization strategy. The transaction requirement applies in every mode.
Production patterns ​
- Keep the memory tables in the application's checked-in Drizzle schema.
- Review generated migrations rather than creating tables at request time.
- Apply migrations before deploying code that expects a changed adapter schema.
- Add tenant metadata to the scope when session IDs are not globally unique.
- Treat scope as a storage key, not authorization.
Read Configure memory and Memory compaction for the SDK-side behavior.