Last active
March 5, 2025 19:56
-
-
Save geekish/c3398a60d51d1cf44a906ad5879d00a5 to your computer and use it in GitHub Desktop.
Drizzle schema utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createId } from "@paralleldrive/cuid2"; | |
| import type { AnyPgColumn, PgEnum } from "drizzle-orm/pg-core"; | |
| import { varchar } from "drizzle-orm/pg-core"; | |
| export const idColumn = () => varchar("id", { length: 255 }).notNull().primaryKey().$defaultFn(() => createId()); | |
| export const foreignIdColumn = (name: string) => varchar(name, { length: 255 }).notNull(); | |
| export const foreignId = (name: string, column: AnyPgColumn) => foreignIdColumn(name).references((): AnyPgColumn => column); | |
| export const morphId = (morphName: string) => varchar(`${morphName}_id`, { length: 255 }).notNull(); | |
| export const morphType = (morphName: string, type: PgEnum<[string, ...string[]]>) => type(`${morphName}_type`).notNull(); | |
| export const morphTo = (morphName: string, type: PgEnum<[string, ...string[]]>) => { | |
| return { | |
| [`${morphName}Id`]: morphId(morphName), | |
| [`${morphName}Type`]: morphType(morphName, type), | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment