jawj / zapatos

Zero-abstraction Postgres for TypeScript: a non-ORM database library
https://jawj.github.io/zapatos/
Other
1.3k stars 46 forks source link

invalid return type for db.sum() in lateral subquery #143

Open pocin opened 1 year ago

pocin commented 1 year ago

when using db.sum() shortcut in a subquery the return type should be number| null because the subquery might not match any row.

const resp = await db.select('order', db.all, {
   lateral: db.sum('order_lines', {order_id: db.parent('id')}, {columns: ['amount']})
  }).run(pool)

as is, the current code can crash at runtime with TypeError: Cannot read properties of null so i think it is a bug.

Not sure if zapatos should coalesce under the hood, or return number|null and user should coalesce on their own.