在一个进程多个数据库连接 sinatra 应用程序中使用什么 ORM?
检查了 ActiveRecord、DataMapper、Sequel:有些使用全局变量(静态变量),有些需要在加载模型源文件之前打开数据库连接。在使用不同数据库的 sinatra 应用程序中使用什么 ORM 更好。
Checked ActiveRecord, DataMapper, Sequel: some use globals (static variables) some require open db connection before loading source file with models. What ORM is better to use in sinatra application that uses different databases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataMapper 专为多数据库使用而设计。
您只需输入类似
DataMapper.setup(:repository_one, "mysql://localhost/my_db_name")
即可设置多个存储库。然后,DataMapper 会跟踪已在哈希中设置的所有存储库,您可以引用这些存储库并将其用于范围界定:
DataMapper.repository(:repository_one){ MyModel.all }
(默认范围是 DataMapper.repository ,您可以通过说 DataMapper.setup(:default, "postgres://localhost/my_primary_db") 等进行设置)
DataMapper is designed for multi-database use.
You can set up multiple repositories just by saying something like
DataMapper.setup(:repository_one, "mysql://localhost/my_db_name")
.DataMapper then tracks all the repositories that have been setup in a hash that you can reference and use for scoping:
DataMapper.repository(:repository_one){ MyModel.all }
(The default scope just being DataMapper.repository, which you can set up by saying
DataMapper.setup(:default, "postgres://localhost/my_primary_db")
or the like)看来大多数 ORM 都可以使用不同的数据库。
对于 DataMapper,请参阅知识理论答案。
对于 Sequel,您可以将数据库处理程序传递给模型:
其中 db 是打开的数据库。
对于ActiveRecord,您可以使用create_connection 方法。
It seems that it is possible to use different databases in most of ORMs.
For DataMapper look at knowtheory answer.
For Sequel you can pass database handler to model:
where db is opened database.
For ActiveRecord you can use establish_connection method.
就我个人而言,我更喜欢使用 Sequel 进行所有 ORM 和基本数据库访问,并且在 Sinatra/Padrino 以及任何其他需要访问 Rails 之外的数据库时使用 Sequel。
我使用过 DataMapper,但感觉 Sequel 更简单、更灵活,但也许这就是我的想法。 ActiveRecord 单独使用还可以,但我认为它与 Rails 结合使用效果最好。
哪个“更好”?我认为这是主观的,主要与你的大脑如何工作有关。
Personally I prefer Sequel for all my ORM and basic database accesses and is what I use with Sinatra/Padrino and any other time I need to access a database outside of Rails.
I've used DataMapper but felt Sequel was easier and more flexible, but maybe that's just how my mind works. ActiveRecord is OK on its own, but I think it works best in combination with Rails.
Which is "better"? I think that is subjective and mostly is tied to how your brain works.