Last active
January 27, 2024 03:31
-
-
Save jasonraimondi/303605d99ef3906519512768d96a18c7 to your computer and use it in GitHub Desktop.
Drizzle Schema Example
This file contains 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 { | |
pgEnum, | |
pgTable, | |
uniqueIndex, | |
text, | |
integer, | |
boolean, | |
timestamp, | |
} from "drizzle-orm/pg-core"; | |
export const roleEnum = pgEnum("roles_enum", ["admin"]); | |
export const users = pgTable( | |
"users", | |
{ | |
id: text("id").primaryKey(), | |
email: text("email").notNull(), | |
passwordHash: text("password_hash"), | |
tokenVersion: integer("token_version").default(0).notNull(), | |
isEmailConfirmed: boolean("is_email_confirmed").default(false).notNull(), | |
nickname: text("nickname"), | |
lastLoginAt: timestamp("last_login_at", { mode: "date", precision: 3 }), | |
lastLoginIP: text("last_login_ip"), | |
createdIP: text("created_ip").notNull(), | |
createdAt: timestamp("created_at", { mode: "date", precision: 3 }).defaultNow().notNull(), | |
updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }), | |
roles: roleEnum("roles").notNull(), | |
}, | |
(users) => { | |
return { | |
emailIndex: uniqueIndex("email_idx").on(users.email), | |
}; | |
}, | |
); | |
export const userTokenTypeEnum = pgEnum("user_token_type_enum", [ | |
"email_confirmation", | |
"forgot_password", | |
]); | |
export const userTokens = pgTable("user_token", { | |
id: text("id").primaryKey(), | |
type: userTokenTypeEnum("type").notNull(), | |
expiresAt: timestamp("expires_at", { mode: "date", precision: 3 }).notNull(), | |
createdAt: timestamp("created_at", { mode: "date", precision: 3 }).defaultNow().notNull(), | |
userId: text("user_id") | |
.references(() => users.id) | |
.notNull(), | |
}); | |
export const providerEnum = pgEnum("providers_enum", ["facebook", "github", "google"]); | |
export const userProviders = pgTable("user_providers", { | |
provider: providerEnum("provider").notNull(), | |
userId: text("user_id") | |
.references(() => users.id) | |
.notNull(), | |
createdAt: timestamp("createdAt", { mode: "date", precision: 3 }).defaultNow().notNull(), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment