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

ghetto-db

v1.0.2

Published

Quick and dirty JSON database that writes directly to local machine

Downloads

1,795

Readme

Ghetto DB

Interface to store and read JSON files directly on the local machine. The databse will have a root directory and store each record as an individual JSON file at the top level of that directory.

  • ghetto-db-root
    • record1.json
    • record2.json
    • record3.json

Setup

npm i ghetto-db

Initialize

import { GhettoDB } from 'ghetto-db';
// const { GhettoDB } = require('ghetto-db') // Also supports CJS

const db = new GhettoDB('./db/dev'); // The path to the directory that will contain all the JSON files for this database

Constructor

The constructor takes in a path as a parameter, which will serve as the root for all records. The path will be relative to where the process is running. By default the path will be set to './ghetto-db-root'

new GhettoDB('./path');

Methods

Hood(recordName[,...entries])

  • recordName {String} name of the record and the file ending in .json
  • entries {Object} add entries to database using separate arguments
  • Returns: {HoodInstance} an interface to interact with a single array stored in a json file

Creates an interface to interact with a single record (json file).

import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
const brooklyn = db.Hood('brooklyn', { foo: 'bar' });

Hood.add(...entries)

  • entries {Object} add entries to database using separate arguments
  • Returns: {Promise} Fulfills with all entries in the record.
import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
const bronx = db.Hood('bronx');
const entries = await bronx.add({foo: 'bar'}, {foo2, 'bar2'});
// [ {idx: 0, foo: 'bar'}, {idx: 1, foo2: 'bar2'} ]

Hood.read(filter[,method])

  • filter {Function|Object} argument in function is the entry as it appears in the database and should return a boolean where true returns the entry. Objects will match where keys and values correspond to keys and values of entries in the database.
  • method {string} Default 'filter'
  • Returns: {Promise} Fulfills with an array of entries if method is set to 'filter' or a single entry if method is set to 'find'.
import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
const soHo = db.Hood('soHo');
await soHo.add({foo: 'bar'}, {foo2, 'bar2'}, {foo: 'bar'});

const entries = await soHo.read()
// [ {idx: 0, foo: 'bar'}, {idx: 1, foo2: 'bar2'}, {idx: 2, foo: 'bar'} ]

const entriesWhereFooIsBar = await soHo.read(({foo}) => foo === 'bar');
// // [ {idx: 0, foo: 'bar'}, {idx: 2, foo: 'bar'} ]

const entriesWhereFooIsBar2 = await soHo.read({foo: 'bar'});
// // [ {idx: 0, foo: 'bar'}, {idx: 2, foo: 'bar'} ]

Hood.readOne(filter)

  • filter {Function|Object} see Hood.read
  • Returns: {Promise} fulfills with a single entry

Hood.readIdx(idx)

  • idx {Number}
  • Returns: {Promise} fulfills with a single entry

Hood.update(filter, updater)

  • filter {Function|Object} argument in function is the entry as it appears in the database and should return a boolean where true returns the entry. Objects will match where keys and values correspond to keys and values of entries in the database.
  • updater {Function} takes in an entry from the database and returns the updated entry. Modifications of idx are ignored.
  • Returns: {Promise} Fulfills with an array of entries that are updated.

Updates entries in the record.

import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
const soHo = db.Hood('soHo');
await soHo.add({foo: 'bar'}, {foo2, 'bar2'}, {foo: 'bar'});

const updatedEntries = await soHo.update({foo: 'bar'}, ({foo}) => ({bar: foo}))
// [ {idx: 0, bar: 'bar'}, {idx: 2, bar: 'bar'} ]

const allEntries = await soHo.read();
// [ {idx: 0, bar: 'bar'}, {idx: 1, foo2: 'bar2'}, {idx: 2, bar: 'bar'} ]

Hood.updateOne(filter, updater)

  • filter {Function|Object} see Hood.update
  • updater {Function} see Hood.update
  • Returns: {Promise} fulfills with the single updated entry or null if nothing matched

Hood.updateIdx(idx, updater)

  • idx {Number} the corresponding idx
  • updater {Function} see Hood.update
  • Returns: {Promise} fulfills with the single updated entry or null if nothing matched

Hood.delete(filter)

  • filter {Function|Object} argument in function is the entry as it appears in the database and should return a boolean where true returns the entry. Objects will match where keys and values correspond to keys and values of entries in the database.
  • Returns: {Promise} Fulfills with an array of now deleted entries.
import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
const queens = db.Hood('queens');
await queens.add({foo: 'bar'}, {foo2, 'bar2'}, {foo: 'bar'});

const deletedEntries = await queens.delete({foo: 'bar'})
// [ {idx: 0, foo: 'bar'}, {idx: 2, foo: 'bar'} ]

const remainingEntries = await queens.read()
// [ {idx: 1, foo2: 'bar2'} ]

Hood.deleteOne(filter)

  • filter {Function|Object} see Hood.delete
  • Returns: {Promise} fulfills with the single deleted entry or null if nothing matched

Hood.deleteIdx(idx)

  • idx {Number} the corresponding idx
  • Returns: {Promise} fulfills with the single deleted entry or null if nothing matched

storeRecord(recordName, data)

Creates a JSON file using the recordName (string) and data (any primitive type). If successful, returns the parsed data stored in the record.

This method will override any existing data in the record. To update a record and have access to the existing data see updateRecord.

import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
db.storeRecord('users', []).then(() => console.log('created users record'));

readRecord(recordName)

Returns parsed data from a JSON file using the recordName (string).

const { GhettoDB }  = require('ghetto-db');

const db = new GhettoDB('./db/dev');
db.readRecord('users').then(() => );

updateRecord(recordName, callback || data)

Updates a record using the recordName (string) and either a callback (function) or data (any primitive type). The callback function takes in the existing data as a parameter. If successful, returns the data stored in the record. This method will throw an error if the record does not already exist.

const { GhettoDB }  = require('ghetto-db');

const db = new GhettoDB('./db/dev');

    // assume 'users' record is an empty array
  db.updateRecord('users', (users) => users.push('John Doe'))
    .then((data) => {
        console.log(data),
        // will console.log [ "John Doe" ]
    });

destroyRecord(recordName)

Destroys a record by deleting the JSON file using the recordName (string). Returns undefined.

import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
db.destroyRecord('users').then(() => console.log('Record was deleted'));

getRecords()

Returns an array of all the record names.

import { GhettoDB } from 'ghetto-db';

const db = new GhettoDB('./db/dev');
db.getRecords().then((recordNames) => console.log(recordNames));