Update
Use db.update() to modify existing rows in a table. The update builder provides type-safe column updates with filtering support.
Methods
Builder methods (UpdateBuilder)
| Method | Description |
|---|---|
.set() | Provide column values to update |
.from(...sources) | Add FROM sources (tables, CTEs, or values aliases) to update in |
Query methods (UpdateQuery)
| Method | Description |
|---|---|
.where(condition) | Filter which rows are updated |
.returning(cols) | Specify columns or aliased expressions to return after update (use "*" for all columns) |
Basic Usage
Update with WHERE
Always use .where() to specify which rows to update:
import { eq } from "durcno";
import { db } from "./db/index.ts";
import { Users } from "./db/schema.ts";
// Update a specific user's email
await db
.update(Users)
.set({ email: "newemail@example.com" })
.where(eq(Users.id, 1n));
Update Multiple Columns
Pass multiple columns to .set():
await db
.update(Users)
.set({
email: "updated@example.com",
type: "admin",
})
.where(eq(Users.id, 1n));
Without a .where() clause, the update will affect all rows in the table. Always include a filter unless you intend to update every row.
Partial Updates
All columns in .set() are optional—only specify the columns you want to change:
// Only update the email, leave other columns unchanged
await db
.update(Users)
.set({ email: "new@example.com" })
.where(eq(Users.id, 1n));
Complex Filters
Use filter operators for complex conditions:
import { and, eq, lte } from "durcno";
// Update all inactive users created before 2024
await db
.update(Users)
.set({ type: "user" })
.where(
and(eq(Users.type, "admin"), lte(Users.createdAt, new Date("2024-01-01"))),
);
Returning Updated Data
Use .returning() to get data back from updated rows:
// Return specific columns
const updated = await db
.update(Users)
.set({ email: "updated@example.com" })
.where(eq(Users.id, 1n))
.returning({ id: true, email: true });
// Type: { id: bigint; email: string | null }[]
// Return all columns except some
const updated = await db
.update(Users)
.set({ email: "updated@example.com" })
.where(eq(Users.id, 1n))
.returning({ createdAt: false });
// Type: { id: bigint; username: string; email: string | null; type: "admin" | "user" }[]
// Return all columns using the wildcard
const updated = await db
.update(Users)
.set({ email: "updated@example.com" })
.where(eq(Users.id, 1n))
.returning("*");
// Type: { id: bigint; username: string; email: string | null; type: "admin" | "user"; ... }[]
Aliased Returning
Use a direct object to rename output keys or project scalar expressions. Values may be columns of the updated table, scalar SqlFns, or raw sql fragments:
import { lower, sql } from "durcno";
const updated = await db
.update(Users)
.set({ email: "updated@example.com" })
.where(eq(Users.id, 1n))
.returning({
userId: Users.id,
name: Users.username,
loweredName: lower(Users.username),
emailLength: sql<number>`length(${Users.email})`,
});
// Type: { userId: bigint; name: string; loweredName: string; emailLength: number }[]
Aliased expressions cannot be mixed with boolean true/false flags in the same object. Aggregate and window expressions are rejected, matching PostgreSQL. Columns are qualified with the updated table, so aliased returning stays unambiguous when the statement has a FROM source.
Without Returning
Without .returning(), the update returns null:
const result = await db
.update(Users)
.set({ email: "updated@example.com" })
.where(eq(Users.id, 1n));
// Type: null
Auto-generated Values with .$updateFn()
Columns with .$updateFn() automatically generate values on every update, even if not explicitly included in .set():
// Schema with `.$updateFn()`
const Posts = table("public", "posts", {
id: pk(),
title: varchar({ length: 200, notNull }),
updatedAt: timestamp({ notNull }).$updateFn(() => new Date()),
});
// `updatedAt` is automatically set by `.$updateFn()`
await db.update(Posts).set({ title: "New Title" }).where(eq(Posts.id, 1n));
// updatedAt will be auto-generated
// You can still override with an explicit value
await db
.update(Posts)
.set({
title: "New Title",
updatedAt: new Date("2024-06-15"), // Override updateFn
})
.where(eq(Posts.id, 1n));
See Dynamic Value Generation for more details on .$insertFn() and .$updateFn().
Using SQL Expressions
Use sql() for raw SQL expressions in updates:
import { sql } from "durcno";
// Set createdAt to current timestamp
await db
.update(Users)
.set({ createdAt: sql`NOW()` })
.where(eq(Users.id, 1n));
Using Column References
Columns can be updated using values from other columns on the same table. Columns are referenced directly from the table definition:
// Copy values from another column
await db
.update(Users)
.set({ bio: Users.description })
.where(eq(Users.id, 1n));
Type Compatibility — TypeScript validates column value types at compile-time: you can only assign a column whose select type is assignable to the target column's update type. For instance, a nullable column (T | null) cannot be assigned to a notNull column (T).
Using Scalar SQL Functions
Durcno supports scalar SQL functions directly in .set(), including arithmetic, string, and conditional functions:
Arithmetic Functions
Increment or modify numeric columns using add(), sub(), mul(), div(), etc.:
import { add, sub } from "durcno";
// Increment user score by 10 and decrement user points by 5
await db
.update(Users)
.set({ score: add(Users.score, 10), points: sub(Users.points, 5) })
.where(eq(Users.id, 1n));
String Functions
Transform string columns using lower(), upper(), concat(), trim(), etc.:
import { concat, lower } from "durcno";
// Normalize username to lowercase and append suffix to bio
await db
.update(Users)
.set({
username: lower(Users.username),
bio: concat(Users.bio, " - verified"),
})
.where(eq(Users.id, 1n));
Conditional Functions
Use coalesce() to provide fallback values for nullable columns:
import { coalesce } from "durcno";
// Set bio to fallback if currently null
await db
.update(Users)
.set({ bio: coalesce(Users.bio, "No bio provided") })
.where(eq(Users.id, 1n));
Scalar Subqueries
A single-column SelectQuery can be assigned directly. The value is typed T | null, so use coalesce(...) for non-null columns:
import { coalesce, count, eq } from "durcno";
await db
.update(Users)
.set({
score: coalesce(
db
.from(Posts)
.select(() => ({ total: count("*") }))
.where(() => eq(Posts.userId, Users.id)),
0,
),
})
.where(eq(Users.id, 1n));
The subquery must select exactly one column. An aggregate inside the subquery is scoped to that subquery and is allowed; a bare aggregate in .set() remains rejected.
No Aggregate Functions — Aggregate functions (such as count(), sum(), or avg()) are not allowed directly in UPDATE SET and will produce a TypeScript compilation error. Wrapping the aggregate in a scalar subquery is allowed.
Bulk per-row Updates (UPDATE ... FROM)
Use .from() to update many rows to different values in one statement. A values alias is declared with db.values(name, columns) and its rows are bound with .as():
import { eq } from "durcno";
const V = db.values("v", {
cartItemId: CartItems.id,
orderItemId: CartItems.orderItemId,
}).as([
{ cartItemId: 1n, orderItemId: 100n },
{ cartItemId: 2n, orderItemId: 101n },
]);
await db
.update(CartItems)
.from(V)
.set({ orderItemId: V.orderItemId })
.where(eq(CartItems.id, V.cartItemId));
The values alias is a typed virtual table: each declared column wraps a source column (from any table, or a fresh column definition like bigint({ notNull })), so rows are type-checked per cell and values are encoded with the source column's SQL type. A column key names the alias column; reference it as V.key anywhere in .set() or .where().
.from()must be chained before.set()— mirroring how joins precede.select()— because the set payload type-checks against the sources' columns.- Repeated
.from()calls append sources (FROM a, b). undefinedcells renderNULL. An alias with no rows throws when the statement is built, since an emptyVALUESlist is not valid SQL..returning()returns rows of the updated table only.
Updating From a Table or CTE
Any real table or CTE can also be a FROM source, which turns the update into a join. This is useful for syncing a table from its source of truth:
await db
.update(UserProfiles)
.from(Users)
.set({ bio: Users.username })
.where(eq(UserProfiles.userId, Users.id));
A CTE joins by its instance after being attached with db.with(...):
import { eq, isNotNull } from "durcno";
const verifiedCte = db
.with("verified")
.as(
db
.from(Users)
.select(() => ({ id: Users.id, email: Users.email }))
.where(() => isNotNull(Users.isVerified)),
);
await db
.with(verifiedCte)
.update(UserProfiles)
.from(verifiedCte)
.set({ website: verifiedCte.email })
.where(eq(UserProfiles.userId, verifiedCte.id));
A FROM source is referenced by its own name — self-joining the updated table needs an SQL alias, which is not supported yet; use a values alias (or a CTE) for that shape.
Primary Key Restriction
Primary key columns cannot be updated and are strictly excluded from the set object type in TypeScript:
// ❌ TypeScript Error - primary key property 'id' is excluded from set()
await db
.update(Users)
.set({ id: 999 }) // Property 'id' does not exist in type ...
.where(eq(Users.id, 1n));
Partial Updates & Undefined Properties
Properties assigned undefined in .set({...}) are automatically ignored and omitted from the generated SET clause. This allows passing partial update objects directly from application handlers without manual field filtering:
import { type InferUpdate } from "durcno";
const patch: InferUpdate<typeof Users> = {
username: "new_name",
bio: undefined, // bio is omitted from the SQL SET clause and left unchanged in DB
};
await db
.update(Users)
.set(patch)
.where(eq(Users.id, 1n));
Empty Updates — If all properties in .set({...}) evaluate to undefined (or the object contains no columns to set), executing the query will throw an error:
// Throws: Error: "No columns to set in UPDATE query."
await db
.update(Users)
.set({ bio: undefined })
.where(eq(Users.id, 1n));
Method Chaining Order
Methods can be chained in any order:
// These are equivalent
await db
.update(Users)
.set({ email: "a@b.com" })
.where(eq(Users.id, 1n))
.returning({ id: true });
await db
.update(Users)
.set({ email: "a@b.com" })
.returning({ id: true })
.where(eq(Users.id, 1n));
Type Safety
Durcno ensures type safety for update values:
// ✅ Valid - correct types
await db
.update(Users)
.set({ email: "valid@example.com" })
.where(eq(Users.id, 1n));
// ❌ TypeScript Error - wrong type
await db
.update(Users)
.set({ email: 123 }) // email expects string
.where(eq(Users.id, 1n));
// ❌ TypeScript Error - invalid enum value
await db
.update(Users)
.set({ type: "superadmin" }) // Not in enum
.where(eq(Users.id, 1n));
Related
- Filters — Complete list of filter operators for
.where()conditions.