> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shipthing.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 数据库

> ShipThing 中数据库和 ORM 的配置方式。

ShipThing 旨在提供一个健壮、类型安全的数据库客户端，使您能够轻松地使用数据库，同时在整个应用程序中保持强类型。我们支持以下工具：

* 提供声明式的方式来定义数据库模式
* 生成类型安全的数据库客户端
* 处理数据库迁移
* 提供强大的查询功能，并完全支持 TypeScript

## 默认配置

默认情况下，ShipThing 使用 [Neon](https://neon.tech) 作为数据库提供商，使用 [Prisma](https://prisma.io) 作为 ORM。但是，如果您愿意，可以轻松切换到其他提供商，例如：

* 您可以使用其他 ORM，如 [Drizzle](/zh/migrations/database/drizzle)、Kysely 或其他任何类型安全的 ORM。
* 您可以使用其他数据库提供商，如 [PlanetScale](/zh/migrations/database/planetscale)、[Prisma Postgres](/zh/migrations/database/prisma-postgres)、[Supabase](/zh/migrations/database/supabase)、[EdgeDB](/zh/migrations/database/edgedb) 或其他任何 PostgreSQL/MySQL 提供商。

## 使用

数据库和 ORM 配置位于 `@repo/database` 中。您可以将此包导入任何服务器端组件，如下所示：

```page.tsx {1,4} theme={"system"}
import { database } from '@repo/database';

const Page = async () => {
  const users = await database.user.findMany();

  // 对用户数据做一些操作！
}
```

## 模式定义

数据库模式定义在 `packages/database/prisma/schema.prisma` 中。此模式文件使用 Prisma 的模式定义语言来描述数据库表、关系和类型。

### 添加新模型

假设我们要添加一个名为 `Post` 的新模型，用于描述博客文章。博客内容将以 JSON 格式生成，使用 [Tiptap](https://tiptap.dev/) 等库。为此，我们将在您的模式中添加以下内容：

```prisma packages/database/prisma/schema.prisma {1-7} theme={"system"}
model Post {
  id        String   @id @default(cuid())
  title     String
  content   Json
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
```

### 部署更改

要部署您的模式更改，请运行以下命令：

```sh Terminal theme={"system"}
pnpm migrate
```

这将运行以下命令：

* `npx prisma format` 格式化模式文件
* `npx prisma generate` 生成 Prisma 客户端
* `npx prisma db push` 将模式更改推送到数据库

## 可视化数据库编辑器

<Tip>`studio` 应用程序运行在 3005 端口。</Tip>

ShipThing 包含 Prisma Studio，这是一个可视化的数据库编辑器。要启动它，请运行以下命令：

```sh Terminal theme={"system"}
pnpm --filter database dev
```
