nodejs dirty check if key exists

I'm a newbie to Node.js. I am using Dirty to save data into a .db file, and I have no idea to check if the key I inserted through db.set() already exists.

In other words: I want to check if the key exists, and if it does, do something.

Just using the basic documentation provided on: https://github.com/felixge/node-dirty

I would say the best way to handle this is to:

if(!!db.get(key)){
   //your value already exists
}

This system appears to be designed for the naive approach, and this is certainly the naive approach.

You can check if a key exists by using dirty.get(key) and checking if the returned value is defined (in case it is a "falsey" value such as false, zero, the empty string, etc.):

var myKey='...', myVal=dirty.get(myKey);
if (typeof(myVal) !== 'undefined') {
  // Do something...
}

[Original Answer Below]

You can check if a file exists by using the fs.exists(...) function in node.js:

fs.exists(myDatabaseFilename, function(exists) {
  if (exists) { /* Do something... */ }
});