Skip to content

Model.findOne ​

Call User.findOne(filter) on an imported model. It uses the same filter fields and operators as findMany, binds every value as a parameter, and checks the generated query against the local SQLite schema snapshot.

Its return type is Promise<Result<Optional<User, void>, DbError>>. Await the call to receive the Result, which carries database errors. On success, the inner Optional contains the matching record or is absent when no record matches. Narrow it with if (user) or provide a fallback with ??.

The query uses LIMIT 1. When the filter can match multiple rows, the selected row is not deterministic unless the filter identifies a unique row, such as one using @id or @unique.

This fragment assumes User is an imported @model interface with an email field.

tyrus
import { DbError } from "tyrus:db";
import { User } from "./models/User";

async function findUser(): Promise<Result<Optional<User, void>, DbError>> {
    const data = await User.findOne({ id: 1 }) onfail {
        return Err(data);
    };
    return Ok(data);
}

Use findMany when the result should be a list, including the empty-list case. Database failures remain recoverable DbError values inside the awaited Result.