Files
dahoam/src/db/schema.ts
T
niklas 5001cae163
images / check (push) Successful in 30s
images / image (push) Successful in 21s
add ability to move cards
2026-11-19 18:07:50 +01:00

197 lines
6.0 KiB
TypeScript

import {
boolean,
customType,
index,
integer,
pgTable,
text,
timestamp,
uuid,
} from "drizzle-orm/pg-core"
// ---- Better Auth core tables ----
export const user = pgTable("user", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").notNull().default(false),
image: text("image"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
})
export const session = pgTable(
"session",
{
id: text("id").primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
token: text("token").notNull().unique(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
ipAddress: text("ip_address"),
userAgent: text("user_agent"),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
},
(t) => [index("session_user_id_idx").on(t.userId)]
)
export const account = pgTable(
"account",
{
id: text("id").primaryKey(),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
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"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
},
(t) => [index("account_user_id_idx").on(t.userId)]
)
export const verification = 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(),
},
(t) => [index("verification_identifier_idx").on(t.identifier)]
)
// ---- Dashboard tables ----
const bytea = customType<{ data: Uint8Array }>({ dataType: () => "bytea" })
/** A set of cards, for example Home and Work. */
export const dashboard = pgTable(
"dashboard",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
name: text("name").notNull(),
// null = bundled default wallpaper
wallpaperUrl: text("wallpaper_url"),
// percent of black over the wallpaper, see DEFAULT_WALLPAPER_DIM
wallpaperDim: integer("wallpaper_dim").notNull().default(40),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
(t) => [index("dashboard_user_id_idx").on(t.userId)]
)
/** A named group of links on a dashboard. */
export const card = pgTable(
"card",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
dashboardId: uuid("dashboard_id")
.notNull()
.references(() => dashboard.id, { onDelete: "cascade" }),
// order on the dashboard
position: integer("position").notNull().default(0),
name: text("name").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
(t) => [
index("card_user_id_idx").on(t.userId),
index("card_dashboard_id_idx").on(t.dashboardId),
]
)
export const link = pgTable(
"link",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
cardId: uuid("card_id")
.notNull()
.references(() => card.id, { onDelete: "cascade" }),
// order inside the card
position: integer("position").notNull().default(0),
name: text("name").notNull(),
url: text("url").notNull(),
// image URL, an upload ("/api/icon?id=…") or a dashboard-icons name ("plex");
// null = guess from name
icon: text("icon"),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
(t) => [
index("link_user_id_idx").on(t.userId),
index("link_card_id_idx").on(t.cardId),
]
)
export const settings = pgTable("settings", {
userId: text("user_id")
.primaryKey()
.references(() => user.id, { onDelete: "cascade" }),
title: text("title").notNull().default("Dahoam"),
// the dashboard that opens after sign-in; null = the oldest one
defaultDashboardId: uuid("default_dashboard_id").references(
() => dashboard.id,
{ onDelete: "set null" }
),
searchEngine: text("search_engine", {
enum: ["duckduckgo", "searxng", "google"],
})
.notNull()
.default("duckduckgo"),
// the big clock, see src/lib/clock.ts
clockFont: text("clock_font", {
enum: ["serif", "sans", "light", "rounded", "slab"],
})
.notNull()
.default("serif"),
clockStyle: text("clock_style", { enum: ["solid", "glass", "tinted"] })
.notNull()
.default("solid"),
})
/** A link icon that the user uploaded, served from /api/icon. */
export const icon = pgTable(
"icon",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
mime: text("mime").notNull(),
data: bytea("data").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
(t) => [index("icon_user_id_idx").on(t.userId)]
)
/** The wallpaper that was uploaded for a dashboard, served from /api/wallpaper. */
export const wallpaper = pgTable("wallpaper", {
dashboardId: uuid("dashboard_id")
.primaryKey()
.references(() => dashboard.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
mime: text("mime").notNull(),
data: bytea("data").notNull(),
})