-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/backend/src/modules/table/ports/trpc/table-trpc.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { AppRouter, createExpressMiddleware, createRouter } from '@egodb/trpc' | ||
import { Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common' | ||
import { CommandBus } from '@nestjs/cqrs' | ||
import { NextFunction, Request, Response } from 'express' | ||
|
||
@Injectable() | ||
export class TableTrpcMiddleware implements OnModuleInit, NestMiddleware { | ||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
private appRouter!: AppRouter | ||
onModuleInit() { | ||
this.appRouter = createRouter(this.commandBus) | ||
} | ||
|
||
use(req: Request, res: Response, next: NextFunction) { | ||
return createExpressMiddleware(this.appRouter)(req, res, next) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { Module } from '@nestjs/common' | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common' | ||
import { CqrsModule } from '@nestjs/cqrs' | ||
import { dbAdapters } from './adapters' | ||
import { commandHandlers } from './commands' | ||
import { restfulControllers } from './ports/restful' | ||
import { TableTrpcMiddleware } from './ports/trpc/table-trpc.middleware' | ||
|
||
@Module({ | ||
imports: [CqrsModule], | ||
controllers: [...restfulControllers], | ||
providers: [...commandHandlers, ...dbAdapters], | ||
}) | ||
export class TableModule {} | ||
export class TableModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(TableTrpcMiddleware).forRoutes('/trpc') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './commands' | ||
export * from './repository' | ||
export * from './table' | ||
export * from './table.command-bus' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ICommandBus } from '@egodb/domain' | ||
|
||
export type ITableCommandBus = ICommandBus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Command } from './command' | ||
|
||
export interface ICommandBus<TCommand extends Command = Command, TResult = any> { | ||
execute(command: TCommand): Promise<TResult> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { inferAsyncReturnType } from '@trpc/server' | ||
import * as trpcExpress from '@trpc/server/adapters/express' | ||
import { type Handler } from 'express' | ||
import { AppRouter } from './router' | ||
|
||
const createContext = ({ req, res }: trpcExpress.CreateExpressContextOptions) => ({}) // no context | ||
type Context = inferAsyncReturnType<typeof createContext> | ||
|
||
export const createExpressMiddleware = (appRouter: AppRouter): Handler => { | ||
return trpcExpress.createExpressMiddleware({ | ||
router: appRouter, | ||
createContext, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './express-context' | ||
export * from './router' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@egodb/trpc", | ||
"version": "0.0.0", | ||
"description": "", | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [], | ||
"main": "./dist/index", | ||
"scripts": { | ||
"build": "tsc", | ||
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"", | ||
"prebuild": "rimraf dist", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"types": "./dist/index.d", | ||
"dependencies": { | ||
"@egodb/core": "workspace:^0.0.0", | ||
"@trpc/server": "10.0.0-rc.8", | ||
"zod": "^3.19.1" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.17.14", | ||
"tsconfig": "workspace:^0.0.0", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { router } from './trpc' | ||
|
||
import { ITableCommandBus } from '@egodb/core/dist' | ||
import { createTableRouter } from './table' | ||
|
||
export const createRouter = (commandBus: ITableCommandBus) => { | ||
const appRouter = router({ | ||
table: createTableRouter(commandBus), | ||
}) | ||
return appRouter | ||
} | ||
|
||
export type AppRouter = ReturnType<typeof createRouter> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CreateTableCommand, ITableCommandBus } from '@egodb/core/dist' | ||
import { z } from 'zod' | ||
import { publicProcedure, router } from './trpc' | ||
|
||
export const createTableRouter = (commandBus: ITableCommandBus) => | ||
router({ | ||
create: publicProcedure | ||
.input( | ||
z.object({ | ||
name: z.string(), | ||
}), | ||
) | ||
.mutation(({ input }) => { | ||
const cmd = new CreateTableCommand({ name: input.name }) | ||
return commandBus.execute(cmd) | ||
}), | ||
get: publicProcedure | ||
.input( | ||
z.object({ | ||
id: z.string(), | ||
}), | ||
) | ||
.query(({ input }) => { | ||
const cmd = new CreateTableCommand({ name: input.id }) | ||
return commandBus.execute(cmd) | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { initTRPC } from '@trpc/server' | ||
const t = initTRPC.create() | ||
|
||
export const middleware = t.middleware | ||
export const router = t.router | ||
export const publicProcedure = t.procedure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "tsconfig/lib.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"], | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"baseUrl": "." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from 'tsup' | ||
|
||
export default defineConfig({ | ||
entry: ['index.ts'], | ||
outDir: 'dist', | ||
splitting: true, | ||
sourcemap: true, | ||
clean: true, | ||
dts: true, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.