Querying content

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

    Content can be queried within React server components (and functions that run on the server such as generateStaticParams).

    import {cms} from '@/cms'
    
    export default async function HomePage() {
      const homePage = await cms.get(HomePage())
      return <h1>{homePage.title}</h1>
    }

    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))

    Pages can be queried with search terms. Any (rich) text field with the searchable option set to true is indexed.

    const results = await cms.find(
      Page().search('query', 'content')
    )

    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.isGreater(10))
    )
    const teenager = await cms.find(Human().where(
       Human.age.isGreater(10).or(
         Human.age.isLess(20)
       )
    )
    const applesOrOranges = await cms.find(
      Fruit().where(Fruit.title.isIn(['apple', 'orange']))
    )

    Conditions can be created by accessing the fields of the table instances and using the operators of Expr:

    BlogPost.title // This is an Expr<string> which has an `is` method to compare
    BlogPost.title.is("Test") // Compare with a value, results in an Expr<boolean>

    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})
    )