What’s the best way to talk to a database while using Sinatra?

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.

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 "https://stackoverflow.com/" do
  Post.get(1).title
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)