01 Software

Query Builder

Supabase-style type-safe query builder

Query Builder

Access the per-collection query builder via the client.from(collection) method.

Client.from() returns a ReadOnlyQueryBuilder (read-only), while ServerClient.from() returns the full CollectionQueryBuilder (read + write).

Read Operations

find() - List Query

const { docs, totalDocs, hasNextPage } = await client.from('posts').find({
  limit: 10,
  page: 1,
  sort: '-createdAt',
  where: { _status: { equals: 'published' } },
  depth: 2,
  select: { title: true, slug: true },
})

Return type: PayloadFindResponse<T> — see Response Format for the full type definition.

findById() - Find by ID

const post = await client.from('posts').findById('post_id')
// Returns: T (document object directly)

count() - Document Count

const { totalDocs } = await client.from('posts').count({
  where: { _status: { equals: 'published' } }
})

Write Operations

Write operations are only available on ServerClient.

create()

const { doc, message } = await serverClient.from('posts').create({
  title: 'New Post',
  _status: 'draft',
})
// Returns: PayloadMutationResponse<T> { doc, message, errors? }

update()

const { doc } = await serverClient.from('posts').update('post_id', {
  title: 'Updated Post',
})

remove()

const deleted = await serverClient.from('posts').remove('post_id')
// Returns: T (deleted document)

updateMany() / removeMany()

await serverClient.from('posts').updateMany(
  { _status: { equals: 'draft' } },   // where
  { _status: 'published' },            // data
)

await serverClient.from('posts').removeMany(
  { _status: { equals: 'draft' } }, // where
)

Query Options

OptionTypeDescription
pagenumberPage number (default: 1)
limitnumberItems per page (default: 10)
sortstringSort order (-createdAt = descending)
whereWhereFilter conditions (see Filtering)
depthnumberRelationship query depth (0-10, default: 1)
selectRecord<string, boolean>Field selection
populateRecord<string, boolean | Record<string, boolean>>Per-collection field selection for populated relationships
joinsRecord<string, object | false> | falseJoin field control (pagination, filter, or disable)

Metadata Query

Generate Next.js metadata from collection documents. See the Metadata docs for details.

const metadata = await client.from('posts').findMetadata(
  { where: { slug: { equals: 'my-post' } } },
  { siteName: 'My Site' },
)

const metadata = await client.from('posts').findMetadataById('post_id')

Response Type Summary

OperationResponse
find()PayloadFindResponse<T> - { docs, totalDocs, hasNextPage, ... }
findById()T
findMetadata()Metadata | null
findMetadataById()Metadata
create()PayloadMutationResponse<T> - { doc, message }
update()PayloadMutationResponse<T> - { doc, message }
remove()T
count(){ totalDocs: number }

On this page