2025-08-29 15:31:23 +02:00
|
|
|
import {relations} from "drizzle-orm";
|
2026-02-13 16:48:13 +01:00
|
|
|
import {boolean, integer, pgEnum, json, pgTable, text, timestamp, uuid} from "drizzle-orm/pg-core";
|
2025-08-29 15:31:23 +02:00
|
|
|
import {createSelectSchema} from "drizzle-zod";
|
|
|
|
|
import {z} from "zod";
|
|
|
|
|
import {project} from "./06_project";
|
2026-01-17 20:27:06 +01:00
|
|
|
import {member} from "@/db/schema/04_member";
|
2025-08-29 15:31:23 +02:00
|
|
|
import {invitation} from "@/db/schema/05_invitation";
|
|
|
|
|
import {organization} from "@/db/schema/03_organization";
|
2025-12-21 16:55:12 +01:00
|
|
|
import {Account as BetterAuthAccount} from "better-auth";
|
2025-08-29 15:31:23 +02:00
|
|
|
import {timestamps} from "@/db/schema/00_common";
|
2025-05-16 15:54:17 +02:00
|
|
|
|
2025-12-22 20:32:18 +01:00
|
|
|
export const userThemeEnum = pgEnum("user_themes", ["light", "dark", "system"]);
|
|
|
|
|
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
export const user = pgTable("user", {
|
|
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
name: text("name").notNull(),
|
|
|
|
|
email: text("email").notNull().unique(),
|
|
|
|
|
emailVerified: boolean("email_verified").notNull(),
|
|
|
|
|
image: text("image"),
|
|
|
|
|
role: text("role"),
|
2025-12-22 20:32:18 +01:00
|
|
|
theme: userThemeEnum().notNull().default("light"),
|
2025-05-16 15:54:17 +02:00
|
|
|
banned: boolean("banned"),
|
|
|
|
|
banReason: text("ban_reason"),
|
|
|
|
|
banExpires: timestamp("ban_expires"),
|
2026-01-17 20:27:06 +01:00
|
|
|
lastConnectedAt: timestamp(),
|
2025-12-21 16:55:12 +01:00
|
|
|
lastChangedPasswordAt: timestamp(),
|
|
|
|
|
twoFactorEnabled: boolean("two_factor_enabled").default(false),
|
2025-08-29 15:31:23 +02:00
|
|
|
...timestamps
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const session = pgTable("session", {
|
|
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
|
|
|
token: text("token").notNull().unique(),
|
|
|
|
|
ipAddress: text("ip_address"),
|
|
|
|
|
userAgent: text("user_agent"),
|
|
|
|
|
userId: uuid("user_id")
|
|
|
|
|
.notNull()
|
2025-08-29 15:31:23 +02:00
|
|
|
.references(() => user.id, {onDelete: "cascade"}),
|
2026-02-23 12:04:48 +01:00
|
|
|
providerId: text("provider_id"),
|
2025-05-16 15:54:17 +02:00
|
|
|
impersonatedBy: text("impersonated_by"), //id or name ????
|
|
|
|
|
activeOrganizationId: text("active_organization_id"),
|
2025-08-29 15:31:23 +02:00
|
|
|
...timestamps
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const account = pgTable("account", {
|
|
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
accountId: text("account_id").notNull(),
|
|
|
|
|
providerId: text("provider_id").notNull(),
|
|
|
|
|
userId: uuid("user_id")
|
|
|
|
|
.notNull()
|
2025-08-29 15:31:23 +02:00
|
|
|
.references(() => user.id, {onDelete: "cascade"}),
|
2025-05-16 15:54:17 +02:00
|
|
|
accessToken: text("access_token"),
|
|
|
|
|
refreshToken: text("refresh_token"),
|
|
|
|
|
idToken: text("id_token"),
|
|
|
|
|
accessTokenExpiresAt: timestamp("access_token_expires_at"),
|
|
|
|
|
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
|
|
|
|
|
scope: text("scope"),
|
|
|
|
|
password: text("password"),
|
2025-08-29 15:31:23 +02:00
|
|
|
...timestamps
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const verification = pgTable("verification", {
|
|
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
identifier: text("identifier").notNull(),
|
|
|
|
|
value: text("value").notNull(),
|
|
|
|
|
expiresAt: timestamp("expires_at").notNull(),
|
2025-08-29 15:31:23 +02:00
|
|
|
...timestamps
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
|
|
|
|
|
2025-12-22 20:32:18 +01:00
|
|
|
export const passkey = pgTable("passkey", {
|
2026-02-13 16:48:13 +01:00
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
name: text("name"),
|
|
|
|
|
publicKey: text("public_key").notNull(),
|
|
|
|
|
userId: uuid("user_id")
|
2025-12-22 20:32:18 +01:00
|
|
|
.notNull()
|
2026-02-13 16:48:13 +01:00
|
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
|
|
|
credentialID: text("credential_i_d").notNull(),
|
|
|
|
|
counter: integer("counter").notNull(),
|
|
|
|
|
deviceType: text("device_type").notNull(),
|
|
|
|
|
backedUp: boolean("backed_up").notNull(),
|
|
|
|
|
transports: text("transports"),
|
|
|
|
|
aaguid: text("aaguid"),
|
|
|
|
|
...timestamps
|
2025-12-22 20:32:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const twoFactor = pgTable("two_factor", {
|
|
|
|
|
id: uuid().defaultRandom().primaryKey(),
|
|
|
|
|
secret: text("secret").notNull(),
|
|
|
|
|
backupCodes: text("backup_codes").notNull(),
|
|
|
|
|
userId: uuid("user_id")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id, {onDelete: "cascade"}),
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-13 16:48:13 +01:00
|
|
|
export const ssoProvider = pgTable("sso_provider", {
|
|
|
|
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
|
|
|
issuer: text("issuer").notNull(),
|
|
|
|
|
oidcConfig: json("oidc_config"),
|
|
|
|
|
samlConfig: json("saml_config"),
|
|
|
|
|
userId: uuid("user_id").references(() => user.id, { onDelete: "cascade" }),
|
|
|
|
|
providerId: text("provider_id").notNull().unique(),
|
|
|
|
|
organizationId: text("organization_id"),
|
|
|
|
|
domain: text("domain").notNull(),
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-29 15:31:23 +02:00
|
|
|
export const userRelations = relations(user, ({many}) => ({
|
2025-05-16 15:54:17 +02:00
|
|
|
sessions: many(session),
|
|
|
|
|
accounts: many(account),
|
2026-02-13 16:48:13 +01:00
|
|
|
ssoProviders: many(ssoProvider),
|
2025-05-16 15:54:17 +02:00
|
|
|
memberships: many(member),
|
|
|
|
|
invitations: many(invitation),
|
2026-02-13 16:48:13 +01:00
|
|
|
passkeys: many(passkey),
|
2025-05-16 15:54:17 +02:00
|
|
|
}));
|
|
|
|
|
|
2025-08-29 15:31:23 +02:00
|
|
|
export const sessionRelations = relations(session, ({one}) => ({
|
2025-05-16 15:54:17 +02:00
|
|
|
user: one(user, {
|
|
|
|
|
fields: [session.userId],
|
|
|
|
|
references: [user.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-08-29 15:31:23 +02:00
|
|
|
export const accountRelations = relations(account, ({one}) => ({
|
2025-05-16 15:54:17 +02:00
|
|
|
user: one(user, {
|
|
|
|
|
fields: [account.userId],
|
|
|
|
|
references: [user.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-02-13 16:48:13 +01:00
|
|
|
export const ssoProviderRelations = relations(ssoProvider, ({ one }) => ({
|
|
|
|
|
user: one(user, {
|
|
|
|
|
fields: [ssoProvider.userId],
|
|
|
|
|
references: [user.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-08-29 15:31:23 +02:00
|
|
|
export const projectRelations = relations(project, ({one}) => ({
|
2025-05-16 15:54:17 +02:00
|
|
|
organization: one(organization, {
|
|
|
|
|
fields: [project.organizationId],
|
|
|
|
|
references: [organization.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-02-13 16:48:13 +01:00
|
|
|
export const passkeyRelations = relations(passkey, ({ one }) => ({
|
|
|
|
|
user: one(user, {
|
|
|
|
|
fields: [passkey.userId],
|
|
|
|
|
references: [user.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
export const userSchema = createSelectSchema(user);
|
|
|
|
|
export type User = z.infer<typeof userSchema>;
|
2025-08-02 11:14:51 +02:00
|
|
|
|
2025-12-30 13:38:01 +01:00
|
|
|
export const userThemeEnumSchema = createSelectSchema(userThemeEnum)
|
|
|
|
|
export type UserThemeEnum = z.infer<typeof userThemeEnumSchema>;
|
|
|
|
|
|
|
|
|
|
|
2025-12-21 16:55:12 +01:00
|
|
|
export const sessionSchema = createSelectSchema(session);
|
|
|
|
|
export type Session = z.infer<typeof sessionSchema>;
|
|
|
|
|
|
|
|
|
|
export const accountSchema = createSelectSchema(account);
|
|
|
|
|
export type Account = z.infer<typeof accountSchema>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type FixedAccount = Omit<BetterAuthAccount, 'updatedAt'> & {
|
2025-08-02 11:14:51 +02:00
|
|
|
updatedAt: Date | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type UserWithAccounts = User & {
|
|
|
|
|
accounts: FixedAccount[];
|
|
|
|
|
};
|