update
Update existing records in a database table using the query builder.
Import
import { update } from '@evershop/postgres-query-builder';
Syntax
update(table: string): UpdateQuery
Parameters
table
Type: string
The name of the table to update.
Return Value
Returns an UpdateQuery instance that can be chained with additional methods.
Examples
Basic Update
import { update } from '@evershop/postgres-query-builder';
import { pool } from '@evershop/evershop/lib/postgres';
const result = await update('customer')
.given({
full_name: 'Jane Doe',
status: 1
})
.where('customer_id', '=', 123)
.execute(pool);
console.log(result.updatedId); // The ID of the updated record
console.log(result); // The full updated row
Update with WHERE Conditions
import { update } from '@evershop/postgres-query-builder';
import { pool } from '@evershop/evershop/lib/postgres';
const result = await update('product')
.given({
status: 0,
updated_at: new Date()
})
.where('qty', '=', 0)
.and('status', '=', 1)
.execute(pool);
Update with prime()
import { update } from '@evershop/postgres-query-builder';
import { pool } from '@evershop/evershop/lib/postgres';
const result = await update('product')
.given({ name: 'Updated Product Name' })
.prime('price', 149.99)
.prime('updated_at', new Date())
.where('product_id', '=', 456)
.execute(pool);
Update in Transaction
import { update, startTransaction, commit, rollback } from '@evershop/postgres-query-builder';
import { getConnection } from '@evershop/evershop/lib/postgres';
const connection = await getConnection();
try {
await startTransaction(connection);
// Update inventory
await update('product')
.given({ qty: 5 })
.where('product_id', '=', 123)
.execute(connection, false);
// Update order status
await update('order')
.given({ status: 'completed' })
.where('order_id', '=', 789)
.execute(connection, false);
await commit(connection);
} catch (error) {
await rollback(connection);
throw error;
}
Conditional Update
import { update } from '@evershop/postgres-query-builder';
import { pool } from '@evershop/evershop/lib/postgres';
const result = await update('customer')
.given({
status: 0,
deactivated_at: new Date()
})
.where('last_login', '<', new Date('2024-01-01'))
.and('status', '=', 1)
.execute(pool);
Update with Dedicated Connection
import { update } from '@evershop/postgres-query-builder';
import { getConnection } from '@evershop/evershop/lib/postgres';
const connection = await getConnection();
try {
const result = await update('order')
.given({
status: 'shipped',
shipped_at: new Date()
})
.where('order_id', '=', 123)
.execute(connection);
console.log(result.updatedId);
} finally {
connection.release();
}
Methods
given(data)
Provide data to update as an object.
Parameters:
data- Object containing column names as keys and new values
Returns: UpdateQuery
update('product').given({
name: 'Updated Name',
price: 149.99,
status: 1
})
prime(field, value)
Set a single field value. Can be called multiple times.
Parameters:
field- Column namevalue- New value
Returns: UpdateQuery
update('product')
.given({ name: 'Updated Name' })
.prime('price', 149.99)
.prime('updated_at', new Date())
where(field, operator, value)
Add a WHERE condition. Required for update queries.
Parameters:
field- Column nameoperator- Comparison operator (e.g.,=,>,<,!=)value- Value to compare
Returns: Where
update('customer')
.given({ status: 0 })
.where('customer_id', '=', 123)
and(field, operator, value)
Add an AND condition to the WHERE clause.
Parameters:
field- Column nameoperator- Comparison operatorvalue- Value to compare
Returns: Node
update('product')
.given({ status: 0 })
.where('qty', '=', 0)
.and('status', '=', 1)
execute(connection, releaseConnection?)
Execute the update query.
Parameters:
connection-PoolorPoolClientinstancereleaseConnection- Whether to release the connection after execution (default:true)
Returns: Promise<any> - The updated row with an updatedId property
const result = await update('product')
.given({ status: 1 })
.where('product_id', '=', 123)
.execute(pool);
Return Value Details
The execute() method returns the updated row with all fields, plus an updatedId property:
const result = await update('customer')
.given({ full_name: 'Jane Doe' })
.where('customer_id', '=', 123)
.execute(pool);
// result contains:
// {
// customer_id: 123,
// email: 'jane@example.com',
// full_name: 'Jane Doe',
// updated_at: '2025-11-08T10:30:00.000Z',
// updatedId: 123 // Same as the primary key value
// }
See Also
- insert - Insert records
- select - Select records
- del - Delete records
- pool - Database connection pool
- getConnection - Get database connection