db-core-codegen

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

db-core Code Generation

db-core 代码生成

Use this skill when you need to generate or update any db-core layer in a module:
  1. DB beans
  2. Document beans
  3. DAOs
  4. DB/document converters
  5. Procedures that call DAOs
This skill is designed to keep generation and scaffolding deterministic, schema-driven, and safe across module variants.
当你需要在模块中生成或更新任何db-core层时使用此技能:
  1. DB beans
  2. Document beans
  3. DAOs
  4. DB/document转换器
  5. 调用DAO的流程
此技能旨在确保在不同模块变体中,代码生成和搭建过程具备确定性、schema驱动且安全可靠。

What this skill guarantees

此技能的保障

  1. Schema-first decisions: generation inputs and converter context fields are inferred from discovered schema/model files, not script aliases alone.
  2. Capability-aware planning: generation resolves from module capabilities (or
    generate
    script), with DAO
    --schemas
    validation in both
    --plan auto
    and
    --plan chain
    .
  3. Predictable converter scaffolds: defaults to
    generated/beans/*
    imports, enforces
    convertFrom
    /
    convertTo
    mapping rules, and preserves stable context parameter order.
  4. Safe optional wrappers: validates and uses
    src/beans/*
    and
    src/dao/*
    only when those wrappers actually exist.
  5. Procedure-safe boundaries: procedure APIs stay DB-oriented while DAO APIs stay document-oriented, with convention-aware scaffolding defaults.
  6. Default audit ownership:
    created_at
    /
    updated_at
    ownership stays in DAO unless explicitly overridden.
  7. Preflight pause/resume: pause only when
    node_modules
    is missing; ask the user to run
    npm run install-snapshot
    (override script name via
    --install-script
    ), then resume after user confirmation.
  8. Self-contained execution: command usage, contracts, and post-change checks are defined in this skill without requiring external definition docs.
  9. Module-local context only: inference, examples, conventions, and edits are restricted to the selected
    --module
    root.
  1. Schema优先决策:生成输入和转换器上下文字段从已发现的schema/model文件中推断,而非仅依赖脚本别名。
  2. 能力感知规划:基于模块能力(或
    generate
    脚本)解析生成流程,在
    --plan auto
    --plan chain
    模式下均会验证DAO的
    --schemas
    参数。
  3. 可预测的转换器脚手架:默认导入
    generated/beans/*
    ,强制执行
    convertFrom
    /
    convertTo
    映射规则,并保持稳定的上下文参数顺序。
  4. 安全的可选包装器:仅当
    src/beans/*
    src/dao/*
    包装器实际存在时才会验证并使用它们。
  5. 流程安全边界:流程API保持面向DB的设计,而DAO API保持面向文档的设计,同时遵循约定优先的脚手架默认规则。
  6. 默认审计归属:
    created_at
    /
    updated_at
    的归属权默认由DAO管理,除非显式覆盖。
  7. 预检查暂停/恢复:仅当
    node_modules
    缺失时暂停流程;要求用户执行
    npm run install-snapshot
    (可通过
    --install-script
    覆盖脚本名称),待用户确认后再恢复。
  8. 自包含执行:命令用法、契约和变更后检查均在此技能中定义,无需依赖外部文档。
  9. 仅模块本地上下文:推断、示例、约定和编辑操作均限制在选定的
    --module
    根目录下。

NEVER do these (anti-patterns)

绝对禁止的操作(反模式)

  1. NEVER manually edit generated artifacts under
    generated/beans/*
    or
    generated/dao/*
    .
    1. Why: generators overwrite these files, so manual edits drift and are lost on regeneration.
    2. Correct action: change schema/model inputs and rerun generation.
  2. NEVER patch generated TypeScript to fix field/type/model mismatches.
    1. Why: generated output is derivative; schema/datamodel files are the source of truth.
    2. Correct action: update the schema/datamodel inputs first, then regenerate.
  3. NEVER write or modify code outside the target
    db-core
    scope during this workflow.
    1. Why: cross-scope edits create hidden coupling and unnecessary blast radius.
    2. Exception: update code outside
      db-core
      only when the user explicitly asks for it.
  4. NEVER use sibling/other
    db-core
    modules for context inference, convention inference, or copy patterns.
    1. Why: this introduces cross-module coupling and wrong conventions.
    2. Correct action: infer only from files under the selected module root.
  1. 绝对禁止手动编辑
    generated/beans/*
    generated/dao/*
    下的生成工件。
    1. 原因:生成器会覆盖这些文件,手动修改会导致代码漂移,在重新生成时丢失。
    2. 正确操作:修改schema/model输入后重新运行生成流程。
  2. 绝对禁止通过修补生成的TypeScript代码来修复字段/类型/模型不匹配问题。
    1. 原因:生成的输出是衍生内容,schema/数据模型文件才是唯一可信源。
    2. 正确操作:先更新schema/数据模型输入,再重新生成代码。
  3. 绝对禁止在此工作流中编写或修改db-core范围之外的代码。
    1. 原因:跨范围编辑会引入隐藏耦合和不必要的影响范围。
    2. 例外情况:仅当用户明确要求时,才可以修改db-core范围之外的代码。
  4. 绝对禁止从其他db-core模块中获取上下文推断、约定推断或代码复制模板。
    1. 原因:这会引入跨模块耦合和错误的约定。
    2. 正确操作:仅从选定模块的根目录下的文件中推断信息。

Inputs

输入信息

Collect these before making changes:
  1. module selector
    : exact module path or module hint.
  2. scope
    :
    generation-only
    ,
    converter-only
    ,
    generation-and-converter
    ,
    procedure-methods
    , or
    full-procedure
    .
  3. target entities
    : DB bean + document + DAO + converter names.
  4. persistence contract
    : whether module uses direct
    generated/dao
    or optional
    src/dao
    wrappers.
  5. schema locations
    : discover from generation scripts when not explicitly provided.
在进行变更前需收集以下信息:
  1. module selector
    :精确的模块路径或模块提示词。
  2. scope
    :可选值包括
    generation-only
    converter-only
    generation-and-converter
    procedure-methods
    full-procedure
  3. target entities
    :DB bean + document + DAO + 转换器的名称。
  4. persistence contract
    :模块是否使用直接的
    generated/dao
    或可选的
    src/dao
    包装器。
  5. schema locations
    :未明确提供时,从生成脚本中自动发现。

Module boundary protocol (mandatory)

模块边界协议(强制要求)

  1. Treat the selected
    --module
    directory as the only allowed context root for module files.
  2. Read/inspect only paths that are descendants of that module root when inferring schemas, conventions, converters, procedures, and DAO usage.
  3. Reject any inferred/discovered path that resolves outside module root; do not use it for decisions.
  4. Do not sample procedures/converters from other modules for style inference.
  5. If required files are missing inside module root, report and stop or continue with explicit warning; do not substitute from another module.
  1. 将选定的
    --module
    目录视为模块文件的唯一允许上下文根目录。
  2. 推断schema、约定、转换器、流程和DAO用法时,仅读取和检查该模块根目录的子路径。
  3. 拒绝任何解析到模块根目录之外的推断/发现路径;不得将其用于决策。
  4. 不得从其他模块中采样流程/转换器来进行风格推断。
  5. 如果模块根目录内缺少必要文件,需报告并停止或带显式警告继续;不得从其他模块中替换缺失文件。

Reference loading protocol (mandatory)

参考加载协议(强制要求)

Before any implementation, select
scope
and load only the required references.
  1. generation-only
    :
    1. MANDATORY - read entire
      references/generation-checklist.md
      .
    2. Do NOT load
      references/converter-cookbook.md
      or
      references/procedure-cookbook.md
      .
  2. converter-only
    :
    1. MANDATORY - read entire
      references/converter-cookbook.md
      .
    2. OPTIONAL - read
      references/generation-checklist.md
      only if regeneration is also requested.
    3. Do NOT load
      references/procedure-cookbook.md
      .
  3. generation-and-converter
    :
    1. MANDATORY - read entire
      references/generation-checklist.md
      and
      references/converter-cookbook.md
      .
    2. Do NOT load
      references/procedure-cookbook.md
      unless procedure scaffolding is explicitly requested.
  4. procedure-methods
    or
    full-procedure
    :
    1. MANDATORY - read entire
      references/procedure-cookbook.md
      .
    2. OPTIONAL - read
      references/generation-checklist.md
      only when generation is in scope.
    3. Do NOT load
      references/converter-cookbook.md
      unless converter changes are explicitly requested.
  5. Always report which references were loaded and why before running commands.
在任何实现之前,需选定
scope
并仅加载所需的参考文档。
  1. generation-only
    1. 强制要求 - 完整读取
      references/generation-checklist.md
    2. 禁止加载
      references/converter-cookbook.md
      references/procedure-cookbook.md
  2. converter-only
    1. 强制要求 - 完整读取
      references/converter-cookbook.md
    2. 可选 - 仅当同时要求重新生成时,才读取
      references/generation-checklist.md
    3. 禁止加载
      references/procedure-cookbook.md
  3. generation-and-converter
    1. 强制要求 - 完整读取
      references/generation-checklist.md
      references/converter-cookbook.md
    2. 禁止加载
      references/procedure-cookbook.md
      ,除非显式要求搭建流程。
  4. procedure-methods
    full-procedure
    1. 强制要求 - 完整读取
      references/procedure-cookbook.md
    2. 可选 - 仅当生成在范围内时,才读取
      references/generation-checklist.md
    3. 禁止加载
      references/converter-cookbook.md
      ,除非显式要求修改转换器。
  5. 在运行命令前,需报告已加载的参考文档及其原因。

Discovery-first workflow (required)

发现优先工作流(必需)

Before creating/updating converters or procedures, do all of the following:
  1. Apply
    Reference loading protocol (mandatory)
    and declare loaded files.
  2. Parse module generation scripts and resolve schema/model paths:
    1. document generator (for example
      json-to-schemas --datamodel ... --out ...
      )
    2. DB generator (for example
      jsonschema-to-typescript-objects --input ... --beans-path ...
      )
    3. DAO generator (for example
      pdm-to-daos --datamodel ... --schemas ... --out ...
      )
  3. Resolve all script-relative paths against module root, verify they exist, and verify each resolved path stays within module root.
  4. Read discovered schemas/models to capture per-entity:
    1. required document keys (PK/SK/index keys)
    2. @entity
      discriminator/default
    3. field requiredness and types
  5. Infer converter context fields from schema requirements:
    1. include required document keys not sourced from DB bean fields
    2. prioritize partition/sort keys, then required index keys
    3. keep parameter names consistent with existing module converter/procedure patterns
  6. If multiple context-field candidates are plausible:
    1. rank candidates with brief reasoning
    2. choose the best candidate and proceed
    3. surface the decision in output
  7. Missing discovered model/schema files should fail inference (or be surfaced as warnings when non-blocking).
在创建/更新转换器或流程之前,需完成以下所有步骤:
  1. 遵循「参考加载协议(强制要求)」并声明已加载的文件。
  2. 解析模块生成脚本并解析schema/model路径:
    1. 文档生成器(例如
      json-to-schemas --datamodel ... --out ...
    2. DB生成器(例如
      jsonschema-to-typescript-objects --input ... --beans-path ...
    3. DAO生成器(例如
      pdm-to-daos --datamodel ... --schemas ... --out ...
  3. 基于模块根目录解析所有脚本相对路径,验证它们是否存在,并确保每个解析后的路径都在模块根目录内。
  4. 读取已发现的schemas/models以捕获每个实体的以下信息:
    1. 必需的文档键(PK/SK/索引键)
    2. @entity
      鉴别器/默认值
    3. 字段的必填性和类型
  5. 从schema要求中推断转换器上下文字段:
    1. 包含并非来自DB bean字段的必需文档键
    2. 优先考虑分区键/排序键,其次是必需的索引键
    3. 保持参数名称与现有模块的转换器/流程模式一致
  6. 如果存在多个合理的上下文字段候选:
    1. 对候选进行排序并给出简要理由
    2. 选择最佳候选并继续
    3. 在输出中明确说明该决策
  7. 缺失已发现的model/schema文件会导致推断失败(或在非阻塞情况下显示为警告)。

Script commands

脚本命令

Run from repository root.
从仓库根目录运行。

Generation

代码生成

bash
python .agents/skills/db-core-codegen/scripts/run_dbcore_codegen.py \
  --module <module_root> \
  --plan auto
--plan auto
:
  1. Uses
    generate
    script if available.
  2. Otherwise resolves chain by capability (DB, document, DAO, optional tx DAO).
--plan chain
:
  1. Always resolves chain by capability.
  2. Requires DB + document + DAO capabilities.
  3. Includes tx DAO only when available or explicitly requested.
Install preflight:
  1. Before running generation scripts, checks
    generated/beans/db
    ,
    generated/beans/document
    , and
    generated/dao
    .
  2. Also checks whether
    node_modules
    exists.
  3. If
    node_modules
    is missing, do not run install commands automatically; pause and ask the user to run
    npm run install-snapshot
    .
  4. Missing/empty generated outputs are surfaced in preflight metadata, but generation still proceeds when
    node_modules
    exists.
  5. Override install script name with
    --install-script <script-name>
    .
Pause/resume protocol:
  1. On preflight failure, stop generation and report the exact command and module root to the user.
  2. Do not execute install/setup commands on behalf of the user.
  3. After user confirms completion, rerun the same generation command to resume.
bash
python .agents/skills/db-core-codegen/scripts/run_dbcore_codegen.py \
  --module <module_root> \
  --plan auto
--plan auto
  1. 如果可用则使用
    generate
    脚本。
  2. 否则根据能力解析流程链(DB、document、DAO、可选的tx DAO)。
--plan chain
  1. 始终根据能力解析流程链。
  2. 需要DB + document + DAO能力。
  3. 仅当可用或显式要求时才包含tx DAO。
预检查安装:
  1. 在运行生成脚本之前,检查
    generated/beans/db
    generated/beans/document
    generated/dao
    目录。
  2. 同时检查
    node_modules
    是否存在。
  3. 如果
    node_modules
    缺失,不得自动运行安装命令;需暂停并要求用户执行
    npm run install-snapshot
  4. 预检查元数据中会显示缺失/空的生成输出,但只要
    node_modules
    存在,生成流程仍会继续。
  5. 可通过
    --install-script <script-name>
    覆盖安装脚本名称。
暂停/恢复协议:
  1. 预检查失败时,停止生成流程并向用户报告确切的命令和模块根目录。
  2. 不得代表用户执行安装/设置命令。
  3. 用户确认完成后,重新运行相同的生成命令以恢复流程。

Converter scaffolding

转换器脚手架

Flat converter layout:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_converter.py \
  --module <module_root> \
  --converter-dir . \
  --db-type Account \
  --document-type Account \
  --entity-id com.example.document.account \
  --with-list
Nested converter layout:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_converter.py \
  --module <module_root> \
  --converter-dir events/event-subscription \
  --db-type EventSubscription \
  --document-type EventSubscription \
  --entity-id com.example.document.event_subscription \
  --context-field user_id:userId
If
--context-field
is omitted, context fields are inferred from discovered schema/model metadata.
扁平化转换器布局:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_converter.py \
  --module <module_root> \
  --converter-dir . \
  --db-type Account \
  --document-type Account \
  --entity-id com.example.document.account \
  --with-list
嵌套转换器布局:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_converter.py \
  --module <module_root> \
  --converter-dir events/event-subscription \
  --db-type EventSubscription \
  --document-type EventSubscription \
  --entity-id com.example.document.event_subscription \
  --context-field user_id:userId
如果省略
--context-field
,则从已发现的schema/model元数据中推断上下文字段。

Procedure scaffolding

流程脚手架

CRUD method scaffolding with convention-aware defaults:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_procedure.py \
  --module <module_root> \
  --db-type Link \
  --document-type Link \
  --methods get,post,update
Common overrides:
  1. --timestamp-strategy procedure|dao
    to override timestamp ownership (default:
    dao
    ).
  2. --post-generate-id always|never
    to force UUID generation behavior.
  3. --beans-import-style index|entity
    and
    --dao-import-style index|entity
    .
  4. --dao-class <EntityDaoClass>
    and
    --dao-method-*
    when DAO naming differs.
遵循约定优先默认规则的CRUD方法脚手架:
bash
python .agents/skills/db-core-codegen/scripts/generate_db_procedure.py \
  --module <module_root> \
  --db-type Link \
  --document-type Link \
  --methods get,post,update
常见覆盖配置:
  1. --timestamp-strategy procedure|dao
    :覆盖时间戳归属(默认值:
    dao
    )。
  2. --post-generate-id always|never
    :强制UUID生成行为。
  3. --beans-import-style index|entity
    --dao-import-style index|entity
  4. --dao-class <EntityDaoClass>
    --dao-method-*
    :当DAO命名不符合约定时使用。

Contracts

契约

Generation contract

生成契约

  1. Validate selected DAO/tx DAO scripts have correct
    --schemas
    usage.
  2. Run install preflight checks before generation; pause for manual user action only when
    node_modules
    is missing.
  3. Validate generated outputs:
    1. generated/beans/db
    2. generated/beans/document
    3. generated/dao
  4. Validate wrappers only if wrapper files exist.
  5. Validate package export/type contracts when present.
  1. 验证选定的DAO/tx DAO脚本是否正确使用
    --schemas
    参数。
  2. 生成前运行安装预检查;仅当
    node_modules
    缺失时暂停,等待用户手动操作。
  3. 验证生成输出:
    1. generated/beans/db
    2. generated/beans/document
    3. generated/dao
  4. 仅当包装器文件存在时才验证它们。
  5. 若存在包导出/类型契约,则进行验证。

Converter contract

转换器契约

  1. convertFrom(document)
    maps
    Document -> DB
    .
  2. convertTo(db, ...context)
    maps
    DB -> Document
    .
  3. convertTo
    must set
    @entity
    .
  4. Context keys must come from schema-first inference and be explicit typed parameters.
  5. Context parameter order should be stable:
    1. partition key context
    2. sort key context not available on DB bean
    3. required index context
  6. Failures must throw
    ConverterException
    .
  1. convertFrom(document)
    实现
    Document -> DB
    的映射。
  2. convertTo(db, ...context)
    实现
    DB -> Document
    的映射。
  3. convertTo
    必须设置
    @entity
    字段。
  4. 上下文键必须来自schema优先的推断,并且是显式的类型化参数。
  5. 上下文参数顺序应保持稳定:
    1. 分区键上下文
    2. DB bean中不存在的排序键上下文
    3. 必需的索引键上下文
  6. 执行失败时必须抛出
    ConverterException

Procedure contract

流程契约

  1. Procedure boundary accepts/returns DB objects.
  2. DAO boundary accepts/returns document objects.
  3. Procedures should use DAOs via module capability:
    1. generated/dao
      by default.
    2. src/dao
      wrappers only when that capability exists in the module.
  4. DAO selection must be explicit from method intent:
    1. Use
      TableDao
      for query/list/index pagination and non-atomic batch access.
    2. Use
      TransactionDao
      only when atomic all-or-nothing behavior or cross-item invariant enforcement is required.
    3. If atomicity is not explicitly required, default to
      TableDao
      (or
      EntityDao
      for single-item key CRUD).
  5. Capture and report DAO choice inputs when scaffolding procedure methods:
    1. operation type (
      query/list
      ,
      single-item CRUD
      ,
      multi-item mutation
      )
    2. atomicity required (
      true/false
      )
    3. invariant scope (single item vs cross-item/cross-entity)
  6. Audit timestamps (
    created_at
    /
    updated_at
    and camel variants) are DAO-managed by default.
    1. Use
      --timestamp-strategy procedure
      only when a module intentionally follows that non-default pattern.
    2. Surface the chosen strategy in output.
  1. 流程边界接收/返回DB对象。
  2. DAO边界接收/返回文档对象。
  3. 流程应通过模块能力使用DAO:
    1. 默认使用
      generated/dao
    2. 仅当模块具备该能力时才使用
      src/dao
      包装器。
  4. DAO的选择必须明确匹配方法意图:
    1. 使用
      TableDao
      处理查询/列表/索引分页和非原子批量访问。
    2. 仅当需要原子化的全有或全无行为,或需要跨实体约束校验时才使用
      TransactionDao
    3. 若未显式要求原子性,默认使用
      TableDao
      (或针对单键CRUD使用
      EntityDao
      )。
  5. 搭建流程方法时,需捕获并报告DAO选择的输入依据:
    1. 操作类型(
      query/list
      单条数据CRUD
      多条数据变更
    2. 是否需要原子性(
      true/false
    3. 约束范围(单条数据 vs 跨数据/跨实体)
  6. 审计时间戳(
    created_at
    /
    updated_at
    及其驼峰式变体)默认由DAO管理。
    1. 仅当模块有意遵循非默认模式时,才使用
      --timestamp-strategy procedure
    2. 在输出中明确说明所选的策略。

Output checklist

输出检查清单

After changes:
  1. Generation plan is explicit and valid for detected module capabilities.
  2. Converter files compile and exports are updated idempotently.
  3. No hard failures due to absent optional wrapper files.
  4. Discovered schema/model paths are reported.
  5. Inferred context fields are reported per entity with brief reasoning.
  6. Dry-run output is available (human +
    --json
    modes).
  7. --json
    output includes:
    1. discovered_schema_paths
    2. selected_script_paths
    3. validated_schema_targets
    4. inferred_context_fields
    5. context_decisions
    6. entity_metadata
    7. install_preflight
      (
      script
      ,
      script_exists
      ,
      command
      ,
      executed
      ,
      manual_action_required
      ,
      missing_outputs
      ,
      missing_node_modules
      )
  8. Procedure scaffold output (when used) reports:
    1. resolved conventions
    2. resolved import styles
    3. selected mutation strategy
  9. Report
    loaded_references
    and
    reference_selection_reason
    .
  10. Confirm no manual edits to generated files and no out-of-scope (
    db-core
    ) changes unless explicitly requested by the user.
  11. Report
    module_root
    and confirm all module-file context paths used for inference were under that root.
变更完成后:
  1. 生成计划明确且符合检测到的模块能力。
  2. 转换器文件可编译,且导出内容支持幂等更新。
  3. 不存在因可选包装器文件缺失导致的严重失败。
  4. 已报告发现的schema/model路径。
  5. 已按实体报告推断的上下文字段及简要理由。
  6. 提供试运行输出(人类可读 +
    --json
    模式)。
  7. --json
    输出包含:
    1. discovered_schema_paths
    2. selected_script_paths
    3. validated_schema_targets
    4. inferred_context_fields
    5. context_decisions
    6. entity_metadata
    7. install_preflight
      script
      script_exists
      command
      executed
      manual_action_required
      missing_outputs
      missing_node_modules
  8. 流程脚手架输出(若使用)需报告:
    1. 解析后的约定
    2. 解析后的导入风格
    3. 选定的变更策略
  9. 报告
    loaded_references
    reference_selection_reason
  10. 确认未手动编辑生成文件,且未进行超出db-core范围的变更,除非用户明确要求。
  11. 报告
    module_root
    ,并确认所有用于推断的模块文件上下文路径均在该根目录下。

References

参考文档

Load these only through the
Reference loading protocol (mandatory)
above.
  1. references/generation-checklist.md
  2. references/converter-cookbook.md
  3. references/procedure-cookbook.md
仅可通过上述「参考加载协议(强制要求)」加载以下文档。
  1. references/generation-checklist.md
  2. references/converter-cookbook.md
  3. references/procedure-cookbook.md