docs(crud): add implementation examples and complete docs task
This commit is contained in:
69
docs/product-engineering/crud-examples.md
Normal file
69
docs/product-engineering/crud-examples.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# CRUD Examples
|
||||
|
||||
## Goal
|
||||
|
||||
Provide concrete implementation patterns for new entities adopting `@cms/crud`.
|
||||
|
||||
## Example: Service Factory Wiring
|
||||
|
||||
```ts
|
||||
import { createCrudService } from "@cms/crud"
|
||||
import { createPageInputSchema, updatePageInputSchema } from "@cms/content"
|
||||
|
||||
const pageCrudService = createCrudService({
|
||||
resource: "page",
|
||||
repository: pageRepository,
|
||||
schemas: {
|
||||
create: createPageInputSchema,
|
||||
update: updatePageInputSchema,
|
||||
},
|
||||
auditHooks: pageAuditHooks,
|
||||
})
|
||||
```
|
||||
|
||||
## Example: Repository Contract
|
||||
|
||||
```ts
|
||||
const pageRepository = {
|
||||
list: () => db.page.findMany({ orderBy: { updatedAt: "desc" } }),
|
||||
findById: (id: string) => db.page.findUnique({ where: { id } }),
|
||||
create: (input: CreatePageInput) => db.page.create({ data: input }),
|
||||
update: (id: string, input: UpdatePageInput) =>
|
||||
db.page.update({
|
||||
where: { id },
|
||||
data: input,
|
||||
}),
|
||||
delete: (id: string) => db.page.delete({ where: { id } }),
|
||||
}
|
||||
```
|
||||
|
||||
## Example: Action Usage
|
||||
|
||||
```ts
|
||||
export async function createPage(input: unknown, context?: CrudMutationContext) {
|
||||
return pageCrudService.create(input, context)
|
||||
}
|
||||
|
||||
export async function updatePage(id: string, input: unknown, context?: CrudMutationContext) {
|
||||
return pageCrudService.update(id, input, context)
|
||||
}
|
||||
|
||||
export async function deletePage(id: string, context?: CrudMutationContext) {
|
||||
return pageCrudService.delete(id, context)
|
||||
}
|
||||
```
|
||||
|
||||
## Testing Expectations
|
||||
|
||||
- validation failure returns `CrudValidationError`
|
||||
- missing IDs return `CrudNotFoundError`
|
||||
- repository methods are called in expected order
|
||||
- audit hooks receive `actor`, `metadata`, `before`, `after`
|
||||
|
||||
## Adoption Checklist
|
||||
|
||||
1. Add entity schemas in `@cms/content`
|
||||
2. Add repository + service in `@cms/db`
|
||||
3. Add unit tests for contract + validation
|
||||
4. Wire route/action permission checks before mutations
|
||||
5. Add docs links and TODO updates
|
||||
Reference in New Issue
Block a user