Use this skill when creating or reviewing Next.js Route Handlers (app/api), adding rate limiting, input validation, or auth to API endpoints. Trigger words: API ルート, Route Handler, app/api, エンドポイント, レート制限, 入力検証, CORS, ヘッダー, 認証付き API, hardening.
mongoose-modeling
Use this skill when designing MongoDB schemas, defining Mongoose models, choosing embed-vs-reference, or adding indexes. Trigger words: スキーマ設計, モデル定義, Mongoose, コレクション, インデックス, 埋め込み, 参照, embed, reference, index.
含まれるファイル(1)
- SKILL.md2.3 KB
SKILL.md(原文)
インストールする前に、エージェントに与えられる指示の中身を確認できます。
Mongoose モデリング
いつ使うか
- 新しいコレクションのスキーマを設計するとき。
- 埋め込み(embed)か参照(reference)かを判断するとき。
- インデックスを設計・追加するとき。
やること
- スキーマは「どうクエリするか」から逆算して設計する(アクセスパターン駆動)。
- 一緒に読む・1 対少数・更新が一体 → 埋め込み。独立して伸びる・多対多 → 参照。
- 主要クエリフィールドにインデックスを張る。複合は ESR(Equality → Sort → Range) 順。
- Mongoose の型と Zod スキーマを 1 箇所で同期させる(DB 境界も Zod 検証)。
- 接続は使い回す(開発時のホットリロードで多重接続しないようキャッシュ)。
守るルール
- ✅
_id以外の主要クエリフィールドへインデックス必須。 - ✅ 複合インデックスは ESR ルール順。
- ✅ 読み取り専用は
.lean()で plain object を返す。 - ✅
timestamps: trueでcreatedAt/updatedAtを持たせる。 - ❌
export defaultでモデルを出さない(named export)。 - ❌ 接続文字列をハードコードしない。
典型例(コード片)
// models/user.ts
import { Schema, model, models, type InferSchemaType } from 'mongoose';
const userSchema = new Schema(
{
email: { type: String, required: true, unique: true },
name: { type: String, required: true },
role: { type: String, enum: ['user', 'admin'], default: 'user' },
},
{ timestamps: true },
);
userSchema.index({ role: 1, createdAt: -1 }); // ESR: role=Equality, createdAt=Sort
export type User = InferSchemaType<typeof userSchema>;
export const UserModel = models.User ?? model('User', userSchema);
アンチパターン
- 巨大配列を無限に埋め込む(ドキュメント 16MB 上限・更新コスト増)。
- インデックスを張らずに
findを量産 →COLLSCAN。 - スキーマと TypeScript 型が二重管理でズレる。
レビュー
まだレビューはありません。使ってみた感想をお寄せください。
同じリポジトリのスキル
概要と使いどころ
Use this skill when implementing full-text search, autocomplete, or vector/semantic search and RAG with MongoDB Atlas. Trigger words: 全文検索, あいまい検索, オートコンプリート, ベクトル検索, セマンティック検索, 類似検索, Atlas Search, $search, $vectorSearch, RAG, embedding.
Use this skill when implementing sign-in, sessions, authorization checks, or protecting routes and Server Actions. Trigger words: 認証, ログイン, サインイン, セッション, 認可, 権限チェック, Better Auth, Auth.js, 保護, ミドルウェア, getSession.
Use this skill when starting a new session, opening this project for the first time, or when you need to recall the project's tech stack, hard rules, and which other skills exist. Trigger words: セッション開始, このプロジェクト, 全体像, どのスキル, 何から, getting started, overview.
Use this skill when setting up or modifying CI pipelines, GitHub Actions workflows, or automated checks (lint, typecheck, test, build) for this project. Trigger words: CI, GitHub Actions, ワークフロー, パイプライン, 自動テスト, lint チェック, typecheck, ビルド検証, .github/workflows.
Use this skill when writing end-to-end tests, browser automation, or verifying user flows in a real browser. Trigger words: E2E, Playwright, ブラウザテスト, ユーザーフロー, 結合テスト, シナリオテスト, ログインテスト, getByRole, expect.