Hassan2bit

introduction to prisma orm

disclamer: This was written for me to easily read and set up a prisma orm in a future project incase i forget.

for more helpful info please head over to the prisma documentation

Table of contents:

Prerequisites:

what is prisma?

prisma orm (object relational mapper) is a next gen tool that abstract the low level sql(structured query language) code to high level with intuitive data modelling, automated migrations, type-safety & auto-completion. to seammlessly interact with databases.

how does prisma works?

To easily grasp how prisma does it thing, we need to understand these 3 essential tool kits below:

The prisma client is one of the major component of prisma that serves as the interpreter when querying a database. It allows you to perform crud operations, filtering, sorting, and aggregation.

The prisma schema is strict structure we planned to organize our data in, it validate the prisma client operation, it is like checking a shape against a fitting hole.

after setting up prisma.schema which contains the db source connection and data models, we would have to migrate our schema, and that is where is prisma migrate comes in.

Migrating does two essential things!

  1. It structures the database in a tabular form with the neccesary headers that matched the schema we defined
  2. It regenerate a proper prisma client query code based on the predefined schema

With the above conditions in place, we can then use prisma studio to perform crud operations directly to our data base, it is a web based gui that allows you to directly edit your data.

how to set up prisma for db connection

I'm assuming you have nodejs installed

1. install dependencies

mkdir newproject && cd newproject
npm install prisma @prisma/client express
npx prisma init

what this does:

2) Configure datasource (prisma/schema.prisma)

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

generator client {
  provider = "prisma-client-js"
}

what this does

3) Define models (schema)

model Prompt {
  id               Int     @id @default(autoincrement())
  emotionCategory  String
  text             String
}

What each piece does

4) creating a database

run

npx prisma migrate dev --name init

What it does (two outcomes)

5) src/db.js (Prisma client bootstrap)

Example:

const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
module.exports = { prisma };

What this does

6) src/index.js (Express entrypoint)

const express = require("express");
const { prisma } = require("./db.js");

const app = express();
app.use(express.json());

app.get("/prompts", async (req, res) => {
  const data = await prisma.prompt.findMany();
  res.json(data);
});

app.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});

what this does

7) Run server

run:

node src/index.js

What to expect and verification

Console prints Server running on http://localhost:3000.

Open http://localhost:3000/resources in browser to see running server

Note

Inspect DB with Prisma Studio:

runnig

npx prisma studio

This opens a browser UI to view/edit rows (default port shown in terminal).

footer: im a just a mere learner kindly correct on whatever might be wrong or done better by sending me an email: hassanamiri.ai at gmail dot com

thank you.