Skip to content

Commit

Permalink
fixed PostgreSQL setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Damm committed Jan 3, 2020
1 parent d43af02 commit aecc60e
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion util-pg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const { Client } = require('pg')
const { convertToString, generateSessionExirationDate } = require('./util-common.js')

const validateConfiguration = (callback) => {
const client = createClient()

client.connect().then(() => {
return checkIfTableExists(client)
}).then((result) => {
if (result.rows[0].exists === true) {
callback(null)
} else {
callback(new Error('table does not exist'))
}
}).catch((error) => {
return callback(error)
}).then(() => client.end())
}

const getConfiguration = (callback) => {
const client = createClient()

Expand Down Expand Up @@ -41,6 +57,10 @@ const createClient = () => {
})
}

const checkIfTableExists = (client) => {
return client.query('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'public\' AND table_name = \'configuration\')')
}

const createTableIfNotExists = (client) => {
return client.query('CREATE TABLE IF NOT EXISTS configuration (id serial, data text)')
}
Expand Down Expand Up @@ -71,5 +91,6 @@ module.exports = {
writeConfiguration,
readConfiguration,
getConfiguration,
setConfiguration
setConfiguration,
validateConfiguration
}

0 comments on commit aecc60e

Please sign in to comment.