Primitives.org.ai

DB()

Create a database instance with schema

Create a database instance with typed schemas. Returns db and all primitive exports.

Namespace IS Context

Every database is scoped by a Domain namespace - and that namespace IS the $context. The database doesn't just store data; it stores data WITH meaning:

// The namespace provides context for everything in this database
const { db } = DB({
  Customer: { name: 'string', tier: 'free | pro | enterprise' },
  Order: { customer: 'Customer', total: 'number' },
}, { namespace: 'acme.do' })

// AI understands these aren't just tables -
// they're YOUR Customer, YOUR Order, in YOUR context

This is why AI can reason about your data: the namespace provides both semantic context (JSON-LD $context) and cognitive context (how AI should think about your domain).

Import

import { DB } from 'ai-database'

Syntax

const { db, nouns, verbs, events, actions, artifacts } = DB(schema, options?)

Basic Usage

const { db } = DB({
  Post: { title: 'string', content: 'markdown', author: 'Author.posts' },
  Author: { name: 'string' },
})

await db.Post.create({ title: 'Hello World' })
await db.Author.create({ name: 'Alice' })

Full Destructuring

const { db, nouns, verbs, events, actions, artifacts } = DB({
  Post: { title: 'string', author: 'Author.posts' },
  Author: { name: 'string' },
})

// db - CRUD operations
await db.Post.create({ title: 'Hello' })
await db.Post.list()

// nouns - type definitions
const postNoun = await nouns.get('Post')

// verbs - action definitions
const publishVerb = verbs.get('publish')

// events - event log
events.on('Post.created', handler)

// actions - durable execution
const action = await actions.start('generatePosts', { count: 100 })

// artifacts - embeddings and cached content
const embedding = await artifacts.get(postId, 'embedding')

Schema Types

TypeDescription
'string'Text field
'number'Numeric field
'boolean'True/false
'markdown'Markdown text
'json'JSON object
'date'Date only
'datetime'Date and time
'url'URL string

Modifiers

ModifierExampleDescription
?'string?'Optional field
[]'string[]'Array
.backref'Author.posts'Relation with backref
[.backref]['Tag.posts']Many-to-many relation

Options

const { db } = DB(schema, {
  url: 'sqlite://./data.db',  // storage backend
  embeddings: {
    model: 'text-embedding-3-large',
    dimensions: 3072,
  },
})
OptionTypeDescription
urlstringDatabase URL
embeddingsobjectEmbedding configuration
embeddings.modelstringEmbedding model
embeddings.dimensionsnumberVector dimensions

Environment Variables

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

On this page