Given the frustration aired in my previous post I decided to do something about it. The tl;dr version of the post is that I just want to be able to store data for future use without the overhead of additional services, servers, schemas, versioning etc.
My solution is Ocelite. Ocelite is built on top of sqlite3 and provides an easy way to store and retrieve data for Node.js applications but the pattern works across languages.
Here is a snippet for saving and retrieving data:
var Ocelite = require('ocelite');
var db = new Ocelite();
db.init('data.db', ['user'], cb);
db.save('user', {name: 'Barbara Fusinska', twitter: 'basiafusinska'}, ['twitter'], cb);
db.get('user', 'twitter', 'basiafusinska', function(err, arr) { console.log(arr); cb(); });
db.all('user', function(err, arr) { console.log(arr); cb(); });
To start we initialise the database and define categories for the data we want to store, in this case users, using `db.init
`. In future if we want to store additional categories then we can extend the array, when the application is reload it’s automatically taken into account without requiring any migration scripts.
To save data via `db.save
` we state the category of data along with the block of data being stored. The third parameter is optional and allows you to define properties of the data block we want to index.
If we index our object we can use the `db.get
` function to return them in future via the related lookup value, for example a twitter handle. If we want to return all our users then we can use the `db.all
` function.
You can install it as an NPM package.
$ npm install ocelite
That’s it. Nothing else. No SQL insert statements, no migration scripts, just saving data. The source code is available at http://github.com/OcelotUproar/ocelite
One final thing, why call it Ocelite? My company is called Ocelot Uproar, I like Ocelots, it’s a nod towards Sqlite and naming is hard.
The Yak has been shaved.