Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
ghetto-db
Advanced tools
Readme
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.
npm i ghetto-db
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
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');
Hood(recordName[,...entries])
recordName
{String} name of the record and the file ending in .jsonentries
{Object} add entries to database using separate argumentsCreates 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 argumentsimport { 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'
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
Hood.readIdx(idx)
idx
{Number}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.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
null
if nothing matchedHood.updateIdx(idx, updater)
idx
{Number} the corresponding idx
updater
{Function} see Hood.update
null
if nothing matchedHood.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.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
null
if nothing matchedHood.deleteIdx(idx)
idx
{Number} the corresponding idx
null
if nothing matchedstoreRecord(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));
FAQs
Quick and dirty JSON database that writes directly to local machine
The npm package ghetto-db receives a total of 265 weekly downloads. As such, ghetto-db popularity was classified as not popular.
We found that ghetto-db demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.