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

v2.1.0

Published

JsonFlexDB is a versatile Node.js module that provides a flexible and lightweight solution for basic CRUD operations using a JSON file as a data store. With JsonFlexDB, you can easily manage and query JSON-based data with the simplicity of a document-orie

Downloads

10

Readme

JsonFlexDB

JsonFlexDB is a lightweight and versatile JSON-based database module for Node.js. It provides a simple yet powerful solution for handling data persistence in your applications. JsonFlexDB is suitable for a wide range of projects, from small-scale applications to large-scale systems.

Features

  • Ease of Use: JsonFlexDB offers a simple and intuitive API, making it easy for developers to perform CRUD operations on JSON-based data.

  • Flexible Queries: Perform queries on your data using a flexible and powerful query syntax. JsonFlexDB supports both indexed and non-indexed queries for efficient data retrieval.

  • Async/Await Support: Leverage the power of asynchronous programming with seamless async/await support for all database operations.

  • Visualization: Visualize your data directly in the console using the built-in visualize method, making it easier to understand the structure of your stored data.

Installation

To get started with JsonFlexDB, install it using npm:

npm install json-flex-db

Usage

const JsonFlexDB = require("json-flex-db");

// New Feature
// Optional: Define a schema for your data
const schema = {
  name: { type: "string", required: true },
  age: { type: "number" },
  email: { type: "string", validate: (value) => /\S+@\S+\.\S+/.test(value) },
};

// Initialize JsonFlexDB with the file path for your JSON data
const db = new JsonFlexDB("data.json", schema);

// Example: Insert a new document
const document1 = {
  _id: "1",
  name: "John Doe",
  age: 30,
  email: "[email protected]",
};
const document2 = {
  _id: "2",
  name: "Sam Wilson",
  age: 32,
  email: "[email protected]",
};
(async () => {
  // Example: Insert Document1 and Document2
  await db.insert(document1);
  await db.insert(document2);

  // Example: Find documents based on a query
  const results = await db.find({ age: 30 });
  console.log("Results based on age:", results);

  // Example: Update documents based on a query
  const updateQuery = { age: 30 };
  const updates = { age: 31 };
  const numUpdated = await db.update(updateQuery, updates);
  console.log(`Updated ${numUpdated} documents`);

  // Example: Remove documents based on a query
  const removeQuery = { age: 31 };
  const numRemoved = await db.remove(removeQuery);
  console.log(`Removed ${numRemoved} documents`);

  // Example: Visualize DB in a Console table
  db.visualize();
})();

For more detailed usage instructions, consult the Documentation section below.

Documentation

JsonFlexDB(filePath, schema = {})

Creates a new instance of JsonFlexDB.

  • filePath (string): The file path where the JSON database is stored.
  • schema (object, optional): The schema for validation (default is an empty object).

load()

Loads data from the JSON file.

save()

Saves data to the JSON file.

ensureLoaded()

Ensures that data is loaded from the file.

createIndex(indexName)

Creates an index for optimizing queries.

  • indexName (string): The name of the index.

findOne(query)

Finds a single document based on a query.

  • query (object): The query object.

Returns: A promise that resolves to the matching document or null if not found.

getAutoIncrementId()

Gets the next available auto-incremented ID.

Returns: A promise that resolves to the next available ID.

find(query, returnKeys = true)

Finds matching elements in the data based on the provided query.

  • query (object): The query object used to filter the data.
  • returnKeys (boolean): Indicates whether to return the keys of the matching elements.

Returns: A promise that resolves to an array of matching documents.

insert(document)

Inserts a document into the database.

  • document (object): The document to insert.

Returns: A promise that resolves to the key of the inserted document.

update(query, updates)

Updates documents based on a query.

  • query (object): The query object.
  • updates (object): The updates to apply.

Returns: A promise that resolves to the number of updated documents.

remove(query)

Removes documents based on a query.

  • query (object): The query object.

Returns: A promise that resolves to the number of removed documents.

getAll()

Gets all documents in the database.

Returns: An object representing all documents in the database.

visualize()

Visualizes the data in the console using console.table.

Changelog

Refer to the changelog for a detailed history of changes made to JsonFlexDB.

Contributing

Contributions are welcome!

License

JsonFlexDB is MIT licensed.