1

首先是一些背景:

我们正在尝试创建一个多租户应用程序,我们考虑首先使用平均堆栈并为每个租户创建多个集合(例如 order_tenant1、order_tenant2 等),然后我们浏览了一些建议反对这种方法的博客,其次我们觉得事务的需要是我们数据库的核心要求,因此我们打开了我们自己的关系数据库,如 mysql 和 mariaDB,我们偶然发现了一个博客 ,其中详细解释了该方法,它说创建视图以获取、更新和插入相关数据租户和视图参数将通过连接字符串定义,因为我们使用的是 node.js 我发现 ORM for mysql sequelizejs非常好。

实际问题:

根据我对平均堆栈的经验,我们在 server.js 文件中定义了 mongo 连接,应用程序在应用程序启动时建立这些连接并保持它们处于活动状态, 在此处输入图像描述 我如何拥有多个 sequelizejs(或就此而言和数据库连接)对象根据属于特定租户的用户连接到数据库,并为应用程序提供正确的对象以进行业务逻辑

1)我应该在应用程序获得的每个请求上创建一个新的连接对象,然后在处理请求后关闭它吗?

2)或者有没有更好的方法在节点,快递或续集js中处理这个!?

编辑:我们决定使用包含tenant_id作为列的基于行的方法,如上面的博客中所述,但我正在苦苦思考如何通过sequelizejs对象维护与数据库的直接连接对象,即id是属于租户id的用户:1 向他需要服务的应用程序发送一个请求,对象是“db”,这是一个与数据库通信的续集对象,该数据库是使用其连接字符串中的租户 id 1 的详细信息创建的,对于属于租户 id 的用户也是如此:2 它需要使用相同的对象,即“db”,但它必须使用其连接字符串中的租户 id 2 的详细信息创建,因为我想为我必须服务的每个租户维护不同的连接字符串(数据库连接对象)。

4

1 回答 1

0

Multi-tenancy can be implemented as row-based, schema-based, or database-based. Other than that 2010 article, I think you will find few if any other recommendations for doing database-backed multi-tenancy. Systems were not designed to talk to tens or thousands of databases, and so things will keep failing on you. The basic thing you're trying to avoid is SQL injection attacks that reveal other user's data, but the proper way to avoid those is through sanitizing user inputs, which you need to do no matter what.

I highly recommend going with a normal row-based multi-tenancy approach as opposed to schema-based, as described in https://devcenter.heroku.com/articles/heroku-postgresql#multiple-schemas and in this original article: http://railscraft.tumblr.com/post/21403448184/multi-tenanting-ruby-on-rails-applications-on-heroku

Updated:

Your updated question still isn't clear about the difference between database-based and row-based multi-tenancy. You want to do row-based. Which means that you can setup a single Sequelize connection string exactly like the examples, since you'll only have a single database.

Then, your queries to the database will look like:

User.find({ userid: 538 }).complete(function(err, user) {
  console.log(user.values)
})

The multi-tenancy is provided by the userid attribute. I would urge you to do a lot more reading about databases, ORM, and typical patterns before getting started. I think you will find an additional up front investment to pay dividends versus starting development when you don't fully understand how ORMs typically work.

于 2014-04-06T19:52:15.187 回答