Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed issue with outdated PostgreSQL client
- Loading branch information
nash-md
committed
Sep 4, 2018
1 parent
6503dd9
commit 50f1338
Showing
5 changed files
with
131 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,20 @@ | ||
const async = require('async') | ||
const pg = require('pg') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
async.waterfall([ | ||
|
||
function (callback) { | ||
|
||
pg.connect(process.env.DATABASE_URL, function (err, client, done) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null, client) | ||
} | ||
}) | ||
|
||
}, function (client, callback) { | ||
|
||
client.query('CREATE TABLE IF NOT EXISTS configuration (id serial, data text)', | ||
function (err, result) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null, client) | ||
} | ||
} | ||
) | ||
|
||
}, function (client, callback) { | ||
|
||
client.query('TRUNCATE configuration', function (err, result) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null, client) | ||
} | ||
}) | ||
|
||
}, function (client, callback) { | ||
|
||
/* load configuration template */ | ||
fs.readFile(path.join(process.cwd(), './configuration.json'), 'utf8', function (err, data) { | ||
if (err) { | ||
callback(err) | ||
} | ||
callback(null, client, data) | ||
}) | ||
|
||
}, function (client, configuration, callback) { | ||
|
||
client.query('INSERT INTO configuration(data) values($1)', [configuration], | ||
function (err, result) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null, 'table successfully created, configuration saved') | ||
} | ||
} | ||
) | ||
|
||
} | ||
],function (err, message) { | ||
if (err) { | ||
console.log(err) | ||
process.exit(1) | ||
} else { | ||
console.log(message) | ||
} | ||
const { readConfigurationFromFile } = require('./util-file') | ||
const { createClient, createTableIfNotExists, truncateTable, writeConfiguration } = require('./util-pg') | ||
|
||
const client = createClient() | ||
|
||
client.connect().then(() => { | ||
return createTableIfNotExists(client) | ||
}).then((result) => { | ||
return truncateTable(client) | ||
}).then((result) => { | ||
return readConfigurationFromFile() | ||
}).then((configurationAsString) => { | ||
return writeConfiguration(client, configurationAsString) | ||
}).then((result) => { | ||
console.log('table successfully created, configuration saved') | ||
process.exit(0) | ||
}).catch((error) => { | ||
console.log(error) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
module.exports.generateSessionExirationDate = function (seconds) { | ||
// TODO, throw exception is parameter is not set | ||
var now = new Date() | ||
var offset = (now.getTimezoneOffset() * 60 * 1000) * -1 | ||
var date = new Date(now.getTime() + offset + (seconds * 1000)) | ||
const generateSessionExirationDate = (seconds) => { | ||
const now = new Date() | ||
const offset = (now.getTimezoneOffset() * 60 * 1000) * -1 | ||
const date = new Date(now.getTime() + offset + (seconds * 1000)) | ||
|
||
return date | ||
} | ||
|
||
module.exports.convertToString = function (err) { | ||
return JSON.stringify(err, Object.getOwnPropertyNames(err)) | ||
const convertToString = (error) => { | ||
return JSON.stringify(error, Object.getOwnPropertyNames(error)) | ||
} | ||
|
||
module.exports = { | ||
generateSessionExirationDate, | ||
convertToString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,43 @@ | ||
var fs = require('fs') | ||
var uc = require('./util-common.js') | ||
const util = require('util') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { convertToString, generateSessionExirationDate } = require('./util-common.js') | ||
|
||
module.exports.convertToString = function (err) { | ||
return uc.convertToString(err) | ||
} | ||
const readConfigurationFromFile = () => { | ||
const readFile = util.promisify(fs.readFile) | ||
|
||
module.exports.generateSessionExirationDate = function (seconds) { | ||
return uc.generateSessionExirationDate(seconds) | ||
return readFile(path.join(process.cwd(), './configuration.json')) | ||
} | ||
|
||
module.exports.getConfiguration = function (callback) { | ||
|
||
fs.readFile('configuration.json', 'utf8', function (err, data) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
const writeConfigurationToFile = (configuration) => { | ||
const configurationAsString = JSON.stringify(configuration, null, 4) | ||
const writeFile = util.promisify(fs.writeFile) | ||
|
||
try { | ||
var configuration = JSON.parse(data) | ||
} catch (exception) { | ||
return callback(exception) | ||
} | ||
return writeFile(path.join(process.cwd(), './configuration.json'), configurationAsString) | ||
} | ||
|
||
callback(null, configuration) | ||
const getConfiguration = (callback) => { | ||
readConfigurationFromFile().then((data) => { | ||
callback(null, JSON.parse(data.toString())) | ||
}).catch((error) => { | ||
console.log(error) | ||
return callback(error) | ||
}) | ||
|
||
} | ||
|
||
exports.setConfiguration = function (configuration, callback) { | ||
var configurationAsString = JSON.stringify(configuration, null, 4) | ||
|
||
fs.writeFile('configuration.json', configurationAsString, function (err) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null) | ||
} | ||
const setConfiguration = (configuration, callback) => { | ||
writeConfigurationToFile(configuration).then(() => { | ||
callback(null) | ||
}).catch((error) => { | ||
return callback(error) | ||
}) | ||
|
||
} | ||
|
||
module.exports = { | ||
convertToString, | ||
generateSessionExirationDate, | ||
readConfigurationFromFile, | ||
writeConfigurationToFile, | ||
getConfiguration, | ||
setConfiguration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,75 @@ | ||
var pg = require('pg') | ||
var uc = require('./util-common.js') | ||
|
||
module.exports.convertToString = function (err) { | ||
return uc.convertToString(err) | ||
} | ||
const { Client } = require('pg') | ||
const { convertToString, generateSessionExirationDate } = require('./util-common.js') | ||
|
||
const getConfiguration = (callback) => { | ||
const client = createClient() | ||
|
||
client.connect().then(() => { | ||
return readConfiguration(client) | ||
}).then((result) => { | ||
if (result.rows.length === 1) { | ||
callback(null, JSON.parse(result.rows[0].data)) | ||
} else { | ||
callback(new Error(`configuration invalid, ${result.rows.length} rows found`)) | ||
} | ||
}).catch((error) => { | ||
return callback(error) | ||
}) | ||
|
||
module.exports.generateSessionExirationDate = function (seconds) { | ||
return uc.generateSessionExirationDate(seconds) | ||
} | ||
|
||
module.exports.getConfiguration = function (callback) { | ||
|
||
pg.connect(process.env.DATABASE_URL, function (err, client, done) { | ||
|
||
client.query('SELECT * FROM configuration', function (err, result) { | ||
done() | ||
const setConfiguration = (configuration, callback) => { | ||
const configurationAsString = JSON.stringify(configuration, null, 4) | ||
|
||
if (err) { | ||
return callback(err) | ||
} else { | ||
if (result.rows.length) { | ||
callback(null, JSON.parse(result.rows[0].data)) | ||
} else { | ||
callback(new Error('configuration database is empty')) | ||
} | ||
} | ||
|
||
}) | ||
const client = createClient() | ||
|
||
client.connect().then(() => { | ||
return truncateTable(client) | ||
}).then((result) => { | ||
return writeConfiguration(client, configurationAsString) | ||
}).then((result) => { | ||
return callback(null) | ||
}).catch((error) => { | ||
console.log(error) | ||
return callback(error) | ||
}) | ||
|
||
} | ||
|
||
exports.setConfiguration = function (configuration, callback) { | ||
var configurationAsString = JSON.stringify(configuration, null, 4) | ||
|
||
pg.connect(process.env.DATABASE_URL, function (err, client, done) { | ||
|
||
client.query('TRUNCATE configuration', function (err, result) { | ||
|
||
if (err) { | ||
return callback (err) | ||
} else { | ||
|
||
client.query('INSERT INTO configuration(data) values($1)', [configurationAsString], | ||
function (err, result) { | ||
done() | ||
|
||
if (err) { | ||
callback(err) | ||
} else { | ||
callback(null) | ||
} | ||
const createClient = () => { | ||
return new Client({ | ||
connectionString: process.env.DATABASE_URL, | ||
}) | ||
} | ||
|
||
} | ||
) | ||
const createTableIfNotExists = (client) => { | ||
return client.query('CREATE TABLE IF NOT EXISTS configuration (id serial, data text)') | ||
} | ||
|
||
} | ||
const truncateTable = (client) => { | ||
return client.query('TRUNCATE configuration') | ||
} | ||
|
||
}) | ||
const writeConfiguration = (client, configuration) => { | ||
const query = { | ||
text: 'INSERT INTO configuration(data) values($1)', | ||
values: [configuration], | ||
} | ||
|
||
}) | ||
return client.query(query) | ||
} | ||
|
||
const readConfiguration = (client) => { | ||
return client.query('SELECT * FROM configuration') | ||
} | ||
|
||
module.exports = { | ||
convertToString, | ||
generateSessionExirationDate, | ||
createClient, | ||
createTableIfNotExists, | ||
truncateTable, | ||
writeConfiguration, | ||
readConfiguration, | ||
getConfiguration, | ||
setConfiguration | ||
} |