storing-data
它能做什么
这项技能说明 Agent-Native 应用将记录保存在 SQL 中,而图片和视频等大文件则保存到文件存储中。适合添加数据模型或读写应用数据的开发者。
适用场景
- ✓当你为应用添加新的数据模型或表的时候。
- ✓当你需要判断数据应放在数据库中还是文件中的时候。
- ✓当应用需要保存图片、视频或 PDF 的时候。
技能链接
https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/storing-data/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请添加一种保存用户上传图片的方式,并在数据库中保留每张图片的记录。”
每周 5 个新技能,1 分钟读完。
工作原理
记录和引用保存在数据库中,二进制内容通过文件存储工具处理。数据库中只保存返回的 ID 或网址。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Storing Data — SQL is the Source of Truth
Rule
All application data lives in SQL (local PGlite, hosted Postgres in production). The agent and UI share the same database. SQL stores structured records, metadata, references, and searchable text — not large raw file payloads. Do not store durable app data in the filesystem unless the app is explicitly running a Local File Mode artifact flow described below.
Large binary or file-like payloads (images, video/audio, PDFs, ZIPs, screenshots, session replay chunks, thumbnails, generated assets, data: URLs, and base64 file bodies) must go through configured file/blob storage such as uploadFile() or putPrivateBlob(). Persist only the returned URL, asset id, or opaque blob handle in SQL. If storage is unavailable in hosted or persistent-database mode, fail closed with setup guidance instead of falling back to base64 in application_state, settings, resources, or app tables.
Uploaded files an action opens later (chat attachments, import controls, chunked commits) are one core AttachmentRef: mint with mintAttachmentRef, open with resolveAttachment and unwrapAttachment from @agent-native/core/private-blob. Never write a template descriptor, never branch on error text, and never truncate a ref (ATTACHMENT_REF_MAX_CHARS is the floor for any prompt or field that carries one). Each failure is typed: notFound, forbiddenScope, expired and malformed are definitive and stop the turn, except a value that is not a ref at all or is missing (malformed with unrecognized_scheme or empty), which is an ordinary action error the model corrects by passing the listed ref; only storageUnavailable is retryable, and the same ref works once storage is back, so never ask the user to attach the file again for it.
Local File Mode exception: some artifact apps (Content, Plans, Slides, Dashboards, Designs, etc.) can intentionally use repo files as the source of truth for the artifact itself. This must be explicit via agent-native.json, AGENT_NATIVE_MODE=local-files, or an app-owned local-file action helper. In that mode, the UI and agent still go through app actions, but those actions read/write scoped files through @agent-native/core/local-artifacts instead of SQL rows. App state, auth, settings, credentials, collaboration metadata, and hosted database mode remain SQL. File-to-database or file-to-provider synchronization is an explicit sync step, not an implicit side effect of editing.
When you add a data model, a list, or a read path, also follow the performance skill: project only the columns a list renders, index the columns hot queries filter/sort on, and avoid query waterfalls — so apps stay fast as data grows.
How It Works
Agent-Native apps use Drizzle ORM over PostgreSQL. Local development uses PGlite at data/pglite; production and shared preview deploys need a persistent hosted PostgreSQL DATABASE_URL.
For app code, use Drizzle's schema/query DSL by default. Raw SQL is an escape hatch for additive migrations, health checks, or one-off maintenance, not the normal way to build features.
Typed raw SQL fragments
When Drizzle cannot express a PostgreSQL query, import sql from
@agent-native/core/db and pass its fragment directly to
getDbExec().execute(). Use sql.value(value) for bound values,
sql.ident(name) for validated lowercase identifiers, sql.in(values) for a
nonempty list (it includes parentheses), and sql.join(fragments, separator)
or nested fragments for composition. The tag rejects plain string interpolation
at compile time; do not build { sql: ...${value}... } or pass interpolated
templates directly to execute(). The diff-scoped guard checks new
interpolated SQL reaching getDbExec().execute() through the helper and wrapper
paths it analyzes. A reviewed exception needs // raw-sql-ok: <reason> as
a standalone comment immediately before the sink statement or after it on the
same line. Markers inside SQL text, callbacks, and object properties do not opt
out. The guard does not audit Drizzle sql.raw() or raw adapter query() calls,
so use bound Drizzle fragments or the typed kernel instead of interpolating
dynamic values through those APIs.
Schema and migration ownership
Define PostgreSQL tables with Drizzle's drizzle-orm/pg-core exports and use
drizzle-orm for query operators. Import only framework-owned sharing helpers,
such as ownableColumns() and createSharesTable(), fro
常见问题
应用数据可以作为磁盘上的文件保存吗?
通常不可以。除非使用特定的文件模式,持久的应用数据不应放在文件系统中。
我应该把图片以 base64 文本形式存入数据库吗?
不应该。技能要求在出现这种情况时给出设置指引并报错,而不是把文件内容存入数据库。
对于文件,我在数据库中应保存什么?
只保存其网址、资产 ID 或不透明的句柄。
详情
作者的原始描述
How to store application data in agent-native apps. All data lives in SQL. Use when adding data models, deciding where to store data, or reading/writing application data.
repo: BuilderIO/agent-native
热度
- GitHub 星标
- 7.6k
- 排名
- #13185
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …