Working on arthome

This commit is contained in:
2024-08-25 18:33:37 +02:00
parent a5120d006a
commit a1e31a89a7
49 changed files with 3139 additions and 284 deletions

View File

@@ -1,10 +1,18 @@
import { drizzle } from 'drizzle-orm/d1'
import postgres from 'postgres'
import { drizzle } from 'drizzle-orm/postgres-js'
import * as schema from '../database/schema'
export { sql, eq, and, or, asc, desc, sum } from 'drizzle-orm'
export { sql, eq, and, or, asc, desc, sum, isNull } from 'drizzle-orm'
export const tables = schema
export function useDB() {
return drizzle(hubDatabase(), { schema })
export function useDrizzle() {
const config = useRuntimeConfig()
return drizzle(postgres(config.postgres.url, { prepare: false }), { schema })
}
export type UserType = typeof schema.users.$inferSelect
export type UserInsert = typeof schema.users.$inferInsert
export type TabType = typeof schema.tabs.$inferSelect
export type CategoryType = typeof schema.categories.$inferSelect

20
server/utils/dbFields.ts Normal file
View File

@@ -0,0 +1,20 @@
import * as pg from 'drizzle-orm/pg-core'
/**
* A centralized list of standardized Drizzle ORM schema field definitions to prevent duplication errors
*/
export const createdAt = pg
.timestamp('created_at', { mode: 'date', precision: 3 })
.defaultNow()
export const updatedAt = pg
.timestamp('updated_at', { mode: 'date', precision: 3 })
.$onUpdate(() => new Date())
export const id = pg.integer('id').primaryKey({ autoIncrement: true })
export const timestamps = {
createdAt,
updatedAt,
}

58
server/utils/users.ts Normal file
View File

@@ -0,0 +1,58 @@
import type { SQL } from 'drizzle-orm'
import type { UserInsert } from '~~/server/utils/db'
export async function findUserById(userId: number) {
return useDrizzle()
.query
.users
.findFirst({
where: eq(tables.users.id, userId),
})
}
export async function findUserByGitHubId(githubId: number) {
return useDrizzle()
.query
.users
.findFirst({
where: eq(tables.users.githubId, githubId),
})
}
export async function findUserByGoogleId(googleId: string) {
return useDrizzle()
.query
.users
.findFirst({
where: eq(tables.users.googleId, googleId),
})
}
export async function findUserBy(query: SQL | undefined) {
return useDrizzle()
.query
.users
.findFirst({
where: query,
})
}
export async function createUser(user: UserInsert) {
return useDrizzle()
.insert(tables.users)
.values(user)
.returning()
}
export async function updateUser(userId: number, user: Partial<UserInsert>) {
return useDrizzle()
.update(tables.users)
.set(user)
.where(eq(tables.users.id, userId))
}
export async function deleteProfilePicture(avatar: string) {
if (avatar.startsWith('profile-pictures/')) {
await hubBlob().delete(avatar)
}
}