Insert​

Insert new data with e.insert:

  1. const runtime = new edgedb.Duration(0,0,0,0,2,28);
  2. e.insert(e.Movie, {
  3. title: e.str("Spider-Man: No Way Home"),
  4. runtime: e.duration(runtime),
  5. });

For convenience, the second argument e.insert function can also accept plain JS data.

  1. const runtime = new edgedb.Duration(0,0,0,0,2,28);
  2. e.insert(e.Movie, {
  3. title: "Spider-Man: No Way Home",
  4. runtime: runtime,
  5. });

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.

  1. const runtime = new edgedb.Duration(0,0,0,0,2,28);
  2. e.insert(e.Movie, {
  3. title: "Spider-Man: No Way Home",
  4. runtime: runtime
  5. }).unlessConflict();

To specify an on clause:

  1. const runtime = new edgedb.Duration(0,0,0,0,2,28);
  2. e.insert(e.Movie, {
  3. title: "Spider-Man: No Way Home",
  4. runtime: runtime
  5. }).unlessConflict(movie => ({
  6. on: movie.title, // can be any expression
  7. }));

To specify an on...else clause:

  1. const runtime = new edgedb.Duration(0,0,0,0,2,28);
  2. e.insert(e.Movie, {
  3. title: "Spider-Man: Homecoming",
  4. runtime: runtime
  5. }).unlessConflict(movie => ({
  6. on: movie.title,
  7. else: e.update(movie, () => ({
  8. set: {
  9. runtime: runtime
  10. }
  11. })),
  12. }));