feat: als and prisma
This commit is contained in:
19
core/als/request-context.middleware.ts
Normal file
19
core/als/request-context.middleware.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { RequestContextService } from './request-context.service';
|
||||
|
||||
@Injectable()
|
||||
export class RequestContextMiddleware implements NestMiddleware {
|
||||
constructor(private readonly ctx: RequestContextService) {}
|
||||
|
||||
use(req: Request, _: Response, next: NextFunction) {
|
||||
const context = {
|
||||
requestId: randomUUID(),
|
||||
correlationId: (req.headers['x-correlation-id'] as string) ?? undefined,
|
||||
headers: req.headers as Record<string, string>,
|
||||
};
|
||||
|
||||
this.ctx.run(context, next);
|
||||
}
|
||||
}
|
||||
8
core/als/request-context.module.ts
Normal file
8
core/als/request-context.module.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RequestContextService } from './request-context.service';
|
||||
|
||||
@Module({
|
||||
providers: [RequestContextService],
|
||||
exports: [RequestContextService],
|
||||
})
|
||||
export class RequestContextModule {}
|
||||
48
core/als/request-context.service.ts
Normal file
48
core/als/request-context.service.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AsyncLocalStorage } from 'async_hooks';
|
||||
import { RequestContext } from './request-context.type';
|
||||
|
||||
@Injectable()
|
||||
/**
|
||||
* RequestContext holds per-request metadata including:
|
||||
* - HTTP request
|
||||
* - Auth payload (decoded JWT, optional)
|
||||
* - Prisma transaction client (optional)
|
||||
* - Correlation ID and other future cross-cutting concerns
|
||||
*/
|
||||
export class RequestContextService {
|
||||
private readonly als = new AsyncLocalStorage<RequestContext>();
|
||||
|
||||
run(context: RequestContext, fn: () => void) {
|
||||
this.als.run(context, fn);
|
||||
}
|
||||
|
||||
get(): RequestContext {
|
||||
const store = this.als.getStore();
|
||||
if (!store) {
|
||||
throw new Error('RequestContext not initialized');
|
||||
}
|
||||
return store;
|
||||
}
|
||||
|
||||
set<K extends keyof RequestContext>(key: K, value: RequestContext[K]) {
|
||||
this.get()[key] = value;
|
||||
}
|
||||
|
||||
// Helpers
|
||||
get user() {
|
||||
return this.get().user;
|
||||
}
|
||||
|
||||
get tx() {
|
||||
return this.get().tx;
|
||||
}
|
||||
|
||||
set tx(tx) {
|
||||
this.set('tx', tx);
|
||||
}
|
||||
|
||||
get isTransaction(): boolean {
|
||||
return !!this.get().tx;
|
||||
}
|
||||
}
|
||||
10
core/als/request-context.type.ts
Normal file
10
core/als/request-context.type.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Prisma } from 'prisma/generated/prisma/client';
|
||||
import { AuthPayload } from 'src/auth/types';
|
||||
|
||||
export interface RequestContext {
|
||||
requestId: string;
|
||||
correlationId?: string;
|
||||
headers: Record<string, string>;
|
||||
user?: AuthPayload;
|
||||
tx?: Prisma.TransactionClient;
|
||||
}
|
||||
Reference in New Issue
Block a user