JavaScript
PostgreSQL

Write your first query with Prisma Client

Now that you have generated the Prisma Client, you can start writing queries to read and write data in your database.

If you're building a REST API, you can use Prisma Client in your route handlers to read and write data in the database based on incoming HTTP requests. If you're building a GraphQL API, you can use Prisma Client in your resolvers to read and write data in the database based on incoming queries and mutations.

For the purpose of this guide however, you'll just create a plain Node.js script to learn how to send queries to your database using Prisma Client. Once you have an understanding of how the API works, you can start integrating it into your actual application code (e.g. REST route handlers or GraphQL resolvers).

Create a new file named index.ts and add the following code to it:

index.ts
1import { PrismaClient } from '@prisma/client'
2
3const prisma = new PrismaClient()
4
5async function main() {
6 // ... you will write your Prisma Client queries here
7}
8
9main()
10 .catch((e) => {
11 throw e
12 })
13 .finally(async () => {
14 await prisma.$disconnect()
15 })

Create a new file named index.js and add the following code to it:

index.js
1const { PrismaClient } = require('@prisma/client')
2
3const prisma = new PrismaClient()
4
5async function main() {
6 // ... you will write your Prisma Client queries here
7}
8
9main()
10 .catch((e) => {
11 throw e
12 })
13 .finally(async () => {
14 await prisma.$disconnect()
15 })

Here's a quick overview of the different parts of the code snippet:

  1. Import the PrismaClient constructor from the @prisma/client node module
  2. Instantiate PrismaClient
  3. Define an async function named main to send queries to the database
  4. Call the main function
  5. Close the database connections when the script terminates

Depending on what your models look like, the Prisma Client API will look different as well. For example, if you have a User model, your PrismaClient instance exposes a property called user on which you can call CRUD methods like findMany, create or update. The property is named after the model, but the first letter is lowercased (so for the Post model it's called post, for Profile it's called profile).

The following examples are all based on the models in the Prisma schema.

Inside the main function, add the following query to read all User records from the database and print the result:

index.ts
1async function main() {
2 const allUsers = await prisma.user.findMany()
3 console.log(allUsers)
4}
index.js
1async function main() {
2 const allUsers = await prisma.user.findMany()
3 console.log(allUsers)
4}

Now run the code with your current TypeScript setup. If you're using ts-node, you can run it like this:

$npx ts-node index.ts

Now run the code with this command:

$node index.js

This will print an array of User records as plain old JavaScript objects.

Write data into the database

The findMany query you used in the previous section only reads data from the database. In this section, you'll learn how to write a query to write new records into the Post and User tables.

Adjust the main function to send a create query to the database:

index.ts
1async function main() {
2 await prisma.user.create({
3 data: {
4 name: 'Alice',
5 email: 'alice@prisma.io',
6 posts: {
7 create: { title: 'Hello World' },
8 },
9 profile: {
10 create: { bio: 'I like turtles' },
11 },
12 },
13 })
14
15 const allUsers = await prisma.user.findMany({
16 include: {
17 posts: true,
18 profile: true,
19 },
20 })
21 console.dir(allUsers, { depth: null })
22}
index.js
1async function main() {
2 await prisma.user.create({
3 data: {
4 name: 'Alice',
5 email: 'alice@prisma.io',
6 posts: {
7 create: { title: 'Hello World' },
8 },
9 profile: {
10 create: { bio: 'I like turtles' },
11 },
12 },
13 })
14
15 const allUsers = await prisma.user.findMany({
16 include: {
17 posts: true,
18 profile: true,
19 },
20 })
21 console.dir(allUsers, { depth: null })
22}

This code creates a new User record together with new Post and Profile records using a nested write query. The User record is connected to the two other ones via the Post.authorUser.posts and Profile.userUser.profile relation fields respectively.

Notice that you're passing the include option to findMany which tells Prisma Client to include the posts and profile relations on the returned User objects.

Run the code with your current TypeScript setup. If you're using ts-node, you can run it like this:

$npx ts-node index.ts

Run the code with this command:

$node index.js

Before moving on to the next section, you'll "publish" the Post record you just created using an update query. Adjust the main function as follows:

index.ts
1async function main() {
2 const post = await prisma.post.update({
3 where: { id: 1 },
4 data: { published: true },
5 })
6 console.log(post)
7}
index.js
1async function main() {
2 const post = await prisma.post.update({
3 where: { id: 1 },
4 data: { published: true },
5 })
6 console.log(post)
7}

Run the code with your current TypeScript setup. If you're using ts-node, you can run it like this:

$npx ts-node index.ts

Now run the code using the same command as before:

$node index.js
Edit this page on GitHub