Hikki

A light weight persistence system for use with key/value type data. Hikki allows you to use a repository with one or more adapters for reading and writing data. Data is represented as a Hash and should be seralizable to/from JSON. Current adapters include:

  • Hikki:Adapters::MemoryAdapter
  • Hikki:Adapters::RedisAdapter
  • Hikki:Adapters::MongoAdapter
  • Hikki:Adapters::MemcacheAdapter

Installation

Add this line to your application's Gemfile:

gem 'hikki'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hikki

Usage

By default Hikki will use the included memory adapter.

# Create a new repository
repository = Hikki::Repository.new

# Create an index on the name field for the people collection
repository.index :people, :name

# Save a hash of data
repository.save :people, { id: '1', name: 'Alex' }

# If you don't specify an id, most adapters will make one for you
repository.save :people, { name: 'Jim' } # => { 'id' => '8107326a-77b6-4018-bd0b-7588442ce36b', 'name' => 'Jim' }

# Get the data back
repository.find :people, '1' # => { 'id' => '1', 'name' => 'Jim' }

# Grab all of the records in a collection
repository.all :people

# You can add a limit and/or offset
repository.all :people, { limit: 10, offset: 20 }

# Find by a field (Also can take limit/offset options)
repository.find_by :people, :name, 'Alex'

# Remove a record
repository.remove :people, '1'

# Remove all records
repository.remove_all :people

Hikki supports repositories with multiple adapters for reading and writing. It will write to all specified writers and read from the specified readers until a non-empty result is found.

redis = Hikki::Adapters::RedisAdapter.new
mongo = Hikki::Adapters::MongoAdapter.new

# Hikki::Repository.new(writers, readers)
repository = Hikki::Repository.new([redis, mongo], [redis, mongo])

You can subclass Hikki::Repository and add conversions to your models.

class MyRepository < Hikki::Repository
  def save(model)
    result = super(model.class.name, model.to_hash)
    model.update_from_hash(result)
  end

  def find(klass, id)
    result = super(klass.name, id)
    klass.new(result)
  end
end

Contributing

  1. Fork it ( http://github.com/originate/hikki/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write your specifications
  4. Implement your specifications
  5. Commit your changes (git commit -am 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new Pull Request