feat(core-cms): compose @repo/blog/cms into payload config

This commit is contained in:
2026-05-04 22:29:50 +02:00
parent 1d59698ebe
commit a92341fb74
9 changed files with 93 additions and 24 deletions

View File

@@ -15,7 +15,6 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@repo/core-cms": "workspace:*",
"@repo/core-shared": "workspace:*",
"@trpc/server": "^11.0.0",
"inversify": "^6.2.0",

View File

@@ -12,7 +12,7 @@ function generateSlug(title: string): string {
export async function createArticleUseCase(input: {
title: string;
content: unknown;
content?: unknown;
authorId: string;
slug?: string;
}): Promise<Article> {

View File

@@ -5,11 +5,9 @@ vi.mock("payload", () => ({
getPayload: vi.fn(),
}));
vi.mock("@repo/core-cms", () => ({
default: {} as never,
}));
describe("PayloadArticlesRepository", () => {
const mockConfig = {} as never;
it("maps a Payload doc to a domain Article on getArticleBySlug", async () => {
const { getPayload } = await import("payload");
const findMock = vi.fn().mockResolvedValue({
@@ -30,7 +28,7 @@ describe("PayloadArticlesRepository", () => {
find: findMock,
});
const repo = new PayloadArticlesRepository();
const repo = new PayloadArticlesRepository(mockConfig);
const result = await repo.getArticleBySlug("hello");
expect(findMock).toHaveBeenCalledWith({
@@ -52,7 +50,7 @@ describe("PayloadArticlesRepository", () => {
find: vi.fn().mockResolvedValue({ docs: [] }),
});
const repo = new PayloadArticlesRepository();
const repo = new PayloadArticlesRepository(mockConfig);
const result = await repo.getArticleBySlug("missing");
expect(result).toBeUndefined();
});

View File

@@ -1,8 +1,8 @@
import "reflect-metadata";
import { injectable } from "inversify";
import { getPayload } from "payload";
import type { SanitizedConfig } from "payload";
import config from "@repo/core-cms";
import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface";
import type { Article } from "@/entities/article";
@@ -38,8 +38,14 @@ function mapDoc(doc: PayloadArticleDoc): Article {
@injectable()
export class PayloadArticlesRepository implements IArticlesRepository {
private config: SanitizedConfig;
constructor(config: SanitizedConfig) {
this.config = config;
}
async getArticle(id: string): Promise<Article | undefined> {
const payload = await getPayload({ config });
const payload = await getPayload({ config: this.config });
try {
const doc = await payload.findByID({
collection: "articles",
@@ -53,7 +59,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
}
async getArticleBySlug(slug: string): Promise<Article | undefined> {
const payload = await getPayload({ config });
const payload = await getPayload({ config: this.config });
const result = await payload.find({
collection: "articles",
where: { slug: { equals: slug } },
@@ -70,7 +76,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
limit?: number;
offset?: number;
}): Promise<Article[]> {
const payload = await getPayload({ config });
const payload = await getPayload({ config: this.config });
const where: Record<string, { equals: string }> = {};
if (options?.status) where.status = { equals: options.status };
if (options?.authorId) where.author = { equals: options.authorId };
@@ -88,7 +94,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
}
async createArticle(input: Article): Promise<Article> {
const payload = await getPayload({ config });
const payload = await getPayload({ config: this.config });
const created = await payload.create({
collection: "articles",
data: {
@@ -107,7 +113,7 @@ export class PayloadArticlesRepository implements IArticlesRepository {
id: string,
input: Partial<Article>,
): Promise<Article | undefined> {
const payload = await getPayload({ config });
const payload = await getPayload({ config: this.config });
try {
const updated = await payload.update({
collection: "articles",

View File

@@ -10,7 +10,7 @@ import { createArticleUseCase } from "@/application/use-cases/create-article.use
const createInputSchema = z.object({
title: z.string().min(1).max(255),
content: z.unknown(),
content: z.unknown().optional(),
authorId: z.string(),
slug: z.string().optional(),
});
@@ -35,7 +35,12 @@ export async function createArticleController(
cause: parsed.error,
});
}
return createArticleUseCase(parsed.data);
return createArticleUseCase({
title: parsed.data.title,
content: parsed.data.content ?? null,
authorId: parsed.data.authorId,
slug: parsed.data.slug,
});
}
export async function getArticlesController(

View File

@@ -13,6 +13,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@repo/blog": "workspace:*",
"payload": "^3.14.0",
"@payloadcms/db-postgres": "^3.14.0",
"@payloadcms/richtext-lexical": "^3.14.0"

View File

@@ -67,6 +67,7 @@ export interface Config {
};
blocks: {};
collections: {
articles: Article;
'payload-kv': PayloadKv;
users: User;
'payload-locked-documents': PayloadLockedDocument;
@@ -75,6 +76,7 @@ export interface Config {
};
collectionsJoins: {};
collectionsSelect: {
articles: ArticlesSelect<false> | ArticlesSelect<true>;
'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
@@ -115,6 +117,42 @@ export interface UserAuthOperations {
password: string;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "articles".
*/
export interface Article {
id: number;
title: string;
/**
* Auto-generated from title if left empty
*/
slug?: string | null;
content?: {
root: {
type: string;
children: {
type: any;
version: number;
[k: string]: unknown;
}[];
direction: ('ltr' | 'rtl') | null;
format: 'left' | 'start' | 'center' | 'right' | 'end' | 'justify' | '';
indent: number;
version: number;
};
[k: string]: unknown;
} | null;
status: 'draft' | 'published';
/**
* Temporary text field; restored to users relationship in Plan 3.
*/
author: string;
publishedAt?: string | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv".
@@ -163,10 +201,15 @@ export interface User {
*/
export interface PayloadLockedDocument {
id: number;
document?: {
relationTo: 'users';
value: number | User;
} | null;
document?:
| ({
relationTo: 'articles';
value: number | Article;
} | null)
| ({
relationTo: 'users';
value: number | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
@@ -209,6 +252,21 @@ export interface PayloadMigration {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "articles_select".
*/
export interface ArticlesSelect<T extends boolean = true> {
title?: T;
slug?: T;
content?: T;
status?: T;
author?: T;
publishedAt?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv_select".

View File

@@ -4,12 +4,14 @@ import { lexicalEditor } from "@payloadcms/richtext-lexical";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { articles } from "@repo/blog/cms";
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
export default buildConfig({
editor: lexicalEditor(),
collections: [],
collections: [articles],
globals: [],
secret: process.env.PAYLOAD_SECRET || "default-secret-change-me",
db: postgresAdapter({