Giter Site home page Giter Site logo

arc's Introduction

#Arc (Arel Connection) Arc is a database connection engine that provides everything Arel needs to construct an AST. You can use sqlite, postgresql and/or mysql.

Arc gives you:

  • quoting and casting of values as they enter and exit the data store
  • standard and thread safe CRUD interface for executing queries
  • a hash-like interface that provides information about the tables and columns in your database (see below)

There are two dependencies: q and arel itself.

Arc lets you use arel without the cost of including active record as a dependency.

Arel is a very capable and inspired bit of code which provides machinery for building an abstract syntax tree(ast) of a complex sql query in pure ruby.

##Arc is not: Arc isn't an ORM. There has been some recent discussion about the state of ruby ORMs. Arc does not make any attempt to pass judgement against any of the fine ORMs out there. Arc came out of a need for a lighter weight method for manipulating data in a database. Arc gives developers flexibility to build their own frameworks and write smaller libraries with fewer dependencies.

##Installation Add this to your Gemfile

gem 'pg' #'mysql2' or 'sqlite'
gem 'arc'

##Basics ####Connect to a database:

require 'arc'
@store = Arc::DataStores[:postgres].new({
  database: arc_development,
  host: localhost,
  user: superman
})
@store[:superheros]
# => <Arc::DataStores::ObjectDefinitions::SqliteTable:0x007f86d4026f68 @name="superheros">
@store[:superheros].column_names
# => [:is_awesome, :name, :id ]
@store[:superheros][:id]
# => #<Arc::DataStores::ObjectDefinitions::SqliteColumn @name='id'>
@store[:superheros][:id].primary_key?
# => true
@store[:superheros][:is_awesome].type
# => :bool

####Execute a query Build an Arel query and pass it to one of the store's CRUD methods:

"Read" Example:

s = Arel::Table.new :superheros
s.project(s[:name], s[:id], s[:is_awesome])
@store.read s
# => [{ :id => 1, :name => 'superman', :is_awesome => true  }
# =>  { :id => 2, :name => 'batman',   :is_awesome => false }
# =>  { :id => 3, :name => 'ironman',  :is_awesome => nil   }]

"Create" example

im = Arel::InsertManager.new
im.insert([
  [superheros[:name], 'green hornet'],
  [superheros[:is_awesome], true]
])
@store.create im
# => { :id => 4, :name => 'green hornet', :is_awesome => true }

##Advanced Arc handles some of the more complex features of arel, like complex joins:

s = Arel::Table.new :superheros
sp = Arel::Table.new :superheros_powers
p = Arel::Table.new :powers
stmt = s.join(sp).on(s[:id].eq(sp[:superhero_id]))
 .join(p).on(p[:id].eq(sp[:power_id]))
 .project(
   s[:name].as('superhero_name'),
   p[:name].as('power_name')
 )
@store.read stmt
# => [{:superhero_name => 'superman', :power_name => 'flight'},
# =>  {:superhero_name => 'superman', :power_name => 'x-ray-vision'},
# =>  {:superhero_name => 'batman', :power_name => "a belt'}]

##TODO Arc is a new library. The test suite has excellent coverage, but bugs need to be identified, tested and fixed. Next step is to add RDoc magic. A long term goal would be to add on to the schema interface to allow for some ddl operations.

##Development Install dependencies with bundler Make sure you have bundler installed, point your terminal to the project's root directory and run

$ bundle install

Running the tests:

$ rake test

To run the tests agains a particular adapter

$ rake test:adapter[<your-adapter-here>]

arc's People

Contributors

jacobsimeon avatar

Stargazers

Simon Escobar Benitez avatar Tamay Eser Uysal avatar Guillermo Iguaran avatar  avatar

Watchers

 avatar James Cloos 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.