Comments (6)
I know,Interceptor
is the best choice.
It was a pleasure talking to you. Thank you very much.
I'll show you when I write the code.
from ack-nestjs-boilerplate.
Based on my experience, if the provider scope is set to request
and the request scope provider is injected to another provider. Then, other providers will be forced to request scope too.
So, if you inject the request scope provider into every provider (in the service layer), that will decrease your application performance
.
ex:
- without request context
service1 (scope default) -> controller1 (scope default)
service1 (scope default) -> controller2 (scope default)
- with request context
requestContext (scope request) -> service1 (scope request) -> controller1 (scope request)
requestContext (scope request) -> service1 (scope request) -> controller2 (scope request)
Definition of request Scope from nestjs official doc
A new instance of the provider is created exclusively for each incoming request. The instance is garbage-collected after the request has completed processing.
Maybe you should try this approach
Cons
RequestId
will undefined in exception filter.
- UserService, remove the request context
class UserService {
constructor(
private userRepository: UserRepository,
) {}
update(id: number, dto: UpdateUserDto) {
const result = this.userRepository.update(id, dto);
return result;
}
}
- UserController, i don't really know what happen with
@UpdateRecordLog
. I assume the@UpdateRecordLog
is decorator group betweenRecordInterceptor
andRECORD_META
class UserController {
@Put(':id')
@UpdateRecordLog(
'update user ${entity.name}',
'user ${entity.name} update field ${patch.name} form ${patch.origin} to ${patch.current}'
)
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateUserDto) {
return this.UserService.update(id, dto);
}
}
- Record Interceptor, you do not need
RequestContext
@Injectable()
export class RecordInterceptor implements NestInterceptor<unknown> {
constructor(
@Inject(LoggerService) private readonly loggerService: LoggerService
) {}
intercept(
context: ExecutionContext,
next: CallHandler<unknown>
): Observable<unknown> {
const call$ = next.handle();
// ! set request id here
const requestId = uuid.v1();
const record_meta = Reflect.getMetadata(RECORD_META, context.getHandler());
if (record_meta == null) {
return call$;
}
return call$.pipe(
tab(async (data: unknown) => {
// !data is response from controller
// !you can insert the record in this
// for example, insert data into loggerservice
await this.loggerService.put(requestId , data);
return data;
})
);
}
}
For the next, try to put the RequestId
in middleware and then put the RequestId
into RequestContext
from express.
Ex:
to put look src/common/middleware/request-id.middleware.ts
in this project
to get look src/common/logger/interceptors/logger.interceptor.ts
in this project
also you can get in exception filter
, look src/common/error/filters/error.http.filter.ts
Do not forget to read the https://docs.nestjs.com/faq/request-lifecycle
. This doc will (maybe) help you
from ack-nestjs-boilerplate.
Thank you for your reply.
I want to make an operation log feature,record user actions, creation, modification, and deletion.
Each record needs to have an operator.
For the modification action, the modified field and the value before the modification and the value after the modification are required.
Developers can customize the format output record content.
from ack-nestjs-boilerplate.
Ohh, I see...
I can only recommend that you try using the interceptor pre
and interceptor post
instead of injecting request context
into every service provider.
Hope you got the new insight.
from ack-nestjs-boilerplate.
This issue is stale because it has been open for 30 days with no activity.
from ack-nestjs-boilerplate.
This issue was closed because it has been inactive for 14 days since being marked as stale.
from ack-nestjs-boilerplate.
Related Issues (20)
- Docker compose service dependency to ackDatabase HOT 1
- apikey problem HOT 1
- Api key Issue HOT 1
- role creation HOT 3
- How to create and use API Key? HOT 1
- Why do we have a try/catch block at the end of every api in the controller ? HOT 1
- ERROR [I18nService] Translation
- 500 Internal Server for every api endpoint HOT 1
- E2E And Integration Tests HOT 2
- 500 Internal Server Error '/api/v1/auth/user/info' HOT 1
- Grateful! I hope the documentation can be more complete, how to expand development. HOT 4
- How about adding redis store for caching ? HOT 6
- Is there any someone use this boilerplate for production ? HOT 4
- Add TZ to example environment variables (.env.example) HOT 1
- Error while running seed if there are other module imported on usermodule HOT 1
- Password and related information are leaking. HOT 1
- Request for Two-Factor Authentication (2FA) Feature Implementation HOT 3
- Unable to connect to the database. Retrying (1)... HOT 4
- error TS2688: Cannot find type definition file for 'cron' HOT 2
- Issue with 'reflect-metadata' Conflict in NestJS Project and '@nestjs-sentry' Package HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ack-nestjs-boilerplate.