Jalasem
Jalasem

Reputation: 28391

Use an encrypted file as your database instead of mongodb for your node/express app

How can I use an encrypted file as your database instead of MongoDB or MySQL for your NodeJS/express app?

where you will give your DB will be of a JSON format and there will be a service that backs it up at regular interval.

It could look like this

{
  "info": {
    "version": "0.0.1",
    "name": "Give your DB a name"
  },
  "data": {

  }
}

and have methods like

myDB.info() // => {"version" : "0.0.2", "name": "demo DB"}

myDB.get('foo'); // => 'bar'

myDB.set('foo', 'bar');

myDB.delete(key);

myDB.backup(function(err) {
    if(err) throw err;
    else console.log('DB backed up at %s', new Date().toString());
});

myDB.search('stats.comments_lookup[{stats.page.id}]', function(res) {
    console.log("search result: ", res);
});

I saw this npm package called express-db but it's outdated and not maintained. Any better option or way of doing this?

Upvotes: 0

Views: 894

Answers (1)

Harikrishnan
Harikrishnan

Reputation: 9979

Instead of using encrypted database or files, just encrypt data and store it in database.

You can use crypto-js module

var CryptoJS = require("crypto-js");

// Encryption 
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');
//Store it in any database

//Retrieve from database
// Decryption 
var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);

console.log(plaintext);

Advantage
In most cases you don't really need to encrypt entire data in a data set. So just encrypt sensitive data and store other data as plain text. Your application will run faster as encryption is really a CPU intensive process.

Upvotes: 1

Related Questions