编写数据库模型与校验逻辑
按现有项目约定实现数据库模式与类型化模型,补齐更新校验、索引上线计划及相关测试。
完整提示词
<context>
Mongoose adds a schema to a database that has none, and its defaults trip people up. Validators run on `save()` and `create()` but not on `updateOne`, `findOneAndUpdate` or `updateMany` unless `runValidators: true` is set, so updates write invalid data. `unique: true` is an index, not a validator: it fails with a duplicate key error (E11000) only if the index exists, and it is not built if `autoIndex` is off or the collection already holds duplicates. `autoIndex` builds indexes at startup, which is fine in development and risky on large production collections; production indexes belong in a migration or a deliberate `syncIndexes` step. Middleware differs by operation: `pre('save')` does not run for `findOneAndUpdate`, and `this` means a document in one and a query in the other. Arrays that grow without limit inside a document hit the 16 MB cap and slow every read; references with `populate` or a separate collection fit unbounded children. `lean()` returns plain objects without getters, virtuals or defaults, which is faster for reads but changes what callers get. Since Mongoose 7, `strictQuery` defaults to `false`, so filter fields that are not in the schema are sent to MongoDB as they are instead of being stripped; set it explicitly rather than relying on the version default. With TypeScript, types inferred from the schema (`InferSchemaType`, `HydratedDocument`) avoid a hand-written interface drifting from the schema.
</context>
<task>
Data model or change: {{data_model}}
{{#setup}}Setup: {{setup}}{{/setup}}
1. Read the existing schemas, models, connection setup (including `autoIndex` and `strict` options) and the queries that use the affected collections. Follow the project's conventions for file layout, timestamps and id handling.
2. Decide embed or reference for each relation from size and access pattern: embed small bounded data read with the parent, reference unbounded or independently updated data. Say why in one line each.
3. Write the schemas: types, `required`, enums, `min`/`max`, custom validators, defaults, `timestamps`, and compound indexes that match the real queries (equality fields first, then sort, then range), with `unique` and `partialFilterExpression` where needed.
4. Add virtuals, instance or static methods and middleware only where they earn their place, with the right hook for each operation. Set `runValidators: true` where updates bypass validation.
5. Export typed models (inferred types or a typed schema) and use `lean()` with the matching type for read-only paths.
6. Plan the index rollout: how new indexes get built in production (migration script or `syncIndexes` run on purpose), and what happens if existing data violates a new unique index.
7. Write tests against a test database or an in-memory server, covering validation on create and on update, the unique index, and middleware. Run them and the type check, and report the results.
</task>
<constraints>
- Do not drop indexes or collections, or run `syncIndexes` against a shared or production database yourself.
- Do not rely on `unique` for validation without saying the index must exist and existing duplicates must be cleaned first.
- Do not invent fields beyond the stated model; list missing information as questions.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Schemas
Each schema and model in a fenced block with its path.
## Design notes
Bullets on embed or reference choices, indexes, validators, middleware and lean use.
## Index rollout
How and when indexes are built in each environment, and the duplicate cleanup if needed.
## Check
Commands run with real results.
## Open questions
Numbered, or "None".
</output_format>可替换内容
- {{data_model}}
- {{#setup}}
- {{setup}}
- {{/setup}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、数据库建模、数据校验、索引部署、类型检查