由于@nestjs/terminus不为 Prisma 提供健康检查,我尝试根据他们的Mongoose 健康检查创建它。
当我尝试时:
import * as Prisma from 'prisma';
...
...
private getContextConnection(): any | null {
const {
getConnectionToken,
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('prisma') as typeof Prisma;
try {
return this.moduleRef.get(getConnectionToken('DatabaseConnection') as string, {
strict: false,
});
} catch (err) {
return null;
}
}
...
...
const connection = options.connection || this.getContextConnection();
if (!connection) {
throw new ConnectionNotFoundError(
this.getStatus(key, isHealthy, {
message: 'Connection provider not found in application context',
}),
); …Run Code Online (Sandbox Code Playgroud) 我的prisma.service.ts看起来像这样:
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
async enableShutdownHooks(app: INestApplication) {
this.$on('beforeExit', async () => {
await app.close();
});
}
}
Run Code Online (Sandbox Code Playgroud)
根据Prisma 文档,我应该将它们放在请求处理程序的上下文之外。这将是我在main.ts上创建的应用程序。在定义应用程序本身之前将中间件放在那里对我来说看起来很奇怪并且不起作用。我更愿意将其放在prisma.service.ts文件本身上
到目前为止,我读过的所有文章和文档都讨论了 Cloudwatch 和 Winston 在普通 Node 应用程序上的集成,但没有讨论 Nestjs
到目前为止,我的app.module.ts上有:
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongooseModule.forRoot(
`mongodb://${environment.MONGO_INITDB_ROOT_USERNAME}:${environment.MONGO_INITDB_ROOT_PASSWORD}@${environment.MONGODB_HOST}/${environment.MONGO_INITDB_DATABASE}`,
),
VoucherModule,
ApiKeyModule,
WinstonModule.forRoot(loggerConfig),
],
Run Code Online (Sandbox Code Playgroud)
其中 loggerConfig 是基本的 Winston 配置,具体取决于环境。
使用winston-cloudwatch包,我需要创建一个新的 Transporter 并将其添加到 Winston,但似乎找不到方法来执行此操作。
amazon-web-services node.js winston amazon-cloudwatch nestjs
我试图遵循这个线程,但我不断收到错误。
变换响应.interceptor.ts:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiResponseInterface } from '@walletxp/shared-interfaces';
@Injectable()
export class TransformResponseInterceptor<T>
implements NestInterceptor<T, ApiResponseInterface<Record<string, unknown>>>
{
intercept(context: ExecutionContext, next: CallHandler): Observable<ApiResponseInterface<Record<string, unknown>>> {
return next.handle().pipe(map((data) => ({ success: true, data })));
}
}
Run Code Online (Sandbox Code Playgroud)
对于它的测试,transform-response.interceptor.spec.ts:
import { TransformResponseInterceptor } from './transform-response.interceptor';
const interceptor = new TransformResponseInterceptor();
const executionContext: any = {
switchToHttp: jest.fn().mockReturnThis(),
getRequest: jest.fn().mockReturnThis(),
}; …Run Code Online (Sandbox Code Playgroud) nestjs ×4
prisma ×2
health-check ×1
http ×1
interceptor ×1
jestjs ×1
node.js ×1
typescript ×1
winston ×1