npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-nosql-db

v1.0.7

Published

Handler for local JSON (NoSQL) Databases with data sending, receiving, updating, deleting, and even with extra ".on" events!

Downloads

4

Readme

node-nosql-db

Handler for local JSON (NoSQL) Databases with data sending, receiving, updating, deleting, and even with extra ".on" events!


📥 Installation

Make sure you have the latest version of Node.js and then install the package either with NPM or with YarnPKG.

$ npm i node-nosql-db@latest
$ yarn add node-nosql-db@latest

🔧 Functions and Events

Let's get to know the functions and events in this module.

Functions:

Events:

  • .on("add") - Emitted when a new entry is added to the database.
  • .on("delete") - Emitted when an entry is deleted from the database.
  • .on("update") - Emitted when an entry is updated in the database.

📚 Documentations

Here's the documentation for the functions and events in this module.

The .loadDB() function loads the database data and returns it as a JSON object. So, it is possible to store the data in a variable as a snapshot of the database, and then revert the data back into the database if any wrong changes are made.

// File: app.js
// The main JavaScript file.

const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

let data = userDB.loadDB(); // Load the database data in a variable.
console.log(data); // Print the data.
// File: /database/users.json
// The JSON file, which will be manipulated as a database.

{
  "1": {
    "name": "John Doe",
    "email": "[email protected]",
    "age": "50"
  },
  "2": {
    "name": "Jane Doe",
    "email": "[email protected]",
    "age": "45"
  },
  "3": {
    "name": "Jack Doe",
    "email": "[email protected]",
    "age": "21"
  },
  "4": {
    "name": "Jill Doe",
    "email": "[email protected]",
    "age": "20"
  }
}
# Output
# Command Prompt or Bash Shell

$ node app.js
{
  "1": {
    "name": "John Doe",
    "email": "[email protected]",
    "age": "50"
  },
  "2": {
    "name": "Jane Doe",
    "email": "[email protected]",
    "age": "45"
  },
  "3": {
    "name": "Jack Doe",
    "email": "[email protected]",
    "age": "21"
  },
  "4": {
    "name": "Jill Doe",
    "email": "[email protected]",
    "age": "20"
  }
}

The .post() function adds a new entry to the JSON database. The function takes two arguments: the ID of the entry, and the data to be added. The ID is a unique identifier for the entry that is 100% required, and the data is the JSON Object to be added to the database under the given ID. The function triggers the .on("add") event, which is emitted when a new entry is added to the database.

// File: app.js
// The main JavaScript file.

const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

// Add a new entry to the database.
userDB.post("5", {
  // The ID of the entry...
  name: "Hemil Patel",
  email: "[email protected]", // ...and the data to be added.
  age: "23",
});

let data = userDB.loadDB(); // Load the new database snapshot in a variable.
console.log(data); // Print the data.

Assuming the database file will be the one in the above example, the output will be:

# Output
# Command Prompt or Bash Shell

$ node app.js
{
  "1": {
    "name": "John Doe",
    "email": "[email protected]",
    "age": "50"
  },
  "2": {
    "name": "Jane Doe",
    "email": "[email protected]",
    "age": "45"
  },
  "3": {
    "name": "Jack Doe",
    "email": "[email protected]",
    "age": "21"
  },
  "4": {
    "name": "Jill Doe",
    "email": "[email protected]",
    "age": "20"
  }
  "5": {
    "name": "Hemil Patel",
    "email": "[email protected]",
    "age": "23"
  }
}

The .get() function returns the data with the given ID. The function takes one argument: the ID of the entry. The function automatically filters the data with the given ID, and returns it as a JSON Object. This also enables us to work with the data in a more significant way. It can be used as a getter in a project like a RESTful API, where the API user can filter the data by only putting the ID of the entry in the URL as a query parameter. It also sends an error if the ID is not found in the database.

// File: app.js
// The main JavaScript file.

const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

// Get the data with the given ID.
let data = userDB.get("1");
console.log(data); // Print the data.

Assuming the database file will be the one in the above example, the output will be:

# Output
# Command Prompt or Bash Shell

$ node app.js
{
  "name": "John Doe",
  "email": "[email protected]",
  "age": "50"
}

The .put() function updates the data with the given ID. The function takes two arguments: the ID of the entry, and the data to be updated. The ID is a unique identifier for the entry that is 100% required, and the data is the JSON Object to be updated in the database under the given ID. The function triggers the .on("update") event, which is emitted when an entry is updated in the database.

// File: app.js
// The main JavaScript file.

const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

// Update the data with the given ID.
userDB.put("1", {
  // The ID of the entry...
  age: "55", // ...and the data to be updated.
});

let data = userDB.loadDB(); // Load the new database snapshot in a variable.
console.log(data); // Print the data.

Assuming the database file will be the one in the above example, the output will be:

# Output
# Command Prompt or Bash Shell

$ node app.js
{
  "1": {
    "name": "John Doe",
    "email": "[email protected]",
    "age": "55"
  },
  "2": {
    "name": "Jane Doe",
    "email": "[email protected]",
    "age": "45"
  },
  "3": {
    "name": "Jack Doe",
    "email": "[email protected]",
    "age": "21"
  },
  "4": {
    "name": "Jill Doe",
    "email": "[email protected]",
    "age": "20"
  }
}

The .delete() function deletes the data with the given ID. The function takes one argument: the ID of the entry. The function triggers the .on("delete") event, which is emitted when an entry is deleted from the database. It permanently drops an entry from the database. So, as long as the entry is not added again, it will not be available in the database.

// File: app.js
// The main JavaScript file.

const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

// Delete the data with the given ID.
userDB.delete("1");

let data = userDB.loadDB(); // Load the new database snapshot in a variable.
console.log(data); // Print the data.

Assuming the database file will be the one in the above example, the output will be:

# Output
# Command Prompt or Bash Shell

$ node app.js
{
  "2": {
    "name": "Jane Doe",
    "email": "[email protected]",
    "age": "45"
  },
  "3": {
    "name": "Jack Doe",
    "email": "[email protected]",
    "age": "21"
  },
  "4": {
    "name": "Jill Doe",
    "email": "[email protected]",
    "age": "20"
  }
}
// File: app.js
// The main JavaScript file.

// Basic Setup.
const JSON_DB = require("node-nosql-db"); // Import the module.
const userDB = new JSON_DB("./database/users.json"); // Create a new instance of the JSON_DB class.

// On "add" event.
userDB.on("add", (newSnapshot, newId, dataAdded) => {
  console.log(`Added:\nID: ${newId}\nData:\n${dataAdded}`); // Print the data.

  // Print the new snapshot.
  console.log("New Snapshot:");
  console.log(newSnapshot);
});

// On "update" event.
userDB.on("update", (newSnapshot, oldSnapshot, id, dataUpdated) => {
  console.log(`Updated:\nID: ${id}\nData:\n${dataUpdated}`); // Print the data.

  // Print the old and new snapshots.
  console.log("Old Snapshot:");
  console.log(oldSnapshot);
  console.log("New Snapshot:");
  console.log(newSnapshot);
});

// On "delete" event.
userDB.on("delete", (newSnapshot, oldSnapshot, idDeleted, dataDeleted) => {
  console.log(`Deleted:\nID: ${id}\nData:\n${dataDeleted}`); // Print the data.

  // Print the old and new snapshots.
  console.log("Old Snapshot:");
  console.log(oldSnapshot);
  console.log("New Snapshot:");
  console.log(newSnapshot);
});

🔚 The End

I'll be adding more features to this library in the future. If you have any suggestions, please let me know. I'll be happy to add them. If you want to contribute, please feel free to fork the repository and make your own changes. Also, if you want to report a bug, please feel free to open an issue on the GitHub repository.

Anyways, have fun with this library!!! 😉

Made with ❤️ by TheCodeHeist.