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

lowly-db

v1.0.6

Published

A lightweight file-based JSON database with schema validation and advanced features.

Downloads

518

Readme

LowlyDB

NPM Version NPM Downloads NPM License

LowlyDB is a lightweight, file-based NoSQL database for quick data storage and retrieval in JSON format. It offers basic CRUD operations, schema validation, and transactional support.


Features

  • CRUD Operations: Create, Read, Update, and Delete records easily.
  • Schema Validation: Validate data records against a schema.
  • Auto-Increment ID: Automatically generates unique IDs for new records.
  • Transactional Support: Perform multiple actions atomically.
  • File-Based Storage: Stores data persistently in a JSON file.
  • Simple API: Easy-to-use methods for interacting with the database.

Installation

npm install lowly-db

Usage

Initialization

import LowlyDB from "lowly-db";

const db = new LowlyDB("myDatabase.json", {
  name: "string",
  age: "number"
});

API Methods

constructor(filePath, schema = null)

  • Parameters:
    • filePath (string): Path to the JSON file for storage.
    • schema (object): Optional schema for data validation.
  • Description: Initializes the database with the specified file and schema.

create(record)

  • Parameters:
    • record (object): The record to create.
  • Returns: The created record.
  • Description: Adds a new record to the database. If no id is provided, it auto-generates one.
  • Example:
    db.create({ name: "Alice", age: 25 });

read(query = {})

  • Parameters:
    • query (object): The query to filter records.
  • Returns: An array of matching records.
  • Description: Retrieves records matching the query. Returns all records if the query is empty.
  • Example:
    db.read({ age: 25 });

update(query, updates)

  • Parameters:
    • query (object): The query to find records to update.
    • updates (object): The fields to update.
  • Description: Updates all records matching the query.
  • Example:
    db.update({ name: "Alice" }, { age: 26 });

delete(query)

  • Parameters:
    • query (object): The query to find records to delete.
  • Description: Deletes all records matching the query.
  • Example:
    db.delete({ age: 25 });

findById(id)

  • Parameters:
    • id (number|string): The record's ID.
  • Returns: The matching record or null if not found.
  • Description: Finds a record by its unique ID.
  • Example:
    db.findById(1);

deleteById(id)

  • Parameters:
    • id (number|string): The record's ID.
  • Description: Deletes a record by its unique ID.
  • Example:
    db.deleteById(1);

count(query = {})

  • Parameters:
    • query (object): The query to filter records.
  • Returns: The number of matching records.
  • Description: Counts the records matching the query.
  • Example:
    db.count({ age: 25 });

clear()

  • Description: Deletes all records in the database.
  • Example:
    db.clear();

transaction(actions)

  • Parameters:
    • actions (function[]): An array of functions that take the database instance as a parameter.
  • Description: Performs multiple actions atomically. Rolls back changes if an error occurs.
  • Example:
    db.transaction([
      db => db.create({ name: "John", age: 30 }),
      db => db.update({ name: "John" }, { age: 31 })
    ]);

Error Handling

  • Schema Validation: Throws an error if a record doesn't match the schema.
  • Duplicate IDs: Throws an error if an attempt is made to create a record with an existing ID.

Example

import LowlyDB from "lowly-db";

const db = new LowlyDB("users.json", { name: "string", age: "number" });

// Create a new record
db.create({ name: "Alice", age: 25 });

// Read records
const users = db.read({ age: 25 });
console.log(users);

// Update a record
db.update({ name: "Alice" }, { age: 26 });

// Delete a record
db.delete({ age: 26 });

License

This project is licensed under the MIT License.


Acknowledgements

This library was inspired by the need for lightweight, file-based data storage solutions for simple projects.