Shôden is a persistance library on top of Postgres. Uses JSONB as an storage abstraction so no need for migrations.
gem install shoden
Shoden connects by default using a DATABASE_URL
env variable.
But you can change the connection string by calling Shoden.url=
Shoden needs a setup method to create the proper tables. You should do that after connecting
Shoden.setup
class Fruit < Shoden::Model
attribute :type
end
Fruit.create type: "Banana"
To find by an id:
Fruit[1]
class User < Shoden::Model
attribute :email
collection :posts, :Post
end
class Post < Shoden::Model
attribute :title
attribute :content
reference :owner, :User
end
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
class User < Shoden::Model
attribute :email
attribute :country
index :country
unique :email
end
You can query models or relations using the filter
method.
User.filter(email: "[email protected]")
User.first
User.last
User.count
You can go through the entire set using: User.all
which will give you a
Enumerator::Lazy