From e04eedb2c44805ec43fe3149ffa233d2e2b3c27f Mon Sep 17 00:00:00 2001 From: dm Date: Thu, 7 Jan 2021 14:37:54 +0100 Subject: [PATCH 1/4] 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", From b2cf8cf3360b58e64d519b02a5f06b469248b05c Mon Sep 17 00:00:00 2001 From: dm Date: Thu, 7 Jan 2021 14:54:24 +0100 Subject: [PATCH 2/4] change to unindented config definition --- config/default.json | 10 ++++------ models/db.js | 32 ++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/config/default.json b/config/default.json index 3fbea80..f9062ba 100644 --- a/config/default.json +++ b/config/default.json @@ -8,12 +8,10 @@ "invite_code": "top-sekrit", "storage_type": "postgres", - "storage_config": { - "host": "localhost", - "database": "spacedeck", - "username": "postgres", - "password": "postgres" - }, + "storage_postgres_host": "localhost", + "storage_postgres_database": "spacedeck", + "storage_postgres_username": "postgres", + "storage_postgres_password": "postgres", "storage_local_path": "./storage", "storage_local_db": "./database.sqlite", diff --git a/models/db.js b/models/db.js index bdb90a7..40eb52d 100644 --- a/models/db.js +++ b/models/db.js @@ -10,20 +10,24 @@ let sequelize; 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 - }); + sequelize = new Sequelize( + config.get('storage_postgres_database'), + config.get('storage_postgres_username'), + config.get('storage_postgres_password'), + { + host: config.get('storage_postgres_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'); From 18d09b49be259bb0d328faec024661b66537ca81 Mon Sep 17 00:00:00 2001 From: dm Date: Thu, 7 Jan 2021 15:26:13 +0100 Subject: [PATCH 3/4] unify config, add postgres decimal fix --- config/default.json | 11 +++++----- models/db.js | 50 +++++++++++++-------------------------------- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/config/default.json b/config/default.json index f9062ba..fcc459e 100644 --- a/config/default.json +++ b/config/default.json @@ -7,11 +7,12 @@ "endpoint": "http://localhost:9666", "invite_code": "top-sekrit", - "storage_type": "postgres", - "storage_postgres_host": "localhost", - "storage_postgres_database": "spacedeck", - "storage_postgres_username": "postgres", - "storage_postgres_password": "postgres", + "storage_dialect": "sqlite", + + "storage_host": "localhost", + "storage_database": "spacedeck", + "storage_username": "username", + "storage_password": "password", "storage_local_path": "./storage", "storage_local_db": "./database.sqlite", diff --git a/models/db.js b/models/db.js index 40eb52d..03de63d 100644 --- a/models/db.js +++ b/models/db.js @@ -6,50 +6,28 @@ function sequel_log(a,b,c) { } const Sequelize = require('sequelize'); -let sequelize; - -if(config.has('storage_type') && config.get('storage_type') === 'postgres') { - console.log('connecting to postgres'); - sequelize = new Sequelize( - config.get('storage_postgres_database'), - config.get('storage_postgres_username'), - config.get('storage_postgres_password'), - { - host: config.get('storage_postgres_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', - +const sequelize = new Sequelize( + config.get('storage_database'), + config.get('storage_username'), + config.get('storage_password'), + { + host: config.get('storage_host'), + dialect: config.get('storage_dialect'), 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 - }); -} + operatorsAliases: false, + // SQLite only + storage: config.get('storage_local_db') + } +); +// https://github.com/sequelize/sequelize/issues/8019#issuecomment-384316346 +Sequelize.postgres.DECIMAL.parse = function (value) { return parseFloat(value); }; var User; var Session; From 65476a0d0941fac15b90aaba9ab44120f9dc6e11 Mon Sep 17 00:00:00 2001 From: dm Date: Thu, 7 Jan 2021 15:41:37 +0100 Subject: [PATCH 4/4] remove pg dependency, add docs --- README.md | 20 ++++++++++++++++++++ package.json | 1 - 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a70e100..0816c80 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,26 @@ For advanced media conversion: By default, media files are uploaded to the ```storage``` folder. The database is stored in ```database.sqlite``` by default. +# Other databases (Not officially supported) + +## Postgres + +Add the [pg](https://www.npmjs.com/package/pg) module and change the config/default.json to + +``` +"storage_dialect": "postgres", +``` + +Adapt the other values as needed + +``` +"storage_host": "localhost", +"storage_database": "spacedeck", +"storage_username": "username", +"storage_password": "password", +``` + + # Run with Docker - configure `config/default.json` diff --git a/package.json b/package.json index 5e44408..d53987b 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "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",