Project

rowdb

0.01
No commit activity in last 3 years
No release in over 3 years
A local JSON database using Rudash for easy Hash traversal.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0
>= 0
 Project Readme

Gem Version

A local JSON database for Ruby inspired by lowdb, using Rudash for easy Hash traversal.

Usage

# Load the database.
db = Rowdb.new('db.json')

# Set a default structure.
db.defaults({ 'checklist' => [] }).write()

# Set data.
db.set('checklist[0]', 'milk')
  .set('checklist[1]', 'cheese')
  .set('checklist[2]', 'eggs')
  .write()

# Push data.
db.get('checklist')
  .push('spam')
  .write()

# Get data.
db.get('checklist').value() # => ["milk", "cheese", "eggs", "spam"]

Method execution is lazy, values are only returned or written after calling value() or write().

Installation

In your Gemfile add:

gem "rowdb"

Then run:

bundle install

Or install globally with:

gem install rowdb

API

All methods return a Rowdb instance which you can chain other methods on.

new()

Rowdb.new(file_name, adapter = :sync, js_var = "db")

Example:

db = Rowdb.new('db.json')

Params:

  • file_name - The path to the file. Accepts path relative to execution path or absolute path. Acceptable file extensions are ".json" and ".js".
  • adapter - The method to read and write data. Defaults to a synchronous file system.
  • js_var - The name of the variable that the JSON is assigned to when file_name ends in ".js".

defaults()

Rowb.defaults(data)

Params:

  • Hash data - The default structure of the database.

get()

Rowdb.get(path)

Params:

  • String path - The path query mirroring Rudash API.

set()

Rowdb.set(path, value)

Params:

  • String path - The path query mirroring Rudash API.
  • Object value - The value you wish to store.

push()

Rowdb.push(value)

Push a value onto an array. Must be chained after a get() which gets the array to push to.

Example:

db.get('items')
  .push('item')

Params:

  • Object value - The value you wish to store.

value()

Rowdb.value()

Returns: The value stored in the database.

write()

Rowdb.write()

Saves the data to the file system.