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

@kreisler/js-jsondb

v2.0.0

Published

A javascript Class that reads JSON file as a database.

Downloads

17

Readme

js-jsondb

A Javascript Class that reads JSON file as a database. Use for sample DBs.

Usage

Install package

npm i @kreisler/js-jsondb

Initialize

import JsonDb from "@kreisler/js-jsondb";

or

const JsonDb = require("@kreisler/js-jsondb");
// Crea una instancia de la clase JsonDb y especifica la ruta de la carpeta
const json_db = new JsonDb("json_files"); // Or passing the directory of your json files with no trailing slash, default is the current directory. E.g.  new JsonDb( '/var/www/html/json_files' )

Inserting

Insert into your new JSON file. Using users.json as example here

json_db.insert("users", { id: 1, name: "Juan" });
json_db.insert("users", { id: 2, name: "Ana" });
json_db.insert("users", { id: 3, name: "Mario" });

or

json_db.insert("users", [
  { id: 1, name: "Juan" },
  { id: 2, name: "Ana" },
  { id: 3, name: "Mario" },
]);

Get

Get back data

All data:
// Obtiene todos los objetos del archivo "users.json"
const allUsers = json_db.select("users");
console.log(allUsers);

/*
[
  { id: 1, name: 'Juan' },
  { id: 2, name: 'Ana' },
  { id: 3, name: 'Mario' }
]
*/

Find data:

// Obtiene los objetos del archivo "users.json" cuyo atributo "name" es "Juan"
// Se puede usar && y/o || para ser más especifico
const juanUsers = json_db.select("users", (item) => item.name === "Juan");
console.log(juanUsers);

/*
[ { id: 1, name: 'Juan' } ]
*/
// Obtiene los objetos del archivo "users.json" cuyo atributo "id" es mayor a 1
const usersWithIdGreaterThanOne = json_db.select(
  "users",
  (item) => item.id > 1
);
console.log(usersWithIdGreaterThanOne);
/*
[ { id: 2, name: 'Ana' }, { id: 3, name: 'Mario' } ]
*/

Updating

You can also update same JSON file with these methods

// Actualiza el atributo "name" de los objetos del archivo "users.json" cuyo atributo "id" es 1
json_db.update("users", (element) => element.id === 1, {
  name: "Juan Lopez",
});
/*
[
  { id: 1, name: 'Juan Lopez' },
  { id: 2, name: 'Ana' },
  { id: 3, name: 'Mario' }
]
*/

or

json_db.update(
  "users",
  (element) => element.id > 1,
  (element) => ({
    name: `${element.name} :)`,
  })
);
/*
[
  { "id": 1, "name": "Juan" },
  { "id": 2, "name": "Ana :)" },
  { "id": 3, "name": "Mario :)" }
]
*/

All rows in the JSON file can also be modified

// Actualiza el atributo "name" de todos los objetos del archivo "users.json"
json_db.update("users", null, {
  name: "Juan Perez",
});

/*
[
  { id: 1, name: 'Juan Perez' },
  { id: 2, name: 'Juan Perez' },
  { id: 3, name: 'Juan Perez' }
]
*/

Deleting Row

// Eliminamos todos los usuarios cuyo nombre empiece con la letra 'J'
json_db.delete("users", ({ name }) => name.startsWith("J"));
/*
[ { id: 2, name: 'Ana' }, { id: 3, name: 'Mario' } ]
*/

Delete file:

json_db.delete("users");