fraud

πŸ—„ File-based key-JSON database with nodecache

View the Project on GitHub AnandChowdhary/fraud

πŸ—„ Fraud

NPM Build Coveralls Type definitions GitHub Vulnerabilities

Fraud is a promise-based library for Node.js for a data storage solution for times when MongoDB is an overkill. It’s essentially a wrapper around the native fs filesystem and nodecache, with added utilities.

⭐ How it works

Add fraud to your project with NPM:

npm install fraud

Add it to your project:

const Fraud = require("fraud");

Initialize it with the directory of your database:

const database = new Fraud({
    directory: "./database"
});

Now, you can use Fraud functions to use your file system as a JSON database. This is much faster then querying MongoDB in use cases where you only search for the primary key (in this case, the file name).

πŸ’‘ Why Fraud

Real use case: Switching API configuration from MySQL (managed RDS in the same region as an EC2) to Fraud, Oswald Labs Platform was able to reduce event tracking latency from 100-150ms to 30-70ms.

How it works

  1. When you use read(key), it queries in-memory cache
  2. If found, returns it from the cache (super fast)
  3. Otherwise, return it from the file system
  4. Save it in cache, so future requests are from the cache

πŸ’» Configuration

You can use the following options in the constructor:

const database = new Fraud({
  directory: "./database", // Where files are stored
  extension: "json", // File extension for JSON files
  update: info => { // Function to call when files are updated
      console.log(info);
  },
  softDelete: false, // Set to true to not delete files, just rename and hide them,
  deletePrefix: "__deleted_", // If soft delete is enabled, use this prefix,
  stdTTL: 0, // Standard TTL number of seconds (0 is unlimited)
  checkperiod: 0 // Period in seconds to automatically perform delete check (0 is no check)
});

Methods

You can use the following methods for programatical access:

Method Description
create(fileName, object, overwrite?) Creates a new file
delete(fileName) Deletes a file
read(fileName) Reads a file
readAll() Reads all files (3.0.0+)
update(fileName, object) Patches a file
list() Lists all available files
exists(fileName) (note) Returns whether file exists
listCache() Lists all available cached files (5.0.0+)

There are also sync versions of each function above (e.g., createSync()).

Note: It is better to use read() and catch a promise rejection than using exists(), because exists skips the cache and only checks the file system to make sure it’s available. You should also be careful when using readAll() and list() for the same reason.

For example, you can create a new file like this:

database.create("ara", {
    name: "Ara Isaacson",
    email: "hi@araassistant.com",
    phone: {
        countryCode: 31,
        number: "XXXXXXXXX"
    }
});

Then, updating a value is as simple as:

database.update("ara", {
    phone: {
        countryCode: 1
    }
});

Which can be read like this:

database.read("ara").then(user => console.log(user.phone));
// { countryCode: 1, number: "XXXXXXXXX" }

Or using async/await:

const user = await database.read("ara");
console.log(user.phone);
// { countryCode: 1, number: "XXXXXXXXX" }

πŸ› οΈ Development

Start development server with Nodemon:

yarn start

Production

Build a production version:

yarn build

✍️ Todo

πŸ“ License

MIT

Thanks to @aleximb for suggesting the name!