Database
Your data, flowing like conversation
Chain operations. Batch relationships. Ask questions.
import { DB } from 'ai-database'
const { db } = DB({
Lead: { name: 'string', company: 'Company.leads' },
Company: { name: 'string' }
})
// Chain without await
const leads = db.Lead.list()
const qualified = await leads.filter(l => l.score > 80)
// Batch relationship loading
const enriched = await leads.map(lead => ({
name: lead.name,
company: lead.company, // Batch loaded!
}))Promise Pipelining
Chain database operations without await:
const leads = db.Lead.list()
const topLeads = leads.filter(l => l.score > 80)
const names = topLeads.map(l => l.name)
// Only await when you need the result
const result = await namesBatch Relationship Loading
Eliminate N+1 queries with .map():
const enriched = await db.Lead.list().map(lead => ({
lead,
company: lead.company, // All loaded in ONE query
orders: lead.orders, // Same here!
}))Natural Language
Ask your database questions:
const results = await db.Lead`who closed deals this month?`
const pending = await db.Order`what's stuck in processing?`All Operations
CRUD
| Method | Returns | Description |
|---|---|---|
get | DBPromise<T> | Get by ID |
list | DBPromise<T[]> | List all |
find | DBPromise<T[]> | Filter by criteria |
first | DBPromise<T> | Get first match |
search | DBPromise<T[]> | Semantic search |
create | Promise<T> | Create new |
update | Promise<T> | Update existing |
delete | Promise<boolean> | Delete by ID |
Chainable
| Method | Description |
|---|---|
.filter() | Filter results |
.sort() | Sort results |
.limit() | Limit count |
.map() | Transform with batch loading |
.forEach() | Process with concurrency |
Schema
| Export | Description |
|---|---|
DB() | Create database |
schema | Define types |
relationships | Define relations |
Reactive
Examples
Sales Pipeline
const { db } = DB({
Lead: {
name: 'string',
score: 'number',
company: 'Company.leads',
},
Company: { name: 'string', industry: 'string' }
})
// High-value leads with companies
const qualified = await db.Lead.list()
.filter(l => l.score > 80)
.map(l => ({ lead: l, company: l.company }))
// Ask naturally
const stale = await db.Lead`who hasn't responded in 2 weeks?`Customer Success
const { db } = DB({
Customer: {
name: 'string',
healthScore: 'number',
csm: 'User.customers',
},
User: { name: 'string' }
})
// At-risk with their CSMs
const atRisk = await db.Customer.list()
.filter(c => c.healthScore < 50)
.map(c => ({ customer: c, csm: c.csm }))Large-Scale Processing
// Process with AI - 10 at a time
const result = await db.Lead.forEach(async lead => {
const analysis = await ai`analyze ${lead}`
await db.Lead.update(lead.$id, { analysis })
}, {
concurrency: 10,
onProgress: p => console.log(`${p.completed}/${p.total}`),
onError: 'continue',
})
console.log(`Done: ${result.completed}, Failed: ${result.failed}`)Installation
npm install ai-databaseConfiguration
DATABASE_URL=./content # filesystem
DATABASE_URL=sqlite://./data # SQLite
DATABASE_URL=:memory: # in-memoryWas this page helpful?