home / skills / forge-town / skills / dao-best-practice

dao-best-practice skill

/skills/dao-best-practice

This skill helps standardize DAO files to align with Drizzle ORM best practices, enforcing structure, naming, types, and performance patterns.

npx playbooks add skill forge-town/skills --skill dao-best-practice

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
1.2 KB
---
name: dao-best-practice
description: 用于规范化DAO文件,确保遵循Drizzle ORM最佳实践,包括文件结构、方法命名、类型安全和性能优化。当需要重构或创建DAO文件以符合项目标准时使用。
---

# DAO规范化技能

此技能帮助规范化DAO文件,使其符合项目的最佳实践。

## 使用说明

1. 识别需要规范化的DAO文件,通常位于 `apps/web/src/db/models/daos/`。

2. 阅读参考指南:[DAO最佳实践指南](references/dao-best-practice.md) 以了解规范。
3. 代码示例:[DAO代码参考](references/template-dao.ts) 作为参考。
4. 使用[检查清单](references/checklist.md)验证文件是否符合规范。

5. 重构文件以匹配指南中的模式。

**重要:** 调用完毕技能后,强制查看[检查清单](references/checklist.md),并确保返回的内容完全匹配清单中的所有项目。

## 核心规范

- 文件结构:文件名与表名一致,导出为对象。
- 方法:使用驼峰命名,标准CRUD方法。
- 类型安全:使用Drizzle类型推断。
- 性能:使用分页、联表查询等。

如果需要更多细节,请阅读参考文件。

Overview

This skill standardizes DAO files to follow Drizzle ORM best practices for file layout, method naming, type safety, and performance. It guides refactors or new DAO creation to meet project conventions and ensures every DAO passes the project's verification checklist. The skill integrates reference patterns and enforces final checklist validation before completion.

How this skill works

The skill scans DAO files in the configured DAO directory, compares each file against the reference patterns and checklist, and identifies deviations in structure, naming, types, and queries. It suggests or applies refactors to align exports, CRUD method signatures, and Drizzle-based typings. Before finishing, it forces a final verification step ensuring the file fully meets the checklist items.

When to use it

  • Refactoring existing DAOs to match project standards
  • Creating new DAO files for new database tables
  • Performing code reviews focused on data access patterns
  • Fixing type inference or runtime errors related to Drizzle usage
  • Optimizing queries for performance (pagination, joins)

Best practices

  • Name DAO files to match the table and export a single DAO object
  • Use camelCase for methods and implement standard CRUD methods consistently
  • Leverage Drizzle type inference for input/output types and avoid any types
  • Prefer paginated queries and joins instead of fetching large result sets
  • Keep query logic encapsulated in DAO methods and avoid business logic leakage
  • Run the project's checklist verification as the final step for each DAO

Example use cases

  • Refactor users.dao.ts to replace loose types with Drizzle-inferred types and standardize method names
  • Create a new orders.dao.ts with paginated list and join-to-customer methods following the DAO pattern
  • Audit DAOs before a release to ensure all files export the expected DAO object and method signatures
  • Resolve performance issues by converting full-table scans to cursor-based pagination in DAO methods
  • Update DAOs to use explicit transaction-safe patterns and typed input objects for inserts/updates

FAQ

Which directory does the skill inspect by default?

It targets the project's DAO directory configured in the workflow; adjust the path if your DAOs are stored elsewhere.

Does the skill modify files automatically?

It can suggest or apply refactors depending on workflow permissions, but always performs a final checklist verification before completing changes.