当前位置:网站首页>Why should you consider using prism
Why should you consider using prism
2022-07-01 17:03:00 【New titanium cloud suit】
The new titanium cloud service has been accumulatively shared with you 657 Technical dry goods


Prisma Is rapidly becoming my first choice ORM frame , There's a reason . Before we create a web Project time , Setting up a linked database has always been a tedious task . although ORM It's been around for a while , And in most cases, this process has been simplified . However , They always seem to be lacking in some aspect .
For a developer , every last ORM frame , Usually, some developers hope it can do better . But for me , it This design pattern brings me the ability to quickly view and edit data .Prisma While solving these problems for me, I also solved some problems I didn't know .
What is? Prisma
Prisma Call yourself the next generation Node.js、TypeScript、Go The database of ORM frame
Prisma Is an open source database tool chain project , Help developers build applications faster and reduce errors , Support PostgreSQL、MySQL、MongoDB、SQL Server and SQLite.
Prisma Schema
Prisma At the heart of it is through Primsa Schema File to define the data model . It uses a method called PSL(Prisma Schema Language) Attribute language , It takes a few minutes to get familiar with .Schema The document has three main components .
The first is your data source datasource, This configuration database URL And database types . under these circumstances ,provider Used to indicate the type of database used . As shown below :
datasource db {
url = env("DATABASE_URL")
provider = "postgresql"
}The next part is the generator (Generator) Field . This field specifies what clients should be generated according to the data model . This function is how you use Prisma Generated client CRUD. We will introduce these later , This part looks like this :
generator client {
provider = "prisma-client-js"
}Last , We need to define our application model data model (Data model) Fields in the database , This will be the focus of this document . Here you create the mapping relationship between your application model and data . This part also needs the longest time to adapt , However , It will still look familiar .PSL Not creating ordinary JS object , Instead, format the model in a different way , But all fields interact in the same way as you are used to . The following is an example of a user model :
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}We have one user model with six fields ,"id " It's an integer , It has a default value and increases automatically . There are also some standard , The rest of the 、 The only type attribute you can use is Official documents ) Explore in . Your next question may be " How the relationship between models works ?", Fortunately, they are quite easy to understand . The following is a model of user posts .
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}This association is defined in the author field of the post . The meaning is very simple ,author Field is a user , It refers to the user model User. Then we create an association , Use the post model Post Fields on "authorId " Reference user model User Medium "id " Field .
The last big problem to solve is to keep your database synchronized with your defined model ,Prisma adopt Prisma Migrate Do this . This is a simple command line , You can migrate your database with any changes you may have , For details, please refer to Migration function of official documents .<a name="v8LM3"></a>
CRUD
Remember your Prisma Schema Generator in file Generator part , Here is where you want to use . Yours Generator Block is Prisma Something used to decide how to build its client . It is built for type safe query of your data . This means that the operation function of querying your query data will become easier to understand and use .
const newUser = await prisma.user.create({
data: {
name: 'Alice',
email: '[email protected]',
},
})
const users = await prisma.user.findMany()This is an example of creating and finding users . When generating a client for your data ,Prisma Add methods to Prisma In the object , It is completely related to your data . Continue to take users and posts as examples , We will also be able to do this .
prisma.post.create()If you want to learn more about more complex query operations , Please check here .<a name="DrufO"></a>
Prisma The benefits of
Introspection

Here are at least two features that let me at least consider it , For any project , Even those I'm already using different ORM Project .Introspection It's a function , It allows you to use the database you have created to generate a match with your database Prisma Pattern . therefore , If you have a database that has been set up in the way you like , But I want to try Prisma, This process is seamless and simple . Yours Prisma Schema The file has been generated , You can start using Prisma. All this is done in a single command .
prisma db pullPrisma Studio

Prisma Also created a local web client , You can change or update the data at any time .Prisma Studio It's very good to test for quickly checking data or changing something . It may not be like PgAdmin or MySql Workbench So advanced , But it still provides enough choices for most database work . The only thing I'm not satisfied with is , You can't use Sanity So customize the appearance interface .
Sources
https://www.prisma.io/
original text :https://medium.com/@iben.oneal/why-you-should-consider-prisma-io-a5d963f68c4e
Learn about the new titanium cloud service
Previous technical dry goods
· Ten thousand words long text : Cloud Architecture Design Principles | attach PDF download
· Ten thousand words long text | Use RBAC Restricted pair Kubernetes Access to resources
· Ten thousand words long text | oriented k8s Programming , How to write a Operator
· Terraform actual combat | Ten thousand words long text
· CephFS Performance benchmarking and cluster optimization | Ten thousand word summary
· Low code development , Development by the whole people , Eliminate professional programmers !
· Domestic mainstream public cloud VPC Use comparison and summary
· Ceph OSD Troubleshooting | Ten thousand words experience summary
· IT Hybrid cloud strategy : What is it? 、 Why? , How to build ?

Share

Poke at
边栏推荐
- 判断二叉树是否为二叉搜索树
- Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
- 拼接字符串,得到字典序最小的结果
- SQL question brushing 586 Customers with the most orders
- 软件工程导论——第六章——详细设计
- National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report
- Sword finger offer II 015 All modifiers in the string
- The amazing open source animation library is not only awesome, but also small
- How wild are hackers' ways of making money? CTF reverse entry Guide
- [Supplément linguistique c] déterminer quel jour est demain (date de demain)
猜你喜欢

In aks, use secret in CSI driver mount key vault

Machine learning 11 clustering, outlier discrimination

Cookies and session keeping technology

Redis 分布式鎖

Template Engine Velocity Foundation

Detailed explanation of string's trim() and substring()

免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!

SystemVerilog-结构体(二)

Leetcode records - sort -215, 347, 451, 75
![Integer array merge [JS]](/img/0d/70535e0eb1c299bda25159b58c70d7.png)
Integer array merge [JS]
随机推荐
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
【Try to Hack】vulnhub DC4
Redis 分布式鎖
C language input / output stream and file operation
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
官宣!香港科技大学(广州)获批!
String class
单例模式的懒汉模式跟恶汉模式的区别
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
China BMS battery management system Market Research Report (2022 Edition)
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
P2592 [zjoi2008] birthday party (DP)
China nylon 11 industry research and future forecast report (2022 Edition)
Dataframe gets the number of words in the string
redis -- 数据类型及操作
【C語言補充】判斷明天是哪一天(明天的日期)
SQL注入漏洞(Mysql与MSSQL特性)
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
MySQL learning summary
How wild are hackers' ways of making money? CTF reverse entry Guide
