import { Singleton, Container, Scope, Inject } from "typescript-ioc";
import { Value,Job, Context, logger,OpResult } from "mslib"
import * as Knex from 'knex'

@Singleton
class UtsDblockKnex {

    @Value('uts.dblock.host')
    private utsDblockHost: string

    @Value('uts.dblock.user')
    private utsDblockUser: string

    @Value('uts.dblock.password')
    private utsDblockPassword: string

    @Value('uts.dblock.database')
    private utsDblockDatabase: string

    @Value('uts.dblock.port')
    private utsDblockPort: string

    private knex: Knex;

    get(): Knex {
        if (this.knex) {
            return this.knex;
        }
        this.knex = Knex({
            client: 'pg',
            // debug: true,
            connection: {
                host: this.utsDblockHost,
                user: this.utsDblockUser,
                password: this.utsDblockPassword,
                database: this.utsDblockDatabase,
                port: Number(this.utsDblockPort)
            },
            pool: { min: 1, max: 10 }
        });

        Container.bind(Knex).factory(() => {
            let knexFactory = Container.get(UtsDblockKnex);
            return knexFactory.get();
        }).scope(Scope.Singleton);
        return this.knex;
    }
}

export { UtsDblockKnex, Knex }
