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:
@@ -32,7 +32,7 @@ COPY --from=deps /app/node_modules ./node_modules
|
|||||||
# Copy Drizzle files for migrations
|
# Copy Drizzle files for migrations
|
||||||
COPY --from=builder /app/drizzle ./drizzle
|
COPY --from=builder /app/drizzle ./drizzle
|
||||||
COPY --from=builder /app/drizzle.config.ts ./
|
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 the port
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
@@ -177,6 +177,12 @@ export const usersRelations = relations(users, ({ many }) => ({
|
|||||||
|
|
||||||
export type User = typeof users.$inferSelect;
|
export type User = typeof users.$inferSelect;
|
||||||
export type NewUser = typeof users.$inferInsert;
|
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 Wishlist = typeof wishlists.$inferSelect;
|
||||||
export type NewWishlist = typeof wishlists.$inferInsert;
|
export type NewWishlist = typeof wishlists.$inferInsert;
|
||||||
export type Item = typeof items.$inferSelect;
|
export type Item = typeof items.$inferSelect;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { fail, redirect } from '@sveltejs/kit';
|
import { fail, redirect } from '@sveltejs/kit';
|
||||||
import type { Actions, PageServerLoad } from './$types';
|
import type { Actions, PageServerLoad } from './$types';
|
||||||
import { db } from '$lib/server/db';
|
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 { eq } from 'drizzle-orm';
|
||||||
import bcrypt from 'bcrypt';
|
import bcrypt from 'bcrypt';
|
||||||
import { env } from '$env/dynamic/private';
|
import { env } from '$env/dynamic/private';
|
||||||
@@ -64,11 +64,12 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 14);
|
const hashedPassword = await bcrypt.hash(password, 14);
|
||||||
|
|
||||||
await db.insert(users).values({
|
const newUser: NewUser = {
|
||||||
name: sanitizedName,
|
name: sanitizedName,
|
||||||
username: sanitizedUsername,
|
username: sanitizedUsername,
|
||||||
password: hashedPassword
|
password: hashedPassword
|
||||||
});
|
};
|
||||||
|
await db.insert(users).values(newUser);
|
||||||
|
|
||||||
throw redirect(303, '/signin?registered=true');
|
throw redirect(303, '/signin?registered=true');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad, Actions } from './$types';
|
import type { PageServerLoad, Actions } from './$types';
|
||||||
import { db } from '$lib/server/db';
|
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';
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params, locals }) => {
|
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
|
// 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,
|
userId: session.user.id,
|
||||||
wishlistId,
|
wishlistId,
|
||||||
ownerToken: null // Explicitly set to null - no edit access from reservation view
|
ownerToken: null // Explicitly set to null - no edit access from reservation view
|
||||||
});
|
};
|
||||||
|
await db.insert(savedWishlists).values(newSavedWishlist);
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad, Actions } from './$types';
|
import type { PageServerLoad, Actions } from './$types';
|
||||||
import { db } from '$lib/server/db';
|
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';
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params, locals }) => {
|
export const load: PageServerLoad = async ({ params, locals }) => {
|
||||||
@@ -77,7 +77,7 @@ export const actions: Actions = {
|
|||||||
return order > max ? order : max;
|
return order > max ? order : max;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
await db.insert(items).values({
|
const newItem: NewItem = {
|
||||||
wishlistId: wishlist.id,
|
wishlistId: wishlist.id,
|
||||||
title: title.trim(),
|
title: title.trim(),
|
||||||
description: description?.trim() || null,
|
description: description?.trim() || null,
|
||||||
@@ -87,7 +87,8 @@ export const actions: Actions = {
|
|||||||
currency: currency?.trim() || 'DKK',
|
currency: currency?.trim() || 'DKK',
|
||||||
color: color?.trim() || null,
|
color: color?.trim() || null,
|
||||||
order: String(maxOrder + 1)
|
order: String(maxOrder + 1)
|
||||||
});
|
};
|
||||||
|
await db.insert(items).values(newItem);
|
||||||
|
|
||||||
return { success: true };
|
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
|
// 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,
|
userId: session.user.id,
|
||||||
wishlistId: wishlist.id,
|
wishlistId: wishlist.id,
|
||||||
ownerToken: wishlist.ownerToken, // Store ownerToken to grant edit access
|
ownerToken: wishlist.ownerToken, // Store ownerToken to grant edit access
|
||||||
isFavorite: false
|
isFavorite: false
|
||||||
});
|
};
|
||||||
|
await db.insert(savedWishlists).values(newSavedWishlist);
|
||||||
|
|
||||||
return { success: true, message: 'Wishlist claimed successfully' };
|
return { success: true, message: 'Wishlist claimed successfully' };
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user