Skip to content

Commit

Permalink
feat: no more factories
Browse files Browse the repository at this point in the history
  • Loading branch information
gjuchault committed Jan 11, 2024
1 parent 4125e56 commit 2eb7ed8
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 231 deletions.
11 changes: 6 additions & 5 deletions src/application/healthcheck/__tests__/get-healthcheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { err, ok } from "neverthrow";
import type { HealthcheckRepository } from "~/repository/healthcheck/index.js";
import { buildMockDependencyStore } from "~/test-helpers/mock.js";

import { getHealthcheck, GetHealthcheckResult } from "../get-healthcheck.js";
import type { GetHealthcheckResult } from "../get-healthcheck.js";
import { getHealthcheck } from "../get-healthcheck.js";

const mockHealthyCache = {
echo: mock.fn(() => Promise.resolve("1")),
Expand All @@ -29,7 +30,7 @@ await describe("getHealthcheck()", async () => {
await describe("given a healthy cache and database", async () => {
const dependencyStore = buildMockDependencyStore({
cache: mockHealthyCache,
repository: { healthcheck: mockHealthyRepository },
healthcheckRepository: mockHealthyRepository,
});

await describe("when called", async () => {
Expand All @@ -55,7 +56,7 @@ await describe("getHealthcheck()", async () => {
await describe("given an unhealthy cache and healthy database", async () => {
const dependencyStore = buildMockDependencyStore({
cache: mockUnhealthyCache,
repository: { healthcheck: mockHealthyRepository },
healthcheckRepository: mockHealthyRepository,
});

await describe("when called", async () => {
Expand All @@ -81,7 +82,7 @@ await describe("getHealthcheck()", async () => {
await describe("given a healthy cache and unhealthy database", async () => {
const dependencyStore = buildMockDependencyStore({
cache: mockHealthyCache,
repository: { healthcheck: mockUnhealthyRepository },
healthcheckRepository: mockUnhealthyRepository,
});

await describe("when called", async () => {
Expand All @@ -107,7 +108,7 @@ await describe("getHealthcheck()", async () => {
await describe("given a healthy cache and database", async () => {
const dependencyStore = buildMockDependencyStore({
cache: mockUnhealthyCache,
repository: { healthcheck: mockUnhealthyRepository },
healthcheckRepository: mockUnhealthyRepository,
});

await describe("when called", async () => {
Expand Down
7 changes: 3 additions & 4 deletions src/application/healthcheck/get-healthcheck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from "node:os";
import v8 from "node:v8";

import { DependencyStore } from "~/store";
import type { DependencyStore } from "~/store";

export interface GetHealthcheckResult {
database: "healthy" | "unhealthy";
Expand All @@ -16,10 +16,9 @@ export async function getHealthcheck({
dependencyStore: DependencyStore;
}): Promise<GetHealthcheckResult> {
const cache = dependencyStore.get("cache");
const { healthcheck: healthcheckRepository } =
dependencyStore.get("repository");
const repository = dependencyStore.get("healthcheckRepository");

const databaseResult = await healthcheckRepository.getHealthcheck();
const databaseResult = await repository.getHealthcheck({ dependencyStore });

let cacheResult: "healthy" | "unhealthy" = "healthy";

Expand Down
22 changes: 0 additions & 22 deletions src/application/healthcheck/get-users.ts

This file was deleted.

14 changes: 4 additions & 10 deletions src/application/healthcheck/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { DependencyStore } from "~/store.js";
import type { DependencyStore } from "~/store.js";

import type { GetHealthcheckResult } from "./get-healthcheck.js";
import { getHealthcheck } from "./get-healthcheck.js";
export { getHealthcheck } from "./get-healthcheck.js";

export interface HealthcheckApplication {
getHealthcheck(): Promise<GetHealthcheckResult>;
}

export async function createHealthcheckApplication({
export async function startHealthcheckApplication({
dependencyStore,
}: {
dependencyStore: DependencyStore;
}): Promise<HealthcheckApplication> {
}): Promise<void> {
const taskScheduling = dependencyStore.get("taskScheduling");

const enqueueSomeTask = await taskScheduling.createTask<{ id: string }>(
Expand All @@ -22,10 +22,4 @@ export async function createHealthcheckApplication({
);

await enqueueSomeTask([{ id: "123" }]);

return {
async getHealthcheck() {
return getHealthcheck({ dependencyStore });
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { UserEmail, UserId, UserName } from "~/domain/user.js";
import type { UserRepository } from "~/repository/user/index.js";
import { buildMockDependencyStore } from "~/test-helpers/mock.js";

import { getUsers, GetUsersResult } from "../get-users.js";
import { getUsers, GetUsersResult } from "./get-users.js";

const mockRepository: UserRepository = {
get: mock.fn(() =>
Expand All @@ -27,16 +27,14 @@ const mockRepository: UserRepository = {
await describe("getUsers()", async () => {
await describe("given a repository", async () => {
const dependencyStore = buildMockDependencyStore({
repository: { user: mockRepository },
userRepository: mockRepository,
});

await describe("when called", async () => {
let result: GetUsersResult;

before(async () => {
result = await getUsers({
dependencyStore,
});
result = await getUsers({ dependencyStore });
});

await it("returns healthy", () => {
Expand Down
19 changes: 19 additions & 0 deletions src/application/users/get-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { NonEmptyArray } from "@gjuchault/typescript-service-sdk";
import type { Result } from "neverthrow";

import type { User } from "~/domain/user.js";
import type { GetUsersError } from "~/repository/user";
import type { DependencyStore } from "~/store";

export type GetUsersResult = Result<NonEmptyArray<User>, GetUsersError>;

export async function getUsers({
dependencyStore,
}: {
dependencyStore: DependencyStore;
}): Promise<GetUsersResult> {
const repository = dependencyStore.get("userRepository");
const users = await repository.get({}, { dependencyStore });

return users;
}
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import {
createTelemetry,
} from "@gjuchault/typescript-service-sdk";

import { createHealthcheckApplication } from "~/application/healthcheck/index.js";
import { startHealthcheckApplication } from "~/application/healthcheck/index.js";
import { config } from "~/config.js";
import { createAppRouter } from "~/presentation/http/index.js";
import { createRepository } from "~/repository/index.js";
import * as healthcheckRepository from "~/repository/healthcheck/index.js";
import * as userRepository from "~/repository/user/index.js";

import { dependencyStore } from "./store";

Expand Down Expand Up @@ -63,13 +64,10 @@ export async function startApp() {
database = await createDatabase({ config, dependencyStore });
dependencyStore.set("database", database);

const repository = createRepository({ dependencyStore });
dependencyStore.set("repository", repository);
dependencyStore.set("healthcheckRepository", healthcheckRepository);
dependencyStore.set("userRepository", userRepository);

const healthcheckApplication = await createHealthcheckApplication({
dependencyStore,
});
dependencyStore.set("healthcheckApplication", healthcheckApplication);
await startHealthcheckApplication({ dependencyStore });

const appRouter = createAppRouter({ dependencyStore });

Expand Down
5 changes: 2 additions & 3 deletions src/presentation/http/routes/healthcheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
import { z } from "zod";

import type { GetHealthcheckResult } from "~/application/healthcheck/get-healthcheck.js";
import { getHealthcheck } from "~/application/healthcheck/get-healthcheck.js";
import type { DependencyStore } from "~/store";

export type RouterGetHealthcheckResult = ServerInferResponses<
Expand Down Expand Up @@ -44,11 +45,9 @@ export function bindHealthcheckRoutes({
}: {
dependencyStore: DependencyStore;
}) {
const healthcheckApplication = dependencyStore.get("healthcheckApplication");

return {
async getHealthcheck(): Promise<RouterGetHealthcheckResult> {
const healthcheck = await healthcheckApplication.getHealthcheck();
const healthcheck = await getHealthcheck({ dependencyStore });

if (!isHealthcheckFullyHealthy(healthcheck)) {
return {
Expand Down
8 changes: 3 additions & 5 deletions src/repository/healthcheck/__tests__/get-healthcheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import { slonikHelpers } from "@gjuchault/typescript-service-sdk";

import { buildMockDependencyStore } from "~/test-helpers/mock.js";

import { createHealthcheckRepository, GetHealthcheckResult } from "../index.js";
import { getHealthcheck, GetHealthcheckResult } from "../index.js";

await describe("getHealthcheck()", async () => {
await describe("given a healthy database", async () => {
const { query, database } = slonikHelpers.createMockDatabase([]);
const dependencyStore = buildMockDependencyStore({ database });
const repository = createHealthcheckRepository({ dependencyStore });

await describe("when called", async () => {
let result: GetHealthcheckResult;

before(async () => {
result = await repository.getHealthcheck();
result = await getHealthcheck({ dependencyStore });
});

await it("returns ok", () => {
Expand All @@ -34,13 +33,12 @@ await describe("getHealthcheck()", async () => {
await describe("given an unhealthy database", async () => {
const { query, database } = slonikHelpers.createFailingQueryMockDatabase();
const dependencyStore = buildMockDependencyStore({ database });
const repository = createHealthcheckRepository({ dependencyStore });

await describe("when called", async () => {
let result: GetHealthcheckResult;

before(async () => {
result = await repository.getHealthcheck();
result = await getHealthcheck({ dependencyStore });
});

await it("returns err", () => {
Expand Down
24 changes: 11 additions & 13 deletions src/repository/healthcheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import { z } from "zod";
import { DependencyStore } from "~/store";

export interface HealthcheckRepository {
getHealthcheck(): Promise<GetHealthcheckResult>;
getHealthcheck(_: {
dependencyStore: DependencyStore;
}): Promise<GetHealthcheckResult>;
}

export type GetHealthcheckResult = Result<"healthy", "databaseError">;

export function createHealthcheckRepository({
export async function getHealthcheck({
dependencyStore,
}: {
dependencyStore: DependencyStore;
}): HealthcheckRepository {
async function getHealthcheck(): Promise<GetHealthcheckResult> {
const database = dependencyStore.get("database");
}): Promise<GetHealthcheckResult> {
const database = dependencyStore.get("database");

try {
await database.query(sql.type(z.unknown())`select 1`);
try {
await database.query(sql.type(z.unknown())`select 1`);

return ok("healthy");
} catch {
return err("databaseError");
}
return ok("healthy");
} catch {
return err("databaseError");
}

return { getHealthcheck };
}
24 changes: 0 additions & 24 deletions src/repository/index.ts

This file was deleted.

Loading

0 comments on commit 2eb7ed8

Please sign in to comment.