Skip to main content

Update

Set

.set specifies the fields to update in a given query in the form of { field: value }. If you pass multiple objects they will be merged into one.

const { db } = useEasybase();

await db('MOVIES').set({ title: "Pulp Fiction" }).one()
> 1

// { "title": "Pulp Fiction", "rating": 83 },

Using .all applies the same query logic as it does with Select and Delete.

await db('MOVIES').set({ title: "Pulp Fiction" }).all()
> 4

// [
// { "title": "Pulp Fiction", "rating": 83 },
// { "title": "Pulp Fiction", "rating": 75 },
// { "title": "Pulp Fiction", "rating": 55 },
// { "title": "Pulp Fiction", "rating": 59 }
// ]

Where

tip

Note that .where works in update queries just as it does in select queries.

Filter the rows to update with .where.

In this example, all records that are either titled The Lion King or have a rating greater than 80 will be set to have a rating of -1.

await db('MOVIES').where(
e.or(
e.eq("title", "The Lion King"), // Equals
e.gt("rating", 80) // Greater than
)
).set({ rating: -1 }).all()
> 2

// [
// { "title": "Avatar", "rating": -1 },
// { "title": "The Lion King", "rating": -1 }
// ]

Update specific record with .where.

// Select returns a unique identifier called _key
let singleRecord = db('MOVIES').return().where(e.like('title', "Jurassic World")).one()

await db('MOVIES').where({ _key: singleRecord._key }).set({ title: "Pulp Fiction" }).one()
> 1

// { "title": "Pulp Fiction", "rating": 59 }