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

nocobase-db

它能做什么

这项技能指导你的 AI 处理 NocoBase 3 应用的数据库层。适合为应用添加数据表、迁移或数据逻辑的开发者。

适用场景

  • ✓当你需要在 NocoBase 3 应用中添加或修改数据表或字段时。
  • ✓当你编写的代码需要读取或保存记录,并且要使用事务时。
  • ✓当你配置数据库连接,或编写迁移和种子数据时。

技能链接

https://github.com/nocobase/nocobase/blob/v3-develop/packages/libs/db/skills/nocobase-db/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请帮我在 NocoBase 应用中,通过迁移添加一张与客户关联的订单表。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会先判断任务应放在哪一层:配置、迁移、表结构构建器,还是运行时数据访问层。然后用正确的接口和事务处理方式编写代码。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

The database in a NocoBase 3 application

Most mistakes here are made before the first line of code, by putting a task at the wrong layer: changing schema from runtime code, reading records through the schema builder, or reimplementing relation writes on top of raw queries.

Six areas, in the order an application grows into them:

Area Owns
Configuration which databases exist, and which dialect each one speaks
Migrations and seeds schema history and install data
Builder how a migration declares tables, fields and constraints
Repository and Query runtime reads and writes
Transactions making several writes succeed or fail together
Collections what the database actually resolves to, for code and for you

The exact parameters and return types are always the TypeScript declarations in @nocobase/db. This Skill decides which API to reach for, where the code belongs, and the boundaries the types cannot express.

Where the code lives

server/config/database.ts          connections and the default connection
config.yml                         per-environment overrides and credentials (gitignored)
database/<connection>/migrations   schema history for that connection
database/<connection>/seeds        install data for that connection
database/<connection>/collections  resolved Collection artifacts
server/routes/, server/providers   runtime reads and writes

<connection> is a connection name from server/config/database.ts, so the default connection's migrations are in database/main/migrations/. Adding a connection means adding its directory alongside and keeping database/tsconfig.json in place, so migrations and seeds are typechecked and linted as server source.

Runtime code never changes schema, and a migration never imports runtime code. That split is what these directories encode.

1. Configuration

Connections are declared in code and overridden per environment:

Location Responsibility
server/config/database.ts Connection defaults through defineAppDatabaseConfig
config.yml Environment overrides including credentials — gitignored
config.example.yml The documented structure and credential placeholders; keep it in step
import {
  connectionEnvironment,
  defineAppDatabaseConfig,
} from '@nocobase/app-server/database';

export default defineAppDatabaseConfig(
  ({ paths }) => ({
    default: 'main',
    connections: {
      main: {
        dialect: 'sqlite',
        filename: paths.storage('database.sqlite'),
        schemaManagement: 'managed',
        debug: false,
      },
    },
  }),
  { env: connectionEnvironment('main') },
);

The second argument maps environment variables onto the section: connectionEnvironment('main') declares DB_DIALECT, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_SSL and DB_FILENAME for the main connection, each overriding config.yml. Only main has them; a connection you add declares its own with a prefix naming it, connectionEnvironment('analytics', 'DB_ANALYTICS'), merged into the same env.

The dialects are sqlite, postgres, mysql, mssql, oracle, dameng, kingbase and oceanbase. Each is an optional peer of @nocobase/app-server shipped as its own package: install @nocobase/db-<dialect> into dependencies — so it reaches the deployed server — and configure the dialect. No import or driver registration is needed, and the dialect package brings its own underlying driver.

Switching a dialect means changing both layers. config.yml deep-merges into the code defaults, so editing only the code leaves a stale YAML override in place — with fields belonging to the previous dialect still applied. Remove filename

常见问题

我可以在普通的运行时代码中修改数据表吗?

技能说明,数据表的修改应放在迁移中,而不是运行时代码里。

这项技能适用于 NocoBase 2 吗?

不适用。NocoBase 2 使用不同的数据库包。

什么是种子数据?

种子数据是写入数据库的安装数据,应用启动时需要它们。

详情

作者的原始描述

Work with the database in a NocoBase 3 application: configure connections and dialects, add migrations and seeds, declare schema with the Collection Builder, read and write records with Repository or QueryAdapter, run transactions, and read Collection definitions. Use for any task under database/ or any server code that touches data. Do not use for the NocoBase 2 @nocobase/database package.

repo: nocobase/nocobase

热度

GitHub 星标
25k
排名
#10083
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能