The Prisma client is an auto-generated library that provides type-safe access to your database. It is used as a replacement for traditional ORMs in your API servers or microservice architecure.
// Fetch all posts
const allPosts: Post[] = await prisma.posts()
[
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: false,
id: 'cjnymovq9s3hi0a51oz6rp83k',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Introducing the Analytical Engine'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: true,
id: 'cjnymovqcs3hk0a51qfk40z0b',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Building General-Purpose Computers'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: false,
id: 'cjnymovqjs3hm0a51hfdprde6',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'RESTful APIs Considered Harmful'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: true,
id: 'cjnymovqls3ho0a51rhg7h2xv',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Why Algorithms are Awesome'
},
{
updatedAt: '2018-11-01T13: 31: 14.127Z',
published: false,
id: 'cjnymovv7s3hu0a51j6he7hhs',
createdAt: '2018-11-01T13: 31: 14.127Z',
title: 'Five Things You Didn't Know About Compilers'
},
{
updatedAt: '2018-11-01T13: 31: 14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13: 31: 14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13: 31: 14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13: 31: 14.127Z',
title: 'Progamming with English Words'
},
{
updatedAt: '2018-11-01T13: 31: 14.308Z',
published: false,
id: 'cjnymow08s3i70a51akud49tl',
createdAt: '2018-11-01T13: 31: 14.308Z',
title: 'Working at Xerox PARC'
},
{
updatedAt: '2018-11-01T13: 31: 14.308Z',
published: true,
id: 'cjnymow0as3i90a51muoz70zp',
createdAt: '2018-11-01T13: 31: 14.308Z',
title: 'Introduction to VLSI Systems'
}
]
// Fetch user by ID
const userById: User = await prisma.user({
id: "cjnymovv3s3ht0a516fhmria8"
})
{
id: 'cjnymovv3s3ht0a516fhmria8',
name: 'Grace',
email: 'grace@prisma.io'
}
// Fetch user by email
const userByEmail: User = await prisma.user({
email: "ada@prisma.io"
})
{
id: 'cjnymovq4s3hh0a51ghqhok2f',
name: 'Ada',
email: 'ada@prisma.io'
}
// Retrieving the posts of a user
const postsByUser: Post[] = await prisma
.user({ email: "ada@prisma.io" })
.posts();
[
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: false,
id: 'cjnymovq9s3hi0a51oz6rp83k',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Introducing the Analytical Engine'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: true,
id: 'cjnymovqcs3hk0a51qfk40z0b',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Building General-Purpose Computers'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: false,
id: 'cjnymovqjs3hm0a51hfdprde6',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'RESTful APIs Considered Harmful'
},
{
updatedAt: '2018-11-01T13: 31: 13.948Z',
published: true,
id: 'cjnymovqls3ho0a51rhg7h2xv',
createdAt: '2018-11-01T13: 31: 13.948Z',
title: 'Why Algorithms are Awesome'
}
]
// Retrieving all comments of a post
const commentsOnPost: Comment[] = await prisma
.post({ id: "__POST_ID__" })
.comments();
[
{
id: 'cjnymow0cs3ib0a517qntpuyf',
createdAt: '2018-11-01T13: 31: 14.308Z',
text: 'I love this.'
},
{
id: 'cjnymow0fs3ie0a51yres9ry8',
createdAt: '2018-11-01T13: 31: 14.308Z',
text: 'Very interesting!'
}
]
// Retrieving the comments of a post author
const commentsOfPostAuthor: Comment[] = await prisma
.post({ id: "cjnymovqcs3hk0a51qfk40z0b" })
.author()
.comments();
[
{
id: 'cjnymovvfs3i00a51q1mgzn6o',
createdAt: '2018-11-01T13: 31: 14.127Z',
text: 'Great work!'
},
{
id: 'cjnymow0cs3ib0a517qntpuyf',
createdAt: '2018-11-01T13: 31: 14.308Z',
text: 'I love this.'
}
]
// Fetch all published posts about GraphQL or REST
const posts: Post[] = await prisma.posts({
where: {
OR: [
{ title_contains: "GraphQL" },
{ title_contains: "REST" }
],
published: true
},
orderBy: "createdAt_ASC"
});
[
{
updatedAt: '2018-11-01T13: 31: 14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13: 31: 14.127Z',
title: 'GraphQL - The Query Language of the Future'
}
]
// Fetch all comments of a particular post
// that start with "Great"
const comments: Comment[] = await prisma
.post({
id: "cjnymovvds3hy0a51xtxyhyh2"
})
.comments({
where: {
text_starts_with: "Great"
}
});
[
{
id: 'cjnymovvfs3i00a51q1mgzn6o',
createdAt: '2018-11-01T13: 31: 14.127Z',
text: 'Great work!'
}
]
// Sort posts chronologically
const sortedPosts: Post[] = await prisma.posts({
orderBy: "createdAt_ASC"
});
[{
updatedAt: '2018-11-01T13:31:13.948Z',
published: false,
id: 'cjnymovq9s3hi0a51oz6rp83k',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'Introducing the Analytical Engine'
},
{
updatedAt: '2018-11-01T13:31:13.948Z',
published: true,
id: 'cjnymovqcs3hk0a51qfk40z0b',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'Building General-Purpose Computers'
},
{
updatedAt: '2018-11-01T13:31:13.948Z',
published: false,
id: 'cjnymovqjs3hm0a51hfdprde6',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'RESTful APIs Considered Harmful'
},
{
updatedAt: '2018-11-01T13:31:13.948Z',
published: true,
id: 'cjnymovqls3ho0a51rhg7h2xv',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'Why Algorithms are Awesome'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: false,
id: 'cjnymovv7s3hu0a51j6he7hhs',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Five Things You Didn't Know About Compilers'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Progamming with English Words'
},
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: false,
id: 'cjnymow08s3i70a51akud49tl',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Working at Xerox PARC'
},
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: true,
id: 'cjnymow0as3i90a51muoz70zp',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Introduction to VLSI Systems'
}]
// Fetch the posts from position 6 to position 10
const paginatedPosts1: Post[] = await prisma.posts({
first: 5,
skip: 5
});
[{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Progamming with English Words'
},
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: false,
id: 'cjnymow08s3i70a51akud49tl',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Working at Xerox PARC'
},
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: true,
id: 'cjnymow0as3i90a51muoz70zp',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Introduction to VLSI Systems'
}]
// Fetch the last 3 posts after having
// skipped 2 posts from the end of the list
const paginatedPosts2: Post[] = await prisma.posts({
last: 3,
skip: 2
});
[{
updatedAt: '2018-11-01T13:31:14.127Z',
published: false,
id: 'cjnymovv7s3hu0a51j6he7hhs',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Five Things You Didn't Know About Compilers'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Progamming with English Words'
}]
// Fetch the first 5 posts after a cursor
const paginatedPosts3: Post[] = await prisma.posts({
first: 5,
after: "cjnymovqjs3hm0a51hfdprde6"
});
[{
updatedAt: '2018-11-01T13:31:13.948Z',
published: true,
id: 'cjnymovqls3ho0a51rhg7h2xv',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'Why Algorithms are Awesome'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: false,
id: 'cjnymovv7s3hu0a51j6he7hhs',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Five Things You Didn't Know About Compilers'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Progamming with English Words'
},
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: false,
id: 'cjnymow08s3i70a51akud49tl',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Working at Xerox PARC'
}]
// Fetch the last 5 posts before a cursor
const paginatedPosts4: Post[] = await prisma.posts({
last: 5,
before: "cjnymow08s3i70a51akud49tl"
});
[{
updatedAt: '2018-11-01T13:31:13.948Z',
published: false,
id: 'cjnymovqjs3hm0a51hfdprde6',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'RESTful APIs Considered Harmful'
},
{
updatedAt: '2018-11-01T13:31:13.948Z',
published: true,
id: 'cjnymovqls3ho0a51rhg7h2xv',
createdAt: '2018-11-01T13:31:13.948Z',
title: 'Why Algorithms are Awesome'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: false,
id: 'cjnymovv7s3hu0a51j6he7hhs',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Five Things You Didn't Know About Compilers'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvbs3hw0a51slwjkii7',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'GraphQL - The Query Language of the Future'
},
{
updatedAt: '2018-11-01T13:31:14.127Z',
published: true,
id: 'cjnymovvds3hy0a51xtxyhyh2',
createdAt: '2018-11-01T13:31:14.127Z',
title: 'Progamming with English Words'
}]
// Create a new user
const newUser: User = await prisma.createUser({
name: "Alice",
email: "alice@prisma.io"
});
{
id: 'cjnyr53gasxe00a51i4p2nx8n',
name: 'Alice',
email: 'alice@prisma.io',
role: 'USER'
}
// Update an existing user
const updatedUser: User = await prisma.updateUser({
data: {
role: "ADMIN"
},
where: {
email: "alice@prisma.io"
}
});
{
id: 'cjnyr53gasxe00a51i4p2nx8n',
name: 'Alice',
email: 'alice@prisma.io',
role: 'ADMIN'
}
// Delete an existing user
const deletedUser: User = await prisma.deleteUser({
email: "alice@prisma.io"
});
{
id: 'cjnyr53gasxe00a51i4p2nx8n',
name: 'Alice',
email: 'alice@prisma.io',
role: 'ADMIN'
}
// Create a new user with two posts in a
// single transaction
const newUser: User = await prisma.createUser({
email: "alice@prisma.io",
posts: {
create: [
{
title: "Join us for Prisma Day. June 19, Berlin!"
},
{
title: "Follow Prisma on Twitter!"
}
]
}
});
{
id: 'cjnyrgr97szhf0a51tjyz2e14',
name: null,
email: 'alice@prisma.io'
}
// Change the author of a post in a single transaction
const updatedPost: Post = await prisma.updatePost({
data: {
author: {
connect: {
email: "alice@prisma.io"
}
}
},
where: {
id: "cjnymow0as3i90a51muoz70zp"
}
});
{
updatedAt: '2018-11-01T13:31:14.308Z',
published: true,
id: 'cjnymow0as3i90a51muoz70zp',
createdAt: '2018-11-01T13:31:14.308Z',
title: 'Introduction to VLSI Systems'
}
// Update existing user or create if
// they don't exist yet
const upsertedUser: User = await prisma.upsertUser({
update: {
role: "ADMIN"
},
where: {
email: "alice@prisma.io"
},
create: {
name: "Alice",
email: "alice@prisma.io",
role: "ADMIN"
}
});
{
id: 'cjnyrfmr9szcj0a51802wzot9',
name: null,
email: 'alice@prisma.io'
}
// Grant admin access to all users
// who have a prisma.io-email
const updatedUsers: BatchPayload = await prisma
.updateManyUsers({
data: {
role: "ADMIN"
},
where: {
email_ends_with: "@prisma.io"
}
});
{ count: 3 }
// Delete two users with specific email adresses
// (emails that don't exist are silently ignored)
const deletedUsers: BatchPayload = await prisma
.deleteManyUsers({
email_in: ["alice@prisma.io", "bob@prisma.io"]
});
{ count: 1 }
// Fetch only the ID of a user with a specific email
const user: any = await prisma
.user({ email: "ada@prisma.io" })
.$fragment(`fragment UserId on User { id }`);
{ id: 'cjnyt92ujtbt50a51c5orfb3s' }
// Fetch all posts with their authors and comments,
// retrieving only specific fields
const posts: any = await prisma.posts().$fragment(`
fragment PostWithAuthorsAndComments on Post {
title
author { name }
comments { text }
}
`);
[
{
"title": "Introducing the Analytical Engine",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Building General-Purpose Computers",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "RESTful APIs Considered Harmful",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Why Algorithms are Awesome",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Five Things You Didn't Know About Compilers",
"author": {
"name": "Grace"
},
"comments": []
},
{
"title": "GraphQL - The Query Language of the Future",
"author": {
"name": "Grace"
},
"comments": []
},
{
"title": "Progamming with English Words",
"author": {
"name": "Grace"
},
"comments": [
{
"text": "Great work!"
}
]
},
{
"title": "Working at Xerox PARC",
"author": {
"name": "Lynn"
},
"comments": []
},
{
"title": "Introduction to VLSI Systems",
"author": {
"name": "Lynn"
},
"comments": [
{
"text": "I love this."
},
{
"text": "Very interesting!"
}
]
}
]
// Fetch all posts with their authors and comments,
// retrieving only specific fields
const query = `
query {
posts {
title
author {
name
}
comments {
text
}
}
}
`;
const postsWithAuthorAndComments: any = await prisma.$graphql(query);
{
"posts": [
{
"title": "Introducing the Analytical Engine",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Building General-Purpose Computers",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "RESTful APIs Considered Harmful",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Why Algorithms are Awesome",
"author": {
"name": "Ada"
},
"comments": []
},
{
"title": "Five Things You Didn't Know About Compilers",
"author": {
"name": "Grace"
},
"comments": []
},
{
"title": "GraphQL - The Query Language of the Future",
"author": {
"name": "Grace"
},
"comments": []
},
{
"title": "Progamming with English Words",
"author": {
"name": "Grace"
},
"comments": [
{
"text": "Great work!"
}
]
},
{
"title": "Working at Xerox PARC",
"author": {
"name": "Lynn"
},
"comments": []
},
{
"title": "Introduction to VLSI Systems",
"author": {
"name": "Lynn"
},
"comments": [
{
"text": "I love this."
},
{
"text": "Very interesting!"
}
]
}
]
}
// Create a new user
const mutation = `
mutation($email: String!) {
createUser(data: {
email: $email
}) {
id
}
}
`;
const variables = { email: "bob@prisma.io" };
const newUser: any = await prisma.$graphql(mutation, variables);
{
"createUser": {
"id": "cjnyt99c3tbv90a51xow43iu3"
}
}
// Fires for every write on `User`
const userIterator: AsyncIterator<User> = await prisma
.$subscribe
.user()
.node();
// Endless loop waiting for write-events
while (true) {
const result: IteratorResult<User> = await userIterator.next();
}
{
id: 'cjnyssxjqt8hf0a51u6gr923x',
name: 'Bob',
email: 'bob@prisma.io',
role: 'ADMIN'
}
const newPostIterator: AsyncIterator<Post> = await prisma
.$subscribe
.post({
mutation_in: ["CREATED"],
node: {
OR: [
{ title_contains: "GraphQL" },
{ title_contains: "REST" }
]
}
})
.node();
// Endless loop waiting for write-events
while (true) {
const result: IteratorResult<Post> = await newPostIterator.next();
}
{
updatedAt: '2018-11-01T16:28:05.325Z',
published: false,
id: 'cjnyt0bilt9tr0a511751giub',
createdAt: '2018-11-01T16:28:05.325Z',
title: 'Join us for Prisma Day. June 19, Berlin!'
}
type Post {
id: ID! @id
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
title: String!
published: Boolean!
author: User!
}
type User {
id: ID! @id
name: String
email: String! @unique
accessRole: AccessRole!
posts: [Post!]!
}
type Comment {
id: ID! @id
createdAt: DateTime! @createdAt
text: String!
writtenBy: User!
}
enum AccessRole {
USER
ADMIN
}
Get started with a practical example to explore the different use cases of the Prisma client. Follow the setup instructions and you’re up-and-running with just a few commands.
Learn how Prisma is used to build GraphQL servers. The Prisma client manages data access in your GraphQL resolver functions.
When building a REST API with Prisma, the Prisma client is used as a replacement for traditional ORMs.