Files
agentic-dev/packages/media/src/infrastructure/repositories/media.repository.ts
Danijel Martinek 8a36d803b3 feat(media): full Clean Architecture scaffold
Media is now a complete vertical-feature package mirroring auth/blog
structure: entities (models + errors), application (repositories +
use-cases), infrastructure (real Payload-backed + mock siblings),
interface-adapters (per-use-case controllers), DI (symbols + module +
container + bind-production), integrations/api (mediaRouter), factory,
contract suite, and feature integration tests.

Wired into:
- packages/core-api/src/root.ts (added `media: mediaRouter`)
- apps/web-next/src/server/bind-production.ts (calls bindProductionMedia)
- tsconfig.base.json (added @repo/media/api and ./di/bind-production aliases)

56 new tests in @repo/media (13 test files); core-api router test updated
to assert media. procedures. All 26 turbo tasks green.

Refactor log: §2, §4.1, §4.2, §5.1, §6.1
Spec: §6.5

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-06 00:29:05 +02:00

77 lines
2.0 KiB
TypeScript

import "reflect-metadata";
import { injectable } from "inversify";
import { getPayload } from "payload";
import type { SanitizedConfig } from "payload";
import type { IMediaRepository } from "../../application/repositories/media.repository.interface";
import type { Media } from "../../entities/models/media";
type PayloadMediaDoc = {
id: string | number;
alt?: string | null;
url?: string | null;
filename?: string | null;
mimeType?: string | null;
filesize?: number | null;
width?: number | null;
height?: number | null;
};
function mapDoc(doc: PayloadMediaDoc): Media {
return {
id: String(doc.id),
alt: doc.alt ?? "",
url: doc.url ?? "",
filename: doc.filename ?? "",
mimeType: doc.mimeType ?? "",
filesize: doc.filesize ?? 0,
...(doc.width != null && { width: doc.width }),
...(doc.height != null && { height: doc.height }),
};
}
@injectable()
export class MediaRepository implements IMediaRepository {
private config: SanitizedConfig;
constructor(config: SanitizedConfig) {
this.config = config;
}
async getMedia(id: string): Promise<Media | undefined> {
const payload = await getPayload({ config: this.config });
try {
const doc = await payload.findByID({
collection: "media",
id,
overrideAccess: true,
});
return mapDoc(doc as PayloadMediaDoc);
} catch {
return undefined;
}
}
async listMedia(opts?: { limit?: number; offset?: number }): Promise<Media[]> {
const payload = await getPayload({ config: this.config });
const result = await payload.find({
collection: "media",
limit: opts?.limit ?? 50,
page: opts?.offset
? Math.floor(opts.offset / (opts.limit ?? 50)) + 1
: 1,
overrideAccess: true,
});
return result.docs.map((d) => mapDoc(d as PayloadMediaDoc));
}
async deleteMedia(id: string): Promise<void> {
const payload = await getPayload({ config: this.config });
await payload.delete({
collection: "media",
id,
overrideAccess: true,
});
}
}