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

json-local-database

v0.0.5

Published

JSON local database is like mongoDB database. It's a simple local database that uses JSON files as collections. A perfect solution of electron/node apps that needs a local database.

Downloads

15

Readme

json-local-database

json-local-database JSON local database is like mongoDB database. It's a simple local database that uses JSON files as collections. A perfect solution of electron/node apps that needs a local database..

This package creates a JSON file for each collection inside the application's json-local-database folder. The json-local-database folder is created when you create your first collection.


Table of Contents:


Installation

The preferred way of installation is to install it locally on the application.

npm install json-local-database --save

Using json-local-database

After installing the package, you can now use it in your application. To use it, just require it in your application.

const db = require("json-local-database");

Creating a collection

The function createCollection() creates a json file [collection].json inside the application local-db folder. It will return an error if the collection/json file is already created.

db.createCollection(collectionName, callback(success, message)=>{});

Example

const db = require("json-local-database");

db.createCollection("users", (success, message) => {
  if (success) {
    console.log(message);
  } else {
    console.log("error. ", message);
  }
});

/*
	Output:
    	Success: true
        Message: Success or error message

	Result file (users.json):
        Inside the file : 
          {
            "users": []
          }
*/

Inserting Single Object to a Collection

The function db.insertOne() insert an object into the array of the collection.

db.insertOne(
  collectionName,
  { name: "Sanan", age: 24 },
  (success, data, message) => {}
);

Example

To insert an object to the collection, use the insertOne() function. The function accepts 3 parameters, the collection name, the object to be inserted and the callback function.

db.insertOne("users", { name: "Sanan", age: 24 }, (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
	Output:
    	Success: true
        data: { name: 'Sanan', age: 24, id: 1702051713569 }
        Message: Success or Error Message if any

*/

Inserting an Array of Objects to a Collection

The function db.insertMany() insert an array of object into the specified collection.

db.insertOne(
  collectionName,
  [
    { name: "Sanan", age: 24 },
    { name: "Junaid", age: 32 },
  ],
  (success, data, message) => {}
);

Example

To insert multiple objects to the collection, use the insertMany() function. The function accepts 3 parameters, the collection name, the array of objects to be inserted and the callback function.

db.insertMany(
  "users",
  [
    { name: "Sanan", age: 24 },
    { name: "Junaid", age: 32 },
  ],
  (success, data, message) => {
    if (success) {
      console.log(data);
    } else {
      console.log("error. ", message);
    }
  }
);

/*
	Output:
    	Success: true
        data: [
                { name: 'Sanan', age: 24, id: 1702052585609 },
                { name: 'Junaid', age: 32, id: 1702052585609 },
              ]

        Message: Success or Error Message if any

*/

Getting All Saved Objects from a Collection

The function db.getAll() finds all the saved objects from the collection and returns in an array.

db.getAll(collectionName, (success, data, message) => {});

Example

To get all the data from the collection, use the getAll() function. The function accepts 2 parameters, the collection name and the callback function.

db.getAll("users", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                { name: 'Sanan', age: 24, id: 1702052585609 },
                { name: 'Junaid', age: 32, id: 1702052585609 },
              ]
        Message: Success or Error Message if any

*/

Getting Count of Saved Objects

The function db.count() finds the count of all the saved objects from the collection and returns the count.

db.count(collectionName, (success, data, message) => {});

Example

To get the count of all the data from the collection, use the count() function. The function accepts 2 parameters, the collection name and the callback function.

db.count("users", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: 2
        Message: Success or Error Message if any

*/

Search in a Collection

The function db.search() finds the saved objects from the collection that matches the search query and returns in an array.

db.search(collectionName, key, value, , (success, data, message) => {});

Example

To search in the collection, use the db.search() function. The function accepts 4 parameters, the collection name, key, value and the callback function.

db.search("users", "name", "Sanan", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                { name: 'Sanan', age: 24, id: 1702052585609 },
              ]
        Message: Success or Error Message if any

*/

Get Values of a Key in a Collection

The function db.getFieldValues() finds the values of a key in the collection and returns in an array.

db.getFieldValues(collectionName, key, (success, data, message) => {});

Example

To get the values of a key in the collection, use the db.getFieldValues() function. The function accepts 3 parameters, the collection name, key and the callback function.

db.getFieldValues("users", "name", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                "Sanan",
                "Junaid"
              ]
        Message: Success or Error Message if any

*/

Search by Field in a Collection

The function db.searchByField() finds the saved objects from the collection having the key and returns in an array of matched objects.

db.searchByField(collectionName, key, (success, data, message) => {});

Example

To search by field in the collection, use the db.searchByField() function. The function accepts 3 parameters, the collection name, key and the callback function.

db.searchByField("users", "name", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                { name: 'Sanan', age: 24, id: 1702052585609 },
                { name: 'Junaid', age: 32, id: 1702052585609 },
              ]
        Message: Success or Error Message if any

*/

Filter by Condition in a Collection

The function db.filter() finds the saved objects from the collection having the key and returns in an array of matched objects.

db.filter(collectionName, condition, (success, data, message) => {});

Example

To filter by condition in the collection, use the db.filter() function. The function accepts 3 parameters, the collection name, condition and the callback function.

db.filter("users", { name: "Sanan" }, (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                { name: 'Sanan', age: 24, id: 1702052585609 },
              ]
        Message: Success or Error Message if any

*/

Filter by Less Than or Greater Than Condition in a Collection

The function db.filter() also finds the saved objects with less than or greater than condition from the collection and returns in an array of matched objects.

db.filter(collectionName, condition, (success, data, message) => {});

Example

To filter by less than or greater than condition in the collection, use the db.filter() function. The function accepts 3 parameters, the collection name, condition and the callback function. The condition object should be in the format {key: {$lt: value}} or {key: {$gt: value}}.

db.filter("users", { age: { $gt: 30 } }, (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data: [
                { name: 'Junaid', age: 32, id: 1702052585609 },
              ]
        Message: Success or Error Message if any

*/

Update a Single Record Collection

The function db.updateOne() updates the saved objects from the collection that matches the search query and returns updated object.

db.updateOne(collectionName, where, newValue, , (success, data, message) => {});

Example

To update in the collection, use the db.update() function. The function accepts 4 parameters, the collection name, where, newValue and the callback function.

db.update(
  "users",
  { age: 24 }, // where
  { age: 25 }, // newValue
  (success, data, message) => {
    if (success) {
      console.log(data);
    } else {
      console.log("error. ", message);
    }
  }
);

/*
  Output:
    	Success: true
        data:   { name: 'Sanan', age: 25, id: 1702052585609 },
        Message: Success or Error Message if any

*/

Update Multiple Records in a Collection

The function db.updateMany() updates the saved objects from the collection that matches the search query and returns updated array of object.

db.updateMany(collectionName, where, newValue, , (success, data, message) => {});

Example

To update in the collection, use the db.updateMany() function. The function accepts 4 parameters, the collection name, where, newValue and the callback function.

db.updateMany(
  "users",
  { age: 24 }, // where
  { age: 25 }, // newValue
  (success, data, message) => {
    if (success) {
      console.log(data);
    } else {
      console.log("error. ", message);
    }
  }
);

/*
  Output:
    	Success: true
        data:   [
                  { name: 'Sanan', age: 25, id: 1702052585609 },
                  { name: 'Junaid', age: 25, id: 1702052585609 },
                ]
        Message: Success or Error Message if any

*/

Delete a Single Record in a Collection

The function db.deleteOne() deletes the saved objects from the collection that matches the search query and returns deleted object.

db.deleteOne(collectionName, where, (success, data, message) => {});

Example

To delete in the collection, use the db.deleteOne() function. The function accepts 3 parameters, the collection name, where and the callback function.

db.deleteOne(
  "users",
  { name: "Junaid" }, // where
  (success, data, message) => {
    if (success) {
      console.log(data);
    } else {
      console.log("error. ", message);
    }
  }
);

/*
  Output:
    	Success: true
        data:   { name: 'Sanan', age: 25, id: 1702052585609 },
        Message: Success or Error Message if any

*/

Delete Multiple Records in a Collection

The function db.deleteMany() deletes the saved objects from the collection that matches the search query and returns deleted array of object.

db.deleteMany(
  "users",
  { age: 25 }, // where
  (success, data, message) => {
    if (success) {
      console.log(data);
    } else {
      console.log("error. ", message);
    }
  }
);

/*
  Output:
    	Success: true
        data:   [
                  { name: 'Sanan', age: 25, id: 1702052585609 },
                  { name: 'Junaid', age: 25, id: 1702052585609 },
                ]
        Message: Success or Error Message if any

*/

Clear a Collection

The function db.clearCollection() clears the collection and returns the empty array.

db.clearCollection(collectionName, (success, data, message) => {});

Example

To clear the collection, use the db.clearCollection() function. The function accepts 2 parameters, the collection name and the callback function.

db.clearCollection("users", (success, data, message) => {
  if (success) {
    console.log(data);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        data:   []
        Message: Success or Error Message if any

*/

Delete a Collection

The function db.deleteCollection() deletes the collection and returns the success message. It deletes the physical file from the json-local-database folder.

db.deleteCollection("users", (success, message) => {
  if (success) {
    console.log(message);
  } else {
    console.log("error. ", message);
  }
});

/*
  Output:
    	Success: true
        Message: Collection deleted successfully or Success or Error Message if any

*/

For contributions, please see the CONTRIBUTE.md file. Thank you.