From 06e97372f02e78e93d061641cdfb4eeb72209c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= Date: Fri, 10 May 2024 17:25:53 +0200 Subject: [PATCH] WIP: add different implementation sketches --- src/sketch/dir1/take1.ts | 33 +++++++++++++++++++++++++++++ src/sketch/dir2/take2.ts | 33 +++++++++++++++++++++++++++++ src/sketch/dir3/domain.ts | 8 +++++++ src/sketch/dir3/repo.ts | 12 +++++++++++ src/sketch/dir3/resolver.ts | 11 ++++++++++ src/sketch/dir3/services/service.ts | 19 +++++++++++++++++ src/sketch/dir3/services/types.ts | 0 src/sketch/existing/existing.ts | 24 +++++++++++++++++++++ src/sketch/index.ts | 0 9 files changed, 140 insertions(+) create mode 100644 src/sketch/dir1/take1.ts create mode 100644 src/sketch/dir2/take2.ts create mode 100644 src/sketch/dir3/domain.ts create mode 100644 src/sketch/dir3/repo.ts create mode 100644 src/sketch/dir3/resolver.ts create mode 100644 src/sketch/dir3/services/service.ts create mode 100644 src/sketch/dir3/services/types.ts create mode 100644 src/sketch/existing/existing.ts create mode 100644 src/sketch/index.ts diff --git a/src/sketch/dir1/take1.ts b/src/sketch/dir1/take1.ts new file mode 100644 index 0000000..1593003 --- /dev/null +++ b/src/sketch/dir1/take1.ts @@ -0,0 +1,33 @@ +import { knex } from "../../db"; +import { Knex } from "knex"; + +type Thing = { + id: string; + name: string; +}; + +// talk to the DB +const repo = + (db: Knex) => + async (id: string): Promise => { + return (await db.where({ id }).first()) ?? null; + }; + +// business / domain logic +const service = + (thingGetter: (id: string) => Promise) => + async (id: string): Promise => { + return thingGetter(id); + }; + +const getThingClient = (id: string | undefined): Knex => { + if (!id) return knex; + return knex; +}; + +// graphql entry +export const resolver = async (args: { id: string }): Promise => { + const thing = await service(repo(getThingClient(args.id)))(args.id); + if (!thing) throw new Error("not found"); + return thing; +}; diff --git a/src/sketch/dir2/take2.ts b/src/sketch/dir2/take2.ts new file mode 100644 index 0000000..7a35667 --- /dev/null +++ b/src/sketch/dir2/take2.ts @@ -0,0 +1,33 @@ +import { Knex } from "knex"; +import { knex } from "../../db"; +type Thing = { + id: string; + name: string; +}; + +type ServedThing = { + foo: number; +} & Thing; + +// talk to the DB +const repo = + ({ db }: { db: Knex }) => + async (id: string): Promise => { + return (await db().where({ id }).first()) ?? null; + }; + +// business / domain logic +const service = + ({ thingGetter }: { thingGetter: (id: string) => Promise }) => + async (id: string): Promise => { + const thing = await thingGetter(id); + const foo = 123; + return thing ? { ...thing, foo } : null; + }; + +// graphql entry +export const resolver = async (id: string): Promise => { + const thing = await service({ thingGetter: repo({ db: knex }) })(id); + if (!thing) throw new Error("not found"); + return thing; +}; diff --git a/src/sketch/dir3/domain.ts b/src/sketch/dir3/domain.ts new file mode 100644 index 0000000..3a7f24f --- /dev/null +++ b/src/sketch/dir3/domain.ts @@ -0,0 +1,8 @@ +export type Thing = { + id: string; +}; + +export type ThingRepo = { + findThing: (id: string) => Promise; + queryThing: () => Promise; +}; diff --git a/src/sketch/dir3/repo.ts b/src/sketch/dir3/repo.ts new file mode 100644 index 0000000..aaa7750 --- /dev/null +++ b/src/sketch/dir3/repo.ts @@ -0,0 +1,12 @@ +import { Knex } from "knex"; +import { Thing } from "./domain"; + +const findThing = + ({ db }: { db: Knex }) => + async (id: string): Promise => { + return null; + }; + +export const thingRepo = ({ db }: { db: Knex }) => ({ + findThing: findThing({ db }), +}); diff --git a/src/sketch/dir3/resolver.ts b/src/sketch/dir3/resolver.ts new file mode 100644 index 0000000..84c2d98 --- /dev/null +++ b/src/sketch/dir3/resolver.ts @@ -0,0 +1,11 @@ +import { ServedThing, service } from "./services/service"; +import { thingRepo } from "./repo"; +import { knex } from "../../db"; + +export const resolver = async (id: string): Promise => { + const thing = await service({ + thingRepo: thingRepo({ db: knex }), + })(id); + if (!thing) throw new Error("not found"); + return thing; +}; diff --git a/src/sketch/dir3/services/service.ts b/src/sketch/dir3/services/service.ts new file mode 100644 index 0000000..b6dcf35 --- /dev/null +++ b/src/sketch/dir3/services/service.ts @@ -0,0 +1,19 @@ +import { Thing, type ThingRepo } from "../domain"; + +export type ServedThing = { + foo: number; +} & Thing; + +export const service = + ({ thingRepo }: { thingRepo: Pick }) => + async (id: string): Promise => { + const thing = await thingRepo.findThing(id); + const foo = 123; + return thing ? { ...thing, foo } : null; + }; + +export const service2 = ({ + thingRepo, +}: { + thingRepo: Pick; +}) => {}; diff --git a/src/sketch/dir3/services/types.ts b/src/sketch/dir3/services/types.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/sketch/existing/existing.ts b/src/sketch/existing/existing.ts new file mode 100644 index 0000000..bb8f713 --- /dev/null +++ b/src/sketch/existing/existing.ts @@ -0,0 +1,24 @@ +import { knex } from "../../db"; +type Thing = { + id: string; + name: string; +}; + +const Things = () => knex("things"); + +// talk to the DB +const repo = async (id: string): Promise => { + return (await Things().where({ id }).first()) ?? null; +}; + +// business / domain logic +const service = async (id: string): Promise => { + return repo(id); +}; + +// graphql entry +export const resolver = async (id: string): Promise => { + const thing = await service(id); + if (!thing) throw new Error("not found"); + return thing; +}; diff --git a/src/sketch/index.ts b/src/sketch/index.ts new file mode 100644 index 0000000..e69de29