Prisma, how to clear the database

While testing a site that used Prisma I had the need to clear the database from time to time, to clear the test data I entered.

You can clear items entered by using:
await prisma.user.deleteMany({})

If for some reason you want to iterate on …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

While testing a site that used Prisma I had the need to clear the database from time to time, to clear the test data I entered.

You can clear items entered by using:

await prisma.user.deleteMany({})

If for some reason you want to iterate on the items to do some processing, you can iterate over them in this way:

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

const deleteUser = async (user) => {
  return await prisma.user.delete({
    where: { id: user.id }
  })
}

const deleteUsers = async () => {
  users.map((user) => deleteUser(user))
}

deleteUsers()

In this case I am not doing anything more than the previous example, which makes all this code redundant, but you could do anything you want inside deleteUser().

I had a problem though because I had a relation between 2 tables, tweets and users. A tweet was associated to a user. First I had to remove all tweets, then remove all users, so I wrote this function:

export const clearData = async (prisma) => {
  const users = await prisma.user.findMany({})
  const tweets = await prisma.tweet.findMany({})

  const deleteUser = async (user) => {
    return await prisma.user.delete({
      where: { id: user.id }
    })
  }
  const deleteTweet = async (tweet) => {
    return await prisma.tweet.delete({
      where: { id: tweet.id }
    })
  }

  const deleteTweets = async () => {
    return Promise.all(tweets.map((tweet) => deleteTweet(tweet)))
  }

  const deleteUsers = async () => {
    return Promise.all(users.map((user) => deleteUser(user)))
  }

  await deleteTweets()
  await deleteUsers()
}

Notice the use of Promise.all() to wrap users.map() so I could use await on it, so all tweets are removed before I start deleting users.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-07-14T05:00:00+00:00) Prisma, how to clear the database. Retrieved from https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/

MLA
" » Prisma, how to clear the database." flaviocopes.com | Sciencx - Wednesday July 14, 2021, https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/
HARVARD
flaviocopes.com | Sciencx Wednesday July 14, 2021 » Prisma, how to clear the database., viewed ,<https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/>
VANCOUVER
flaviocopes.com | Sciencx - » Prisma, how to clear the database. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/
CHICAGO
" » Prisma, how to clear the database." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/
IEEE
" » Prisma, how to clear the database." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/. [Accessed: ]
rf:citation
» Prisma, how to clear the database | flaviocopes.com | Sciencx | https://www.scien.cx/2021/07/14/prisma-how-to-clear-the-database/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.