Created
September 24, 2015 05:50
-
-
Save benhutchins/1969dc66ec216910ac4f to your computer and use it in GitHub Desktop.
Flat-file database in Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = require('fs') | |
let path = require('path') | |
class FlatFileDatabase { | |
constructor (root) { | |
this.root = root | |
} | |
get (collectionName, key) { | |
let file = path.join(this.root, collectionName, key + '.json') | |
return JSON.parse(fs.readFileSync(file)) | |
} | |
save (collectionName, key, data) { | |
let file = path.join(this.root, collectionName, key + '.json') | |
return fs.writeFileSync(file, JSON.stringify(data)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment