Update
Update objects with the e.update
function.
Copy
e.update(e.Movie, () => ({
filter_single: { title: "Avengers 4" },
set: {
title: "Avengers: Endgame"
}
}))
You can reference the current value of the object’s properties.
Copy
e.update(e.Movie, (movie) => ({
filter: e.op(movie.title[0], '=', ' '),
set: {
title: e.str_trim(movie.title)
}
}))
You can conditionally update a property by using an optional parameter and the coalescing infix operator.
Copy
e.params({ id: e.uuid, title: e.optional(e.str) }, (params) =>
e.update(e.Movie, (movie) => ({
filter_single: { id: params.id },
set: {
title: e.op(params.title, "??", movie.title),
}
}))
);
Note that e.update
will return just the { id: true }
of the updated object. If you want to select further properties, you can wrap the update in a e.select
call. This is still just a single query to the database.
Copy
e.params({ id: e.uuid, title: e.optional(e.str) }, (params) => {
const updated = e.update(e.Movie, (movie) => ({
filter_single: { id: params.id },
set: {
title: e.op(params.title, "??", movie.title),
},
}));
return e.select(updated, (movie) => ({
title: movie.title,
}));
});
Updating links
EdgeQL supports some convenient syntax for appending to, subtracting from, and overwriting links.
Copy
update Movie set {
# overwrite
actors := Person,
# add to link
actors += Person,
# subtract from link
actors -= Person
}
In the query builder this is represented with the following syntax.
Overwrite a link
Copy
const actors = e.select(e.Person, ...);
e.update(e.Movie, movie => ({
filter_single: {title: 'The Eternals'},
set: {
actors: actors,
}
}))
Add to a link
Copy
const actors = e.select(e.Person, ...);
e.update(e.Movie, movie => ({
filter_single: {title: 'The Eternals'},
set: {
actors: { "+=": actors },
}
}))
Subtract from a link
Copy
const actors = e.select(e.Person, ...);
e.update(e.Movie, movie => ({
filter_single: {title: 'The Eternals'},
set: {
actors: { "-=": actors },
}
}))
Bulk updates
You can use a for loop to perform bulk updates.