Advanced

    Complex Relations

    When fetching data, you will eventually need information from a linked entry. Instead of performing multiple fetches, Alinea allows you to nest these requests within a single call.

    await cms.first({
      locale,
      url,
      type: ParentSchema,
      select: {
        ...ParentSchema,
        // Use filters from another schema to retrieve related data
        filters: ChildSchema.filters.find({
          select: {
            id: Query.id,
            title: Query.title,
            // Fetch multiple levels of nested relations in a single, optimized query
            filterItems: Query.children({
              select: {
              id: Query.id,
              title: Query.title
              }
            })
          }
        })
      }
    })

    Query Logic

    OR Logic

    By default, providing an array of values to a filter field or using the {in: [...]} operator will apply OR logic. This means the query will return any entry that matches at least one of the specified criteria. It is the most efficient way to broaden your search across multiple possible values, such as categories or tags.

    await cms.find({
      type: ChildSchema,
      filter: {
        category: {in: ['abc', 'def']}
      }
    })

    For more complex scenarios, such as combining filters across different fields use the explicit or operator. This allows you to group multiple distinct conditions where only one needs to be true for an entry to be included.

    await cms.find({
      type: BlogPostSchema,
      filter: {
        or: [
          {author: 'alinea'},
          {category: 'news'}
        ]
      }
    })

    AND Logic

    Use AND logic when an entry must strictly satisfy multiple conditions simultaneously. In Alinea, this is achieved by using the and operator, which requires every filter within its scope to be true for an entry to be included in the results. This is particularly useful for complex cross-referencing where a partial match is not enough.

    await cms.find({
      type: ChildSchema,
      filter: {
        // Use .map to create a filter condition for each ID
        and: ['abc', 'def'].map(id => ({
          filters: {includes: { _entry: id }}
        }))
      }
    })

    Alinea also provides a global search property. This allows you to perform broad queries across your content without targeting specific structural properties or nested filters.

    await cms.find({
      type: BlogPostSchema,
      search: 'querying',
    })
    
    //You can also pass an array into the search, it then works like a logical OR
    await cms.first({
      type: BlogPostSchema,
      search: ['cats', 'dogs'], // This will return all blogposts containing cats or dogs
    })