/**
 * 车场镜像库
 */

import { logger } from "mslib";
import { Container } from "typescript-ioc";
import Contants from "./Contants";

/**
 * 生成mysql knex实例，外部只需给定配置的connection部分即可，其他配置已给定的默认值(适应非标)，具体配置参考knex/mysql
 * @param config 
 * @returns 
 */
export function generateMySqlKnexInstance(config) {
    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实例，依赖ioc配置'config.parkImageKnex',配置参考knex/mysql
 */
export let knexInstance = null;
let config = Container.getValue('config.parkImageKnex');
if (config) {
    knexInstance = generateMySqlKnexInstance(config);
}