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
This commit is contained in:
Rasmus Q
2026-03-15 21:38:54 +00:00
parent 6ae82d758e
commit bc680fb60b
5 changed files with 22 additions and 12 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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');
}

View File

@@ -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 };
},

View File

@@ -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' };
},