drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too đŸ˜…
https://orm.drizzle.team
Apache License 2.0
21.44k stars 484 forks source link

[FEATURE]: SQLITE - Introspect `BOOLEAN` as `integer("...", { mode : "boolean" })` #2539

Open kevinmitch14 opened 1 week ago

kevinmitch14 commented 1 week ago

Describe what you want

We are trying to use Drizzle with SQLite. The drizzle schema is not the source of truth, so we are trying to introspect the DB and make minor tweaks if needed.

I noticed that BOOLEAN is introspected as numeric() by Drizzle. In SQLite, BOOLEAN is just syntactic sugar, but could that be used to infer number("column", { mode: "boolean" }) instead of numeric("column"). I believe numeric is actually the correct thing to convert to, according to Column Affinity though.

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.0 (2018-04-02) but those keywords are really just alternative spellings for the integer literals 1 and 0 respectively.

https://www.sqlite.org/datatype3.html#boolean_datatype

active BOOLEAN NOT NULL

# Results in
active: numeric("active").notNull(),

After a quick play with Prisma, I noticed that they are introspecting BOOLEAN as a Prisma Boolean. Making it a lot easier to work with in your app logic. The issue with numeric is, anything is accepted.