关于 blink-database
blink-database 是用于操作 Blink 数据库查询、架构和 SDK CRUD 的技能,涵盖 SQL、筛选与批量操作,并说明 SQLite 布尔值及 JWT 保护的用户级访问。
主要功能
- 01执行 SQL 查询
- 02检查数据库架构
- 03通过 SDK 执行 CRUD 和批量操作
- 04筛选并排序记录
- 05处理 SQLite 布尔值
- 06使用 JWT 保护与 RLS
适用场景
- 01创建并检查数据表
- 02查询或更新应用记录
- 03批量处理数据库变更
- 04实施用户级数据访问
MCP 相关
该技能列出两个 MCP 工具:`blink_db_query` 用于执行 SQL 查询,`blink_db_schema` 用于获取数据库架构。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ blink-new/ blink-plugin --skill blink-database安装后在 Claude Code 中输入/blink-database确认已加载。
详细介绍
MCP 工具
blink_db_query · blink_db_schema
blink_db_query— 运行任意 SQL 查询(SELECT、INSERT、UPDATE、DELETE、CREATE TABLE 等)blink_db_schema— 获取完整架构:所有表及其列名、类型、是否允许为空、默认值和主键
快速入门
# Create a table
blink db query "CREATE TABLE todos (id TEXT PRIMARY KEY, title TEXT NOT NULL, user_id TEXT, is_completed INTEGER DEFAULT 0, created_at TEXT DEFAULT (datetime('now')))"
# Inspect schema
blink db query "SELECT name FROM sqlite_master WHERE type='table'"
# Run any SQL
blink db query "SELECT * FROM todos LIMIT 10"
自动大小写转换
SDK 会自动在 camelCase ↔ snake_case 之间转换。在代码中始终使用 camelCase。
// ✅ Correct
await blink.db.emailDrafts.create({ userId: user.id, createdAt: new Date() })
// ❌ Wrong — never use snake_case in SDK calls
await blink.db.email_drafts.create({ user_id: user.id })
CRUD 操作
// Create (ID auto-generates as string)
const todo = await blink.db.todos.create({ title: 'Learn Blink', userId: user.id, isCompleted: false })
// Get by ID
const todo = await blink.db.todos.get('todo_12345')
// List with filtering
const todos = await blink.db.todos.list({
where: { AND: [{ userId: user.id }, { OR: [{ status: 'open' }, { priority: 'high' }] }] },
orderBy: { createdAt: 'desc' },
limit: 20,
offset: 0,
})
// Update
await blink.db.todos.update('todo_12345', { isCompleted: true })
// Delete
await blink.db.todos.delete('todo_12345')
// Upsert
await blink.db.todos.upsert({ id: 'todo_12345', title: 'Updated', userId: user.id })
// Count / Exists
const count = await blink.db.todos.count({ where: { userId: user.id } })
const exists = await blink.db.todos.exists({ where: { id: 'todo_12345' } })
批量操作
await blink.db.todos.createMany([{ title: 'Task 1', userId: user.id }, { title: 'Task 2', userId: user.id }])
await blink.db.todos.updateMany([{ id: 'todo_1', isCompleted: true }, { id: 'todo_2', isCompleted: true }])
await blink.db.todos.deleteMany({ where: { userId: user.id, isCompleted: "1" } })
布尔值处理(重要)
SQLite 返回的布尔值是 "0"/"1" 字符串,而不是 true/false。
// ✅ Correct
const completed = todos.filter(todo => Number(todo.isCompleted) > 0)
// Filter in queries
const done = await blink.db.todos.list({ where: { isCompleted: "1" } })
TypeScript
interface Todo {
id: string // MUST be string, never number
title: string
isCompleted: boolean // Returned as "0"/"1" from SQLite
userId: string
createdAt: string
}
// Typed queries
const todos = await blink.db.table<Todo>('todos').list()
筛选选项
| 选项 | 类型 | 示例 |
|---|---|---|
where | 对象 | { userId: 'abc', status: 'active' } |
orderBy | 对象 | { createdAt: 'desc' } |
limit | 数字 | 20 |
offset | 数字 | 0 |
select | 数组 | ['id', 'title'] |
身份验证与 RLS
默认情况下,REST CRUD 受 JWT 保护 — 初始化时进行身份验证,以处理限定到用户的数据。RLS 会在服务端强制执行按用户划分的访问控制。
const blink = createClient({
projectId: process.env.NEXT_PUBLIC_BLINK_PROJECT_ID,
publishableKey: process.env.NEXT_PUBLIC_BLINK_PUBLISHABLE_KEY,
auth: { mode: 'managed' },
})
异步操作警告
异步操作(AI、上传)完成前,切勿创建数据库记录 — 可为空字段或缺失数据会导致 NOT NULL 约束失败。只有在获取所有数据后才创建记录。
评论
支持 Markdown