Prisma & Render Setup
What is Prisma?
- Prisma is a DB agnostic, type safe ORM. What does type-safe mean? It basically means that this functionality here is aware of the shape of the data that you described.
db.customers.create({
customerName: 'Cardinal',
contactName: 'Tom B. Erichsen',
address: 'Skagen 21',
....
})
- It supports most DBs out there.
- It definitely supports most databases out there, Mongo Postgres, SQLite,even something like PlanetScale which is a service database which is really awesome, it supports too.
- It not only has an SDK for doing basic and advanced querying of a DB, but also handles schemas, migrations, seeding, and sophisticated writes.
- It's slowly but surely becoming the ORM of choice for Node.js projects.
- I think most people just start a project and use Prisma at this point.
Psq
We'll be using PSQL as a DB in this course. You won't have to install anything as we'll be using a hosting and managed DB from Render. Go there, create an account, and then create a FREE psql DB.
But i am using the PSQL in the local machine.
Installation of PSQL
Installing Prisma
Prisma works best when you're using a TypeScript. So in addition to installing Prisma, we're going to convert our app to TypeScript. Don't worry if you don't know TypeScript. We won't be doing all the fancy typed stuff in this course. We just want that sweet autocomplete for our DB interactions through Prisma. Trust me, its magical ✨. On to the installing!
- So TypeScript,it's actually the transpiler that's going to convert the TypeScript to JavaScript.
ts-node
is literally the same thing as running node in the terminal,pointing it at a file, but doing it in a TypeScript environment. @types/node
are just the types for the node runtime, so we get global types.- And then Prisma's Prisma.So we're gonna install those.Make sure you save dev as all of these are dev dependencies.
npm i typescript ts-node @types/node prisma --save-dev
- also install the express typescript types because the backend on the express
npm i @types/express
- After that rename the file server.js to server.ts and index.js to index.ts
- Then create a
tsconfig.json
file which is the config file for TypeScript. Add this to that file:
tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}