select with sql<number> returns string
Hi everyone!
I am new to drizzle and i have been trying to convert one of my sql queries to it
this is what i have been doing on the ts side
the type of the const res does match the type i want as output but the actual data i get from it gives me string values for all the amounts.
I am really confused on why this happens.
5 Replies
typing and runtime output does not match
Drizzle does not (and can't) enforce sql<T> typing. It's just helps inference on the field. You need to use sql``.mapWith()
https://orm.drizzle.team/docs/sql#sqlmapwith
The issue is (probably) coming from the database driver converting the result of sql SUM() to a string to avoid losing precision which is common practice.
thanks for the reply 🙏
i am looking into sql mapWith
https://orm.drizzle.team/docs/sql#sql-select
and trying to do the same as this
But i dont have access to the mapWith function in my code.
I am on drizzle "drizzle-orm": "^0.44.2"
is the doc up to date?
Drizzle ORM - Magic sql`` operator
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Nvm found it
cant use it afer
.as
it solves my issue
thank you ❤️
As TOSL stated it's a data type issue. NUMERIC and DECIMAL data types have exact precision rather than floating point decimals. Database drivers treat these types as strings.
https://www.postgresql.org/docs/current/datatype-numeric.html
https://github.com/porsager/postgres?tab=readme-ov-file#numbers-bigint-numeric
PostgreSQL Documentation
8.1. Numeric Types
8.1. Numeric Types # 8.1.1. Integer Types 8.1.2. Arbitrary Precision Numbers 8.1.3. Floating-Point Types 8.1.4. Serial Types Numeric types consist of …
GitHub
GitHub - porsager/postgres: Postgres.js - The Fastest full featured...
Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare - porsager/postgres