Files
gregorlohaus.com/src/server/dbschema/schema.ts
2026-04-24 11:58:19 +02:00

174 lines
5.3 KiB
TypeScript

// Example model schema from the Drizzle docs
// https://orm.drizzle.team/docs/sql-schema-declaration
import { relations, sql } from "drizzle-orm";
import { index, pgEnum, pgSchema, pgTableCreator, uniqueIndex } from "drizzle-orm/pg-core";
/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
* database instance for multiple projects.
*
* @see https://orm.drizzle.team/docs/goodies#multi-project-schema
*/
export const createTable = pgTableCreator((name) => `gregorlohaus.com_${name}`);
export const layoutPositionEnum = pgEnum('layout_position',['sidebar','header','col1','col2'])
export const cvCategory = createTable(
"cv_category",
(d) => ({
id: d.uuid().primaryKey(),
name: d.varchar({length: 50}),
layoutPosition: layoutPositionEnum()
}),
(t) => [index("name_idx").on(t.name)],
)
export const cvCategoryRelations = relations(cvCategory,({many}) => ({
cvEntry: many(cvEntry)
}))
export const cvEntry = createTable(
"cv_entry",
(d) => ({
id: d.uuid().primaryKey().notNull(),
categoryId: d.uuid('category_id'),
fromTime: d.timestamp().notNull().$type<Date>(),
toTime: d.timestamp().notNull().$type<Date>(),
title: d.varchar({length:50}).notNull(),
description: d.text(),
hideDates: d.boolean(),
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull().$type<Date>(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()).$type<Date>(),
})
)
export const cvEntryRelations = relations(cvEntry, ({one}) => ({
category: one(cvCategory, {
fields: [cvEntry.categoryId],
references: [cvCategory.id]
}),
}));
export const sourceTypeEnum = pgEnum('source_type',['open','closed'])
export const releaseStatus = pgEnum('release_status',['released','unreleased'])
export const stackItemEnum = pgEnum('stack_item',['drizzle','postgres','nextjs','react','servercomponents','php','laravel','reactnative','expo','mysql','nginx','protobuf','grpc','java','graalvm','spring','aws','s3','react-native','linux','debian','htmx','neon'])
export const project = createTable(
"project",
(d) => ({
id: d.uuid().primaryKey().notNull(),
title: d.varchar({length: 50}).notNull(),
description: d.text(),
sourceType: sourceTypeEnum(),
sourceLink: d.varchar({length: 200}),
releaseStatus: releaseStatus(),
releaseLink: d.varchar({length: 200}),
stackId: d.uuid(),
})
)
export const projectRelations = relations(project, ({one}) => ({
techStack: one(techStack, {
fields: [project.stackId],
references: [techStack.id],
})
}))
export const techStack = createTable(
"tech_stack",
(d) => ({
id: d.uuid().primaryKey().notNull(),
stackItems: stackItemEnum().array()
})
)
export const music = createTable(
"music",
(d) => ({
id: d.uuid().primaryKey().notNull(),
title: d.varchar({ length: 100 }).notNull(),
description: d.text(),
fileUrl: d.varchar("file_url", { length: 500 }).notNull(),
fileKey: d.varchar("file_key", { length: 200 }).notNull(),
fileName: d.varchar("file_name", { length: 200 }).notNull(),
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull()
.$type<Date>(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()).$type<Date>(),
})
)
export const blogPost = createTable(
"blog_post",
(d) => ({
id: d.uuid().primaryKey().defaultRandom(),
slug: d.varchar({ length: 200 }).notNull(),
title: d.varchar({ length: 200 }).notNull(),
date: d.varchar({ length: 20 }),
description: d.text(),
tags: d.text().array(),
fileKey: d.varchar("file_key", { length: 200 }).notNull(),
fileUrl: d.varchar("file_url", { length: 500 }).notNull(),
fileName: d.varchar("file_name", { length: 255 }).notNull(),
customId: d.varchar("custom_id", { length: 255 }).notNull(),
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull()
.$type<Date>(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()).$type<Date>(),
}),
(t) => [
uniqueIndex("blog_post_slug_idx").on(t.slug),
uniqueIndex("blog_post_file_key_idx").on(t.fileKey),
uniqueIndex("blog_post_custom_id_idx").on(t.customId),
],
)
export const messageRoleEnum = pgEnum('message_role', ['user', 'assistant'])
export const chatSession = createTable(
"chat_session",
(d) => ({
id: d.uuid().primaryKey().defaultRandom(),
userId: d.varchar({ length: 255 }).notNull(),
createdAt: d.timestamp({ withTimezone: true }).default(sql`CURRENT_TIMESTAMP`).notNull().$type<Date>(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()).$type<Date>(),
})
)
export const chatSessionRelations = relations(chatSession, ({ many }) => ({
messages: many(chatMessage),
}))
export const chatMessage = createTable(
"chat_message",
(d) => ({
id: d.uuid().primaryKey().defaultRandom(),
sessionId: d.uuid('session_id').notNull(),
role: messageRoleEnum().notNull(),
content: d.text().notNull(),
createdAt: d.timestamp({ withTimezone: true }).default(sql`CURRENT_TIMESTAMP`).notNull().$type<Date>(),
})
)
export const chatMessageRelations = relations(chatMessage, ({ one }) => ({
session: one(chatSession, {
fields: [chatMessage.sessionId],
references: [chatSession.id],
}),
}))
export const systemSettings = createTable(
"systemSetting",
(d) => ({
systemPropmt: d.text()
})
)