Giter Site home page Giter Site logo

horza's Introduction

Horza

Horza is a library for decoupling your application from the ORM you have implemented.

Inputs

Horza uses ORM-specific adapters to decouple Ruby apps from their ORMS.

Configure Adapter

Horza.configure do |config|
  config.adapter = :active_record
end

Get Adapter for your ORM Object

# ActiveRecord Example
user = Horza.adapt(User)

# Examples
user.get(id) # Find by id - Return nil on fail
user.get!(id) # Find by id - Error on fail

user.find_first(options) # Find 1 user - Orders by id desc by default - Return nil on fail
user.find_first!(options) # Find 1 user - Orders by id desc by default - Error nil on fail

user.find_all(options) # Find all users that match parameters

user.create(options) # Create record - return nil on fail
user.create!(options) # Create record - raise error on fail

user.update(options) # Update record - return nil on fail
user.update!(options) # Update record - raise error on fail

user.delete(options) # Delete record - return nil on fail
user.delete!(options) # Delete record - raise error on fail

user.association(target: :employer, via: []) # Traverse association

# Special case: create a child record when mass-assignment is disabled for parent instance_methods
# klass is a symbol version of your model name, ie Employer is :employer, SportsCar is :sports_car
parent = { id: parent_id, klass: :employer }

user.create_as_child(parent, options) # Create record - return nil on fail
user.create_as_child!(parent, options) # Create record - raise error on fail

# With args
conditions = { last_name: 'Turner' }

# Ordering
user.find_all(conditions: conditions, order: { last_name: :desc })

# Limiting
user.find_all(conditions: conditions, limit: 20)

# Offset
user.find_all(conditions: conditions, offset: 50)

# Eager loading associations
employer.association(target: :users, eager_load: true)

Options

Base Options

Key Type Details
conditions Hash Key value pairs for the query
order Hash { field => :asc/:desc }
limit Integer Number of records to return
offset Integer Number of records to offset
id Integer The id of the root object (associations only)
target Symbol The target of the association - ie. employer.users would have a target of :users (associations only)
eager_load Boolean Whether to eager_load the association (associations only)

Association Options

Key Type Details
id Integer The id of the root object
target Symbol The target of the association - ie. employer.users would have a target of :users
eager_load Boolean Whether to eager_load the association

Outputs

Horza queries return very dumb vanilla entities instead of ORM response objects. Singular entities are simply hashes that allow both hash and dot notation, and binary helpers. Collection entities behave like arrays.

# Singular Entity
result = user.find_first(first_name: 'Blake')

result # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}
result.class.name # => "Horza::Entities::Single"
result['id'] # => 1
result.id # => 1
result.id? # => true

# Collection Entity
result = user.find_all(last_name: 'Turner')

result.class.name # => "Horza::Entities::Collection"
result.length # => 1
result.size # => 1
result.empty? # => false
result.present? # => true
result.first # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}
result.last # => {"id"=>2, "first_name"=>"Morgan", "last_name"=>"Bruce", "employer_id"=>2}
result[0] # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}

Custom Entities

You can define your own entities by making them subclasses of Horza entities. Just make sure they have the same class name as your ORM classes. Horza will automatically detect custom entities and use them for output.

module CustomEntities
  # Collection Entity
  class Users < Horza::Entities::Collection
  end

  # Singular Entity
  class User < Horza::Entities::Single
  end
end

horza's People

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.