Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma create/update without await

I was writing an automation script for DB manipulation in Prisma. I'm using PostgreSQL as my database. I noticed that whenever I omit "await" keyword on database updations, it wouldn't show any error but the database changes are not reflected.

// Does not work
prisma.model.create({
  data: {
    name: 'tai',
    schema: {
      abc: 8,
      def: 8,
    },
  }
})

But once I use "await", the database does get updated. I skimmed through the documentation but did not have any luck. Can anybody explain the purpose of this behaviour. Also how they managed to implement it.

// Works
await prisma.model.create({
  data: {
    name: 'tai',
    schema: {
      abc: 8,
      def: 8,
    },
  }
})
like image 595
Ashfaq Ur Rahman N Avatar asked Mar 26 '26 11:03

Ashfaq Ur Rahman N


1 Answers

This happens because Prisma queries are then-ables, meaning they only execute when you call await or .then() or .catch(). This is called lazy evaluation. This is different than a regular promise which starts executing immediately. There's an issue in the Prisma docs repository about this which you can check out for more information.

How to implement: Check out this library: https://github.com/sindresorhus/p-lazy

like image 134
Tasin Ishmam Avatar answered Mar 27 '26 23:03

Tasin Ishmam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!