From baf6db88cab17b0983503b63812a0033cdd690bb Mon Sep 17 00:00:00 2001 From: Eladio Mora <8742215+ejose19@users.noreply.github.com> Date: Fri, 20 Aug 2021 11:20:48 -0300 Subject: [PATCH] feat: allow specifying db config through JSON string or JSON file Signed-off-by: Eladio Mora <8742215+ejose19@users.noreply.github.com> --- .../__tests__/noco/NcConfigFactory.test.ts | 60 +++++++++++++------ .../nocodb/src/__tests__/noco/dbConfig.json | 18 ++++++ .../nocodb/src/lib/utils/NcConfigFactory.ts | 12 +++- 3 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 packages/nocodb/src/__tests__/noco/dbConfig.json diff --git a/packages/nocodb/src/__tests__/noco/NcConfigFactory.test.ts b/packages/nocodb/src/__tests__/noco/NcConfigFactory.test.ts index 8ed25d6448..51ec0b4a13 100644 --- a/packages/nocodb/src/__tests__/noco/NcConfigFactory.test.ts +++ b/packages/nocodb/src/__tests__/noco/NcConfigFactory.test.ts @@ -5,6 +5,24 @@ import {NcConfigFactory} from "../../lib"; describe('Config Factory Tests', () => { + const expectedObject = { + client: 'pg', + connection: { + host: 'localhost', + port: 5432, + user: 'postgres', + password: 'xgene', + database: 'abcde' + }, + pool: { + min: 1, + max: 2 + }, + ssl: { + rejectUnauthorized: false + }, + acquireConnectionTimeout: 600000 + }; before(function (done) { done(); @@ -17,27 +35,35 @@ describe('Config Factory Tests', () => { it('Generate config from string', function (done) { const config = NcConfigFactory.metaUrlToDbConfig(`pg://localhost:5432?u=postgres&p=xgene&d=abcde`); - expect(config.client).to.be.eq('pg') - expect(config.connection).to.be.a('object') - expect(config.connection.user).to.be.eq('postgres') - expect(config.connection.port).to.be.eq(5432) - expect(config.connection.password).to.be.eq('xgene') - expect(config.connection.database).to.be.eq('abcde') + const { pool, ssl, ...rest } = expectedObject; + expect(config).to.deep.equal(rest); done(); }); it('Connection string with nested property', function (done) { const config = NcConfigFactory.metaUrlToDbConfig(`pg://localhost:5432?u=postgres&p=xgene&d=abcde&pool.min=1&pool.max=2&ssl.rejectUnauthorized=false`); - expect(config.client).to.be.eq('pg') - expect(config.connection).to.be.a('object') - expect(config.connection.user).to.be.eq('postgres') - expect(config.connection.port).to.be.eq(5432) - expect(config.connection.password).to.be.eq('xgene') - expect(config.connection.database).to.be.eq('abcde') - expect(config.pool).to.be.a('object') - expect(config.pool.min).to.be.eq(1) - expect(config.pool.max).to.be.eq(2) - expect(config.ssl).to.be.a('object') - expect(config.ssl.rejectUnauthorized).to.be.eq(false) + expect(config).to.deep.equal(expectedObject); done(); }); + it('Allow creating config from JSON string', function(done) { + try { + process.env.NC_DB_JSON = JSON.stringify(expectedObject); + + const { meta: { db: config } } = NcConfigFactory.make(); + expect(config).to.deep.equal(expectedObject); + done(); + } finally { + delete process.env.NC_DB_JSON; + } + }); + it('Allow creating config from JSON file', function(done) { + try { + process.env.NC_DB_JSON_FILE = `${__dirname}/dbConfig.json`; + + const { meta: { db: config } } = NcConfigFactory.make(); + expect(config).to.deep.equal(expectedObject); + done(); + } finally { + delete process.env.NC_DB_JSON_FILE; + } + }); }); diff --git a/packages/nocodb/src/__tests__/noco/dbConfig.json b/packages/nocodb/src/__tests__/noco/dbConfig.json new file mode 100644 index 0000000000..a860fe9da8 --- /dev/null +++ b/packages/nocodb/src/__tests__/noco/dbConfig.json @@ -0,0 +1,18 @@ +{ + "client": "pg", + "connection": { + "host": "localhost", + "port": 5432, + "user": "postgres", + "password": "xgene", + "database": "abcde" + }, + "pool": { + "min": 1, + "max": 2 + }, + "ssl": { + "rejectUnauthorized": false + }, + "acquireConnectionTimeout": 600000 +} diff --git a/packages/nocodb/src/lib/utils/NcConfigFactory.ts b/packages/nocodb/src/lib/utils/NcConfigFactory.ts index 17da9e6378..429fa5e93a 100644 --- a/packages/nocodb/src/lib/utils/NcConfigFactory.ts +++ b/packages/nocodb/src/lib/utils/NcConfigFactory.ts @@ -50,8 +50,18 @@ export default class NcConfigFactory implements NcConfig { config.meta.db.connection.filename = path.join(config.toolDir, config.meta.db.connection.filename) } + if (process.env.NC_DB_JSON) { + config.meta.db = JSON.parse(process.env.NC_DB_JSON); + } else if (process.env.NC_DB_JSON_FILE) { + const filePath = process.env.NC_DB_JSON_FILE; - if (process.env.NC_DB) { + if (!fs.existsSync(filePath)) { + throw new Error(`NC_DB_JSON_FILE not found: ${filePath}`); + } + + const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }); + config.meta.db = JSON.parse(fileContent); + } else if (process.env.NC_DB) { config.meta.db = this.metaUrlToDbConfig(process.env.NC_DB) }