Single-record lookup
Retrieve one entity selected by a condition, such as a user identified by an ID.
Drizzle ORM · Module
findFirst() retrieves the first matching record through Drizzle ORM’s relational query API. It limits the query to one result and can load related data.
Use the relational query API on a table available through db.query. Calling findFirst builds a query constrained to one result; optional configuration can filter the table and include related records. The result is inferred from the selected table and can be absent when no row matches.
await db.query.<table>.findFirst(config?)| Name | Type | Description |
|---|---|---|
config | FindFirstConfig | Optional relational-query configuration, including filtering and relation-loading options. |
A promise resolving to the first matching table record, with an inferred object shape; no matching record may be returned.
findFirst alongside findMany for single-record and multi-record access.db.query followed by the table name.with option lets a first matching record be loaded together with related records such as comments.const user = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, 1),
});Fetches at most one user matching the condition; the relational query API returns the first matching record or no record.
Retrieve one entity selected by a condition, such as a user identified by an ID.
Fetch one parent record and selected related rows in one relational query.
Use schema-derived result types when reading one record from a table.
findFirst() do?findFirst() is the relational query API method for retrieving one record from a table. It adds limit 1 to the generated query, and its result is typed from the table schema.
Follow-up: How would you load related data at the same time? Pass a with configuration, such as { with: { comments: true } }.
Use db.query.<table>.findFirst(), for example db.query.users.findFirst(). You can pass a configuration object to add conditions or relations while retaining inferred result types.
Follow-up: How can you restrict the result to a particular row? Supply a where callback using a comparison helper such as eq.
findFirst() over findMany()?The method is useful when the application needs a single matching entity rather than a collection. The documentation demonstrates fetching a user and separately fetching a post together with its comments.
Follow-up: When should a collection method be preferred? Use findMany() when the application needs multiple matching records.
findManywithdb.queryrelations