Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

update

Update existing records in your database

update modifies a single record matched by a unique identifier and returns it. It throws if the record doesn’t exist.

const user = await client.user.update({
  where: { id: "user_123" },
  data: { name: "Alice Smith" },
});

Conditional Updates

The where may narrow the unique lookup with ordinary non-unique scalar filters and AND / OR / NOT — a compare-and-set, in one statement:

// bump the version only if it is still the one we read
await client.document.update({
  where: { id: "doc_1", version: 7 },
  data: { body, version: { increment: 1 } },
});

If the unique key matches but the filter excludes the row, the update is a NotFoundError and nothing is written — not a silent no-op. See Narrowing a Unique Lookup for the full where shape and its two divergences from Prisma.

Scalar Operations

// Set value
await client.post.update({
  where: { id: "post_123" },
  data: { title: "New Title" },
});

// Increment/decrement numbers
await client.post.update({
  where: { id: "post_123" },
  data: {
    views: { increment: 1 },
    likes: { decrement: 1 },
  },
});

// Multiply/divide
await client.product.update({
  where: { id: "product_123" },
  data: {
    price: { multiply: 1.1 },  // 10% increase
  },
});

// Array operations
await client.post.update({
  where: { id: "post_123" },
  data: {
    tags: { push: "new-tag" },
  },
});

With Relations

From an update you can create, connect, connectOrCreate, disconnect, delete, set, update, updateMany, upsert, and deleteMany related records:

await client.user.update({
  where: { id: "user_123" },
  data: {
    posts: {
      create: { title: "New Post" },
    },
  },
});

See Nested writes for each operation.

Options

await client.user.update({
  where: { ... },       // Required: unique identifier
  data: { ... },        // Required: fields to update
  select: { ... },      // Optional: fields to return
  include: { ... },     // Optional: relations to include
});

select and include control the returned shape — see Selecting Fields.

Examples

Update Profile

This uses to-one nested update; it throws if no correlated profile exists.

async function updateProfile(userId: string, data: { name?: string; bio?: string }) {
  return client.user.update({
    where: { id: userId },
    data: {
      name: data.name,
      profile: {
        update: { bio: data.bio },
      },
    },
    include: { profile: true },
  });
}

Increment View Count

async function recordView(postId: string) {
  return client.post.update({
    where: { id: postId },
    data: {
      views: { increment: 1 },
      lastViewedAt: new Date(),
    },
  });
}

Was this page helpful?