Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/content/docs/zh-cn/guides/integrations-guide/db.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ const Comment = defineTable({
- `unique` - 将列标记为唯一。这将防止表中条目的值重复。
- `references` - 通过列引用相关表。这会建立一个外键约束,意味着每个列值必须在引用的表中有一个匹配的值。

`text` 列可以选择性地定义字符串字面量列表以用作生成枚举类型。但是,**这不会在运行时执行任何校验**。删除、添加和更改这些值都应该由你的项目代码来处理。

```ts title="db/config.ts" {8}
import { defineTable, column } from 'astro:db';

// 表定义
const UserTable = defineTable({
columns: {
id: column.number({ primaryKey: true }),
name: column.text(),
rank: column.text({ enum: ['user', 'mod', 'admin'] }),
},
});

// 生成的类型定义
type UserTableInferInsert = {
id?: string;
name: string;
rank: "user" | "mod" | "admin";
}
```

### `indexes`

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