6.3-prisma
Create by fall on 25 Sep 2023 Recently revised in 11 Oct 2023
Prisma
prisma 官方是下一代 ORM,不知道是不是可以这样说,但是 Prisma 支持主流的数据库(Postgresql、Mongodb、mysql)并且有项目组长期维护,所以也值得信任。
安装和初始化
首先,在项目中安装 prisma
,npm i prisma
使用 npx prisma version
查看版本信息
prisma 命令用于初始化和创建文件,@prisma/client
用于创建连接和数据库映射。
然后使用命令 npx prisma init
,
初始化为我们提供了两个新的文件 prisma.prisma
和 .env
。
一般来讲,要保证 prisma 和
@prisma/client
的版本统一,来确保行为一致,所以,全局安装 prisma 时要特别注意
连接 mongo
在 prisma.prisma
中写入如下内容
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
slug String @unique
title String
body String
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId
comments Comment[]
}
首先使用命令
mongosh
进入 mongodb 的命令行界面,会提供以下内容
Current Mongosh Log ID: 6526a8a80aed2e92c3cd7fa9
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.0
Using MongoDB: 5.0.18
Using Mongosh: 1.10.0
prisma 会用到 Connecting to
后面的内容,该链接可以拆解为下面的形式
mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE
# USERNAME: The name of your database user
# PASSWORD: The password for your database user
# HOST: The host where a instance is running
# PORT: The port where your database server is running (typically `27017` for MongoDB)
# DATABASE: The name of the database
我们先使用命令 use myBase
创建数据库 myBase
,然后将下面的内容放到 .env
文件中
# 把路径 myBase 添加到上方 Connecting to 的内容中,组合成下面这样
DATABASE_URL="mongodb://localhost:6200/myBase?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.0"
最后,在 mongosh 命令行中使用 rs.initiate()
命令初始化 Replica Set
rs.initiate()
# {
# info2: 'no configuration specified. Using a default configuration for the set',
# me: '74b3cecd77b6:27017',
# ok: 1
# }
至此,连接的准备工作已经完成
安装 client
npm i @prisma/client
在安装过程中,也同时运行了 prisma generate
,以后,每当更改 prisma 文件的时候,都要运行 prisma generate
。用来生成 node_modules/@prisma/client
库中的内容,然后你就可以自由使用 prisma 进行 crud 了。
# 将数据模型(data model)同步到数据库中
prisma db push
# 创建索引,并且重新生成 @prisma/clinent create new indexes and regenerate Prisma Client.
建议
Here are a few suggestions for a number of more queries you can send with Prisma Client:
Filter all Post
records that contain "hello"
const filteredPosts = await prisma.post.findMany({
where: {
OR: [{ title: { contains: 'hello' } }, { body: { contains: 'hello' } }],
},
})
Create a new Post
record and connect it to an existing User
record
const post = await prisma.post.create({
data: {
title: 'Join us for Prisma Day 2020',
slug: 'prisma-day-2020',
body: 'A conference on modern application development and databases.',
user: {
connect: { email: 'hello@prisma.com' },
},
},
})
Use the fluent relations API to retrieve the Post
records of a User
by traversing the relations
const user = await prisma.comment
.findUnique({
where: { id: '60ff4e9500acc65700ebf470' },
})
.post()
.user()
Delete a User
record
const deletedUser = await prisma.user.delete({
where: { email: 'sarah@prisma.io' },
})
参考文章
作者 | 链接 |
---|---|
prisma 官方文档 | Connect your database (MongoDB) |