My project is written in node JS + Express and has the following architecture:
- routes (e.g controllers)
- models (stateless functional modules)
- app
Models objects only doing SQL queries like this:
exports.setPassword = function (id, password, callback) {
pg.connect(conString, function (err, client, done) {
if (err) callback(err);
else {
client.query('UPDATE users SET password=$1 WHERE id=$2', [password, id], function (err) {
done();
if (err) callback(err);
else callback(null);
});
}
});
};
exports.setStatus = function (id, status, callback) {
pg.connect(conString, function (err, client, done) {
if (err) callback(err);
else {
client.query('UPDATE users SET status=$1 WHERE id=$2', [status, id], function (err) {
done();
if (err) callback(err);
else callback(null);
});
}
});
};
exports.getStayInTouch = function (userId, callback) {
pg.connect(conString, function (err, client, done) {
if (err) callback(err);
else {
client.query('SELECT * FROM stay_in_touch_contacts WHERE user_id=$1', [userId], function (err, results) {
done();
if (err) callback(err);
else callback(null, results.rows[0]);
});
}
});
};
and many many functions connected with the user in that user.js module. The only thing I can do with that architecture is to place functions to the different module.
I'd appreciate any critiques of that architecture. How can I do better?