113 lines
3.3 KiB
TypeScript
113 lines
3.3 KiB
TypeScript
import { relations } from "drizzle-orm";
|
|
import {
|
|
index,
|
|
pgTable,
|
|
text,
|
|
timestamp,
|
|
uniqueIndex,
|
|
integer,
|
|
} from "drizzle-orm/pg-core";
|
|
import { users } from "./public";
|
|
|
|
export const accounts = pgTable(
|
|
"account",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
accountId: text("account_id").notNull(),
|
|
providerId: text("provider_id").notNull(),
|
|
accessToken: text("access_token"),
|
|
refreshToken: text("refresh_token"),
|
|
accessTokenExpiresAt: timestamp("access_token_expires_at"),
|
|
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
|
|
scope: text("scope"),
|
|
idToken: text("id_token"),
|
|
password: text("password"),
|
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
uniqueIndex("account_provider_account_unique").on(
|
|
table.providerId,
|
|
table.accountId,
|
|
),
|
|
index("account_user_id_idx").on(table.userId),
|
|
],
|
|
);
|
|
|
|
export const accountRelations = relations(accounts, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [accounts.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const sessions = pgTable(
|
|
"session",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
token: text("token").notNull(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
ipAddress: text("ip_address"),
|
|
userAgent: text("user_agent"),
|
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
uniqueIndex("session_token_unique").on(table.token),
|
|
index("session_user_id_idx").on(table.userId),
|
|
],
|
|
);
|
|
|
|
export const sessionRelations = relations(sessions, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [sessions.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const verifications = pgTable(
|
|
"verification",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
identifier: text("identifier").notNull(),
|
|
value: text("value").notNull(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
|
},
|
|
(table) => [index("verification_identifier_idx").on(table.identifier)],
|
|
);
|
|
|
|
export const auditLogs = pgTable("audit_log", {
|
|
id: text("id").primaryKey(),
|
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
action: text("action").notNull(),
|
|
});
|
|
|
|
export const deviceCodes = pgTable("deviceCode", {
|
|
id: text("id").primaryKey(),
|
|
deviceCode: text("device_code").notNull(),
|
|
userCode: text("user_code").notNull(),
|
|
userId: text("user_id").references(() => users.id, {
|
|
onDelete: "set null",
|
|
}),
|
|
clientId: text("client_id"),
|
|
scope: text("scope"),
|
|
status: text("status").notNull(),
|
|
expiresAt: timestamp("expires_at"),
|
|
lastPolledAt: timestamp("last_polled_at"),
|
|
pollingInterval: integer("polling_interval"),
|
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
|
});
|