Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Created July 14, 2016 03:33
Show Gist options
  • Save trevnorris/deda7e6fa61eac8efbb82f167491c148 to your computer and use it in GitHub Desktop.
Save trevnorris/deda7e6fa61eac8efbb82f167491c148 to your computer and use it in GitHub Desktop.
Simple usage of ff-jsondb and how it currently stores/saves data
'use strict';
const db = require('ff-jsondb')(db_path);
// This creates a file at db_path + '/data/file.json' of the stringified
// json_object.
db.set('/data/file', json_object);
// The reads in the file at db_path + '/data/file.json' and returns the parsed
// object.
db.get('/data/file');
// So currently if I want to change a single field on an object the entire file
// has to be read in, changed, then written back to disk. Currently this works
// because most of the data I get is from an API that only sends the entire
// JSON object, and it's faster to write it all out instead of transverse the
// object and only write out the diffs.
// In the future I think it would be useful to have an API that allows a single
// object property to be retrieved from the object, sort of like:
db.getValue('/data/file', 'foo.bar');
// Similarly to set an value on a property without needing to read in the
// entire JSON file.
db.setValue('/data/file', 'foo.bar', 42);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment