Skip to content

Model.updateOne ​

Call User.updateOne(filter, changes) on an imported model. The filter must contain an equality condition for a field marked @id or @unique, and changes must contain at least one known model field. The @id field cannot be changed.

The update is parameterized and returns Promise<Result<User?, DbError>>; await it before using the Result. On success, it returns the updated User, or an absent value when no record matched. Database constraint and connection failures are recoverable DbError values. The unique filter requirement prevents the operation from matching multiple records.

This fragment assumes an imported User model with id marked @id and an email field.

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

async function updateUser(): Promise<Result<User?, DbError>> {
    const data = await User.updateOne({ id: 1 }, { email: "[email protected]" }) onfail {
        return Err(data);
    };
    return Ok(data);
}

Use Model.updateMany(filter, changes) when the filter may match multiple records.