prisma create code architecture

Hi guys I am relatively new to prisma amd I am wondering is this the correct way to use the create directive since it seems the code will be slower than an alternative Also I am talking about the add session
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

class DB {
// Method to create a user
async createUser(name: string, email: string | null = null) {
try {
const newUser = await prisma.user.create({
data: {
name: name,
email: email,
is_premium: false,
sessions: {
create: []
}
}
});
return newUser;
} catch (error) {
console.error("Error creating user:", error);
throw error;
}
}

// Method to add session to a user
async addSession(userId: number, session: { creation_date: Date, baseSnapshotId: number }) {
try {
const updatedUser = await prisma.user.update({
where: { id: userId },
data: {
sessions: {
create: {
creation_date: session.creation_date,
baseSnapshotId: session.baseSnapshotId
}
}
},
include: {
sessions: true
}
});
return updatedUser;
} catch (error) {
console.error("Error adding session to user:", error);
throw error;
}
}
}
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

class DB {
// Method to create a user
async createUser(name: string, email: string | null = null) {
try {
const newUser = await prisma.user.create({
data: {
name: name,
email: email,
is_premium: false,
sessions: {
create: []
}
}
});
return newUser;
} catch (error) {
console.error("Error creating user:", error);
throw error;
}
}

// Method to add session to a user
async addSession(userId: number, session: { creation_date: Date, baseSnapshotId: number }) {
try {
const updatedUser = await prisma.user.update({
where: { id: userId },
data: {
sessions: {
create: {
creation_date: session.creation_date,
baseSnapshotId: session.baseSnapshotId
}
}
},
include: {
sessions: true
}
});
return updatedUser;
} catch (error) {
console.error("Error adding session to user:", error);
throw error;
}
}
}
The method I think will be faster is to directly create a session and link it to a user
0 Replies
No replies yetBe the first to reply to this messageJoin