当前位置:网站首页>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
边栏推荐
- Yyds dry inventory MySQL RC transaction isolation level implementation
- 荣威 RX5 的「多一点」产品策略
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- Vulnhub range hacker_ Kid-v1.0.1
- 越来越多地使用 SLO 来实现可观测性|DevOps
- Cookies and session keeping technology
- C language input / output stream and file operation
- 【flask入门系列】Cookie与Session
猜你喜欢
Redis 分布式锁
Pytest learning notes (13) -allure of allure Description () and @allure title()
Transition technology from IPv4 to IPv6
Redis6.0 new features
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
[pyg] document summary and project experience (continuously updated
sql刷题627. 变更性别
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
模板引擎Velocity 基礎
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
随机推荐
中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
P2592 [zjoi2008] birthday party (DP)
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
剑指 Offer 20. 表示数值的字符串
String类
(12) About time-consuming printing
Redis 分布式锁
智能运维实战:银行业务流程及单笔交易追踪
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
LeetCode中等题之TinyURL 的加密与解密
Are you still using charged document management tools? I have a better choice! Completely free
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
[pyg] document summary and project experience (continuously updated
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
Shenyu gateway development: enable and run locally
Concatenate strings to get the result with the smallest dictionary order
整形数组合并【JS】
Leetcode records - sort -215, 347, 451, 75
Judge whether the binary tree is a binary search tree
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单