Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/content/docs/zh-cn/guides/astro-db.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ Astro DB 允许你连接到本地和远程数据库。默认情况下,Astro

要连接到一个托管的远程数据库,使用 `--remote` 标志。这个标志允许你访问远程数据库,允许你 [接受和持久化用户数据](#插入) 在生产环境中。

:::note
虽然远程连接在任何使用静态或服务器渲染模式的部署平台上都是可能的,但目前存在一些限制。非 Node 运行时(如 Cloudflare 和 Deno)不支持在 libSQL 上使用服务器渲染路由时使用 DB。对于这些平台的支持,计划将在未来中实现。
:::

配置你的 build 命令使用 `--remote` 标志:

```json title="package.json" "--remote"
Expand All @@ -260,7 +256,7 @@ astro dev --remote
在开发过程中使用 `--remote` 时请小心。这会连接到你的生产数据库,所有更改(插入、更新、删除)都会被持久化。
:::

`--remote` 标志在本地构建期间和服务器上都使用远程数据库连接。确保在本地开发环境和部署平台中都设置了必要的环境变量。
`--remote` 标志在本地构建期间和服务器上都使用远程数据库连接。确保在本地开发环境和部署平台中都设置了必要的环境变量。此外,你可能还需要为非 Node.js 运行时(如 Cloudflare Workers 或 Deno)[配置 web 模式](/zh-cn/guides/integrations-guide/db/#mode)。

当你部署你的 Astro DB 项目时,确保你的部署平台的 build 命令设置为 `npm run build`(或你包管理器的等效命令)以使用在 `package.json` 中配置的 `--remote` 标志。

Expand Down
64 changes: 58 additions & 6 deletions src/content/docs/zh-cn/guides/integrations-guide/db.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ i18nReady: true
import { FileTree } from '@astrojs/starlight/components';
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro';
import ReadMore from '~/components/ReadMore.astro';

import Since from '~/components/Since.astro';

Astro DB 是一个为 Astro 生态设计的全托管 SQL 数据库:你可以在本地使用 Astro 进行开发,并部署到任何 [libSQL 兼容的数据库](/zh-cn/guides/astro-db/)。

Expand Down Expand Up @@ -93,10 +93,45 @@ export default defineDb({
})
```

## 配置

### `mode`

<p>

**类型:** `'node' | 'web'`<br />
**默认值:** `'node'`<br />
<Since v="0.18.0" pkg="@astrojs/db" />
</p>

配置用于在生产环境中连接到数据库的驱动程序。

默认情况下,Astro DB 在生产部署中使用基于 Node.js 的 libSQL 驱动程序。`node` 驱动程序模式足以满足大多数使用 Node.js 运行时的 Astro 托管或自托管网站的需求。这使你能够通过 `memory:`、`file:`、`ws:`、`wss:`、`libsql`、`http` 和 `https` 等多种协议连接到你的数据库,并且支持[嵌入式副本](/zh-cn/guides/astro-db/#syncurl)等高级功能。

当在非 Node.js 运行时(例如 Cloudflare Workers 或 Deno)的无服务器环境中部署时,可以使用基于 Web 的 libSQL 驱动程序。使用 `web` 模式部署时,你将能够通过 `libsql`、`http` 或 `https` 建立基于 Web 的连接。

要在非 Node.js 环境中使用 web libSQL 驱动模式,请在适配器的配置中设置 `mode` 属性:

```ts title="astro.config.mjs" ins={7}
import { defineConfig } from 'astro/config';
import db from '@astrojs/db';

export default defineConfig({
integrations: [
db({
mode: 'web'
})
]
});
```

## 表配置参考

### `columns`
<p>

**类型:** `ColumnsConfig`
</p>
表的列是使用 `columns` 对象配置:

```ts
Expand Down Expand Up @@ -152,6 +187,11 @@ type UserTableInferInsert = {

### `indexes`

<p>

**类型:** `{ on: string | string[]; unique?: boolean | undefined; name?: string | undefined; }[]`
</p>

表索引用于提高在特定列或列组合上的查找速度。`indexes` 属性接受一个配置对象数组,用于指定要索引的列:

```ts title="db/config.ts" {9-11}
Expand All @@ -173,12 +213,17 @@ const Comment = defineTable({

每个索引都有以下配置选项:

- `on`: `string | string[]` - 要索引的单个列或列名数组。
- `unique`: `boolean` - 设置为 `true` 以在索引列上强制唯一值。
- `name`: `string` (可选) - 为唯一索引指定一个自定义名称。这将覆盖 Astro 根据被索引的表和列名生成的名称(例如 `Comment_authorId_published_idx`)。自定义名称是全局的,因此请确保索引名称在不同的表之间不会冲突。
- `on` - 要索引的单个列或列名数组。
- `unique` (可选) - 设置为 `true` 以在索引列上强制唯一值。
- `name` (可选) - 为唯一索引指定一个自定义名称。这将覆盖 Astro 根据被索引的表和列名生成的名称(例如 `Comment_authorId_published_idx`)。自定义名称是全局的,因此请确保索引名称在不同的表之间不会冲突。

### `foreignKeys`

<p>

**类型:** `{ columns: string | string[]; references: () => Column | Column[]; }[]`
</p>

:::tip

`foreignKeys` 是一个用于关联多个表列的高级 API。如果你只需要引用一个单独的列,试试使用[列 `references` 属性。](#columns)
Expand Down Expand Up @@ -214,8 +259,8 @@ const Comment = defineTable({

每个外键配置对象接受以下属性:

- `columns`: `string[]` - 要与引用表关联的列名数组
- `references`: `() => Column[]` - 返回引用表的列数组的函数
- `columns`: - 要与引用表关联的单个列名或者列名数组
- `references`: - 返回引用表的单列或者列数组的函数

## Astro DB CLI 参考

Expand Down Expand Up @@ -256,6 +301,13 @@ Astro DB 包含一组 CLI 命令,用于与你的本地和 libSQL 兼容数据

### `isDbError()`


<p>

**类型:** `(err: unknown) => boolean`<br />
<Since v="0.9.1" pkg="@astrojs/db" />
</p>

`isDbError()` 函数用于检查一个错误是否是 libSQL 数据库异常。这可能包括在使用引用时的外键约束错误,或在插入数据时缺少字段。你可以将 `isDbError()` 与 try / catch 块结合使用,来处理应用程序中的数据库错误:

```ts title="src/pages/api/comment/[id].ts" "idDbError"
Expand Down