var fs = require('fs');

function db(file) {
  this.file = file;

  fs.exists(file, function(exists) {
    if (exists) return;
    fs.writeFile(file, '{}');
  });

  return database.bind(this);
}

function database(collection) {
  return new Methods(this.file, collection);
}

function Methods(file, collection) {
  this.file = file;
  this.collection = collection || 'default'; }

Methods.prototype.get = function(key, callback) {
  var collection = this.collection;
  fs.readFile(this.file, 'utf8', function(err, data) {
    var json = JSON.parse(data);
    if (json.hasOwnProperty(collection)) {
      callback(err, json[collection][key]);
    } else {
      callback(err);
    }
  });
};

Methods.prototype.getSync = function(key) {
  var json = JSON.parse(fs.readFileSync(this.file, 'utf8'));
  if (json.hasOwnProperty(this.collection)) {
    return json[this.collection][key];
  }
};

Methods.prototype.getAll = function(callback) {
  var collection = this.collection;
  fs.readFile(this.file, 'utf8', function(err, data) {
    var json = JSON.parse(data);
    callback(err, json[collection]);
  });
};

Methods.prototype.getAllSync = function() {
  var json = JSON.parse(fs.readFileSync(this.file, 'utf8'));
  return json[this.collection];
};

Methods.prototype.remove = function(key, callback) {
  var self = this;
  fs.readFile(this.file, 'utf8', function(err, data) {
    var json = JSON.parse(data);
    if (json.hasOwnProperty(self.collection)) {
      delete json[self.collection][key];
    }
    fs.writeFile(self.file, JSON.stringify(json), function(err) {
      if (typeof callback === 'function') {
        callback(err);
      }
    });
  });
};

Methods.prototype.removeSync = function(key) {
  var json = JSON.parse(fs.readFileSync(this.file, 'utf8'));
  if (json.hasOwnProperty(this.collection)) {
    delete json[this.collection][key];
  }
  fs.writeFileSync(this.file, JSON.stringify(json)); 
};

Methods.prototype.set = function(key, value, callback) {
  var self = this;
  fs.readFile(this.file, 'utf8', function(err, data) {
    var json = JSON.parse(data);  
    if (json.hasOwnProperty(self.collection) && !Array.isArray(json[self.collection])) {
      json[self.collection][key] = value;
    } else {
      json[self.collection] = {};
      json[self.collection][key] = value;
    }
    fs.writeFile(self.file, JSON.stringify(json), function(err) {
      if (typeof callback === 'function') {
        callback(err);
      }
    });
  });
};

Methods.prototype.setSync = function(key, value) {
  var json = JSON.parse(fs.readFileSync(this.file, 'utf8'));
  if (json.hasOwnProperty(this.collection) && !Array.isArray(json[this.collection])) {
    json[this.collection][key] = value;
  } else {
    json[this.collection] = {};
    json[this.collection][key] = value;
  }
  fs.writeFileSync(this.file, JSON.stringify(json)); 
};

module.exports = db;

@@ -371,17 +371,17 @@
 efault';
- 
+%0A
 %7D%0A%0AMetho
@@ -1295,36 +1295,37 @@
 llback) %7B%0A  var 
-self
+_this
  = this;%0A  fs.re
@@ -1425,36 +1425,37 @@
 .hasOwnProperty(
-self
+_this
 .collection)) %7B%0A
@@ -1468,28 +1468,29 @@
 delete json%5B
-self
+_this
 .collection%5D
@@ -1494,32 +1494,33 @@
 on%5D%5Bkey%5D;%0A    %7D%0A
+%0A
     fs.writeFile
@@ -1512,36 +1512,37 @@
    fs.writeFile(
-self
+_this
 .file, JSON.stri
@@ -1856,24 +1856,25 @@
 %5D%5Bkey%5D;%0A  %7D%0A
+%0A
   fs.writeFi
@@ -1909,25 +1909,24 @@
 gify(json));
- 
 %0A%7D;%0A%0AMethods
@@ -1981,20 +1981,21 @@
 %7B%0A  var 
-self
+_this
  = this;
@@ -2082,18 +2082,16 @@
 e(data);
-  
 %0A    if 
@@ -2111,20 +2111,21 @@
 roperty(
-self
+_this
 .collect
@@ -2148,28 +2148,29 @@
 sArray(json%5B
-self
+_this
 .collection%5D
@@ -2177,36 +2177,37 @@
 )) %7B%0A      json%5B
-self
+_this
 .collection%5D%5Bkey
@@ -2233,36 +2233,37 @@
 se %7B%0A      json%5B
-self
+_this
 .collection%5D = %7B
@@ -2276,20 +2276,21 @@
    json%5B
-self
+_this
 .collect
@@ -2310,24 +2310,25 @@
 alue;%0A    %7D%0A
+%0A
     fs.write
@@ -2336,12 +2336,13 @@
 ile(
-self
+_this
 .fil
@@ -2801,16 +2801,17 @@
 ue;%0A  %7D%0A
+%0A
   fs.wri
@@ -2854,17 +2854,16 @@
 (json));
- 
 %0A%7D;%0A%0Amod

Fix lint error