Prisma API
Tools :
Language: TypeScript
App Framework: Express.js
ORM: Prisma
Database: PostgresDB
Deployment: Heroku
Project Setup :
# make project dir and initialize npm
mkdir myProject
cd myProject
npm init -y
# install dev dependencies
npm i -D @types/node @types/express prisma typescript tsc-watch ts-node
# @types/_ - type definitions for _
# prisma - orm toolkit
# typescript - add TS
# tsc-watch - nodemon but for transpiling .ts files --> .js
# ts-node - lets you run TS files directly on Node.js (dont need tsc compiler)
# install dependencies
npm i express dotenv pg @prisma/client
# express - for building api endpoints
# dotenv - to read env variables like db url
# pg - postgresdb
# @prisma/client - query builderMake Scripts
You need to define 4 scripts in package.json to:
Directory Setup
Since we installed the prisma as a devdep. above, we can now use its CLI (reference) which has important commands that we need.
If you haven't already, create a .gitignore file with the following content:
Define Project Directory
Create a folder in the project root named /src, with a single index.ts file where all the api endpoints will be defined.
Inside the /prisma dir, create a seed.ts file which is called by the above
npm run seedscript we defined to populate the db w/ intialize data.
Now your project dir should look like:
schema.prisma
main config file for your Prisma setup which houses:
data sources: specify details to connect to postgres db.
generators: specify what clients should be generated based on the data model (prisma client).
data model: specify your app's ACTUAL DATA SCHEMA and their relations.
reads the DB URL from .env automagically, so put ur connection URL in the .env - not here!
seed.ts
Uses the Prisma Client to populate the DB w/ initial data that is required for the app to start.
This ts-node file is ran using the
npm run seedscript defined above.
index.ts
We use Express paired with TS and the Prisma Client to define the actual API endpoints ie. GET/POST/PATCH/DELETE.
.env
This file was generated for you when you ran
npx prisma init, and all you have to do is add in your DB URL here and it is read in the schema.prisma file.
.gitignore
files and folders specified here are not commited to the remote repo.
Should include .env, node_modules, and dist.
package.json
You defined your scripts here.
Create Data Model in schema.prisma
First, install the prisma vs code ext. for syntax highlighting and autofills. Now let's define our actual data models in the schema.prisma file.
Model = Table.
An example model is provided below:
Note:
Instead of manually defining a relation, use the autocomplete feature of the prisma vs code ext.; ie. for a Book-Author model where Book has 1 Author and Author has many Book
Now Synchronize Schemas
Now synchronize your Prisma schema (from schema.prisma) with your database schema.
Note: db push works well for prototyping; otherwise, see prisma migrations.
Create Seeder Function
seed.ts Uues the Prisma Client to populate the DB w/ initial data that is required for the app to start. This ts-node file is ran using the npm run seed script previously defined.
Define API Endpoints
You can use the below express API app example as a guide; pay attention to how the queries are built:
Usually, after any updates in the schema, if you want to flush the DB and reset Prisma to resync the DB:
Last updated