Shôden is a persistance library on top of Postgres. Uses JSONB as an storage abstraction so no need for migrations.
Installation
gem install shoden
Connect
Shoden connects by default using a DATABASE_URL
env variable.
But you can change the connection string by calling Shoden.url=
Setup
Shoden needs a setup method to create the proper tables. You should do that after connecting
Shoden.setup
Models
class Fruit < Shoden::Model
attribute :type
end
Fruit.create type: "Banana"
To find by an id:
Fruit[1]
Relations
class User < Shoden::Model
attribute :email
collection :posts, :Post
end
class Post < Shoden::Model
attribute :title
attribute :content
reference :owner, :User
end
Attributes
Shoden attributes offer you a way to type cast the values, or to perform changes in the data itself.
class Shout < Shoden::Model
attribute :what, ->(x) { x.uppcase }
end
Indexing
class User < Shoden::Model
attribute :email
attribute :country
index :country
unique :email
end
Querying
You can query models or relations using the filter
method.
User.filter(email: "elcuervo@elcuervo.net")
User.first
User.last
User.count
You can go through the entire set using: User.all
which will give you a
Enumerator::Lazy