lifetracker/packages/db/schema.ts
2024-11-14 23:52:59 -08:00

155 lines
4.2 KiB
TypeScript

import type { AdapterAccount } from "@auth/core/adapters";
import { createId } from "@paralleldrive/cuid2";
import { relations } from "drizzle-orm";
import {
AnySQLiteColumn,
index,
integer,
primaryKey,
sqliteTable,
text,
unique,
} from "drizzle-orm/sqlite-core";
function createdAtField() {
return integer("createdAt", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date());
}
export const apiKeys = sqliteTable(
"apiKey",
{
id: text("id")
.notNull()
.primaryKey()
.$defaultFn(() => createId()),
name: text("name").notNull(),
createdAt: createdAtField(),
keyId: text("keyId").notNull().unique(),
keyHash: text("keyHash").notNull(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
},
(ak) => ({
unq: unique().on(ak.name, ak.userId),
}),
);
export const users = sqliteTable("user", {
id: text("id")
.notNull()
.primaryKey()
.$defaultFn(() => createId()),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
image: text("image"),
password: text("password"),
role: text("role", { enum: ["admin", "user"] }).default("user"),
});
export const accounts = sqliteTable(
"account",
{
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
type: text("type").$type<AdapterAccount["type"]>().notNull(),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: integer("expires_at"),
token_type: text("token_type"),
scope: text("scope"),
id_token: text("id_token"),
session_state: text("session_state"),
},
(account) => ({
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
}),
);
export const sessions = sqliteTable("session", {
sessionToken: text("sessionToken")
.notNull()
.primaryKey()
.$defaultFn(() => createId()),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
});
export const verificationTokens = sqliteTable(
"verificationToken",
{
identifier: text("identifier").notNull(),
token: text("token").notNull(),
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
},
(vt) => ({
compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
}),
);
export const days = sqliteTable("day", {
id: integer("id").primaryKey({ autoIncrement: true }),
mood: integer("mood"),
date: text("date").notNull().unique(),
comment: text("comment").notNull(),
});
export const labels = sqliteTable(
"label",
{
id: text("id")
.notNull()
.primaryKey()
.$defaultFn(() => createId()),
createdAt: createdAtField(),
name: text("name").notNull(),
code: integer("code").notNull(),
description: text("description"),
color: text("color").default("#000000"),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
},
(lb) => ({
uniq: unique().on(lb.userId, lb.name)
}),
);
export const config = sqliteTable("config", {
key: text("key").notNull().primaryKey(),
value: text("value").notNull(),
});
// Relations
export const apiKeyRelations = relations(apiKeys, ({ one }) => ({
user: one(users, {
fields: [apiKeys.userId],
references: [users.id],
}),
}));
export const userRelations = relations(users, ({ many }) => ({
labels: many(labels),
}));
export const labelsRelations = relations(
labels,
({ many, one }) => ({
user: one(users, {
fields: [labels.userId],
references: [users.id],
}),
}),
);