Querying content

The CMS instance that is exported in your config file can be used to query the stored data.

import {cms} from '@/cms'

const homePage = await cms.get(HomePage())

Retrieving a page

A single page can be fetched using the get method. Criteria can be passed to filter entries.

// Fetch the first page where field equals the string 'value'
const page = await cms.get(Page({field: 'value'}))

Retrieving multiple pages

Multiple pages can be fetched using the find method.

// Fetch all pages where field equals the string 'value'
const pages = await cms.find(Page({field: 'value'}))

Limiting results

A result set can be limited using skip and take.

// Skip the first 10 pages and return a maximum of 10
const limited = await cms.find(Page().skip(10).take(10))

Querying specific pages

To filter pages on specific fields first narrow the search to a type, then use the where method to specify conditions.

const old = await cms.find(
  Animal().where(Animal.age.greater(10))
)
const teenager = await cms.find(Human().where(
   Human.age.greater(10).or(
     Human.age.less(20)
   )
)
const applesOrOranges = await cms.find(
  Fruit().where(Fruit.title.isIn(['apple', 'orange']))
)

Ordering results

A result set can be ordered by passing one or multiple fields.

const ordered = await cms.find(NewsItem().orderBy(NewsItem.publishDate.desc())

Group by

Results can be grouped by one or more fields.

const grouped = await cms.find(
  NewsItem().groupBy(NewsItem.category)
)

Selecting specific fields

Resulting rows can be narrowed to contain only specific fields.

// Return only titles
const rows = await cms.find(
  Page().select({title: Page.title})
)