26
loading...
This website collects cookies to deliver better user experience
User
table, and an Article
table. Users
can have many Articles
, and each Article
only has one author.yarn add prisma -D
.prisma
at the root of your project, you can create a file called schema.prisma
. This is where the descriptions (or "schemas") of your tables will be stored.migrations
folder for now.datasource
block.database.db
.datasource db {
provider = "sqlite"
url = "file:./database.db"
}
.env
, allowing for a portable development environment.datasource db {
provider = "postgres"
// Access the DATABASE_URL variable.
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
User
table, and an Article
table. Users
can have many Articles
, and each Article
only has one author.model
block:model User {
// Our fields (columns) go here...
}
model User {
id Int
email String
name String
}
@id
decorator. This is a simple app, so we'll make its value automatically increment for each user.model User {
id Int @id @default(autoincrement())
email String
name String
}
@unique
decorator.model User {
id Int @id @default(autoincrement())
email String @unique
name String
}
Article
model. We'll make an ID field in the same way as before, and also add a title field, a content field, and a field to specify when the article was published. Finally, we'll add an authorId
field for the ID of the user who authored the article.model Article {
id Int @id @default(autoincrement())
authorId Int
title String
content String
publishedAt DateTime
}
authorId
, but wouldn't it be nice if there were a field called author
which had the type User? With Prisma, we can actually make this happen!model Article {
id Int @id @default(autoincrement())
authorId Int
author User
title String
content String
publishedAt DateTime
}
@relation
decorator.@relation
decorator uses this syntax:@relation(fields: [authorId], references: [id])
fields
attribute specifies which field of the Article
references the id of the author. The references
attribute specifies which field of the User table the fields
attribute points to.model Article {
id Int @id @default(autoincrement())
authorId Int
author User @relation(fields: [authorId], references: [id])
title String
content String
publishedAt DateTime
}
articles
field to the User
model. We'll make it have the type Article[]
.model User {
id Int @id @default(autoincrement())
email String @unique
name String
articles Article[]
}
schema.prisma
for this article.schema.prisma
looks like:datasource db {
provider = "sqlite"
url = "file:./database.db"
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String
articles Article[]
}
model Article {
id Int @id @default(autoincrement())
authorId Int
author User @relation(fields: [authorId], references: [id])
title String
content String
publishedAt DateTime
}
yarn prisma migrate dev
dev
at the end specifies that we're working in the development environment.initial migration
.yarn prisma generate
.index.js
. Prisma also has built-in Typescript support.PrismaClient
class from @prisma/client
.const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
prisma.[table name].create()
.prisma.user.create({
data: {
name: 'Michael Fatemi',
email: '<REDACTED>',
},
});
prisma.user.create({
select: {
id: true
},
data: {
name: 'Michael Fatemi',
email: '<REDACTED>',
},
});
authorId
manually, as this might violate the foreign key constraint created by SQL. So, we must specify that we are "connecting" a User to the article, through the syntax shown below.async function createArticle(authorId, title, content) {
prisma.article.create({
data: {
author: {
connect: {
id: authorId,
},
},
content,
title,
publishedAt: new Date(),
},
});
}
async function getArticles(userId) {
return await prisma.user.findFirst({
select: {
articles: true
},
where: {
id: userId
}
})
}
lt
), greater than (gt
), equal to (eq
), or others by passing an object to the where
clause instead of a value.async function getUsersWhoWroteAnArticleBefore(date) {
return await prisma.user.findMany({
select: {
id: true,
name: true
},
where: {
articles: {
some: {
publishedAt: {
lt: date
}
}
}
}
})
}