Insert
Insert new data with e.insert
:
const runtime = new edgedb.Duration(0,0,0,0,2,28);
e.insert(e.Movie, {
title: e.str("Spider-Man: No Way Home"),
runtime: e.duration(runtime),
});
For convenience, the second argument e.insert
function can also accept plain JS data.
const runtime = new edgedb.Duration(0,0,0,0,2,28);
e.insert(e.Movie, {
title: "Spider-Man: No Way Home",
runtime: runtime,
});
Handling conflicts
In EdgeQL, “upsert” functionality is achieved by handling conflicts on insert
statements with the unless conflict
clause. In the query builder, this is possible with the .unlessConflict
method (available only on insert
expressions).
In the simplest case, adding .unlessConflict
(no arguments) will prevent EdgeDB from throwing an error if the insertion would violate an exclusivity contstraint. Instead, the query would return the pre-existing object.
const runtime = new edgedb.Duration(0,0,0,0,2,28);
e.insert(e.Movie, {
title: "Spider-Man: No Way Home",
runtime: runtime
}).unlessConflict();
To specify an on
clause:
const runtime = new edgedb.Duration(0,0,0,0,2,28);
e.insert(e.Movie, {
title: "Spider-Man: No Way Home",
runtime: runtime
}).unlessConflict(movie => ({
on: movie.title, // can be any expression
}));
To specify an on...else
clause:
const runtime = new edgedb.Duration(0,0,0,0,2,28);
e.insert(e.Movie, {
title: "Spider-Man: Homecoming",
runtime: runtime
}).unlessConflict(movie => ({
on: movie.title,
else: e.update(movie, () => ({
set: {
runtime: runtime
}
})),
}));