我正在编写一个 nestjs 库,它启动与数据库的连接,提供多种功能(检索数据、插入数据等),并记录执行的查询。
import { Injectable, Inject, Logger } from '@nestjs/common';
import * as ActiveDirectory from 'activedirectory';
import { SECHELPER_CONNECT_OPTIONS } from './constants';
import {
SechelperADFindResult,
SechelperOptions,
DatabaseResult,
ActiveDirectoryResult,
} from './interfaces';
import * as ldap from 'ldapjs';
import { blacklistpassword } from './interfaces';
import { Connection, createConnection, QueryRunner } from 'typeorm';
import * as crypto from 'crypto';
import passwordValidator from 'password-validator';
import { format } from 'date-fns';
import * as _ from 'lodash';
import { REQUEST, HttpAdapterHost } from '@nestjs/core';
import { Request } from 'express';
import * as requestIp from 'request-ip';
@Injectable()
export class SechelperService {
private _dbClient: Connection;
constructor(
@Inject(SECHELPER_CONNECT_OPTIONS)
private options: SechelperOptions,
private readonly logger: Logger,
private adapterHost: HttpAdapterHost,
//@Inject(REQUEST) private readonly request: Request
) {}
async connect(): Promise<any> {
// create db connection if db options exist
if (this.options.dbConnectOptions) {
return this._dbClient
? this._dbClient
: (this._dbClient = await createConnection(
this.options.dbConnectOptions,
));
}
return false;
}
}
如果我取消对 //@Inject(REQUEST) private readonly request: Request
的注释,我将无法对数据库进行查询(以及其他 Typeorm 功能)(为什么?NestJs - not getting Injectable to work on subscriber)。
那么,还有其他方法可以在库中获取请求数据吗?