feat(blog): add bind-dev-seed binder + dev seed
Sets the canonical pattern for all 5 features. Three new files: - src/__seeds__/dev.ts — buildDevArticles() function returning 3 realistic articles (welcome, vertical-feature-architecture, wip-post) built from articleFactory with id/slug/title/status overrides only. - src/di/bind-dev-seed.ts — bindDevSeedBlog() async function that unbinds IArticlesRepository, constructs MockArticlesRepository, seeds it via buildDevArticles(), and rebinds via .toConstantValue. - src/di/bind-dev-seed.test.ts — 3 tests: populated repo, welcome article reachable by slug, idempotent (callable twice). package.json adds the ./di/bind-dev-seed subpath export, parallel to ./di/bind-production. Tests + use cases continue to construct MockArticlesRepository directly — they never go through bindDevSeedBlog. The seed only attaches when called explicitly at app boot.
This commit is contained in:
30
packages/blog/src/di/bind-dev-seed.ts
Normal file
30
packages/blog/src/di/bind-dev-seed.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { blogContainer } from "./container.js";
|
||||
import { BLOG_SYMBOLS } from "./symbols.js";
|
||||
import { MockArticlesRepository } from "../infrastructure/repositories/articles.repository.mock.js";
|
||||
import { buildDevArticles } from "../__seeds__/dev.js";
|
||||
import type { IArticlesRepository } from "../application/repositories/articles.repository.interface.js";
|
||||
|
||||
/**
|
||||
* Replace the default empty mock with a populated one for dev mode + storybook.
|
||||
*
|
||||
* Call this from app boot when `USE_DEV_SEED=true`, mutually exclusive with
|
||||
* `bindProductionBlog(config)`. Tests must NOT call this — they construct
|
||||
* `new MockArticlesRepository()` directly and seed via factories per-test.
|
||||
*
|
||||
* Idempotent: safe to call multiple times; each call rebuilds a fresh
|
||||
* populated repo and rebinds the symbol.
|
||||
*/
|
||||
export async function bindDevSeedBlog(): Promise<void> {
|
||||
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
|
||||
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
|
||||
}
|
||||
|
||||
const repo = new MockArticlesRepository();
|
||||
for (const article of buildDevArticles()) {
|
||||
await repo.createArticle(article);
|
||||
}
|
||||
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
.toConstantValue(repo);
|
||||
}
|
||||
Reference in New Issue
Block a user