feat(admin): add posts CRUD sandbox and shared CRUD foundation

This commit is contained in:
2026-02-10 19:35:41 +01:00
parent de26cb7647
commit 07e5f53793
18 changed files with 887 additions and 38 deletions

View File

@@ -20,6 +20,7 @@
"db:seed": "bun --env-file=../../.env prisma/seed.ts"
},
"dependencies": {
"@cms/crud": "workspace:*",
"@cms/content": "workspace:*",
"@prisma/adapter-pg": "7.3.0",
"@prisma/client": "7.3.0",

View File

@@ -1,2 +1,9 @@
export { db } from "./client"
export { createPost, listPosts } from "./posts"
export {
createPost,
deletePost,
getPostById,
listPosts,
registerPostCrudAuditHook,
updatePost,
} from "./posts"

View File

@@ -1,19 +1,80 @@
import { upsertPostSchema } from "@cms/content"
import {
type CreatePostInput,
createPostInputSchema,
type UpdatePostInput,
updatePostInputSchema,
} from "@cms/content"
import { type CrudAuditHook, type CrudMutationContext, createCrudService } from "@cms/crud"
import type { Post } from "../prisma/generated/client/client"
import { db } from "./client"
const postRepository = {
list: () =>
db.post.findMany({
orderBy: {
updatedAt: "desc",
},
}),
findById: (id: string) =>
db.post.findUnique({
where: { id },
}),
create: (input: CreatePostInput) =>
db.post.create({
data: input,
}),
update: (id: string, input: UpdatePostInput) =>
db.post.update({
where: { id },
data: input,
}),
delete: (id: string) =>
db.post.delete({
where: { id },
}),
}
const postAuditHooks: Array<CrudAuditHook<Post>> = []
const postCrudService = createCrudService({
resource: "post",
repository: postRepository,
schemas: {
create: createPostInputSchema,
update: updatePostInputSchema,
},
auditHooks: postAuditHooks,
})
export function registerPostCrudAuditHook(hook: CrudAuditHook<Post>): () => void {
postAuditHooks.push(hook)
return () => {
const index = postAuditHooks.indexOf(hook)
if (index >= 0) {
postAuditHooks.splice(index, 1)
}
}
}
export async function listPosts() {
return db.post.findMany({
orderBy: {
updatedAt: "desc",
},
})
return postCrudService.list()
}
export async function createPost(input: unknown) {
const payload = upsertPostSchema.parse(input)
return db.post.create({
data: payload,
})
export async function getPostById(id: string) {
return postCrudService.getById(id)
}
export async function createPost(input: unknown, context?: CrudMutationContext) {
return postCrudService.create(input, context)
}
export async function updatePost(id: string, input: unknown, context?: CrudMutationContext) {
return postCrudService.update(id, input, context)
}
export async function deletePost(id: string, context?: CrudMutationContext) {
return postCrudService.delete(id, context)
}