/**
 * 车场镜像knex模块
 * @date 2022-03
 * @author 何华东
 */

import Contants from './Contants';
import { logger } from 'mslib';

const test_config = {
    connection: {
        host: '121.36.160.42',
        port: 3306,
        user: 'nkc_test',
        password: 'nkc_test_ZahJ5een',
        database: 'park_image',
        multipleStatements: true
    }
};

const prod_config = {
    connection: {
        host: '122.112.252.51',
        port: 33390,
        user: 'nostandar',
        password: 'Keytop@!#321',
        database: 'park_image_new',
        multipleStatements: true
    }
};

/**
 * 车场镜像knex配置
 */
export const config = Contants.IS_PROD ? prod_config : test_config;

/**
 * 生成车场镜像knex实例
 * @param config 
 * @returns 
 */
export function generateMySqlKnexInstance(config: {
    connection: {
        host: string,
        port: number,
        user: string,
        password: string,
        database: string,
        multipleStatements: boolean
    }
}) {
    const defaults = {
        client: 'mysql2',
        pool: { min: 0, max: 10 },
        debug: Contants.IS_DEBUG,
        log: {
            warn(message) {
                logger.warn(message);
            },
            error(message) {
                logger.error(message);
            },
            deprecate(message) {
                logger.error(message);
            },
            debug(message) {
                logger.debug(message);
            },
        }
    };

    return require('knex')(Object.assign(defaults, config));
}

/**
 * 车场镜像knex实例
 */
export const knexInstance = generateMySqlKnexInstance(config);
