Primitives.org.ai

list

List all entities of a type

Retrieve all entities of a type with optional filtering and pagination.

Import

import { DB } from 'ai-database'

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

Syntax

db.Type.list()
db.Type.list(options)

Examples

// List all
const posts = await db.Post.list()

// With pagination
const posts = await db.Post.list({
  limit: 10,
  offset: 20,
})

// With sorting
const recent = await db.Post.list({
  orderBy: 'createdAt',
  order: 'desc',
})

// With filtering
const published = await db.Post.list({
  where: { published: true },
})

Options

OptionTypeDescription
whereobjectFilter criteria
orderBystringField to sort by
order'asc' | 'desc'Sort direction
limitnumberMax results
offsetnumberSkip results
includestring[]Relationships to eager load

Filtering

const posts = await db.Post.list({
  where: {
    published: true,
    createdAt: { $gte: lastWeek },
  },
})

Pagination

// First page
const page1 = await db.Post.list({ limit: 10, offset: 0 })

// Second page
const page2 = await db.Post.list({ limit: 10, offset: 10 })

Sorting

// Newest first
const recent = await db.Post.list({
  orderBy: 'createdAt',
  order: 'desc',
})

// Alphabetical
const alphabetical = await db.Post.list({
  orderBy: 'title',
  order: 'asc',
})

Return Type

Returns an array of entities:

const posts = await db.Post.list()
// Post[]

posts.forEach(post => {
  console.log(post.$id, post.title)
})
Was this page helpful?

On this page