<i id='INJyD'><tr id='INJyD'><dt id='INJyD'><q id='INJyD'><span id='INJyD'><b id='INJyD'><form id='INJyD'><ins id='INJyD'></ins><ul id='INJyD'></ul><sub id='INJyD'></sub></form><legend id='INJyD'></legend><bdo id='INJyD'><pre id='INJyD'><center id='INJyD'></center></pre></bdo></b><th id='INJyD'></th></span></q></dt></tr></i><div id='INJyD'><tfoot id='INJyD'></tfoot><dl id='INJyD'><fieldset id='INJyD'></fieldset></dl></div>

  1. <tfoot id='INJyD'></tfoot>
  2. <legend id='INJyD'><style id='INJyD'><dir id='INJyD'><q id='INJyD'></q></dir></style></legend>

  3. <small id='INJyD'></small><noframes id='INJyD'>

      • <bdo id='INJyD'></bdo><ul id='INJyD'></ul>
    1. 使用 sequelize 根据 express.js 中的路由更改数据库连接

      时间:2023-09-04
      <tfoot id='G6I1R'></tfoot>

            <small id='G6I1R'></small><noframes id='G6I1R'>

              <tbody id='G6I1R'></tbody>

          1. <i id='G6I1R'><tr id='G6I1R'><dt id='G6I1R'><q id='G6I1R'><span id='G6I1R'><b id='G6I1R'><form id='G6I1R'><ins id='G6I1R'></ins><ul id='G6I1R'></ul><sub id='G6I1R'></sub></form><legend id='G6I1R'></legend><bdo id='G6I1R'><pre id='G6I1R'><center id='G6I1R'></center></pre></bdo></b><th id='G6I1R'></th></span></q></dt></tr></i><div id='G6I1R'><tfoot id='G6I1R'></tfoot><dl id='G6I1R'><fieldset id='G6I1R'></fieldset></dl></div>

              <legend id='G6I1R'><style id='G6I1R'><dir id='G6I1R'><q id='G6I1R'></q></dir></style></legend>
              • <bdo id='G6I1R'></bdo><ul id='G6I1R'></ul>
                本文介绍了使用 sequelize 根据 express.js 中的路由更改数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                sequelize中是否可以根据路由改变数据库连接?

                Is it possible to change the database connection in sequelize depending on the route?

                例如,用户可以访问网站中的 2 个不同的安装:- example.com/foo- example.com/bar

                For example, Users have access to 2 different installations in a website: - example.com/foo - example.com/bar

                登录后,用户会被重定向到 example.com/foo要获取 foo 站点的所有任务,他们需要访问 example.com/foo/tasks

                Upon login users are redirected to example.com/foo To get all their tasks for the foo site, they need to visit example.com/foo/tasks

                bar 站点使用单独的数据库,因此如果他们想获得 bar 的所有任务,他们必须访问 example.com/bar/任务

                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.com/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?

                *只登录一次

                推荐答案

                这是可能的.有很多方法可以做到这一点.以下是我可能会采用的方法.

                This is possible. There are a number of ways to do this. Here's how I might approach it.

                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:pass@example.com:5432/foo');
                var barDB = new Sequelize('postgres://user:pass@example.com: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.

                我希望这会有所帮助!

                这篇关于使用 sequelize 根据 express.js 中的路由更改数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Sequelize - 在 PostgreSQL 数组查询中不包含字符串 下一篇:续集多对多连接的位置

                相关文章

                <legend id='oKSqq'><style id='oKSqq'><dir id='oKSqq'><q id='oKSqq'></q></dir></style></legend>
                <tfoot id='oKSqq'></tfoot>

                  <small id='oKSqq'></small><noframes id='oKSqq'>

                    <i id='oKSqq'><tr id='oKSqq'><dt id='oKSqq'><q id='oKSqq'><span id='oKSqq'><b id='oKSqq'><form id='oKSqq'><ins id='oKSqq'></ins><ul id='oKSqq'></ul><sub id='oKSqq'></sub></form><legend id='oKSqq'></legend><bdo id='oKSqq'><pre id='oKSqq'><center id='oKSqq'></center></pre></bdo></b><th id='oKSqq'></th></span></q></dt></tr></i><div id='oKSqq'><tfoot id='oKSqq'></tfoot><dl id='oKSqq'><fieldset id='oKSqq'></fieldset></dl></div>

                    • <bdo id='oKSqq'></bdo><ul id='oKSqq'></ul>