Skip to main content
Version: 3.0 Alpha

Find

The find series of APIs are used to query records from the database. It has the following methods:

  • findMany

    Find multiple records that match the query criteria.

  • findUnique

    Find a single record with a unique criteria.

  • findFirst

    Find the first record that matches the query criteria.

  • findUniqueOrThrow

    Similar to findUnique, but throws an error if no record is found.

  • findFirstOrThrow

    Similar to findFirst, but throws an error if no record is found.

Throughout this section all samples are based on the following ZModel schema:

zenstack/schema.zmodel
Loading...

Basic usage

Click here to pop out if the embed doesn't load an interactive terminal.

Filtering

The API provides a very flexible set of filtering options. We've put it into a dedicated section.

Sorting

Use the sort field to control the sort field, direction, and null field placement. Sorting is not supported for findUnique and findUniqueOrThrow.

Click here to pop out if the embed doesn't load an interactive terminal.

Pagination

You can use two strategies for pagination: offset-based or cursor-based. Pagination is not supported for findUnique and findUniqueOrThrow.

Click here to pop out if the embed doesn't load an interactive terminal.

Field selection

You can use the following fields to control what fields are returned in the result:

  • select

    An object specifying the fields to include in the result. Setting a field to true means to include it. If a field is a relation, you can provide an nested object to further specify which fields of the relation to include.

    This field is optional. If not provided, all non-relation fields are included by default. The include field is mutually exclusive with the select field.

  • include

    An object specifying the relations to include in the result. Setting a relation to true means to include it. You can pass an object to further choose what fields/relations are included for the relation, and/or a where clause to filter the included relation records.

    This field is optional. If not provided, no relations are included by default. The include field is mutually exclusive with the select field.

  • omit

    An object specifying the fields to omit from the result. Setting a field to true means to omit it. Only applicable to non-relation fields.

    This field is optional. If not provided, no fields are omitted by default. The omit field is mutually exclusive with the select field.

Click here to pop out if the embed doesn't load an interactive terminal.

Finding distinct rows

You can use the distinct field to find distinct rows based on specific fields. One row for each unique combination of the specified fields will be returned. The implementation uses SQL DISTINCT ON if it's supported by the dialect, otherwise falls back to in-memory deduplication.

Click here to pop out if the embed doesn't load an interactive terminal.
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below