How to perform CRUD operations in Prisma library

Key takeaways:

  • Prisma is an open-source ORM for Node.js and TypeScript that simplifies database access and data modeling.

  • To begin using Prisma, install it and connect to your database by specifying the DATABASE_URL in the schema.prisma file.

  • Define your database tables using the model keyword in the schema.prisma file.

  • To insert data into tables, use the create() method for single entries and createMany() for bulk entries, with an option to skip duplicates.

  • Use findMany() and findUnique() methods to read data from tables, retrieving all entries or specific records based on unique identifiers.

  • Update existing records with the update() method, and use updateMany() to modify multiple records simultaneously.

  • The delete() method removes specific records from tables, while deleteMany() allows for bulk deletion of entries.

  • Prisma’s CRUD operations enhance database interactions, making it easier to manage data in applications and improve user experiences.

Prisma is an open-source database toolkit and ORM (Object-Relational Mapping) for Node.js and TypeScript. It simplifies database access, data modeling, and querying by providing a type-safe and fluent API for working with databases. Let’s explore how to perform CRUD operations in Prisma. We will take a look at all possible patterns to create, read, update, and delete data using Primsa.

Let’s go over the prerequisites so we can see how we can perform CRUD operations in Prisma:

Firstly, install Prisma and connect to your database by providing the DATABASE_URL in the schema.prisma file. We have provided the DATABASE_URL in the .env file. We will also define the tables for our database in schema.prisma file using the model keyword:

{
  "name": "hello-prisma",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "hello-prisma",
      "version": "1.0.0",
      "license": "ISC",
      "devDependencies": {
        "prisma": "^5.8.1"
      }
    },
    "node_modules/@prisma/debug": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.8.1.tgz",
      "integrity": "sha512-tjuw7eA0Us3T42jx9AmAgL58rzwzpFGYc3R7Y4Ip75EBYrKMBA1YihuWMcBC92ILmjlQ/u3p8VxcIE0hr+fZfg==",
      "dev": true
    },
    "node_modules/@prisma/engines": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.8.1.tgz",
      "integrity": "sha512-TJgYLRrZr56uhqcXO4GmP5be+zjCIHtLDK20Cnfg+o9d905hsN065QOL+3Z0zQAy6YD31Ol4u2kzSfRmbJv/uA==",
      "dev": true,
      "hasInstallScript": true,
      "dependencies": {
        "@prisma/debug": "5.8.1",
        "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
        "@prisma/fetch-engine": "5.8.1",
        "@prisma/get-platform": "5.8.1"
      }
    },
    "node_modules/@prisma/engines-version": {
      "version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
      "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2.tgz",
      "integrity": "sha512-f5C3JM3l9yhGr3cr4FMqWloFaSCpNpMi58Om22rjD2DOz3owci2mFdFXMgnAGazFPKrCbbEhcxdsRfspEYRoFQ==",
      "dev": true
    },
    "node_modules/@prisma/fetch-engine": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.8.1.tgz",
      "integrity": "sha512-+bgjjoSFa6uYEbAPlklfoVSStOEfcpheOjoBoNsNNSQdSzcwE2nM4Q0prun0+P8/0sCHo18JZ9xqa8gObvgOUw==",
      "dev": true,
      "dependencies": {
        "@prisma/debug": "5.8.1",
        "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
        "@prisma/get-platform": "5.8.1"
      }
    },
    "node_modules/@prisma/get-platform": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.8.1.tgz",
      "integrity": "sha512-wnA+6HTFcY+tkykMokix9GiAkaauPC5W/gg0O5JB0J8tCTNWrqpnQ7AsaGRfkYUbeOIioh6woDjQrGTTRf1Zag==",
      "dev": true,
      "dependencies": {
        "@prisma/debug": "5.8.1"
      }
    },
    "node_modules/prisma": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.8.1.tgz",
      "integrity": "sha512-N6CpjzECnUHZ5beeYpDzkt2rYpEdAeqXX2dweu6BoQaeYkNZrC/WJHM+5MO/uidFHTak8QhkPKBWck1o/4MD4A==",
      "dev": true,
      "hasInstallScript": true,
      "dependencies": {
        "@prisma/engines": "5.8.1"
      },
      "bin": {
        "prisma": "build/index.js"
      },
      "engines": {
        "node": ">=16.13"
      }
    }
  }
}

Inserting data in a table (Create)

Firstly, we will learn how we can insert one entry or multiple entries in bulk into our tables using the create() and createMany() methods.

Note: To run this code execute the cd usercode && node create.js command in the terminal below.

{
  "name": "hello-prisma",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "hello-prisma",
      "version": "1.0.0",
      "license": "ISC",
      "devDependencies": {
        "prisma": "^5.8.1"
      }
    },
    "node_modules/@prisma/debug": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.8.1.tgz",
      "integrity": "sha512-tjuw7eA0Us3T42jx9AmAgL58rzwzpFGYc3R7Y4Ip75EBYrKMBA1YihuWMcBC92ILmjlQ/u3p8VxcIE0hr+fZfg==",
      "dev": true
    },
    "node_modules/@prisma/engines": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.8.1.tgz",
      "integrity": "sha512-TJgYLRrZr56uhqcXO4GmP5be+zjCIHtLDK20Cnfg+o9d905hsN065QOL+3Z0zQAy6YD31Ol4u2kzSfRmbJv/uA==",
      "dev": true,
      "hasInstallScript": true,
      "dependencies": {
        "@prisma/debug": "5.8.1",
        "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
        "@prisma/fetch-engine": "5.8.1",
        "@prisma/get-platform": "5.8.1"
      }
    },
    "node_modules/@prisma/engines-version": {
      "version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
      "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2.tgz",
      "integrity": "sha512-f5C3JM3l9yhGr3cr4FMqWloFaSCpNpMi58Om22rjD2DOz3owci2mFdFXMgnAGazFPKrCbbEhcxdsRfspEYRoFQ==",
      "dev": true
    },
    "node_modules/@prisma/fetch-engine": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.8.1.tgz",
      "integrity": "sha512-+bgjjoSFa6uYEbAPlklfoVSStOEfcpheOjoBoNsNNSQdSzcwE2nM4Q0prun0+P8/0sCHo18JZ9xqa8gObvgOUw==",
      "dev": true,
      "dependencies": {
        "@prisma/debug": "5.8.1",
        "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2",
        "@prisma/get-platform": "5.8.1"
      }
    },
    "node_modules/@prisma/get-platform": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.8.1.tgz",
      "integrity": "sha512-wnA+6HTFcY+tkykMokix9GiAkaauPC5W/gg0O5JB0J8tCTNWrqpnQ7AsaGRfkYUbeOIioh6woDjQrGTTRf1Zag==",
      "dev": true,
      "dependencies": {
        "@prisma/debug": "5.8.1"
      }
    },
    "node_modules/prisma": {
      "version": "5.8.1",
      "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.8.1.tgz",
      "integrity": "sha512-N6CpjzECnUHZ5beeYpDzkt2rYpEdAeqXX2dweu6BoQaeYkNZrC/WJHM+5MO/uidFHTak8QhkPKBWck1o/4MD4A==",
      "dev": true,
      "hasInstallScript": true,
      "dependencies": {
        "@prisma/engines": "5.8.1"
      },
      "bin": {
        "prisma": "build/index.js"
      },
      "engines": {
        "node": ">=16.13"
      }
    }
  }
}
  • Lines 6–11: We insert a single entry into our table.

  • Lines 17–19: We insert multiple entries into our table and have a record with a duplicate primary key.

  • Line 21: We set skipDuplicates as true meaning that we are going to skip any entries that have a primary key duplicate already present in the table.

Querying data from a table (Read)

Now, we will see how we can read the data that we inserted into our table. We will use the findUnique() and findMany() methods to retrieve our data.

Note: To run this code, click the "Run" button and execute the cd usercode && node read.js command in the terminal below.

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  const users = await prisma.user.findMany()
  console.log('All data present in table')
  console.log(users);

  const user = await prisma.user.findUnique({
    where: {email: 'bob@email.com'}
    })
  console.log('Unique data')
  console.log(user);

}

main()
  .then(async () => {
    await prisma.$disconnect()
  })
  .catch(async (e) => {
    console.error(e)
    await prisma.$disconnect()
    process.exit(1)
  })
  • Line 6: The findMany() method returns all the entries in a table if no arguments are provided

  • Line 11: We provide a unique ID as the where key to fetch any unique objects with that ID. If no objects have the specified unique ID, the method simply returns null.

Modifying an entry (Update)

We can update any record present in our tables by using the update() method, or if you want to update multiple records at a time, you can also use the updateMany() method.

Note: To run this code click the "Run" button and execute the cd usercode && node update.js command and to see the changes being reflected run the node read.js command in the terminal below.

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  const user = await prisma.user.create({
    data: {
      name: 'Jim',
      email: 'jim@email.com',
      age: 22,
    },
  })
  console.log(user);

  const users = await prisma.user.createMany({
    data: [
      { name: 'Bob', email: 'bob@email.com',age: 24 },
      { name: 'Stuart', email: 'stuart@email.com',age: 28 },
      { name: 'Bobby', email: 'bob@email.com',age: 24},
    ],
    skipDuplicates: true,
  })
  console.log(users);
}

main()
  .then(async () => {
    await prisma.$disconnect()
  })
  .catch(async (e) => {
    console.error(e)
    await prisma.$disconnect()
    process.exit(1)
  })
  • Lines 7–12: We provide a where key with a unique ID to find the item we want to modify and in the data we provide the field that we want to change and the value that we want to update it with.

  • Lines 21–23: We increment the value of an integer by using the increment key and provide the value that we want to increment with.

Removing an entry (Delete)

Finally, we will learn how we can delete any record from the table by using the delete() and multiple records using the help of the deleteMany() methods.

Note: To run this code click the “Run” button and execute the cd usercode && node delete.js command and to see the changes being reflected run the node read.js command in the terminal below.

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  const user = await prisma.user.delete({
    where: {
      email: 'jimmy@email.com',
    },
  })

  const users = await prisma.user.deleteMany({})
}

main()
  .then(async () => {
    await prisma.$disconnect()
  })
  .catch(async (e) => {
    console.error(e)
    await prisma.$disconnect()
    process.exit(1)
  })
  • Lines 7-9: We use where, providing the unique ID to search for and delete a specific item.

  • Lines 12: We use the deleteMany() method to delete all items from the table.

Conclusion

Prisma CRUD operations streamline your database interactions, enabling you to efficiently manage data within your applications. This enhanced functionality empowers you to create responsive, adaptable applications that leverage real-time data, driving richer user experiences and deeper insights.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to fetch data in Prisma?

To fetch data in Prisma, use the findMany() method to retrieve multiple records or the findUnique() method to fetch a single record based on a unique identifier.


What are the four CRUD methods?

The four CRUD methods are as follows:

  • Create (inserting new data)
  • Read (retrieving existing data)
  • Update (modifying existing data)
  • Delete (removing data).

How to write a query in Prisma?

To write a query in Prisma, use the Prisma Client to call methods like findMany(), findUnique(), create(), update(), or delete(), passing in the appropriate parameters to interact with your database.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved