Skip to main content

JavaScript 项目中 MongoDB 的自省

Prisma ORM 通过对给定数据库中存储的数据进行采样并推断该数据的架构来内省 MongoDB 架构。

¥Prisma ORM introspects a MongoDB schema by sampling the data stored in the given database and inferring the schema of that data.

为了说明自省,本指南将帮助你从头开始设置 MongoDB。但如果你已经有 MongoDB 数据库,请随意跳转到项目中的 初始化 Prisma ORM

¥For the purposes of illustrating introspection, this guide will help you setup a MongoDB from scratch. But if you have a MongoDB database already, feel free to jump to Initializing Prisma ORM in your project.

设置你的数据库

¥Setting up your Database

要查看实际效果,请首先创建一个包含 2 个集合的 blog 数据库:UserPost。我们建议使用 MongoDB 指南针 进行设置:

¥To see this in action, first create a blog database with 2 collections: User and Post. We recommend MongoDB Compass for setting this up:

Create a blog database using Compass

首先,将用户添加到我们的 User 集合中:

¥First, add a user to our User collection:

Create a user within the User collection

接下来,将一些帖子添加到我们的 Post 集合中。userId 中的 ObjectID 必须与你在上面创建的用户相匹配,这一点很重要。

¥Next, add some posts to our Post collection. It's important that the ObjectID in userId matches the user you created above.

Create some posts within the Post collection

初始化 Prisma ORM

¥Initializing Prisma ORM

现在你已经有了 MongoDB 数据库,下一步是创建一个新项目并初始化 Prisma ORM:

¥Now that you have a MongoDB database, the next step is to create a new project and initialize Prisma ORM:

mkdir blog
cd blog
npm init -y
npm install -D prisma
npx prisma init --datasource-provider mongodb --output ../generated/prisma

初始化 Prisma ORM 将创建一个如下所示的 prisma/schema.prisma 文件:

¥Initializing Prisma ORM will create a prisma/schema.prisma file like the following:

prisma/schema.prisma
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

接下来,你需要调整 .env 文件以将 DATABASE_URL 指向你的 MongoDB 数据库

¥Next you'll need to adjust your .env file to point the DATABASE_URL to your MongoDB database

使用 Prisma ORM 反思 MongoDB

¥Introspecting MongoDB with Prisma ORM

你现在已经准备好进行内省了。运行以下命令来检查你的数据库:

¥You're now ready to introspect. Run the following command to introspect your database:

npx prisma db pull

此命令检查我们的数据库并将推断的模式写入你的 prisma/schema.prisma 文件中:

¥This command introspects our database and writes the inferred schema into your prisma/schema.prisma file:

prisma/schema.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
title String
userId String @db.ObjectId
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String
}

调整架构

¥Tweaking the Schema

为了能够使用 Prisma 客户端连接数据,你可以将 @relation 属性添加到我们的模型中:

¥To be able to join data using Prisma Client, you can add the @relation attributes to our models:

prisma/schema.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
title String
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String
posts Post[]
}
提示

我们正在积极致力于 MongoDB 内省。在 这个问题 中提供对此功能的反馈。

¥We're actively working on MongoDB introspection. Provide feedback for this feature in this issue.

这样,你就可以生成 Prisma 客户端了。

¥And with that, you're ready to generate Prisma Client.