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]: Track constraints in the type system #2536

Open DrTtnk opened 1 week ago

DrTtnk commented 1 week ago

Describe what you want

It would be a nice to have for strict type checking

import * as DP from "drizzle-orm/pg-core";

export const Participant = DP.pgTable(
  "participants",
  {
    id: DP.uuid("id").primaryKey(),
    name: DP.varchar("name", { length: 500 }).notNull(),
    surname: DP.varchar("surname", { length: 500 }).notNull(),
    email: DP.varchar("email", { length: 100 }).$type<DT.Email>().unique("email_unique").notNull(),
    bio: DP.text("bio").notNull(),
  },
  tb => ({ uniq: DP.primaryKey({ columns: [tb.id], name: "no_homonymies" }) }),
);
export type ParticipantInsert = DO.InferInsertModel<typeof Participant>;
export type Constraints = (typeof Participant)["_"]["config"]["contraints"];
//                    ^ ['email_unique', 'no_homonymies', '<the generated one for id>']

Such that in case of insert/update I can discriminate between different constraints and have TS validating my logic

Would this be possible and, hopefully, easy to implement?