jodb

0.1.2 • Public • Published

jodb NPM version Build Status

Need a quick way to get a local database?

Example

var jodb = require('jodb')
var db = jodb('db.json')
 
db('posts').push({ title: 'jodb is awesome'})

Database is automatically saved to db.json

{
  "posts": [
    { "title": "jodb is awesome" }
  ]
}

You can query and manipulate it using any lodash method

db('posts').find({ title: 'jodb is awesome' })

Install

npm install jodb --save

Features

  • Small
  • Serverless
  • lodash rich API
  • In-memory or disk-based
  • Hackable (mixins, id, encryption, ...)

It's also very easy to learn and use since it has only 8 methods and properties.

API

jodb([filename, options])

Creates a disk-based or in-memory database instance. If a filename is provided, it loads or creates it.

var db = jodb()          // in-memory
var db = jodb('db.json') // disk-based

When a filename is provided you can set options.

var db = jodb('db.json', {
  autosave: true, // automatically save database on change (default: true)
  async: true,    // asynchronous write (default: true)
  promise: false  //promise write(default: false)
})

jodb.stringify(obj) and jodb.parse(str)

Overwrite these methods to customize JSON stringifying and parsing.

db._

Database lodash instance. Use it for example to add your own utility functions or third-party libraries.

db._.mixin({
  second: function(array) {
    return array[1]
  }
})
 
var song1 = db('songs').first()
var song2 = db('songs').second()

db.object

Use whenever you want to access or modify the underlying database object.

if (db.object.songs) console.log('songs array exists')

db.save([filename])

Saves database to file.

var db = jodb('db.json')
db.save() // saves to db.json
db.save('copy.json')

Note: In case you directly modify the content of the database object, you'll need to manually call save

delete db.object.songs
db.save()

db.saveSync([filename])

Synchronous version of db.save()

Guide

Operations

With jodb you get access to the entire lodash API, so there's many ways to query and manipulate data. Here are a few examples to get you started.

Please note that data is returned by reference, this means that modifications to returned objects may change the database. To avoid such behaviour, you need to use .cloneDeep().

Also, the execution of chained methods is lazy, that is, execution is deferred until .value() is called.

Sort the top five songs.

db('songs')
  .chain()
  .where({published: true})
  .sortBy('views')
  .take(5)
  .value()

Retrieve song titles.

db('songs').pluck('title')

Get the number of songs.

db('songs').size()

Make a deep clone of songs.

db('songs').cloneDeep()

Update a song.

db('songs')
  .chain()
  .find({ title: 'jodb!' })
  .assign({ title: 'hi!'})
  .value()

Remove songs.

db('songs').remove({ title: 'jodb!' })

Id support

Being able to retrieve data using an id can be quite useful, particularly in servers. To add id-based resources support to jodbdb, you have 2 options.

underscore-db provides a set of helpers for creating and manipulating id-based resources.

var db = jodb('db.json')
 
db._.mixin(require('underscore-db'))
 
var songId = db('songs').insert({ title: 'jodb!' }).id
var song   = db('songs').getById(songId)

uuid returns a unique id.

var uuid = require('uuid')
 
var songId = db('songs').push({ id: uuid(), title: 'jodb!' }).id
var song   = db('songs').find({ id: songId })

Encryption support

In some cases, you may want to make it harder to read database content. By overwriting, jodb.stringify and jodb.parse, you can add custom encryption.

var crypto = require('crypto')
 
var cipher = crypto.createCipher('aes256', secretKey)
var decipher = crypto.createDecipher('aes256', secretKey)
 
jodb.stringify = function(obj) {
  var str = JSON.stringify(obj)
  return cipher.update(str, 'utf8', 'hex') + cipher.final('hex')
}
 
jodb.parse = function(encrypted) {
  var str = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8')
  return JSON.parse(str)
}

Changelog

See details changes for each version in the release notes.

Limits

jodbdb is a convenient method for storing data without setting up a database server. It's fast enough and safe to be used as an embedded database.

However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.

License

MIT - Typicode

Package Sidebar

Install

npm i jodb

Weekly Downloads

4

Version

0.1.2

License

MIT

Last publish

Collaborators

  • fatea