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

jsonl-db

v1.0.1

Published

About json-file is a Node.js library for working with JSON files in the JSON Lines format. It provides a simple API for adding, updating, deleting, and querying data in JSONL files.

Downloads

431

Readme

jsonl-db

jsonl-db is an open source library for working with JSON files in the JSON Lines format (JSONL), a convenient and compact representation of structured data.

This library provides a simple API for adding, updating, deleting and querying data in JSONL files. It also offers methods for reading the files in various ways, such as reading by batch or finding specific data elements.

Requirements

  • Node.js v14 or higher

Installation

Install jsonl-db with your favorite Node package manager like npm / pnpm / yarn.

npm install jsonl-db

Usage

To use jsonl-db in your project, import it as follows:

import jsonlFile from "jsonl-db";

Creating a JSONL file

The jsonlFile function creates a new JSONL file. It takes one argument, the path to the file to create or operate on.

const myJsonlFile = jsonlFile("./data.jsonl");

Adding data

myJsonlFile.add({ name: "John", age: 27 });

The add method adds a single JSON object to the end of the file.

myJsonlFile.addMany([
  { name: "John", age: 27 },
  { name: "Jane", age: 31 },
]);

The addMany method adds an array of JSON objects to the end of the file.

Reading data

myJsonlFile.read((line) => console.log(line));

The read method reads the file line-by-line and executes a callback function for each provided line. In this example, it simply logs each JSON object to the console.

myJsonlFile.readByBatch((batch) => console.log(batch), 500);

The readByBatch method reads the file in batches of a specified size, and executes a callback function for each batch. In this example, batches of two JSON objects are logged to the console.

const firstLine = await myJsonlFile.first();

The first method returns the first JSON object in the file.

const lastLine = await myJsonlFile.last();

The last method returns the last JSON object in the file.

const match = await myJsonlFile.findWhere("name", "John");

The findWhere method searches the file for the first JSON object where the specified attribute equals the specified value. In this example, the JSON object with "name": "John" is returned.

const matches = await myJsonlFile.findMatch(async (line) => {
  return line.age > 30;
});

The findMatch method searches the file for all JSON objects that match a specified condition. In this example, all JSON objects with "age" greater than 30 are returned.

const count = await myJsonlFile.count();

The count method returns the number of JSON objects in the file.

const count = await myJsonlFile.countMatch(async (line) => {
  return line.age > 30;
});

The countMatch method returns the number of JSON objects in the file that match a specified condition.

Updating data

myJsonlFile.updateWhere("name", "John", async (line) => {
  line.age = 28;
  return line;
});

The updateWhere method searches the file for JSON objects where the specified attribute equals the specified value, and updates those objects using a provided update function.

myJsonlFile.updateMatch(
  async (line) => {
    return line.age > 30;
  },
  async (line) => {
    line.isOlderThan30 = true;
    return line;
  }
);

The updateMatch method searches the file for all JSON objects that match a specified condition, and updates those objects using a provided update function.

Deleting data

myJsonlFile.deleteWhere("name", "John");

The deleteWhere method searches the file for JSON objects where the specified attribute equals the specified value, and removes those objects from the file.

myJsonlFile.deleteMatch(async (line) => {
  return line.age > 30;
});

The deleteMatch method searches the file for all JSON objects that match a specified condition, and removes those objects from the file.

myJsonlFile.deleteFile();

The deleteFile method removes the entire file.

Here's an example of what a contributing section might look like in a jsonl-db README:

Contributing

Contributions are welcome and encouraged! To contribute to jsonl-db, please follow these steps:

  1. Fork the repository.
  2. Create a new feature branch: git checkout -b my-feature-branch
  3. Make your changes and write tests for them.
  4. Ensure that all tests pass: npm test
  5. Commit your changes with a descriptive commit message.
  6. Push your feature branch to your fork: git push origin my-feature-branch
  7. Submit a pull request

Please ensure that any contributions you make adhere to the project's coding standards and are properly documented. If you're unsure about anything, don't hesitate to ask for clarification or guidance.

License

jsonl-db is licensed under the MIT License.