admin管理员组文章数量:1394046
Is it possible to change the database connection in sequelize
depending on the route?
For example, Users have access to 2 different installations in a website:
- example/foo
- example/bar
Upon login users are redirected to example/foo
To get all their tasks for the foo
site, they need to visit example/foo/tasks
The bar
site uses a separate database and thus if they want to get all their tasks for bar
they have to go to example/bar/tasks
Every installation has its own database, and all databases have the same schema.
Is it possible to change the database connection depending on which route is visited?
*login only occurs once
Is it possible to change the database connection in sequelize
depending on the route?
For example, Users have access to 2 different installations in a website:
- example./foo
- example./bar
Upon login users are redirected to example./foo
To get all their tasks for the foo
site, they need to visit example./foo/tasks
The bar
site uses a separate database and thus if they want to get all their tasks for bar
they have to go to example./bar/tasks
Every installation has its own database, and all databases have the same schema.
Is it possible to change the database connection depending on which route is visited?
*login only occurs once
Share Improve this question edited May 14, 2015 at 2:36 VulgarBinary 3,5994 gold badges22 silver badges55 bronze badges asked May 12, 2015 at 21:50 zesk8zesk8 1811 gold badge2 silver badges7 bronze badges2 Answers
Reset to default 8This is possible. There are a number of ways to do this. Here's how I might approach it.
Router.js
var router = express.Router()
// This assumes the database is always the 2nd param,
// otherwise you have to enumerate
router.use('/:database/*', function(req, res, next){
req.db = req.params.database;
next();
}
Connection.js
var fooDB = new Sequelize('postgres://user:[email protected]:5432/foo');
var barDB = new Sequelize('postgres://user:[email protected]:5432/bar');
module.exports = {
foo: fooDB,
bar: barDB,
}
Tasks.js
var connection = require('connection);
function getTasks(req, params){
var database = connection[req.db];
//database now contains the db you wish to access based on the route.
}
That said, there are some interesting problems you'll have to face when you want to duplicate the schema across both, but that's probably best for another question.
I hope this helps!
I think is better to change collections depending on the route.
Have foo_tasks
collection and bar_tasks
collection in the same database.
Or have the attribute type
in the tasks
collection that specifies if the task is a "foo task" or a "bar task".
本文标签: javascriptChange database connection depending on route in expressjs with sequelizeStack Overflow
版权声明:本文标题:javascript - Change database connection depending on route in express.js with sequelize - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744086008a2588532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论