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

Using bigint value in a where clause #172

Closed mlms13 closed 5 months ago

mlms13 commented 5 months ago

I have a record ID of type bigint, and I would like to use it in the where clause of a Zapatos query, but I'm unable to use it directly:

async selectOneById(id: bigint): Promise<MyThing> {
  return await db
    .selectExactlyOne(
      'my_cool_table',
      { id: id },
    )
    .run(pool);
  }

This produces the type error:

error TS2322: Type 'bigint' is not assignable to type 'number | `${number}` | SQLFragment<any[], never> | ParentColumn<Column | undefined> | Parameter<number | `${number}`> | SQLFragment<any, number | ... 3 more ... | Parameter<...>> | undefined'.

I read through the docs, and I see examples of how to parse bigints out of the result of selecting from the database, but I don't see how to pass bigint values into Zapatos methods. chatgpt suggested doing:

`${Number(id)}`

But I'm pretty sure this would lead to precision loss and potentially query for the wrong ID.

The best I've come up with so far is basically doing a type conversation that lies about the type:

const bigIntToNumberTemplate = (value: bigint): `${number}` => `${value}` as `${number}`;

I'm wondering if I've missed any better options. Thanks!

jawj commented 5 months ago

I see this issue. I'll take a better look as soon as I can.

jawj commented 5 months ago

Should be available in 6.4.1. Let me know if not!