Some checks failed
Production PR / QA Tests (pull_request) Failing after 13s
106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
import { v7 as uuidv7 } from "uuid";
|
|
import { relations, sql } from "drizzle-orm";
|
|
import { boolean, index, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
|
|
|
export const user = pgTable("user", {
|
|
id: uuid()
|
|
.primaryKey()
|
|
.default(sql`uuid_v7()`)
|
|
.$defaultFn(() => uuidv7()),
|
|
name: text().notNull(),
|
|
email: text().notNull().unique(),
|
|
emailVerified: boolean().default(false).notNull(),
|
|
image: text(),
|
|
createdAt: timestamp().defaultNow().notNull(),
|
|
updatedAt: timestamp()
|
|
.defaultNow()
|
|
.$onUpdate(() => new Date())
|
|
.notNull(),
|
|
});
|
|
|
|
export const session = pgTable(
|
|
"session",
|
|
{
|
|
id: uuid()
|
|
.primaryKey()
|
|
.default(sql`uuid_v7()`)
|
|
.$defaultFn(() => uuidv7()),
|
|
expiresAt: timestamp().notNull(),
|
|
token: text().notNull().unique(),
|
|
createdAt: timestamp().defaultNow().notNull(),
|
|
updatedAt: timestamp()
|
|
.$onUpdate(() => new Date())
|
|
.notNull(),
|
|
ipAddress: text(),
|
|
userAgent: text(),
|
|
userId: uuid()
|
|
.notNull()
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
},
|
|
(table) => [index("session_userId_idx").on(table.userId)]
|
|
);
|
|
|
|
export const account = pgTable(
|
|
"account",
|
|
{
|
|
id: uuid()
|
|
.primaryKey()
|
|
.default(sql`uuid_v7()`)
|
|
.$defaultFn(() => uuidv7()),
|
|
accountId: text().notNull(),
|
|
providerId: text().notNull(),
|
|
userId: uuid()
|
|
.notNull()
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
accessToken: text(),
|
|
refreshToken: text(),
|
|
idToken: text(),
|
|
accessTokenExpiresAt: timestamp(),
|
|
refreshTokenExpiresAt: timestamp(),
|
|
scope: text(),
|
|
password: text(),
|
|
createdAt: timestamp().defaultNow().notNull(),
|
|
updatedAt: timestamp()
|
|
.$onUpdate(() => new Date())
|
|
.notNull(),
|
|
},
|
|
(table) => [index("account_userId_idx").on(table.userId)]
|
|
);
|
|
|
|
export const verification = pgTable(
|
|
"verification",
|
|
{
|
|
id: uuid()
|
|
.primaryKey()
|
|
.default(sql`uuid_v7()`)
|
|
.$defaultFn(() => uuidv7()),
|
|
identifier: text().notNull(),
|
|
value: text().notNull(),
|
|
expiresAt: timestamp().notNull(),
|
|
createdAt: timestamp().defaultNow().notNull(),
|
|
updatedAt: timestamp()
|
|
.defaultNow()
|
|
.$onUpdate(() => new Date())
|
|
.notNull(),
|
|
},
|
|
(table) => [index("verification_identifier_idx").on(table.identifier)]
|
|
);
|
|
|
|
export const userRelations = relations(user, ({ many }) => ({
|
|
sessions: many(session),
|
|
accounts: many(account),
|
|
}));
|
|
|
|
export const sessionRelations = relations(session, ({ one }) => ({
|
|
user: one(user, {
|
|
fields: [session.userId],
|
|
references: [user.id],
|
|
}),
|
|
}));
|
|
|
|
export const accountRelations = relations(account, ({ one }) => ({
|
|
user: one(user, {
|
|
fields: [account.userId],
|
|
references: [user.id],
|
|
}),
|
|
}));
|