Skip to main content

TypeORMMemory

Flexible ORM-based memory storage supporting multiple database types with TypeORM integration.

Import

import { TypeORMMemory } from '@ag-kit/agents/storage';
import { DataSource } from 'typeorm';

Constructor

constructor(config: TypeORMMemoryConfig)

Configuration

config
TypeORMMemoryConfig
required
Configuration for TypeORM memory storage extending BaseMemory options

Database Setup

PostgreSQL Example

import { DataSource } from 'typeorm';
import { TypeORMMemory } from '@ag-kit/agents/storage';

const dataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'password',
  database: 'agkit_memory',
  entities: [MemoryEventEntity], // AG-Kit provides the entity
  synchronize: true, // Auto-create tables (dev only)
  logging: false
});

await dataSource.initialize();

const memory = new TypeORMMemory({
  dataSource,
  sessionId: 'user-session-123',
  enableContextManagement: true
});

MySQL Example

const dataSource = new DataSource({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'password',
  database: 'agkit_memory',
  entities: [MemoryEventEntity],
  synchronize: true
});

await dataSource.initialize();
const memory = new TypeORMMemory({ dataSource });

SQLite Example

const dataSource = new DataSource({
  type: 'sqlite',
  database: './memory.sqlite',
  entities: [MemoryEventEntity],
  synchronize: true
});

await dataSource.initialize();
const memory = new TypeORMMemory({ dataSource });

Basic Usage

// Initialize with database connection
const memory = new TypeORMMemory({
  dataSource: await dataSource.initialize(),
  sessionId: 'user-session-123',
  eventTableName: 'conversation_events',
  enableContextManagement: true
});

// Add and retrieve events
await memory.add({
  message: {
    id: 'msg-1',
    role: 'user',
    content: 'Hello, I need help with my project',
    timestamp: new Date()
  },
  state: { userId: 'user-123' }
});

const events = await memory.list({ limit: 10, maxTokens: 4000 });

Core Interface Methods

TypeORMMemory inherits from BaseMemory and implements all standard memory interface methods with full branching support. For complete API documentation including list(), add(), addList(), delete(), retrieve(), clear(), getCount(), isEmpty(), and all branching methods, see the BaseMemory API Reference.

Unique Features

  • Multi-Database Support: PostgreSQL, MySQL, SQLite, SQL Server, Oracle, and more
  • TypeORM Integration: Full TypeORM query capabilities and entity management
  • Custom Table Names: Configurable table names for events, state, and summaries
  • Transaction Support: ACID compliance for data integrity
  • Migration Support: Database schema versioning and migrations
  • Auto-Initialization: Automatic table creation with synchronize option

Supported Databases

  • MySQL: Full support with optimizations
  • PostgreSQL: Full support with JSON operations
  • SQLite: Full support for development and testing
  • SQL Server: Full support for enterprise environments
  • Oracle: Full support for enterprise environments
  • MariaDB: Full support as MySQL alternative
  • CockroachDB: Full support for distributed deployments

Limitations

  • Setup Complexity: Requires database configuration and management
  • Database Dependency: Needs external database server
  • Migration Management: Schema changes require careful migration planning
  • Connection Management: Must handle database connections and pooling