Filtering and sorting

Teo supports filtering and sorting with the where and orderBy query options respectively.

Filtering

Teo allows you to filter records on any combination of model fields, including related models, and supports a variety of filter conditions.

Filter conditions and operators

The following query filters all Artist records with id equals to 4 or name equals to "Angela Peterson".

const { data: artists } = await teo.artist.findMany({
  where: {
    OR: [
      {
        name: { equals: "Angela Peterson" },
      },
      {
        id: 4,
      }
    ],
  },
})
Hide HTTP response
[
  {
    "id": 4,
    "name": "Angela Peterson",
    "age": 23
  },
  {
    "id": 5,
    "name": "Tony Justin",
    "age": 26
  }
]

Filter on relations

The following query filters all Artist records who has at least one song with name starts with "F".

const { data: artists } = await teo.artist.findMany({
  where: {
    songs: {
      some: {
        name: { startsWith: 'F' }
      }
    }
  },
  include: {
    songs: true
  }
})
Hide HTTP response
[
  {
    "id": 4,
    "name": "Angela Peterson",
    "age": 23,
    "songs": [
      {
        "id": 1,
        "name": "Forever in Safe",
        "published": true
      },
      {
        "id": 2,
        "name": "Fairy Tale",
        "published": true
      }
    ]
  }
]

Sorting

Teo allows you to sort records on any combination of scalar model fields. In the future, sorting by relation will be supported.

Sort by a single field

The following query sorts artists by id.

const { data: artists } = await teo.artist.findMany({
  orderBy: {
    id: "desc"
  }
})
Hide HTTP response
[
  {
    "id": 5,
    "name": "Tony Justin",
    "age": 27
  },
  {
    "id": 4,
    "name": "Angela Peterson",
    "age": 23
  }
]

Sort by multiple fields

The following query sorts artists by age and name.

const { data: artists } = await teo.artist.findMany({
  orderBy: [
    {
      age: "desc"
    },
    {
      name: "asc"
    }
  ]
})
Hide HTTP response
[
  {
    "id": 5,
    "name": "Tony Justin",
    "age": 27
  },
  {
    "id": 4,
    "name": "Angela Peterson",
    "age": 23
  }
]