使用Sinatra与数据库对话的最佳方式是什么?

时间:2021-04-18 14:04:01

As I understand it, the Sinatra framework, unlike Rails, does not provide an ORM. In that case, how do you talk to a DB in a Sinatra app? Or is Sinatra only for apps that don't use a DB?

据我所知,与Rails不同,Sinatra框架并没有提供ORM。在这种情况下,如何在Sinatra应用中与DB对话?或者Sinatra只适用于不使用DB的应用?

3 个解决方案

#1


26  

If you like ActiveRecord, use that. Or something else. Datamapper, for instance. For AR with SQLite, this works:

如果你喜欢ActiveRecord,可以使用它。或者其他东西。例如,Datamapper。对于使用SQLite的AR,这是可行的:

require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'active_record'

class Article < ActiveRecord::Base
end

get '/' do
  Article.establish_connection(
    :adapter => "sqlite3",
    :database => "hw.db"
  )
  Article.first.title
end

#2


60  

If you're using Sinatra, I can't recommend DataMapper highly enough. I have a couple Rails apps where I'm stuck with ActiveRecord, and I'm constantly cursing its shortcomings and design flaws. If you're on Sinatra, DataMapper is a very practical choice.

如果你使用的是Sinatra,我推荐的DataMapper就不够了。我有两个Rails应用程序,我一直在使用ActiveRecord,我一直在抱怨它的缺点和设计缺陷。如果你在Sinatra, DataMapper是一个非常实用的选择。

require "rubygems"
require "sinatra"
require "datamapper"

DataMapper.setup(:default, "sqlite3::memory:")

class Post
  include DataMapper::Resource

  property :id,    Integer, :serial => true
  property :title, String
end

Post.auto_migrate!
first_post = Post.new
first_post.title = "First!"
first_post.save

get "/" do
  Post.get(1).title
end

#3


0  

It is up to you how to communicate with a database, you may choose either one of the ORMs or some NoSQL adapter. There are many options available, some of them were made specially for Sinatra:

如何与数据库通信由您决定,您可以选择ORMs或NoSQL适配器中的一个。有很多选择,其中一些是专门为西纳特拉制作的:

For example, there is Sinatra ActiveRecord Extension
Originally created by Blake Mizerany, creator of Sinatra
It extends Sinatra with ActiveRecord helper methods and Rake tasks

例如,Sinatra ActiveRecord扩展最初由Blake Mizerany创建,Sinatra的创建者使用ActiveRecord助手方法和Rake任务来扩展Sinatra

Another option is Sinatra Sequel Extension.
This small extension adds database configuration, migrations, and Sequel adapters right into Sinatra.

另一个选择是《辛纳屈续集》的续篇。这个小扩展将数据库配置、迁移和后续适配器添加到Sinatra中。

Or sinatra-redis, or sinatra-mongo, and so on. Just search for what you want.

或者sinatra-redis,或者sinatra-mongo等等。只是寻找你想要的。

But you may as well freely use any independent library, check out the Sinatra Recipes on databases, where is listed a couple of examples of how to use popular database mappers with Sinatra. Although it is mentioned there that the suggested practice for this is using DataMapper, I suspect that this is a mere preference, because nothing in Sinatra itself suggests this.

但是,您也可以*地使用任何独立的库,查看数据库上的Sinatra菜谱,其中列出了几个如何使用Sinatra的流行数据库映射器的示例。尽管有人提到,建议使用DataMapper来实现这一点,但我怀疑这只是一种偏好,因为Sinatra本身并没有暗示这一点。

#1


26  

If you like ActiveRecord, use that. Or something else. Datamapper, for instance. For AR with SQLite, this works:

如果你喜欢ActiveRecord,可以使用它。或者其他东西。例如,Datamapper。对于使用SQLite的AR,这是可行的:

require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'active_record'

class Article < ActiveRecord::Base
end

get '/' do
  Article.establish_connection(
    :adapter => "sqlite3",
    :database => "hw.db"
  )
  Article.first.title
end

#2


60  

If you're using Sinatra, I can't recommend DataMapper highly enough. I have a couple Rails apps where I'm stuck with ActiveRecord, and I'm constantly cursing its shortcomings and design flaws. If you're on Sinatra, DataMapper is a very practical choice.

如果你使用的是Sinatra,我推荐的DataMapper就不够了。我有两个Rails应用程序,我一直在使用ActiveRecord,我一直在抱怨它的缺点和设计缺陷。如果你在Sinatra, DataMapper是一个非常实用的选择。

require "rubygems"
require "sinatra"
require "datamapper"

DataMapper.setup(:default, "sqlite3::memory:")

class Post
  include DataMapper::Resource

  property :id,    Integer, :serial => true
  property :title, String
end

Post.auto_migrate!
first_post = Post.new
first_post.title = "First!"
first_post.save

get "/" do
  Post.get(1).title
end

#3


0  

It is up to you how to communicate with a database, you may choose either one of the ORMs or some NoSQL adapter. There are many options available, some of them were made specially for Sinatra:

如何与数据库通信由您决定,您可以选择ORMs或NoSQL适配器中的一个。有很多选择,其中一些是专门为西纳特拉制作的:

For example, there is Sinatra ActiveRecord Extension
Originally created by Blake Mizerany, creator of Sinatra
It extends Sinatra with ActiveRecord helper methods and Rake tasks

例如,Sinatra ActiveRecord扩展最初由Blake Mizerany创建,Sinatra的创建者使用ActiveRecord助手方法和Rake任务来扩展Sinatra

Another option is Sinatra Sequel Extension.
This small extension adds database configuration, migrations, and Sequel adapters right into Sinatra.

另一个选择是《辛纳屈续集》的续篇。这个小扩展将数据库配置、迁移和后续适配器添加到Sinatra中。

Or sinatra-redis, or sinatra-mongo, and so on. Just search for what you want.

或者sinatra-redis,或者sinatra-mongo等等。只是寻找你想要的。

But you may as well freely use any independent library, check out the Sinatra Recipes on databases, where is listed a couple of examples of how to use popular database mappers with Sinatra. Although it is mentioned there that the suggested practice for this is using DataMapper, I suspect that this is a mere preference, because nothing in Sinatra itself suggests this.

但是,您也可以*地使用任何独立的库,查看数据库上的Sinatra菜谱,其中列出了几个如何使用Sinatra的流行数据库映射器的示例。尽管有人提到,建议使用DataMapper来实现这一点,但我怀疑这只是一种偏好,因为Sinatra本身并没有暗示这一点。