Created
May 16, 2025 12:35
-
-
Save gdpp/983530d69dbc32490d61ee52811ea111 to your computer and use it in GitHub Desktop.
Prisma snippet foundation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { PrismaClient } from './generated/prisma' | |
| const prisma = new PrismaClient(); | |
| async function main(){ | |
| // Prisma queries | |
| // Create user example | |
| const user = await prisma.user.create({ | |
| data:{ | |
| name: "John Doe", | |
| email: "[email protected]" | |
| } | |
| }); | |
| // Get all users | |
| const users = await prisma.user.findMany(); | |
| // Create article and associate it with user | |
| const article = await prisma.article.create({ | |
| data: { | |
| title: "Second Gustavo's Article", | |
| body: "Lorem 2 ipsum ament sit 2 regailiz dolore 2 stunt...", | |
| author: { | |
| connect: { | |
| id: 2 | |
| } | |
| } | |
| } | |
| }); | |
| // Get all articles | |
| const articles = await prisma.article.findMany(); | |
| // Create User and Article and associate them | |
| const user = await prisma.user.create({ | |
| data: { | |
| name: "Gustavo Perez", | |
| email: "[email protected]", | |
| articles: { | |
| create: { | |
| title: "First Gustavo's Article", | |
| body: "Lorem ipsum ament sit regailiz dolore stunt...", | |
| } | |
| } | |
| } | |
| }) | |
| // Get all users with articles | |
| const users = await prisma.user.findMany({ | |
| include: { | |
| articles: true, | |
| } | |
| }); | |
| // Loop over Gustavo's articles | |
| const users = await prisma.user.findMany({ | |
| include: { | |
| articles: true, | |
| } | |
| }); | |
| users.forEach((user) => { | |
| console.log(`User: ${user.name}, Email: ${user.email}`) | |
| console.log('Articles:') | |
| user.articles.forEach((article) => { | |
| console.log(`- Title: ${article.title}, Body: ${article.body}`) | |
| }) | |
| console.log('\n') | |
| }) | |
| // Update Data | |
| const user = await prisma.user.update({ | |
| where: { | |
| id: 1 | |
| }, | |
| data: { | |
| name: "Jane", | |
| email: "[email protected]" | |
| } | |
| }) | |
| // Remove record | |
| const article = await prisma.article.delete({ | |
| where: { | |
| id: 2 | |
| } | |
| }) | |
| } | |
| main() | |
| .then(async () => { | |
| await prisma.$disconnect(); | |
| }) | |
| .catch(async (error) => { | |
| console.log(error); | |
| await prisma.$disconnect(); | |
| process.exit(1); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is your Prisma schema file, | |
| // learn more about it in the docs: https://pris.ly/d/prisma-schema | |
| generator client { | |
| provider = "prisma-client-js" | |
| output = "../generated/prisma" | |
| } | |
| datasource db { | |
| provider = "sqlite" | |
| url = env("DATABASE_URL") | |
| } | |
| model User { | |
| id Int @id @default(autoincrement()) | |
| email String @unique | |
| name String? | |
| articles Article[] | |
| } | |
| model Article { | |
| id Int @id @default(autoincrement()) | |
| title String | |
| body String? | |
| authorId Int | |
| author User @relation(fields: [authorId], references: [id]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment