From e04eedb2c44805ec43fe3149ffa233d2e2b3c27f Mon Sep 17 00:00:00 2001 From: dm Date: Thu, 7 Jan 2021 14:37:54 +0100 Subject: [PATCH] add postgres support --- config/default.json | 8 +++++++ models/db.js | 56 +++++++++++++++++++++++++++++++-------------- package.json | 1 + 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/config/default.json b/config/default.json index 2838396..3fbea80 100644 --- a/config/default.json +++ b/config/default.json @@ -7,6 +7,14 @@ "endpoint": "http://localhost:9666", "invite_code": "top-sekrit", + "storage_type": "postgres", + "storage_config": { + "host": "localhost", + "database": "spacedeck", + "username": "postgres", + "password": "postgres" + }, + "storage_local_path": "./storage", "storage_local_db": "./database.sqlite", "storage_region": "eu-central-1", diff --git a/models/db.js b/models/db.js index 8cb3d17..bdb90a7 100644 --- a/models/db.js +++ b/models/db.js @@ -6,24 +6,46 @@ function sequel_log(a,b,c) { } const Sequelize = require('sequelize'); -const sequelize = new Sequelize('database', 'username', 'password', { - host: 'localhost', - dialect: 'sqlite', +let sequelize; - pool: { - max: 5, - min: 0, - acquire: 30000, - idle: 10000 - }, - - // SQLite only - storage: config.get('storage_local_db'), - logging: sequel_log, - - // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators - operatorsAliases: false -}); +if(config.has('storage_type') && config.get('storage_type') === 'postgres') { + console.log('connecting to postgres'); + let postgresConfig = config.get('storage_config'); + sequelize = new Sequelize(postgresConfig.database, postgresConfig.username, postgresConfig.password, { + host: postgresConfig.host, + dialect: 'postgres', + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000 + }, + logging: sequel_log, + // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators + operatorsAliases: false + }); + +} else { + console.log('connecting to sqlite'); + sequelize = new Sequelize('database', 'username', 'password', { + host: 'localhost', + dialect: 'sqlite', + + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000 + }, + + // SQLite only + storage: config.get('storage_local_db'), + logging: sequel_log, + + // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators + operatorsAliases: false + }); +} var User; var Session; diff --git a/package.json b/package.json index d53987b..5e44408 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "node-phantom-simple": "2.2.4", "node-server-screenshot": "^0.2.1", "nodemailer": "^4.6.7", + "pg": "^8.5.1", "phantomjs-prebuilt": "^2.1.16", "read-chunk": "^2.1.0", "request": "^2.88.0",