portability
它能做什么
这项技能展示如何定义数据表并编写数据库查询,使其在本地和托管的 PostgreSQL 中行为一致。适合构建 Agent-Native 应用模板的开发者。
适用场景
- ✓当你为应用定义新的数据库表或架构的时候。
- ✓当你编写原始 SQL 或新的服务器路由来读取或保存数据的时候。
- ✓当你希望应用无需修改即可在本地数据库和托管数据库上运行的时候。
技能链接
https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/portability/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请添加一个用于记录餐食的表,以及一个保存每条记录的服务器路由,并确保它能在任何托管环境中运行。”
每周 5 个新技能,1 分钟读完。
工作原理
技能要求智能体使用 Drizzle 的 PostgreSQL 工具来定义表和查询,使同一段代码能在本地和托管数据库上运行。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Portability
Rule
Templates use one PostgreSQL schema and query contract. Local PGlite and hosted PostgreSQL share the same SQL semantics.
Database
Use Drizzle's PostgreSQL exports directly for schemas and its query builder for reads/writes:
import { sql } from "drizzle-orm";
import {
boolean,
doublePrecision,
integer,
pgTable,
text,
} from "drizzle-orm/pg-core";
export const meals = pgTable("meals", {
id: text("id").primaryKey(),
name: text("name").notNull(),
calories: integer("calories").notNull(),
weight: doublePrecision("weight"),
archived: boolean("archived").notNull().default(false),
createdAt: text("created_at").notNull().default(sql`now()`),
});
| Export | Purpose |
|---|---|
pgTable |
Defines a PostgreSQL table |
text |
Defines a text column, with optional enum values |
integer |
Defines an integer column |
boolean |
Defines a boolean column |
doublePrecision |
Defines a double-precision column |
sql |
Builds SQL expressions such as now() |
Use @agent-native/core/db/schema only for framework-owned sharing helpers such
as ownableColumns() and createSharesTable().
Use Drizzle's PostgreSQL query builder for app code:
import { and, desc, eq } from "drizzle-orm";
const rows = await db
.select()
.from(meals)
.where(and(eq(meals.ownerEmail, userEmail), eq(meals.archived, false)))
.orderBy(desc(meals.createdAt));
Avoid db.execute(...), getDbExec(), and handwritten SQL in actions, handlers, and stores when Drizzle can express the query. Raw SQL should be limited to additive migrations, health checks, carefully reviewed advanced queries, or one-off maintenance scripts. For timestamps in Drizzle schemas, use .default(now()); for migration SQL, use runMigrations().
Raw SQL helpers
getDbExec()— executes parameterized PostgreSQL SQL- Use PostgreSQL types such as
BIGINTdirectly in raw SQL.
Never
When writing docs, say "PostgreSQL" or "PGlite" precisely.
Use Postgres syntax deliberately in advanced queries and migrations, and prefer Drizzle APIs or framework helpers for ordinary application code.
When giving deployment guidance, be precise about durability: local PGlite is for development, while shared and production environments need a persistent hosted PostgreSQL DATABASE_URL.
Hosting Agnostic
The server runs on Nitro with H3 as the HTTP framework. Templates must be deployable to any Nitro-supported target.
Never use Express
All server code uses H3/Nitro: defineEventHandler, readBody, getMethod, setResponseHeader, etc. Express is not a dependency. If you see Express types or patterns anywhere, replace them with H3 equivalents.
No platform-specific config in scaffolded template source
Files like netlify.toml, wrangler.toml, vercel.json, and netlify/functions/ must NOT appear in the CLI scaffold source (packages/core/src/templates/) — apps generated for users stay hosting-agnostic, with platform configuration living in CI/hosting dashboards.
This monorepo's own first-party deployed apps keep platform configuration in app-specific hosting manifests such as templates/*/netlify.toml; this rule only applies to the CLI scaffold source.
No Node APIs in server routes/plugins
Never use fs, child_process, or path in server routes and plugins. Use Nitro abstractions. (Actions in actions/ run in Node.js and can use Node APIs freely.)
No persistent-process assumptions
Never assume a persistent server process. Use the SQL database for all state.
Related Skills
storing-data— Schema patterns and the core SQL storesserver-plugins— Framework routes and H3 handler patternssecurity— SQL injection prevention via parameterized queries
常见问题
架构应使用哪个库?
它使用 Drizzle 的 PostgreSQL 导出来定义表并构建查询。
它能与本地数据库一起使用吗?
可以。本地的 PGlite 与托管的 PostgreSQL 旨在共享相同的 SQL 行为。
什么时候应使用共享的架构辅助函数?
只用于框架的共享辅助函数,例如可拥有的列和 shares 表。
详情
作者的原始描述
How to keep template code PostgreSQL-specific and hosting-agnostic. Use when defining schemas, writing raw SQL, or creating server routes.
repo: BuilderIO/agent-native
热度
- GitHub 星标
- 7.6k
- 排名
- #13172
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …