import type { AdapterAccount } from "@auth/core/adapters"; import { createId } from "@paralleldrive/cuid2"; import { relations, SQL, sql } 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 function calcInverseColor(hexcode: string): string { console.log(hexcode); const hex = hexcode.replace("#", ""); const r = parseInt(hex.substr(0, 2), 16); const g = parseInt(hex.substr(2, 2), 16); const b = parseInt(hex.substr(4, 2), 16); const luminance = r * 0.299 + g * 0.587 + b * 0.114; return luminance > 186 ? "#000000" : "#ffffff"; } export const config = sqliteTable("config", { key: text("key").notNull().primaryKey(), value: text("value").notNull(), }); 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().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 colors = sqliteTable( "color", { id: text("id") .notNull() .primaryKey() .$defaultFn(() => createId()), createdAt: createdAtField(), name: text("name").notNull(), hexcode: text("hexcode").notNull(), inverse: text("inverse"), userId: text("userId") .notNull() .references(() => users.id, { onDelete: "cascade" }), }, (c) => ({ uniq: unique().on(c.userId, c.name) }), ); export const categories = sqliteTable( "category", { id: text("id") .notNull() .primaryKey() .$defaultFn(() => createId()), createdAt: createdAtField(), name: text("name").notNull(), code: integer("code").notNull(), description: text("description"), colorId: text("colorId") .notNull() .references(() => colors.id), parentId: text("parentId").references(() => categories.id), userId: text("userId") .notNull() .references(() => users.id, { onDelete: "cascade" }), }, (lb) => ({ uniq: unique().on(lb.userId, lb.name) }), ); // Relations export const apiKeyRelations = relations(apiKeys, ({ one }) => ({ user: one(users, { fields: [apiKeys.userId], references: [users.id], }), })); export const userRelations = relations(users, ({ many }) => ({ categories: many(categories), colors: many(colors), })); export const categoriesRelations = relations( categories, ({ many, one }) => ({ user: one(users, { fields: [categories.userId], references: [users.id], }), color: one(colors, { fields: [categories.colorId], references: [colors.id], }), parent: one(categories, { fields: [categories.parentId], references: [categories.id], }), }), ); export const colorsRelations = relations( colors, ({ many, one }) => ({ user: one(users, { fields: [colors.userId], references: [users.id], }), categories: many(categories), }), );