TypeScript

Prisma Client

Get started

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.

Reading Data
Writing Data
More Features

Lists & Single Objects

Run examples
// Fetch all posts
const allPosts: Post[] = await prisma.posts()
Show the result
[
  {
    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"
})
Show the result
{
  id: 'cjnymovv3s3ht0a516fhmria8',
  name: 'Grace',
  email: 'grace@prisma.io'
}
// Fetch user by email
const userByEmail: User = await prisma.user({
  email: "ada@prisma.io"
})
Show the result
{
  id: 'cjnymovq4s3hh0a51ghqhok2f',
  name: 'Ada',
  email: 'ada@prisma.io'
}

Fluent API

Run examples
// Retrieving the posts of a user
const postsByUser: Post[] = await prisma
  .user({ email: "ada@prisma.io" })
  .posts();
Show the result
[
  {
    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();
Show the result
[
  {
    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();
Show the result
[
  {
    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.'
  }
]

Filtering & Sorting

Run examples
// 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"
});
Show the result
[
  {
    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"
    }
  });
Show the result
[
  {
    id: 'cjnymovvfs3i00a51q1mgzn6o',
    createdAt: '2018-11-01T13: 31: 14.127Z',
    text: 'Great work!'
  }
]
// Sort posts chronologically
const sortedPosts: Post[] = await prisma.posts({
  orderBy: "createdAt_ASC"
});
Show the result
[{
  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'
}]

Pagination

Run examples
// Fetch the posts from position 6 to position 10
const paginatedPosts1: Post[] = await prisma.posts({
  first: 5,
  skip: 5
});
Show the result
[{
  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
});
Show the result
[{
  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"
});
Show the result
[{
  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"
});
Show the result
[{
  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'
}]

Batching

Run examples
// 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"
  }
  });
Show the result
{ 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"]
  });
Show the result
{ count: 1 }

Create, Updates & Delete

Run examples
// Create a new user
const newUser: User = await prisma.createUser({
  name: "Alice",
  email: "alice@prisma.io"
});
Show the result
{
  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"
  }
});
Show the result
{
  id: 'cjnyr53gasxe00a51i4p2nx8n',
  name: 'Alice',
  email: 'alice@prisma.io',
  role: 'ADMIN'
}
// Delete an existing user
const deletedUser: User = await prisma.deleteUser({
  email: "alice@prisma.io"
});
Show the result
{
  id: 'cjnyr53gasxe00a51i4p2nx8n',
  name: 'Alice',
  email: 'alice@prisma.io',
  role: 'ADMIN'
}

Declarative Nested Writes

Run examples
// 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 GraphQL Conf in 2018"
      },
      {
        title: "Subscribe to GraphQL Weekly for GraphQL news"
      }
    ]
  }
});
Show the result
{
  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"
  }
});
Show the result
{
  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 update 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"
  }
});
Show the result
{
  id: 'cjnyrfmr9szcj0a51802wzot9',
  name: null,
  email: 'alice@prisma.io'
}

Field Selection

Run examples
// 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 }`);
Show the result
{ 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 }
}
`);
Show the result
[
  {
    "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!"
      }
    ]
  }
]

Native GraphQL

Run examples
// 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);
Show the result
{
  "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);
Show the result
{
  "createUser": {
    "id": "cjnyt99c3tbv90a51xow43iu3"
  }
}

Realtime Subscriptions

Run examples
// 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();
}
Show the result
{
  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();
}
Show the result
{
  updatedAt: '2018-11-01T16:28:05.325Z',
  published: false,
  id: 'cjnyt0bilt9tr0a511751giub',
  createdAt: '2018-11-01T16:28:05.325Z',
  title: 'Join us for GraphQL Conf 2019'
}
Datamodel
type Post {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  title: String!
  published: Boolean!
  author: User!
}

type User {
  id: ID! @unique
  name: String
  email: String! @unique
  accessRole: AccessRole!
  posts: [Post!]!
}

type Comment {
  id: ID! @unique
  createdAt: DateTime!
  text: String!
  writtenBy: User!
}

enum AccessRole {
  USER
  ADMIN
}
Read about the datamodel
Supported
Coming Soon

Data Access Features

Seamless JOIN & Relation API
Raw Database Access
Powerful Filter API
Supports SQL & NoSQL DBs
Declarative Nested Writes
Transactions
Realtime Event System
Connection Pooling
Advanced Pagination

Examples of using Prisma client

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.

GraphQL API server

Learn how Prisma is used to build GraphQL servers. The Prisma client manages data access in your GraphQL resolver functions.

REST API server

When building a REST API with Prisma, the Prisma client is used as a replacement for traditional ORMs.