Primitives.org.ai

get

Get entity by ID

Retrieve a single entity by its ID.

Import

import { DB } from 'ai-database'

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

Syntax

db.Type.get(id)
db.Type.get(id, options)

Examples

const post = await db.Post.get('hello-world')
// { $id: 'hello-world', title: 'Hello World', ... }

// Returns null if not found
const missing = await db.Post.get('does-not-exist')
// null

With Relationships

const post = await db.Post.get('hello-world')

// Access related entities
const author = await post.author  // resolves relationship
const tags = await post.tags      // resolves array relationship

Options

const post = await db.Post.get('hello-world', {
  include: ['author', 'tags'],  // eager load relationships
})
OptionTypeDescription
includestring[]Relationships to eager load

Type Safety

const { db } = DB({
  User: {
    name: 'string',
    email: 'string',
    age: 'number?',
  },
})

const user = await db.User.get('alice')

if (user) {
  user.name   // string
  user.email  // string
  user.age    // number | undefined
}

Entity Structure

Every entity has built-in fields:

FieldTypeDescription
$idstringUnique identifier
$typestringType name
createdAtDateCreation timestamp
updatedAtDateLast update timestamp
Was this page helpful?

On this page