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 whenfile_nameends 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.