铁轨 +设计+每个子域一个数据库的 CouchDB
我正在构建一个需要 CouchDB 移动同步功能的应用程序。
因此,对于服务上的每个“帐户”,我想创建一个单独的 CouchDB 数据库实例,以便仅同步该帐户的数据。
我正在使用 CouchRest Model 和 Devise,它通过单独的用户数据库处理子域身份验证。
但是,在运行时为每个模型连接到适当数据库的正确方法是什么?
before_filter 设置命名连接,然后循环遍历每个模型并执行如下操作:
[Post, Tag, Comment].each do |model|
model_server = CouchRest::Server.new(couch_config[:connection])
model_server.default_database = "my_project-#{Rails.env}-#{model.to_s.downcase}"
model.database = model_server.default_database
end
(伪代码)
假设 Web 服务器(Heroku)在单独的线程中运行每个请求,这应该意味着在每个请求上,数据库连接都会动态更改。
看来应该有更简单的方法!
I'm building an application that will require CouchDB's mobile syncing feature.
So for each 'account' on the service I want to create a separate CouchDB database instance so that only this account's data is synced.
I'm using CouchRest Model and Devise, which handles subdomain authentication via a separate users database.
However what is the correct way to connect to the appropriate database at runtime for each model?
A before_filter that sets up a named connection, then loops through each model and does something like this: ?
[Post, Tag, Comment].each do |model|
model_server = CouchRest::Server.new(couch_config[:connection])
model_server.default_database = "my_project-#{Rails.env}-#{model.to_s.downcase}"
model.database = model_server.default_database
end
(Pseudocode)
Assuming that the web server (Heroku) runs each request in a separate thread, this should mean that on each request, the database connection is changed dynamically.
Seems like there should be an easier way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为问题的解决方案,您可以重写数据库方法:
然后从此类中子类化您的模型(帖子、标签、评论)。
使用这个技巧,您需要在控制器中做的只是调用类似的东西
,但是,请注意,当您拥有数千个帐户(数据库)时,这种方法会变得有点混乱。
As a solution to question you can override the database method:
And then just subclass your models (Post, Tag, Comment) from this class.
With this trick all you need to do in controller is just call something like
But, beware that this approach will become a little messy when you'll have several thousands of accounts (databases).