import { NodeCG } from "nodecg-types/types/server" ; import { Result , emptySuccess , success , ServiceBundle , error , Logger } from "nodecg-io-core" ; import { Knex , knex } from "knex" ; export interface SQLConfig { client : string ; logger : Logger ; connection : Record < string , unknown > ; } export type SQLClient = Knex ; module . exports = ( nodecg ) => { new SQLService ( nodecg , "sql" , __dirname , "../schema.json" ) . register ( ) ; } ; class SQLService extends ServiceBundle < SQLConfig , Knex > { async validateConfig ( config ) { if ( config . client === "mysql" || config . client === "pg" ) { if ( ! config . connection . host || ! config . connection . user || ! config . connection . password || ! config . connection . database ) { return error ( "Invalid config. Either host, user, password or database is missing." ) ; } } else if ( config . client === "sqlite3" && ! config . connection . filename ) { return error ( "Invalid config. Filename is missing." ) ; } return emptySuccess ( ) ; } async createClient ( config , logger ) { const knexInstance = knex ( config ) ; logger . info ( "Successfully created sql client." ) ; return success ( knexInstance ) ; } stopClient ( client , logger ) { client . destroy ( ) ; logger . info ( "Successfully stopped sql client." ) ; } }