From bc680fb60b30928223ce668f5cb7eb33936550f0 Mon Sep 17 00:00:00 2001 From: Rasmus Q Date: Sun, 15 Mar 2026 21:38:54 +0000 Subject: [PATCH] refactor: add proper types for all database insert operations - Add missing Select and Insert types for Auth.js tables (Account, Session, VerificationToken) - Update all insert operations to use typed New* variables: - NewUser for user signup - NewItem for adding wishlist items - NewSavedWishlist for saving wishlists - Improves type safety and catches insert errors at compile time --- Dockerfile | 2 +- src/lib/db/schema.ts | 6 ++++++ src/routes/signup/+page.server.ts | 7 ++++--- src/routes/wishlist/[token]/+page.server.ts | 7 ++++--- src/routes/wishlist/[token]/edit/+page.server.ts | 12 +++++++----- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index c4cbaaf..f6d790f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,7 +32,7 @@ COPY --from=deps /app/node_modules ./node_modules # Copy Drizzle files for migrations COPY --from=builder /app/drizzle ./drizzle COPY --from=builder /app/drizzle.config.ts ./ -COPY --from=builder /app/src/lib/server/schema.ts ./src/lib/server/schema.ts +COPY --from=builder /app/src/lib/db ./src/lib/db # Expose the port EXPOSE 3000 diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index f50d1f7..d613ca4 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -177,6 +177,12 @@ export const usersRelations = relations(users, ({ many }) => ({ export type User = typeof users.$inferSelect; export type NewUser = typeof users.$inferInsert; +export type Account = typeof accounts.$inferSelect; +export type NewAccount = typeof accounts.$inferInsert; +export type Session = typeof sessions.$inferSelect; +export type NewSession = typeof sessions.$inferInsert; +export type VerificationToken = typeof verificationTokens.$inferSelect; +export type NewVerificationToken = typeof verificationTokens.$inferInsert; export type Wishlist = typeof wishlists.$inferSelect; export type NewWishlist = typeof wishlists.$inferInsert; export type Item = typeof items.$inferSelect; diff --git a/src/routes/signup/+page.server.ts b/src/routes/signup/+page.server.ts index 32b072f..1e8b4a2 100644 --- a/src/routes/signup/+page.server.ts +++ b/src/routes/signup/+page.server.ts @@ -1,7 +1,7 @@ import { fail, redirect } from '@sveltejs/kit'; import type { Actions, PageServerLoad } from './$types'; import { db } from '$lib/server/db'; -import { users } from '$lib/db/schema'; +import { users, type NewUser } from '$lib/db/schema'; import { eq } from 'drizzle-orm'; import bcrypt from 'bcrypt'; import { env } from '$env/dynamic/private'; @@ -64,11 +64,12 @@ export const actions: Actions = { const hashedPassword = await bcrypt.hash(password, 14); - await db.insert(users).values({ + const newUser: NewUser = { name: sanitizedName, username: sanitizedUsername, password: hashedPassword - }); + }; + await db.insert(users).values(newUser); throw redirect(303, '/signin?registered=true'); } diff --git a/src/routes/wishlist/[token]/+page.server.ts b/src/routes/wishlist/[token]/+page.server.ts index 281ce3e..7c35365 100644 --- a/src/routes/wishlist/[token]/+page.server.ts +++ b/src/routes/wishlist/[token]/+page.server.ts @@ -1,7 +1,7 @@ import { error } from '@sveltejs/kit'; import type { PageServerLoad, Actions } from './$types'; import { db } from '$lib/server/db'; -import { wishlists, items, reservations, savedWishlists } from '$lib/db/schema'; +import { wishlists, items, reservations, savedWishlists, type NewSavedWishlist } from '$lib/db/schema'; import { eq, and } from 'drizzle-orm'; export const load: PageServerLoad = async ({ params, locals }) => { @@ -143,11 +143,12 @@ export const actions: Actions = { } // Save without ownerToken - user is accessing via public link, so no edit access - await db.insert(savedWishlists).values({ + const newSavedWishlist: NewSavedWishlist = { userId: session.user.id, wishlistId, ownerToken: null // Explicitly set to null - no edit access from reservation view - }); + }; + await db.insert(savedWishlists).values(newSavedWishlist); return { success: true }; }, diff --git a/src/routes/wishlist/[token]/edit/+page.server.ts b/src/routes/wishlist/[token]/edit/+page.server.ts index 7d49db6..977b3bc 100644 --- a/src/routes/wishlist/[token]/edit/+page.server.ts +++ b/src/routes/wishlist/[token]/edit/+page.server.ts @@ -1,7 +1,7 @@ import { error } from '@sveltejs/kit'; import type { PageServerLoad, Actions } from './$types'; import { db } from '$lib/server/db'; -import { wishlists, items, savedWishlists } from '$lib/db/schema'; +import { wishlists, items, savedWishlists, type NewItem, type NewSavedWishlist } from '$lib/db/schema'; import { eq, and } from 'drizzle-orm'; export const load: PageServerLoad = async ({ params, locals }) => { @@ -77,7 +77,7 @@ export const actions: Actions = { return order > max ? order : max; }, 0); - await db.insert(items).values({ + const newItem: NewItem = { wishlistId: wishlist.id, title: title.trim(), description: description?.trim() || null, @@ -87,7 +87,8 @@ export const actions: Actions = { currency: currency?.trim() || 'DKK', color: color?.trim() || null, order: String(maxOrder + 1) - }); + }; + await db.insert(items).values(newItem); return { success: true }; }, @@ -277,12 +278,13 @@ export const actions: Actions = { } // Store the ownerToken - user is accessing via edit link, so they get edit access - await db.insert(savedWishlists).values({ + const newSavedWishlist: NewSavedWishlist = { userId: session.user.id, wishlistId: wishlist.id, ownerToken: wishlist.ownerToken, // Store ownerToken to grant edit access isFavorite: false - }); + }; + await db.insert(savedWishlists).values(newSavedWishlist); return { success: true, message: 'Wishlist claimed successfully' }; },