💩 prisma crud operations
Lately I've been working with PostgreSQL databases, using Prisma as the ORM to make life easier. I always forget how to do basic CRUD operations, so here's a little reference post that hopefully helps you as much as it helps me!
🆕 create
const createTodo = async (userId: string, content: string) => {
const todo = await db.todo.create({
data: {
content,
userId,
},
})
return todo
}
📚 read
const getTodos = async (userId: string) => {
const todos = await db.todo.findMany({
where: {
userId: {
equals: userId,
},
},
})
return todos
}
☝🏼 update
const updateTodo = async (id: string, data: Partial<Todo>) => {
const todo = await db.todo.update({
where: {
id,
},
data,
})
return todo
}
⛔ delete
const deleteTodo = async (id: string) => {
const todo = await db.todo.delete({
where: {
id,
},
})
return todo
}