From 80b11e920539f52b05cf55f99d2c24d8ff4e1fb4 Mon Sep 17 00:00:00 2001 From: pantsel Date: Sat, 14 Jul 2018 12:45:48 +0300 Subject: [PATCH] Added ability to specify schema for postgres integrations #218 --- README.md | 2 + api/models/ApiHealthCheck.js | 7 ++ api/models/EmailTransport.js | 7 ++ api/models/KongNode.js | 7 ++ api/models/KongServices.js | 7 ++ api/models/NetdataConnection.js | 7 ++ api/models/Passport.js | 7 ++ api/models/Settings.js | 7 ++ api/models/Snapshot.js | 7 ++ api/models/SnapshotSchedule.js | 8 ++ api/models/User.js | 170 +++++++++++++++++--------------- 11 files changed, 154 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 31aa28235..fa62ba2f8 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ an ephemeral container for the job. $ docker run -p 1337:1337 -e "DB_ADAPTER={adapter_name}" \ -e "DB_URI={full_connection_string}" \ + -e "DB_PG_SCHEMA=my-schema"\ // Optionally define a schema when integrating with prostgres -e "NODE_ENV=development" \ --name konga \ pantsel/konga node ./bin/konga.js prepare @@ -185,6 +186,7 @@ $ docker run -p 1337:1337 -e "DB_USER=your-db-user" \ // Omit if not relevant -e "DB_PASSWORD=your-db-password" \ // Omit if not relevant -e "DB_DATABASE=your-db-name" \ // Defaults to 'konga_database' + -e "DB_PG_SCHEMA=my-schema"\ // Optionally define a schema when integrating with prostgres -e "NODE_ENV=production" \ // or 'development' | defaults to 'development' --name konga \ pantsel/konga diff --git a/api/models/ApiHealthCheck.js b/api/models/ApiHealthCheck.js index 97e2a34e0..91bddf148 100644 --- a/api/models/ApiHealthCheck.js +++ b/api/models/ApiHealthCheck.js @@ -78,4 +78,11 @@ var mongoModel = function() { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/EmailTransport.js b/api/models/EmailTransport.js index b568c277d..cfe3a570d 100644 --- a/api/models/EmailTransport.js +++ b/api/models/EmailTransport.js @@ -131,4 +131,11 @@ var mongoModel = function () { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/KongNode.js b/api/models/KongNode.js index bf44d3f73..dd9beb2ed 100644 --- a/api/models/KongNode.js +++ b/api/models/KongNode.js @@ -132,4 +132,11 @@ var mongoModel = function () { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/KongServices.js b/api/models/KongServices.js index 4c3e5aebe..082600ed9 100644 --- a/api/models/KongServices.js +++ b/api/models/KongServices.js @@ -42,4 +42,11 @@ var defaultModel = _.merge(_.cloneDeep(require('../base/Model')), { var mongoModel = _.omit(_.cloneDeep(defaultModel),["autoPK","attributes.id"]); +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel : defaultModel diff --git a/api/models/NetdataConnection.js b/api/models/NetdataConnection.js index 75c97f87d..6a907c8d9 100644 --- a/api/models/NetdataConnection.js +++ b/api/models/NetdataConnection.js @@ -44,4 +44,11 @@ var mongoModel = function () { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel; diff --git a/api/models/Passport.js b/api/models/Passport.js index 1bcd1c792..2c0b93e00 100644 --- a/api/models/Passport.js +++ b/api/models/Passport.js @@ -154,4 +154,11 @@ var mongoModel = function() { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/Settings.js b/api/models/Settings.js index ffa4bc7bd..73fe397b7 100644 --- a/api/models/Settings.js +++ b/api/models/Settings.js @@ -136,4 +136,11 @@ var mongoModel = function () { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/Snapshot.js b/api/models/Snapshot.js index 234150b77..0b4a0e7be 100644 --- a/api/models/Snapshot.js +++ b/api/models/Snapshot.js @@ -55,4 +55,11 @@ var mongoModel = function () { return obj; } +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/SnapshotSchedule.js b/api/models/SnapshotSchedule.js index f782c686b..05db3a800 100644 --- a/api/models/SnapshotSchedule.js +++ b/api/models/SnapshotSchedule.js @@ -92,4 +92,12 @@ var mongoModel = function () { return obj; } + +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } +} + + module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel diff --git a/api/models/User.js b/api/models/User.js index 02679c77c..669652296 100644 --- a/api/models/User.js +++ b/api/models/User.js @@ -13,101 +13,107 @@ var defUserSeedData = require('../../config/default-user-seed-data.js'); var defaultModel = _.merge(_.cloneDeep(require('../base/Model')), { - tableName: "konga_users", - autoPK: false, - attributes: { - id: { - type: 'integer', - primaryKey: true, - unique: true, - autoIncrement: true - }, - username: { - type: 'string', - unique: true, - required: true - }, - email: { - type: 'email', - unique: true, - required: true - }, - firstName: { - type: 'string' - }, - lastName: { - type: 'string' - }, - admin: { - type: 'boolean', - defaultsTo: false - }, - - node_id: { - type: 'string', - defaultsTo: '' - }, - - active: { - type: 'boolean', - defaultsTo: false - }, - - activationToken : { - type : 'string' - }, - - node: { - model: 'kongnode' - }, - - // Passport configurations - passports: { - collection: 'Passport', - via: 'user' - }, + tableName: "konga_users", + autoPK: false, + attributes: { + id: { + type: 'integer', + primaryKey: true, + unique: true, + autoIncrement: true + }, + username: { + type: 'string', + unique: true, + required: true + }, + email: { + type: 'email', + unique: true, + required: true + }, + firstName: { + type: 'string' + }, + lastName: { + type: 'string' + }, + admin: { + type: 'boolean', + defaultsTo: false + }, + + node_id: { + type: 'string', + defaultsTo: '' + }, + + active: { + type: 'boolean', + defaultsTo: false }, - afterDestroy: function (values, cb) { + activationToken: { + type: 'string' + }, - sails.log("User:afterDestroy:called => ",values); + node: { + model: 'kongnode' + }, + // Passport configurations + passports: { + collection: 'Passport', + via: 'user' + }, + }, - var fns = []; + afterDestroy: function (values, cb) { - values.forEach(function(user){ - fns.push(function(callback){ - // Delete passports - sails.models.passport.destroy({user : user.id}) - .exec(callback) - }) - }) + sails.log("User:afterDestroy:called => ", values); - async.series(fns,cb); - }, + var fns = []; - //seedData object should now come from a file - // the new object has had the password field added - // we need to remove it - seedData: defUserSeedData.seedData.map( function (orig) { - return { - "username": orig.username, - "email": orig.email, - "firstName": orig.firstName, - "lastName": orig.lastName, - "node_id": orig.node_id, - "admin": orig.admin, - "active" : orig.active - } + values.forEach(function (user) { + fns.push(function (callback) { + // Delete passports + sails.models.passport.destroy({user: user.id}) + .exec(callback) + }) }) + + async.series(fns, cb); + + }, + + //seedData object should now come from a file + // the new object has had the password field added + // we need to remove it + seedData: defUserSeedData.seedData.map(function (orig) { + return { + "username": orig.username, + "email": orig.email, + "firstName": orig.firstName, + "lastName": orig.lastName, + "node_id": orig.node_id, + "admin": orig.admin, + "active": orig.active + } + }) }); var mongoModel = function () { - var obj = _.cloneDeep(defaultModel) - delete obj.autoPK - delete obj.attributes.id - return obj; + var obj = _.cloneDeep(defaultModel) + delete obj.autoPK + delete obj.attributes.id + return obj; +} + +if(sails.config.models.connection == 'postgres' && process.env.DB_PG_SCHEMA) { + defaultModel.meta = { + schemaName: process.env.DB_PG_SCHEMA + } } module.exports = sails.config.models.connection == 'mongo' ? mongoModel() : defaultModel