Blog module
npm install cca-blog-moduleThis repository contains the Blog module. It is a module only and is integrated into a larger system (no standalone Express server inside this module).
ts
import express from "express";
import { createPostContainer } from "cca-blog-module/dist/infrastructure/container/createPostContainer";
import { BaseDatabase } from "cca-core";const app = express();
app.use(express.json());
// BaseDatabase instance is provided by the main API
const database = new BaseDatabase(/ ... /);
const { postController } = createPostContainer(database);
app.post("/posts", postController.createPost);
app.get("/posts/:id", postController.getPost);
app.get("/posts", postController.getAllPosts);
app.put("/posts/:id", postController.updatePost);
app.delete("/posts/:id", postController.deletePost);
`Endpoints
$3
Create a post.
- Body: title, content, categoryIds (optional), other fields from PostDTO
- Auth: req.user.id must be set$3
Get one post by ID.
- Param: id$3
Get paginated posts.
- Query: page, limit, orderBy, order
- orderBy whitelist: createdAt, publishedAt, title$3
Update a post.
- Param: id
- Auth: req.user.id must be set$3
Delete a post.
- Param: idResponse format
`ts
type ApiResponse = {
success: boolean;
message: string;
data?: T;
meta?: {
timestamp: string;
};
};
`Post DTO (excerpt)
`ts
export type PostImageUrlsDTO = {
imageId?: string;
originalUrl?: string;
thumbUrl?: string;
smUrl?: string;
mdUrl?: string;
lgUrl?: string;
xlUrl?: string;
title?: string;
description?: string;
};export class PostDTO {
id!: string;
title!: string;
slug!: string;
content!: string;
excerpt!: string;
published!: boolean;
publishedAt?: Date;
authorId!: string;
adminAuthorId?: string;
categoryIds?: string[];
imageIds?: string[];
imageUrls?: PostImageUrlsDTO[];
}
`Validation
Inputs are validated with yup schemas in src/application/validators/postValidation.ts:
- required fields title, content
- unique slug
- category existence checksCaching
Uses BaseCacheService (in-memory). PostsCacheService provides:
- posts:list:* keys for lists
- posts:detail:* keys for post detail
- invalidateAllPostsCaches and invalidatePostCacheBuild / test
`bash
pnpm install
pnpm build:tsup
pnpm test
`Testing
There are no tests in this module yet; pnpm test will fail by design.
Recommended minimal tests:
- PostController create + get (integration)
- GetAllPosts returns data: [] for empty listNotes
- This module does not provide a logger; use the main API logger in integration.
- API response payloads always wrap the result inside data`.