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

gptdb

v0.0.4

Published

A simple in-memory JSON database for Node.js with built-in disk persistence and change watchers.

Downloads

4

Readme

GPTDB

A simple in-memory JSON database for Node.js with built-in disk persistence and change watchers.

This library was generated by ChatGPT and is provided as-is.

Features

  • In-memory database for fast access and manipulation
  • Persists data to disk in JSON format
  • Asynchronous read and write methods
  • Change watchers for real-time updates
  • Supports nested properties using dot notation
  • Provides basic find and get methods

Installation

npm install gptdb

Usage

import { GPTDB } from 'gptdb';

(async () => {
  const db = GPTDB('mydb.json', { value: 'test', something: { name: 'chat', more: { deep: 'value' } }, bookings: [] });
  await db.read();
  console.log(db.get('something.name'));
  db.data.places = [];
  db.data.places.push('Cluj');
  await db.write();

  db.watch('something.name', (oldValue, newValue) => {
    console.log('value changed from', oldValue, 'to', newValue);
  });

  const watcherRef = db.watch('something.more.deep', (oldValue, newValue) => {
    console.log('deep value changed from', oldValue, 'to', newValue);
  });

  db.data.something.name = 'amazing'; // value changed from chat to amazing
  db.data.something.more.deep = 'new deep value'; // deep value changed from value to new deep value
  db.data.something.more.deep = 'yet again';

  watcherRef.remove(); // remove watcher for something.more.deep
  db.data.something.more.deep = 'another one'; // callback not triggered
  await db.write();

  db.data.users = [
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
    { id: 3, name: 'Alise' },
    { id: 4, name: 'Alice' },
  ];

  db.data.bookings = [
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
    { id: 3, name: 'Alise' },
    { id: 4, name: 'Alice' },
  ];

  // Find all users with the name 'Alice'
  const results = db.find((value, path) => {
    return value?.name === 'Alice';
  });

  console.log(results.length); // 4 (2 users, 2 bookings)

  // todo: make this trigger with arrays
  db.watch('db.data.bookings', (oldValue, newValue) => {
    console.log('bookings changed from', oldValue, 'to', newValue);
  });

  db.data.bookings = [...db.data.bookings, { id: 5, name: 'Ralph' }]
})();

API

GPTDB(filePath[, initialData])

Creates a new GPTDB instance.

  • filePath (string): Path to the JSON file where the data will be persisted.
  • initialData (object, optional): Initial data for the database. Defaults to an empty object.

async read()

Reads the data from the JSON file and updates the in-memory data.

async write()

Writes the in-memory data to the JSON file.

find(predicate[, obj, path])

Finds data based on a predicate function.

  • predicate (function): Function that returns true for matched data.
  • obj (object, optional): Object to search in. Defaults to the in-memory data.
  • path (array, optional): Array representing the current path in the data tree. Defaults to an empty array.

get(path)

Gets a specific value from the in-memory data using dot notation.

  • path (string): Path to the value in the data.

watch(path, callback)

Watches for changes on a specific path and calls the callback when the value changes.

  • path (string): Path to the value in the data.
  • callback (function): Function to call when the value changes.

Returns an object with a remove method that can be used to stop watching for changes.

License

MIT License