在一个进程多个数据库连接 sinatra 应用程序中使用什么 ORM?

发布于 2024-08-29 02:05:54 字数 114 浏览 1 评论 0原文

检查了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-09-05 02:05:54

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)

仅一夜美梦 2024-09-05 02:05:54

看来大多数 ORM 都可以使用不同的数据库。
对于 DataMapper,请参阅知识理论答案。
对于 Sequel,您可以将数据库处理程序传递给模型:

class Tag < Sequel::Model(db)
end

其中 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:

class Tag < Sequel::Model(db)
end

where db is opened database.
For ActiveRecord you can use establish_connection method.

少钕鈤記 2024-09-05 02:05:54

就我个人而言,我更喜欢使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文