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-db-manager

v1.2.0

Published

Optimized JSON: Simplifies and Enhances Data Reading, Writing, and Management.

Downloads

38

Readme

json-db-manager

A simple json manager like "data base (DB)" local to your application, very advanced and malleable to use as you want.

  • Compatible with several EcmaScript (ES) technologies
  • Supports both "require()" and "import"

New features or changes

  • We now have a new method, the "fetch" method, which works similarly to 'get' but performs a more in-depth search.

Bugs fixed from the previous version

  • The issue of having to instantiate the 'read' and 'request' events to start the local server via 'hostView' has been fixed.

How to use

const { JsonDB } = require("json-db-manager"); // ES5
import { JsonDB } from "json-db-manager"; // ES6

const db = new JsonDB();

You can choose the spacing by placing the number of spaces inside the JsonDB class, the default being: 0

const db = new JsonDB(2); // Additional spacing

You can also choose between a json storing "base64" or "utf-8" which is the default.

const db = new JsonDB(0, "utf-8"); // Optional encoding

The third parameter is responsible for managing your json file that will be defined in the "path" method. If there is no file with the listed name, it can create the file automatically if "true" is passed. The default is "false"

const db = new JsonDB(0, "utf-8", true); // Optional to create file if it doesn't exist

path is the main method that will provide the methods to manipulate your json file, but it is necessary that the file path is based on the root route of your project.

Methods

GET

Pass the "key" that you want to search for, if no parameter is passed it will return your json file. The get search system is superficial, if you want to search for a "key" that is inside a method it will be necessary to use the fetch method.

// example json
{
  "KeyExample": {"velueExamples": [0, "1", [2, 2.5]]},
  "KeyExample1": 3,
}
console.log(db.path("./dir").get())
// db.path("./dir").get("KeyExample") To search for a specific item

Result exemplo

{
  "KeyExample": {"velueExamples": [0, "1", [2, 2.5]]},
  "KeyExample1": 3,
}

SET

Pass the "key" of your choice and the "value".

// example json
{}
db.path("./dir").set("key", "value"); // Criando por parametro.
db.path("./dir").set({"key1": "value"}); // Formatação objeto.
db.path("./dir").set({"key2": "value", "key3": "value"}); // Formatação com objetos.

Result exemplo

{
  "key": "value", // Criando por parametro.
  "key1": "value", // Formatação com objeto.
  "key2": "value", // Formatação com objetos.
  "key3": "value", // Formatação com objetos.
}

ADD

Its function is to add "value" to the value of "key" if the value is of numeric type.

// example json
{
  "test": 120,
}
db.path("./dir").add("key", 50);

Result exemplo

{
  "test": 170
}

SUB

Subtracts the specified "value" from the value of "key" if the value is numeric.

// example json
{
  "test": 120,
}
db.path("./dir").sub("key", 50);

Result exemplo

{
  "test": 70
}

DELETE

Pass the "key" you want to delete.

// example json
{
  "keyExample": "wow",
  "KeyExample1": 8,
}
db.path("./dir").delete("keyExample");

Result exemplo

{
  //keyExample is deleted
  "KeyExample1": 8,
}

CLEAR

Clears all json content, leaving it ready for new values.

// example json
{
  "test": 120,
}
db.path("./dir").clear();

Result exemplo

{} // Empty

FETCH

No support for Base64

Pass the "key" to search within the json. By default, everything found with the same "key" will be returned.

// example json
{
  "test": 120,
  "mm": 60,
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {
          "mm": 10
        },
        {
          "mm": 11
        },
        {
          "mm": 12
        }
      ]
    }
  }
}

Methods

Get

Returns in an array all the values ​​of the "keys" found. When passing the value of a key in the parameter, that key will be returned if it exists.

console.log(db.path("./dir").fetch("mm").get());
console.log(db.path("./dir").fetch("mm").get(11));

Result exemplo

[ 60, 10, 11, 12 ]
[ 11 ]

Set

Accepts values ​​as string, number, arrays and object. Changes the value of all "keys" it finds for the key selected in "fetch".

db.path("./dir").fetch("mm").set("20:)");

Result exemplo

{
  "test": 120,
  "mm": "20:)", // set "20:)"
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {
          "mm": "20:)" // set "20:)"
        },
        {
          "mm": "20:)" // set "20:)"
        },
        {
          "mm": "20:)" // set "20:)"
        }
      ]
    }
  }
}

Add

Its function is to add "value" to the values ​​of "key" if the value is of numeric type.

db.path("./dir").fetch("mm").add(50);

Result exemplo

{
  "test": 120,
  "mm": 110, // add 50
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {
          "mm": 60 // add 50
        },
        {
          "mm": 61 // add 50
        },
        {
          "mm": 62 // add 50
        }
      ]
    }
  }
}

Sub

Its function is to subtract "value" from the values ​​of "key" if the value is of a numeric type.

db.path("./dir").fetch("mm").sub(50);

Result exemplo

{
  "test": 120,
  "mm": 10, // sub 50
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {
          "mm": -40 // sub 50
        },
        {
          "mm": -41 // sub 50
        },
        {
          "mm": -42 // sub 50
        }
      ]
    }
  }
}

Clear

Used to delete "key" from "json file".

db.path("./dir").fetch("mm").clear();

Result exemplo

{
  "test": 120,
  // removed
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {}, // removed
        {}, // removed
        {} // removed
      ]
    }
  }
}

Mapper

Use it to manipulate your "json file" however you like.

  • obj is the first parameter and provides as an object the methods where the "key" is located.
  • keys being the second parameter, providing the "key" that will be manipulated.

Structure: (obj: object, keys: string) => callback(obj, keys);

db.path("./dir").fetch("mm").mapper((obj, keys) => { if(obj[keys] === 12) delete obj[keys] })

Result exemplo

{
  "test": 120,
  "mm": 60,
  "obj": {
    "mj": 0,
    "se": {
      "test": [
        {
          "mm": 10
        },
        {
          "mm": 11
        },
        {} // deleted
      ]
    }
  }
}

HOSTVIEW

To create a server with your "json file"

  • The "port" method is responsible for listing the server port.
  • The "update" method allows when the page is reloaded to update the information based on the "json file".
  • The "update" is optional and the default is "false"
db.path("./test.json").hostView({ port: 3000, update: true })

"hostView" has two methods, "on" and "start".

The "on" method is responsible for performing manipulations on the "read" and "request" events.

The "read" event is fired when the server is started, the "request" event is fired every time the server is accessed, such as when reloading the page or opening it for the first time.

Object made available by the event "read"

{
  "settings": {
    "port": Number,
    "update": Boolean
  }
}
  • port - Refers to the door chosen by you to the server.
  • update - To indicate if the server will be updating the page.

Object made available by the event "request"

{
  "url": String,
  "method": String,
  "ip": Number 
}
  • url - The route used to display "JSON".
  • method - The requisition method that was made on the server.
  • ip - It is the "IP" of the device that accessed the server on the "json file" route.
let num = 1;
const server = db.path("./test.json").hostView({ port: 3000, update: true })

server.on("read", (req) => {
  console.log(`Project started.\nPort: ${req.settings.port}\n`);
})

server.on("request", (req) => {
  console.log(`Project acessed: ${num} time\nURL acess: "${req.url}"\nAccessed by IP: ${req.ip}`);
  num++
})

server.start() // Server initialization

Result exemplo

// Your browser | example URL: http://localhost:3000/data 

1 {
2   "KeyExample1": 8,
3 }

// Your logs | example

Project started.
Port: 3000

Project acessed: 1 time
URL acess: "/data"
Accessed by IP: 192.168.0.17

To connect other devices to the server, simply be on the same network and use the "IPv4" of your project machine along with the "port" listed.

Tip for "Windows" users:

  • Open "cmd" and run "ipconfig".
  • Look for the numbers in the line that says "IPv4 Address".

Result exemplo

# URL in your browser | example

http://192.168.0.22:3000