I am using convict to define and read configuration in my Typescript Node application. I will have to connect up to a number of different databases. All of these will have the same format for config; server, username, password, dbName. I could of course define them one by one in the schema and be fine, but is there a way to define the schema for one database and then make the json config file an array of dbs which I would then be able to access by the name like a database.
Example of defining each database in the schema:
var convict = require('convict');
// Define schema
var config = convict({
dbs: {
db1: {
host: {
doc: 'Database host name/IP',
format: '*',
default: 'server1.dev.test'
},
user:{
doc: 'Database Username',
format: String
},
pass: {
doc: 'Database password',
format: String,
sensitive: true
}
name: {
doc: 'Database name',
format: String,
default: 'users'
}
},
db2: {
host: {
doc: 'Database host name/IP',
format: '*',
default: 'server1.dev.test'
},
user:{
doc: 'Database Username',
format: String
},
pass: {
doc: 'Database password',
format: String,
sensitive: true
}
name: {
doc: 'Database name',
format: String,
default: 'users'
}
},
}
});
// Load environment dependent configuration
config.loadFile('./config/dev.json');
// Perform validation
config.validate({allowed: 'strict'});
module.exports = config.getProperties();
Then I would access them as such :
config.dbs.db1.user
Since I will have at least 5 databases and that number could grow over time I would like to not have to redefine the exact same schema for each one and rather define it once and then allow the config to have as many objects as necessary in the dbs array. Then ideally I could access them with something like config.dbs['db1'].user.