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')
// nullWith 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 relationshipOptions
const post = await db.Post.get('hello-world', {
include: ['author', 'tags'], // eager load relationships
})| Option | Type | Description |
|---|---|---|
include | string[] | 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:
| Field | Type | Description |
|---|---|---|
$id | string | Unique identifier |
$type | string | Type name |
createdAt | Date | Creation timestamp |
updatedAt | Date | Last update timestamp |
Was this page helpful?