diff --git a/lib/createdb.js b/lib/createdb.js index 347322c..bda00cd 100755 --- a/lib/createdb.js +++ b/lib/createdb.js @@ -1,6 +1,8 @@ #!/usr/bin/env node const args = require("commander"); const packageInfo = require("../package"); +const { startsWith } = require("./mapper"); +var postgresClient = require("pg-native"); var sqlite3 = require("sqlite3"); args @@ -8,10 +10,8 @@ args .option("--sessions ", "Routes file to monitor") .option("--verbose"); -const main = function (argv_) { - const argv = argv_ || process.argv; - args.parse(argv); - let db = new sqlite3.Database(args.sessions, (err) => { +const createDbSqlite = function (sessions) { + let db = new sqlite3.Database(sessions, (err) => { if (err) { return console.error(err.message); } @@ -36,6 +36,32 @@ CREATE TABLE gxitproxy db.close(); }; +const createDbPostgres = function (sessions) { + const db = postgresClient(); + db.connectSync(sessions); + db.querySync(` + CREATE TABLE IF NOT EXISTS gxitproxy ( + key text, + key_type text, + token text, + host text, + port integer, + info text, + PRIMARY KEY (key, key_type) + );` + ) +}; + +const main = function (argv_) { + const argv = argv_ || process.argv; + args.parse(argv); + if (startsWith(args.sessions, "postgresql://")) { + createDbPostgres(args.sessions); + } else { + createDbSqlite(args.sessions); + } +} + exports.main = main; if (require.main === module) { diff --git a/lib/mapper.js b/lib/mapper.js index 7eec533..9dd3dcd 100644 --- a/lib/mapper.js +++ b/lib/mapper.js @@ -179,4 +179,6 @@ var mapFor = function (path, pollingInterval) { return map; }; +exports.endsWith = endsWith; exports.mapFor = mapFor; +exports.startsWith = startsWith;