Generate type safe client with prisma

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 10 months ago

In this lesson you will learn how to generate type safe orm client with prisma generate and do first write operation using it.

Dimitri Ivashchuk: [0:00] We can now move on with Prisma client. Let's yarn add @prisma/client, which downloads the so-called "smart node module," which is an empty module, filled with the command npx prisma generate.

[0:18] It puts into the Prisma client all the things picked up from the schema.prisma, which gives you TypeScript definitions, autocompletion, and all the nice features for Prisma as an ORM. Let's try to run this command.

[0:40] It has been successfully generated. Let's write some simple queries with Prisma client. Let's create the index.ts and add some boilerplate Prisma. We import our generated client, instantiate the new one, and here we want to write our first Prisma operation, which will write something into our database, and then execute this function later.

[1:17] Let's write our first Prisma function, and for that we need to call Prisma. You see that in the methods and properties, we already have users autocompleted for us. We go for that.

[1:31] Then we want to go for create method and we want to pass data with the name, which again is autocompleted, dimitri. Of course, we want to await this.

[1:47] To run this function, we need to execute npx ts-node index.ts. It has been successfully executed. Now we can go directly to psql prisma postgres and just try to lock the users stable. Select * from users. You see that we have our first table entry with id 1 and name dimitri, which we have now provided to Prisma.