Primitives.org.ai

schema

Define schemas with types and relationships

Define database schemas with types, relationships, and AI generation hints.

Import

import { DB } from 'ai-database'

Basic Schema

const { db } = DB({
  Post: {
    title: 'string',
    content: 'markdown',
    author: 'Author.posts',  // bi-directional relation
  },
  Author: {
    name: 'string',
    // posts: Post[] auto-created from backref
  },
})

Field Types

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

Modifiers

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

AI Generation Hints

Schema descriptions guide AI generation:

const { db } = DB({
  Post: {
    title: 'Engaging blog post title',
    content: 'Full markdown article (1500-2500 words)',
    summary: '2-3 sentence summary',
    tags: ['3-5 relevant tags'],
  },
})

// AI uses descriptions for generation
await db.generate({ type: 'Post', data: { topic: 'AI' } })

Relationships

One-to-Many

Post: {
  author: 'Author.posts',  // Post has one Author
}
// Creates: Author.posts (Post[])

Many-to-Many

Post: {
  tags: ['Tag.posts'],  // Post has many Tags
}
// Creates: Tag.posts (Post[])

One-to-One

User: {
  profile: 'Profile.user',  // User has one Profile
}
// Creates: Profile.user (User)

Advanced Schema

import { defineNoun, nounToSchema } from 'ai-database'

const Post = defineNoun({
  singular: 'post',
  plural: 'posts',
  description: 'A blog post or article',

  properties: {
    title: { type: 'string', description: 'The post title' },
    content: { type: 'markdown', description: 'The post body' },
    status: { type: 'string', optional: true },
  },

  relationships: {
    author: { type: 'Author', backref: 'posts' },
    tags: { type: 'Tag[]', backref: 'posts' },
  },

  actions: ['create', 'update', 'delete', 'publish'],
  events: ['created', 'updated', 'deleted', 'published'],
})

const { db } = DB({
  Post: nounToSchema(Post),
})

Self-Describing

Schemas are stored and queryable:

const { db, nouns } = DB({
  Post: { title: 'string' },
})

// Query types at runtime
const types = await nouns.list()
const postNoun = await nouns.get('Post')
postNoun.singular  // 'post'
postNoun.plural    // 'posts'

System Types

TypeDescription
ThingBase type—every entity is a Thing
NounType definitions
VerbAction definitions
EdgeRelationships between types
Was this page helpful?

On this page