我正在使用以下API模板:https://github.com/w3tecch/express-typescript-boilerplate
我已经为Knex编写了一个加载器,因为我不想使用Git存储库中包含的默认TypeORM。
import {MicroframeworkLoader, MicroframeworkSettings} from 'microframework-w3tec';
import * as Knex from 'knex';
import {env} from '../env';
export const knexLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {
const connection = await Knex({
client: 'mysql',
connection: {
host: env.db.host,
user: env.db.username,
password: env.db.password,
database: env.db.database,
},
});
if (settings) {
settings.setData('connection', connection);
settings.onShutdown(() => connection.close());
}
};
如何在控制器使用的随机类中访问常量“连接”?
例如,如何在这里访问它?
import { sha256 } from 'js-sha256';
export class Login {
private database;
public checkLogin(email: string, password: string, company_id: number): boolean {
let login = this.database.connection('users').where({
email,
company_id,
}).select('id, password, salt').then((row) => {
let hashed_password = this.hashPassword(password, login.salt);
if (hashed_password === login.password) {
return true;
}
return false;
});
return false;
}
private hashPassword(password: string, salt: string): string {
let half: number = (salt.length / 2);
return sha256(salt.substr(0, half) + password + salt.substr(half));
}
private generateSalt(): string {
let random_number: string = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
let salt_unminimized: string = sha256(random_number);
return salt_unminimized.substr(0, 100);
}
}