setup drizzle for d1 & create db schema

This commit is contained in:
Michi 2025-09-08 22:03:42 +02:00
parent 3c197d8ebe
commit fc3b1d1af5
8 changed files with 824 additions and 12 deletions

9
api/src/db/client.ts Normal file
View file

@ -0,0 +1,9 @@
import { drizzle } from 'drizzle-orm/d1';
import type { Context } from 'hono';
export function dbConnection(c: Context){
return drizzle({
connection: c.env.db,
casing: 'snake_case'
})
}

45
api/src/db/schema.ts Normal file
View file

@ -0,0 +1,45 @@
import { integer, text, sqliteTable } from "drizzle-orm/sqlite-core";
export const attraction = sqliteTable('attraction', {
id: integer().primaryKey({ autoIncrement: true }),
name: text().notNull(),
apiCode: integer(),
themeparkId: integer().notNull().references(() => themepark.id)
})
export const attractionNotification = sqliteTable('attraction_notification', {
id: integer().primaryKey({ autoIncrement: true}),
userId: integer().notNull().references(() => user.id),
attractionId: integer().notNull().references(() => attraction.id),
notificationMethodId: integer().notNull().references(() => notificationMethod.id)
})
export const logbook = sqliteTable('logbook', {
id: integer().primaryKey({ autoIncrement: true }),
userId: integer().notNull().references(() => user.id),
attractionId: integer().notNull().references(() => attraction.id),
timestamp: integer().notNull(), // unix timecode
expectedWaittime: integer(),
realWaittime: integer()
})
export const notificationMethod = sqliteTable('notification', {
id: integer().primaryKey({ autoIncrement: true }),
webhookUrl: text().notNull(),
shownName: text().notNull(),
userId: integer().notNull().references(() => user.id)
})
export const themepark = sqliteTable('themepark', {
id: integer().primaryKey({ autoIncrement: true }),
name: text().notNull(),
countrycode: text().notNull(),
website: text(),
apiName: text()
})
export const user = sqliteTable('user', {
id: integer().primaryKey({ autoIncrement: true }),
username: text().notNull(),
isActive: integer({ mode: 'boolean' }).default(false)
})

View file

@ -1,4 +1,6 @@
import { Hono } from 'hono'
import { dbConnection } from './db/client'
import { notificationMethod } from './db/schema'
const app = new Hono()