2
I’m working on a system that contains a simple CRUD for tables from a Postgres database, in which new tables can be added eventually. So to avoid rewriting the controllers all the time, I did the following:
const repositorio1 = require('caminho/do/repositorio1');
const repositorio2 = require('caminho/do/repositorio2');
const repositorio3 = require('caminho/do/repositorio3');
const repos = {
'rep1' : repositorio1,
'rep2' : repositorio2,
'rep3' : repositorio3
}
//exemplo de um get
exports.get = async(req, res, next) => {
let rep = (req.url).split('/')[1];
try{
var data = await repos[rep].get();
res.status(200).send(data);
}catch(e){
res.status(500).send("erro");
}
};
That way there’s only one controller for all repositories.
I lose performance with code written that way?
There’s a difference between me writing it like this?
const repos = {
'rep1' : require('caminho/do/repositorio1'),
'rep2' : require('caminho/do/repositorio2'),
'rep3' : require('caminho/do/repositorio3')
}