kysely-review

Review TypeScript code that builds SQL with Kysely for correctness, safety, transactions, and performance, and report findings with severity and file locations. Use when asked to review database query

SKILL.md

GitHub

Kysely review

Report findings without modifying code unless the user asks for fixes.

1. Understand the schema and conventions

  • Find the Database interface (often generated by kysely-codegen or prisma-kysely), the Kysely instance, dialect, and plugins such as CamelCasePlugin.
  • Note which columns are nullable, generated, or defaulted (Generated<>, ColumnType<>), and how the project handles transactions, pagination, and errors.
  • Read migrations or schema files for indexes and constraints when performance or uniqueness matters.

2. Check each query

Correctness

  • updateTable and deleteFrom without a where affect every row. Flag them unless that is clearly intended.
  • executeTakeFirst() returns undefined when nothing matches. Flag non-null assertions or unchecked property access on its result; suggest executeTakeFirstOrThrow() or an explicit check.
  • Left and full joins make the joined columns nullable; check the result handling. selectAll() on a join can silently overwrite columns with the same name; prefer explicit selections.
  • Pagination needs a deterministic orderBy. Upserts need the right onConflict target. Inserts that rely on returning need a dialect that supports it.

Safety

  • Values passed through sql template substitutions are parameterized. sql.raw, sql.lit, sql.ref, sql.id, sql.table, and dynamic ref() or column names built from user input are not: flag injection and require an allowlist.
  • Check authorization-relevant filters (tenant or owner IDs) are present on every query that needs them.

Transactions

  • Inside db.transaction().execute(async (trx) => ...), every query must use trx, not db; otherwise it runs outside the transaction.
  • Every query must be awaited before the callback returns. Avoid slow external calls while a transaction holds locks.

Performance

  • Queries in loops (N+1) should be batched with where(col, "in", ids), joins, or jsonArrayFrom/jsonObjectFrom helpers.
  • Select only needed columns, check that filters and sort columns are indexed, and use batched inserts for many rows.

Types

  • Flag as any, casts on query results, and hand-written row types that duplicate Selectable, Insertable, or Updateable of the Database interface.

3. Verify and report

Confirm each finding by reading the query, the schema types, and its callers; run the project's type check or tests when available. Report findings ordered by severity (critical, high, medium, low) with file:line, the problem, a concrete scenario, and a fix. If there are no findings, say so and list what was checked.

Examples

  • Use: "Review the queries in src/orders.ts." "Check this repository class for Kysely mistakes."
  • Skip: "Write a migration that adds a status column." "Review this Prisma query." "Add an endpoint that lists orders."