Skip to content

Instantly share code, notes, and snippets.

@benhutchins
Created September 24, 2015 05:50
Show Gist options
  • Save benhutchins/1969dc66ec216910ac4f to your computer and use it in GitHub Desktop.
Save benhutchins/1969dc66ec216910ac4f to your computer and use it in GitHub Desktop.
Flat-file database in Node
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