70 lines
1.8 KiB
Markdown
70 lines
1.8 KiB
Markdown
# 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
|