Create a Lowercase constraint (or any custom constraint) in drizzle ORM schema

Emails should be case insensitive, so I achieve this by forcing lowercase. How do I do that in Drizzle?
22 Replies
rphlmr ⚡
rphlmr ⚡5mo ago
You can add a uniqueIndex checking for unique email (compare by lowercase). https://drizzle.run/kwwpxipbfaijyhccbnuryj9t The best thing would be a check constraint but it is not yet supported
alex
alexOP5mo ago
thank
alex
alexOP5mo ago
man.
No description
rphlmr ⚡
rphlmr ⚡5mo ago
lol I guess you have some protection that filter your browsing?
alex
alexOP5mo ago
yes i really dislike getting phished :Haha:
rphlmr ⚡
rphlmr ⚡5mo ago
Yeah It is because my public IP is shared with some 💩
alex
alexOP5mo ago
so you host on some site?
rphlmr ⚡
rphlmr ⚡5mo ago
and its true that Drizzle Run is new :p
alex
alexOP5mo ago
vercel my beloved
rphlmr ⚡
rphlmr ⚡5mo ago
Fly I will buy a dedicated IP some day, depending on the price haha
import { integer, text, pgTable, uniqueIndex } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";

export const users = pgTable(
"users",
{
id: integer("id").primaryKey().generatedByDefaultAsIdentity(),
email: text("email").notNull(),
},
(t) => ({
emailIdx: uniqueIndex("email_idx").on(sql`lower(${t.email})`),
}),
);
import { integer, text, pgTable, uniqueIndex } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";

export const users = pgTable(
"users",
{
id: integer("id").primaryKey().generatedByDefaultAsIdentity(),
email: text("email").notNull(),
},
(t) => ({
emailIdx: uniqueIndex("email_idx").on(sql`lower(${t.email})`),
}),
);
in case you can't bypass
alex
alexOP5mo ago
oh thanks bro you the goat :thumbsup_nerd: lemme hit ur site from search tho to boost ur seo what keywords should i use
rphlmr ⚡
rphlmr ⚡5mo ago
just drizzle run 😄 my seo is bad because of the loading screen (that load an editor and a virtual machine)
alex
alexOP5mo ago
oh man this is very unfortunate
No description
alex
alexOP5mo ago
oh its not a blog post that's why
rphlmr ⚡
rphlmr ⚡5mo ago
No description
rphlmr ⚡
rphlmr ⚡5mo ago
a web app where you can play and share playgrounds made with Drizzle
alex
alexOP5mo ago
very interesting i like it @Raphaël M (@rphlmr) ⚡ dyk if this schema automatically stores it as lowercase as well or does it store it as whatever and compare unique in lowercase silly customer writing [email protected] incoming
rphlmr ⚡
rphlmr ⚡5mo ago
it store as it, if you want you can validate/transform the user input with Zod or Valibot
rphlmr ⚡
rphlmr ⚡5mo ago
Drizzle ORM - drizzle-zod
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
rphlmr ⚡
rphlmr ⚡5mo ago
if you know this package, it is easy to generate a Zod schema from your table definition and extends it to transform your payload
rphlmr ⚡
rphlmr ⚡5mo ago
No description
alex
alexOP5mo ago
fire
Want results from more Discord servers?
Add your server