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

jsondbkit

v1.0.2

Published

The **jsonDB** library provides an easy way to work with JSON data as a database.

Downloads

6

Readme

What is jsonDBKit?

The jsonDBKit library provides an easy way to work with JSON data as a database.

Why jsonDBKit?

  • Easy to use.
  • An easy way to implement a simple database into your project.
  • The JSON file serves as the database.
  • Contains full CRUD methoods.
  • Works perfectly in combination with:
    • Express.js
    • HTTP module
  • All changes are immediately visible.
  • Open source.

Methoods:

  •  jsonDB.connectDB(path: string): Promise<void>
    • Method for connecting to the database. Accepts the path to the JSON file. Upon successful connection, it validates the file structure and creates a unique identifier (jsonID) generated with uuid for the first object in the collections if it doesn't exist yet.
  •  jsonDB.jsonGetAllCollections(): Promise<object>
    • Method to retrieve all collections from the database. Returns an object where the keys are the collection names, and the values are arrays of objects from each collection.
  •  jsonDB.jsonGetCollection(collectionName: string): Promise<object[]>
    • Method to retrieve a specific collection by its name. Accepts the collection name as an argument and returns an array of objects from that collection.
  •  jsonDB.jsonGetOne(collectionName: string, id: string): Promise<object | undefined>
    • Method to retrieve a single object from a collection by its unique identifier. Accepts the collection name and object identifier as arguments. Returns the object or undefined if the object is not found.
  •  jsonDB.jsonAdd(collectionName: string, content: object): Promise<object>
    • Method to add a new object to a collection. Accepts the collection name and the object to add. Returns the added object with a unique identifier assigned.
  •  jsonDB.jsonFoundOneAndUpdate(collectionName: string, id: string, obj: object): Promise<object | undefined>
    • Method to update an object in a collection by its identifier. Accepts the collection name, object identifier, and object with updated data. Returns the updated object or undefined if the object is not found.
  •  jsonDB.jsonDelete(collectionName: string, id: string): Promise<number | undefined> Promise<object | undefined>
    • Method to delete an object from a collection by its identifier. Accepts the collection name and object identifier. Returns the index of the deleted object or undefined if the object is not found.

First steps (My example)

To install the jsondbkit library, run the following command:

npm install --save jsondbkit
  1. Create folder for your database:

  2. Create JSON file inside your folder:

  3. Important!:

    To work successfully with the database, you need to create a schema of your database inside your JSON file.

    Example of your schema:

    {
        "users": [
            {
                "fullname": "John Doe",
                "age":  30,
                "phone": "xxxxxxxxxx"
            }
        ],
        "products": [
            {
                "productName": "Phone"
            }
        ]
    }

    Important Notes!:

    • The object must contain a field - collection (in this example, collections are represented by "products" and "users"). There can be as many collections as desired (the only drawback is that they need to be entered manually at the moment).

    • The collection must be an array.

    • The array must contain objects.

      Note: For proper operation, each collection must contain exactly one object before working with the database

    • The object should not contain the jsonID property. After running, unique identifiers will be added to each object in each collection in the database.

    • After following all instructions and running the application, your JSON file will be automatically overwritten. If you use Nodemon, all subsequent changes (additions, deletions, edits) will occur automatically, overwriting your file with new data, while preserving the entire order.

  4. Create Express Server:

    const express = require('express');
    const app = express();
    const usersRouter = require('./routes/usersRouter');
    const connectToJsonDB = require('./configs/connectToJsonDB');
    const PORT = 3000;
    
    //configs
    connectToJsonDB();
    
    //MIDDLEWARES
    app.use(express.json());
    
    //ROUTES
    app.use('/api/users', usersRouter);
    
    //LISTEN
    app.listen(PORT, () => {
        console.log(`Server is running: http://localhost:${PORT}`);
    });
  5. Create your connect methood:

    I moved the function to a separate file:

    const jsonDB = require('jsondbkit');
    
    
    const connectToJsonDB = () => {
        jsonDB.connectDB('./DB/test.json')
         .then(() => console.log('DB is OK'))
            .catch((err) => console.log(err));
    }
    
    module.exports = connectToJsonDB;
  6. Create routes folder:

    In this example i will show only on "users" collection(for other collections, everything is absolutely identical)

    All routes for users:

     const express = require('express');
     const router = express.Router();
     const usersDLL = require('../DLL/usersDLL');
    
     router.get('/', async(req, res) => {
         const response = await usersDLL.getAllUsers();
    
         res.send(response);
     });
    
     router.get('/:id', async(req, res) => {
         const id = req.params.id;
         const response = await usersDLL.getOneUser(id);
    
         res.send(response);
     });
    
     router.post('/', async(req, res) => {
         const obj = req.body;
         const response = await usersDLL.addNewUser(obj);
            
         res.send(response);
     });
    
     router.put('/:id', async(req, res) => {
         const id = req.params.id;
         const obj = req.body;
         const response = await usersDLL.updateUser(id, obj);
    
         res.send(response);
     });
    
     router.delete('/:id', async(req, res) => {
         const id = req.params.id;
         const response = await usersDLL.deleteUser(id);
    
         res.send(response);
     });
    
     module.exports = router;
  7. Let's move all CRUD methods to a separate folder

     const jsonDB = require('jsondbkit');
    
     const getAllUsers = async () => {
         return jsonDB.jsonGetCollection("users");
     }
    
     const getOneUser = async (id) => {
         return await jsonDB.jsonGetOne("users", id);
     }
    
     const addNewUser = async (obj) => {
         await jsonDB.jsonAdd("users", obj);
         return 'User addded succesfully';
     }
    
     const updateUser = async (id, obj) => {
         await jsonDB.jsonFoundOneAndUpdate("users", id, obj);
         return 'User updated succesfully';
     }
    
     const deleteUser = async (id) => {
         let response = await jsonDB.jsonDelete("users", id);
         if (!response) {
             return 'Not found';
         }
         return 'User deleted succesfully';
     }
    
     module.exports = {
         getAllUsers,
         getOneUser,
         addNewUser,
         updateUser,
         deleteUser
     }
  8. Let's check with Postman:

    • getAllUsers

    • getOneUser

    • addNewUser

      Result in json:

    • updateUser

      Result in json:

    • deleteUser

      Result in json: