当前位置:网站首页>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 pull
Prisma 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
边栏推荐
- In aks, use secret in CSI driver mount key vault
- 中国乙腈市场预测与战略咨询研究报告(2022版)
- ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
- LeetCode中等题之TinyURL 的加密与解密
- China carbon disulfide industry research and investment strategy report (2022 Edition)
- ShenYu 网关开发:在本地启用运行
- Transition technology from IPv4 to IPv6
- SQL question brushing 627 Change gender
- PETRv2:一个多摄像头图像3D感知的统一框架
- Object. fromEntries()
猜你喜欢
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
(1) CNN network structure
String的trim()和substring()详解
SQL question brushing 586 Customers with the most orders
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
官宣!香港科技大学(广州)获批!
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
Yyds dry inventory MySQL RC transaction isolation level implementation
PETRv2:一个多摄像头图像3D感知的统一框架
Redis6.0 新功能
随机推荐
Research and investment strategy report of neutral protease industry in China (2022 Edition)
软件工程导论——第六章——详细设计
Oom caused by improper use of multithreading
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Computed property “xxx“ was assigned to but it has no setter.
P2592 [zjoi2008] birthday party (DP)
China nylon 11 industry research and future forecast report (2022 Edition)
SQL question brushing 627 Change gender
多线程使用不当导致的 OOM
荣威 RX5 的「多一点」产品策略
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
[C language supplement] judge which day tomorrow is (tomorrow's date)
剑指 Offer II 015. 字符串中的所有变位词
FRP intranet penetration, reverse proxy
中国一次性卫生用品生产设备行业深度调研报告(2022版)
Dataframe gets the number of words in the string
越来越多地使用 SLO 来实现可观测性|DevOps
智能运维实战:银行业务流程及单笔交易追踪
PR basic clip operation / video export operation
Redis 分布式锁