Primitives.org.ai

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 names

Batch 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

MethodReturnsDescription
getDBPromise<T>Get by ID
listDBPromise<T[]>List all
findDBPromise<T[]>Filter by criteria
firstDBPromise<T>Get first match
searchDBPromise<T[]>Semantic search
createPromise<T>Create new
updatePromise<T>Update existing
deletePromise<boolean>Delete by ID

Chainable

MethodDescription
.filter()Filter results
.sort()Sort results
.limit()Limit count
.map()Transform with batch loading
.forEach()Process with concurrency

Schema

ExportDescription
DB()Create database
schemaDefine types
relationshipsDefine relations

Reactive

ExportDescription
eventsReal-time changes
actionsLong-running tasks

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-database

Configuration

DATABASE_URL=./content         # filesystem
DATABASE_URL=sqlite://./data   # SQLite
DATABASE_URL=:memory:          # in-memory
Was this page helpful?

On this page