Fixing Missing Returning Clauses in Kysely Queries with Biome and Grit

In my previous post, I showed how to use Biome and Grit to catch common mistakes in Kysely queries, such as missing select clauses or where clauses. Another common issue is forgetting to add a returning clause to insert, update, or delete queries, which can lead to unexpected results when the query is executed with .executeTakeFirstOrThrow().

I've seen many issues in the Kysely repository where users report that their update or delete queries are not returning the expected data because they forgot to add a returning clause. This can be especially problematic when using .executeTakeFirstOrThrow(), which will fail silently if no rows are returned.

Issues

My solution is to write a Biome plugin that detects when an insert, update, or delete query is executed with .executeTakeFirstOrThrow() but does not contain a returning clause.

language js(typescript)

`$chain.$exec()` where {
 $exec <: `executeTakeFirstOrThrow`,
 or {
  $chain <: contains `deleteFrom`,
  $chain <: contains `updateTable`,
  $chain <: contains `insertInto`
 },
 ! or { $chain <: contains `returning`, $chain <: contains `returningAll` },
 register_diagnostic(span=$chain, message="insert, update or delete statements must return a value on executeTakeFirstOrThrow", severity="error")
}

view in repo