跳到正文
Skillpedia
API 与服务器(后端)7.6k · #13172

portability

它能做什么

这项技能展示如何定义数据表并编写数据库查询,使其在本地和托管的 PostgreSQL 中行为一致。适合构建 Agent-Native 应用模板的开发者。

适用场景

  • ✓当你为应用定义新的数据库表或架构的时候。
  • ✓当你编写原始 SQL 或新的服务器路由来读取或保存数据的时候。
  • ✓当你希望应用无需修改即可在本地数据库和托管数据库上运行的时候。

技能链接

https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/portability/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/portability/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 BIGINT directly 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 stores
  • server-plugins — Framework routes and H3 handler patterns
  • security — 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能