Git Product home page Git Product logo

rom-sql's Introduction

ROM::SQL

Gem Version Build Status Dependency Status Code Climate Test Coverage Inline docs

RDBMS suport for Ruby Object Mapper.

Installation

Add this line to your application's Gemfile:

gem 'rom-sql'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rom-sql

Setup

ROM uses Sequel under the hood and exposes its Dataset API in relation objects. For schema migrations you can use its Migration API which is available via repositories.

setup = ROM.setup(:sql, "sqlite::memory")

setup.default.connection.create_table(:users) do
  primary_key :id
  String :name
  Boolean :admin
end

setup.default.connection.create_table(:tasks) do
  primary_key :id
  Integer :user_id
  String :title
end

Relations

class Users < ROM::Relation[:sql]
  base_name :users

  def by_name(name)
    where(name: name)
  end
end

rom = ROM.finalize.env

users = rom.relations.users
tasks = rom.relations.tasks

users.insert(id: 1, name: "Piotr")
tasks.insert(user_id: 1, title: "Be happy")

puts users.by_name("Piotr").with_tasks.to_a.inspect
# => [{:id=>1, :name=>"Piotr", :user_id=>1, :title=>"Be happy"}]

Mapping joins to aggregates

ROM doesn't have a relationship concept like in ActiveRecord or Sequel. Instead it provides a convenient interface for building joined relations that can be mapped to aggregate objects.

There's no lazy-loading, eager-loading or any other magic happening behind the scenes. You're in full control of how data are fetched from the database and it's an explicit operation.

Sequel's association DSL is available in relation definitions which enables association_join interface inside relations. To map joined results to aggregate objects wrap and group mapping transformation can be used

ROM.setup(:sql, "sqlite::memory")

class Users < ROM::Relation[:sql]
  one_to_many :tasks, key: :user_id

  def by_name(name)
    where(name: name)
  end

  def with_tasks
    association_join(:tasks, select: [:title])
  end
end

class UserMapper < ROM::Mapper
  relation :users

  model name: 'User'

  group tasks: [:title]
end

rom = ROM.finalize.env

users = rom.relations.users
tasks = rom.relations.tasks

users.insert(id: 1, name: "Piotr")
tasks.insert(user_id: 1, title: "Be happy")

rom.read(:users).with_tasks.by_name("Piotr").to_a
# => [#<User:0x007fb31542a098 @id=1, @name="Piotr", @tasks=[{:title=>"Be happy"}]>]

ROADMAP

For details please refer to issues.

License

See LICENSE file.

rom-sql's People

Contributors

aflatter avatar chastell avatar gotar avatar mcls avatar mjtko avatar morgoth avatar solnic avatar splattael avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.