Prisma

PRISMA is an evidence-based minimal set of elements for Systematic Review and Meta-Analysis Reporting. Prisma is an open source ORM that makes database management and interaction simple. Prisma Data Proxy manages database connections and pools them.

Is Prisma an ORM

Prisma operates differently than other ORMs. Prisma allows us to specify our models in the declarative Prisma Schema, which serves as the single source of truth for the database schema and the models associated with our Programming Language.

To read and write data into our database in a typed-safe way without the complexity of handling complicated model objects, we used Prisma Client.

In a nutshell, Prisma is a new type of DataMapper ORM that varies from existing ORMs and does not create the difficulties that are often connected with them.

ORM offers a high-level data abstraction that hides the actual database connectivity.

It exposes a programmatic interface through objects for creating, reading, deleting, and manipulating data while masking part of the database’s complexity. The classes and their objects offer us with a programmatic API that allows us to read and write data into databases.

It was discovered that there are two typical ORM patterns: Active Record and Data Mapper, which differ in how data is transported between the object produced and the database.

The primary goal of ORM designs such as Active Record and Data Mapper is that both are used to define classes as their primary building block. To distinguish between Active Record and Data Mapper.

Where the structure of the two representations is closely similar, for example, the model class will contain the matching fields in the database table, the Active Record ORM maps model classes to database tables.

Data Mapping: Data Mapper ORMs separate the application’s in-memory data representation from the database’s representation. Decoupling is accomplished by dividing mapping duty into two classes.

string

1) MappeR ClasS

ORMs with Data Mapper provide additional flexibility between the problem domain as implemented in code and the database. This occurs because Data Mapper allows you to conceal the methods through which our database was constructed. They do this owing to organisational structure, where the two roles would be performed by distinct teams, such as DBAs and backend engineers.

It has been found that not all Data Mappers strictly adhere to this pattern. For example, Type ORM is a popular TypeScript ORM that supports both Active Record and Data Mapper.

As previously stated, the Data Mapper contains two classes: entity and repository.

The entity classes employ decorators to translate class attributes to table columns, and they are also aware of the database that they are implementing.Instead of mapper classes, Respository classes are used for database querying and may contain custom queries.

Decorators are used by repository classes to determine the mapping between entity attributes and Database Columns.

ORM's Benefits

1)ORMs make domain model implementation easier. The domain model is an object model that enables your business logic’s behaviour and data. The fundamental benefit of adopting ORMs is that they allow you to concentrate on real-world business principles rather than database structure or SQL semantics.

2)ORMs help to decrease the quantity of code. As a result, risks from generating repeated SQL statements for standard CRUD (Create Read Update Delete) processes and escaping user input to prevent vulnerabilities such as SQL injections are reduced.

3)ORMs need the use of little or no SQL. This is useful for those who are unfamiliar with SQL yet need to deal with a database.

Prisma is classified into three types, the most common of which are:

1)Prisma Client   2) Prisma Migrate   3) Prisma Studio

prisma studio

Concept

Description

Building block in traditional ORMs

Building block in Prisma

Source of truth in Prisma

Object schema

The in-memory data structures in your applications

Model classes

Generated TypeScript types

Models in the Prisma schema

Data Mapper

The code which transforms between the object schema and the database

Mapper classes

Generated functions in Prisma Client

@map attributes in the Prisma schema

Database schema

The structure of data in the database, e.g., tables and columns

SQL written by hand or with a programmatic API

SQL generated by Prisma Migrate

Prisma schema

Prisma schema

prisma

The Data Source gives the information of the database to which you should connect, for example.

MYSQl

1)If we’re using SQLite as a database OR

MongoDB

2)if we are using MongoDB

Naming Convention for Prisma Schema-

The Schema file’s default name is schema.prisma, which is identified automatically by the Prisma CLI. If it is discovered to have altered, just perform the command Prisma create -schema./database/myschema.prisma.

GENERATORS

Prisma can have one or more of the generators shown in the graphic below…

generator client {

  provider = “prisma-client-js”

}

 

A generator specifies which assets will be produced when we run the prrima generate Command.

Currently, the Provider determines which Prisma Client (language specific) is built. In many circumstances, you can use an NPM package that adheres to generator specifications.

Contains of Data Models

prisma

MODIFICATION of Data Model

The data model definition part of the Prisma Schema defines application models it is also known as Prisma Models.

Represent the entitites for application domain.

Mapped to the tables or collections in MongoDB.

  model Product {

id Int @id  @default(autoincrement())

name String @unique

price Int @default(999)

createdAt DateTime @default(now())

category Category @relation(fields:[categoryId],references:[id])

categoryId Int

}

model
Category
{

  id Int  @id @default(autoincrement())

  name String @unique

  products Product[]

}

 

 

 

 

  • Model Models (model API Primitives) that describe a variety of fields, including model relationships.
  • Enums (API Primitives Enums) (if your connector supports Enums)
  • Field and model attributes and functions that alter their behaviour
  • A model corresponds to the data source’s fundamental structures.
  • A user profile
  • Two Post records that are nested.

Defining Models

  model Product {

id Int @id  @default(autoincrement())

name String @unique

price Int @default(999)

createdAt DateTime @default(now())

category Category @relation(fields:[categoryId],references:[id])

categoryId Int

}

model

Category {

  id Int  @id @default(autoincrement())

  name String @unique

  products Product[]

}

 

Defining an field of ID

An ID distinguishes a model’s records. A model can have just one ID:

model User {

  id      Int      @id @default(autoincrement())

  email   String   @unique

  name    String?

  role    Role     @default(USER)

  posts   Post[]

  profile Profile?

}

 

model User {

  firstName String

  lastName  String

  email     String  @unique

  isAdmin   Boolean @default(false)

 

  @@id([firstName, lastName])

 

}

 

Creating IDs in MongoDB

The MongoDB connection has different criteria for establishing an ID field API than relational databases. A single field with the @id API property must be used to establish an ID.

Creating a default valueUsing the @default API property, you may provide default values for scalar fields in your models.

Creating a distinct field

Add unique features to your models so that individual records of that model may be identified.

Prisma’s Basic Workflow is as follows: Every file that employs a Prisma toolkit tool begins with a Prisma schema File. Its schema provides the models that must be used for the application models in an easy-to-use DATA modelling language.

Migrate Prisma

Prisma Migrate is a critical database schema migration tool that allows you to: Maintain existing data in your database and keep your database schema in sync with your Prisma Schema as it evolves.

 

Three items are configured in these Schemas:

Data Source: In the Prisma model, data sources should link to databases such as PostgreSQL.

Prisma client – should be produced based on Data Models.

Definition of a Data Model: the form of the data per data source and their relationships.

Prisma create: Reads all of the information indicated above from the Prisma Schema in order to generate the right data source client code (e.g. Prisma Client).

 

Prisma migrate programmer: Reads the data sources and data model definition to create a new migration.

migrate dev —name init Prisma

Do not change or remove any migrations that
have already been applied.

Inconsistencies between development and production environment migration histories can result, which can have unintended repercussions – even if the modification does not appear to disrupt anything at first.

create table

Migrate dev is a debugging command that should never be used in production.

Resetting the development database

All you have to do is enter

 npx prisma migrate reset.

1. Drops the database/schema1 if feasible, or does a soft reset if the environment does not allow it.

2. If the database/schema1 was discarded, it creates a new database/schema1 with the same name.

3. All migrations are applied.

4. Executes seed scripts

Prisma Design

Prisma Studio is a visual data editor for your database. Although Prisma Studio is not open source, you may still file bugs in the Prisma repo.

 

Run the npx prisma studio command.

github
github