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

instant.db

v2.0.0

Published

A very basic simple and easy to use database with json files!

Downloads

6

Readme

Instant.db

Instant.db is used to create database using json files.

Quick Example

const { Database } = require('instant.db');

const db = new Database('./some_file.json'); // If file name not provided, this will use database.json as file

db.set('foo', 'bar'); // Sets value for the key
db.get('foo'); // Returns bar
db.exists('foo'); // Returns if the id is entried, returns true
db.typeof('foo'); // Returns string. The typeof data!

db.all(); // Returns [{ ID: 'foo', data: 'bar' }]
db.raw(); // Returns raw data { foo: 'bar' }
db.random(); // Returns a random dataset!
db.random(4); // Returns array of 4 random datasets!

db.keys; // Getter which returns the array of id's ['foo']
db.values; // Getter which returns the array of data's ['bar']
db.cache; // Returns the map key value!
db.entries; // Returns the number of entries!

db.set('foo', ['foo']);
db.push('foo', 'bar', 'baz'); // -> ['foo', 'bar', 'baz'];
db.pull('foo', 'foo', 'bar'); // -> ['baz']

db.set('foo', 0);
db.math('foo', '+', 100); // -> 100
db.math('foo', '-', 100); // -> 0
db.math('foo', '*', 100); // -> 0
db.math('foo', '/', 100); // -> 0
db.math('foo', '**', 100); // -> 0
db.add('foo', 100); // -> 100
db.subtract('foo', 100); // -> 0

db.delete('foo'); // Will delete the foo value!
db.filter((data, id) => id == 'foo'); // Similar to Array.filter but will clear from the database too!
db.clear(); // Clears everything

Typescript

Has typescript support too!

import Database from 'instant.db';

const db = new Database<string>();

db.set('foo', 'bar');
console.log(db.get('foo'));

Database actions

DatabaseAction is a class which is a fake database where you can execute your methods without affecting the main database itself and save it later whenever you want!

const action = db.action();

action.set('foo', 'bar'); // You are setting the value foo as bar in the cache!
console.log(db.get('foo')); // Prints "null" as its still on cache

action.save(); // Now it saves the data from cache to the database
console.log(db.get('foo')); // Prints "bar"!

action.undo(); // Back to the state of database when th action was initiated!
console.log(db.get('foo')); // Returns "null"!

Unlike Database class, DatabaseAction has less methods!

action.set('foo', 'bar'); // Sets value for the key
action.get('foo'); // Returns bar

action.all(); // Returns [{ ID: 'foo', data: 'bar' }]
action.raw(); // Returns raw data { foo: 'bar' }

action.keys; // Getter which returns the array of id's ['foo']
action.values; // Getter which returns the array of data's ['bar']
action.cache; // Returns the map key value!
action.entries; // Returns the number of entries!

action.set('foo', ['foo']);
action.push('foo', 'bar', 'baz'); // -> ['foo', 'bar', 'baz'];
action.pull('foo', 'foo', 'bar'); // -> ['baz']

action.set('foo', 0);
action.math('foo', '+', 100); // -> 100
action.math('foo', '-', 100); // -> 0
action.math('foo', '*', 100); // -> 0
action.math('foo', '/', 100); // -> 0
action.math('foo', '**', 100); // -> 0

action.delete('foo'); // Will delete the foo value!
action.filter((data, id) => id == 'foo'); // Similar to Array.filter but will clear from the database too!

Documents

Documents are nothing but the same database but storing objects instead of just key:value type. For example view the following example:

Database

How normal Database class stores!

{ "foo": "bar" }

Document

How normal Document class stores!

[
    {
        "custom_field": "foo",
        "custom_field_2": "bar"
    }
]

Here is an example of using the Document class!

const { Document } = require('instant.db');
const doc = new Document('./document.json');

doc.insert({
    custom_field: "foo",
    custom_field_2: "bar"
}) // Insert a new object to the db

doc.findOne({
    custom_field: "foo"
}) // Will return `{ custom_field: "foo", custom_field_2: "bar" }`

Document findOne uses a Object query or a callback here is an example below!

doc.insert({ foo: 'bar' });

doc.findOne({ foo: 'bar', bar: 'baz' }); // Will return `{ foo: 'bar' }` even though `bar` field does not exists in it as it finds any object in the document which has the same value in the field!
doc.findOne(obj => obj.foo == 'bar'); // You can also use callbacks to use `findOne` which acts same as Array.prototype.find!

Methods of the Document class!

doc.size // Returns the number of entries in the document!

doc.insert({ foo: 'bar' }); // Inserts object to the document
doc.raw(); // Returns the raw data of the file!
doc.getAll(); // Aliases for get raw method!

doc.findOne({ foo: 'bar', bar: 'baz' }); // Searches a dataset in the document with query or callback
doc.findMany({ foo: 'bar', bar: 'baz' }); // Returns an array of datasets in the document which maches the query or callback

doc.deleteOne({ foo: 'bar', bar: 'baz' }); // Searches a dataset in the document with query or callback and deletes it from the document!
doc.deleteMany({ foo: 'bar', bar: 'baz' }); // Searches all the datasets in the document which maches the query or callback and deletes it

doc.clear(); // Clears all the data in the document!

Support

Discord Server: https://discord.gg/FrduEZd GitHub: https://github.com/Scientific-Guy/instant.db

If any doubts, join our discord server and ask your doubts or just make an issues in our github repo!