jodb
Need a quick way to get a local database?
Example
var jodb = var db =
Database is automatically saved to db.json
"posts": "title": "jodb is awesome"
You can query and manipulate it using any lodash method
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 = // in-memoryvar db = // disk-based
When a filename is provided you can set options.
var db =
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_ var song1 = var song2 =
db.object
Use whenever you want to access or modify the underlying database object.
if dbobjectsongs console
db.save([filename])
Saves database to file.
var db = db // saves to db.jsondb
Note: In case you directly modify the content of the database object, you'll need to manually call save
delete dbobjectsongsdb
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.
value
Retrieve song titles.
Get the number of songs.
size
Make a deep clone of songs.
Update a song.
value
Remove songs.
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 = db_ var songId = idvar song =
uuid returns a unique id.
var uuid = var songId = idvar song =
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 = var cipher = cryptovar decipher = crypto jodb { var str = JSON return cipher + cipher} jodb { var str = decipher + decipher return JSON}
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