Overview of ORM Database Tools
Object Relational Mapping (ORM)
Summary
- Concepts of ORM
- Prisma ORM
- Type ORM
- Sequelize ORM
- Micro ORM
References
1. Concepts of ORM
Object-Relational Mapping (ORM) is a technique that lets you query and manipulates data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase “an ORM”.
An ORM library is a completely ordinary library written in your language of choice that encapsulates the code needed to manipulate the data, so you don’t use SQL anymore; you interact directly with an object in the same language you’re using.
2. Prisma ORM
Postgres Installation with Docker
docker run --name postgresql -e POSTGRES_PASSWORD=admin -p 5432:5432 -d postgres
Prisma Migrate
yarn prisma migrate devcreate productcreate categorycreate product_category
Prisma Studio
yarn prisma studio
Prisma Relations
HTTP/1.1 200 OKX-Powered-By: ExpressContent-Type: application/json; charset=utf-8Content-Length: 150ETag: W/"96-UrprlteTTnqfmSPc6+2Svgk22kw"Date: Thu, 20 Jan 2022 17:45:34 GMTConnection: close{"id": "70df8552-3b15-4532-ba10-084018283e17","id_product": "1d8da63e-c4dc-43e5-87b1-ddf1699dd7f1","id_category": "130b29d6-563b-4a73-8c2e-fe1c737ef99c"}
Create a Product with Category
import { Request, Response } from "express";import { prismaClient } from "../database/prismaClient";export class CreateProductWithExistCategory {async handle(request: Request, response: Response) {const { name, price, bar_code, id_category } = request.body;const product = await prismaClient.productCategory.create({data: {product: {create: {bar_code,name,price,},},category: {connect: {id: id_category,},},},});return response.json(product);}}