Update prisma models for table names, adding user roles, and updating lucia for these changes.

This commit is contained in:
Bradley Shellnut 2023-06-20 11:54:47 -07:00
parent 952dbb0d3c
commit 5a8726f7f0
4 changed files with 75 additions and 72 deletions

View file

@ -15,37 +15,42 @@ model Role {
id String @id @default(cuid()) id String @id @default(cuid())
name String @unique name String @unique
userRoles UserRole[] userRoles UserRole[]
@@map("roles")
} }
model UserRole { model UserRole {
id String @id @default(cuid()) id String @id @default(cuid())
user AuthUser @relation(fields: [userId], references: [id]) user AuthUser @relation(fields: [user_id], references: [id])
userId String user_id String
role Role @relation(fields: [roleId], references: [id]) role Role @relation(fields: [role_id], references: [id])
roleId String role_id String
created_at DateTime @default(now()) @db.Timestamp(6)
updated_at DateTime @updatedAt @db.Timestamp(6)
@@unique([userId, roleId]) @@unique([user_id, role_id])
@@index([userId]) @@index([user_id])
@@index([roleId]) @@index([role_id])
@@map("user_roles")
} }
model AuthUser { model AuthUser {
id String @id @default(cuid()) id String @id @default(cuid())
username String @unique username String @unique
email String? @unique email String? @unique
firstName String? firstName String?
lastName String? lastName String?
roles UserRole[] roles UserRole[]
verified Boolean @default(false) verified Boolean @default(false)
receiveEmail Boolean @default(false) receiveEmail Boolean @default(false)
token String? @unique token String? @unique
collection Collection? collection Collection?
wishlist Wishlist[] wishlist Wishlist[]
theme String @default("system") theme String @default("system")
createdAt DateTime @default(now()) @db.Timestamp(6) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt @db.Timestamp(6) updated_at DateTime @updatedAt @db.Timestamp(6)
auth_session AuthSession[] auth_session AuthSession[]
auth_key AuthKey[] auth_key AuthKey[]
@@map("auth_user") @@map("auth_user")
} }
@ -86,7 +91,7 @@ model Collection {
model CollectionItem { model CollectionItem {
id String @id @default(cuid()) id String @id @default(cuid())
collection_id String collection_id String
collection Collection @relation(references: [id], fields: [collection_id]) collection Collection @relation(references: [id], fields: [collection_id], onDelete: Cascade)
game_id String game_id String
game Game @relation(references: [id], fields: [game_id]) game Game @relation(references: [id], fields: [game_id])
times_played Int times_played Int
@ -109,9 +114,11 @@ model Wishlist {
model WishlistItem { model WishlistItem {
id String @id @default(cuid()) id String @id @default(cuid())
wishlist_id String wishlist_id String
wishlist Wishlist @relation(references: [id], fields: [wishlist_id]) wishlist Wishlist @relation(references: [id], fields: [wishlist_id], onDelete: Cascade)
game_id String game_id String
game Game @relation(references: [id], fields: [game_id]) game Game @relation(references: [id], fields: [game_id])
created_at DateTime @default(now()) @db.Timestamp(6)
updated_at DateTime @updatedAt @db.Timestamp(6)
@@index([game_id, wishlist_id]) @@index([game_id, wishlist_id])
@@map("wishlist_items") @@map("wishlist_items")
@ -120,23 +127,24 @@ model WishlistItem {
model Game { model Game {
id String @id @default(cuid()) id String @id @default(cuid())
name String name String
slug String
description String? description String?
yearPublished Int? year_published Int?
minPlayers Int? min_players Int?
maxPlayers Int? max_players Int?
minPlaytime Int? min_playtime Int?
maxPlaytime Int? max_playtime Int?
minAge Int? min_age Int?
imageUrl String? image_url String?
thumbUrl String? thumb_url String?
url String? url String?
rulesUrl String? rules_url String?
weightAmount Float? weight_amount Float?
weightUnits String? weight_units String?
bggId String? bggId String?
bggUrl String? bggUrl String?
primary_publisher_id String primary_publisher_id String
primaryPublisher Publisher? @relation("PrimaryPublishers", references: [id], fields: [primary_publisher_id]) primary_publisher Publisher? @relation("PrimaryPublishers", references: [id], fields: [primary_publisher_id])
categories Category[] categories Category[]
mechanics Mechanic[] mechanics Mechanic[]
designers Designer[] designers Designer[]
@ -147,8 +155,8 @@ model Game {
collection_items CollectionItem[] collection_items CollectionItem[]
wishlist_items WishlistItem[] wishlist_items WishlistItem[]
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@index([primary_publisher_id]) @@index([primary_publisher_id])
@@map("games") @@map("games")
@ -159,8 +167,8 @@ model GameName {
name String name String
game_id String game_id String
game Game @relation(references: [id], fields: [game_id]) game Game @relation(references: [id], fields: [game_id])
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@index([game_id]) @@index([game_id])
@@map("game_names") @@map("game_names")
@ -170,10 +178,10 @@ model Publisher {
id String @id @default(cuid()) id String @id @default(cuid())
name String name String
games Game[] games Game[]
primaryPublisher Game[] @relation("PrimaryPublishers") primary_publisher Game[] @relation("PrimaryPublishers")
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@map("publishers") @@map("publishers")
} }
@ -184,8 +192,8 @@ model Category {
slug String slug String
games Game[] games Game[]
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@map("categories") @@map("categories")
} }
@ -195,8 +203,8 @@ model Mechanic {
name String name String
games Game[] games Game[]
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@map("mechanics") @@map("mechanics")
} }
@ -206,8 +214,8 @@ model Designer {
name String name String
games Game[] games Game[]
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@map("designers") @@map("designers")
} }
@ -217,8 +225,8 @@ model Artist {
name String name String
games Game[] games Game[]
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@map("artists") @@map("artists")
} }
@ -226,12 +234,12 @@ model Artist {
model Expansion { model Expansion {
id String @id @default(cuid()) id String @id @default(cuid())
name String name String
yearPublished Int? year_published Int?
baseGame Game? @relation(fields: [base_game_id], references: [id]) baseGame Game? @relation(fields: [base_game_id], references: [id])
base_game_id String? base_game_id String?
external_id String @unique external_id String @unique
createdAt DateTime @default(now()) created_at DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt updated_at DateTime @updatedAt @db.Timestamp(6)
@@index([base_game_id]) @@index([base_game_id])
@@map("expansions") @@map("expansions")

16
src/app.d.ts vendored
View file

@ -1,6 +1,11 @@
// See https://kit.svelte.dev/docs/types#app // See https://kit.svelte.dev/docs/types#app
// for information about these interfaces // for information about these interfaces
// and what to do when importing types // and what to do when importing types
import type { AuthUser } from '@prisma/client';
type User = Omit<AuthUser, 'id' | 'created_at' | 'updated_at'>;
// src/app.d.ts // src/app.d.ts
declare global { declare global {
namespace App { namespace App {
@ -33,16 +38,7 @@ declare global {
declare global { declare global {
namespace Lucia { namespace Lucia {
type Auth = import('$lib/lucia').Auth; type Auth = import('$lib/lucia').Auth;
type UserAttributes = { type UserAttributes = User;
email: string;
username: string;
firstName: string;
lastName: string;
role: string;
verified: boolean;
receiveEmail: boolean;
token: string;
};
} }
} }

View file

@ -1,15 +1,15 @@
import prisma from '$lib/prisma'; import prisma from '$lib/prisma';
export async function add_user_to_role(userId: string, roleName: string) { export async function add_user_to_role(user_id: string, role_name: string) {
// Find the role by its name // Find the role by its name
const role = await prisma.role.findUnique({ const role = await prisma.role.findUnique({
where: { where: {
name: roleName name: role_name
} }
}); });
if (!role) { if (!role) {
throw new Error(`Role with name ${roleName} not found`); throw new Error(`Role with name ${role_name} not found`);
} }
// Create a UserRole entry linking the user and the role // Create a UserRole entry linking the user and the role
@ -17,7 +17,7 @@ export async function add_user_to_role(userId: string, roleName: string) {
data: { data: {
user: { user: {
connect: { connect: {
id: userId id: user_id
} }
}, },
role: { role: {

View file

@ -11,12 +11,11 @@ export const auth = lucia({
middleware: sveltekit(), middleware: sveltekit(),
transformDatabaseUser: (userData) => { transformDatabaseUser: (userData) => {
return { return {
userId: userData.id, id: userData.id,
username: userData.username, username: userData.username,
email: userData.email, email: userData.email,
firstName: userData.firstName, firstName: userData.firstName,
lastName: userData.lastName, lastName: userData.lastName,
role: userData.role,
verified: userData.verified, verified: userData.verified,
receiveEmail: userData.receiveEmail, receiveEmail: userData.receiveEmail,
token: userData.token token: userData.token