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

@molo_7/db.json

v1.1.0

Published

a light module to store data in easy way for beginners in `JSON` format

Downloads

11

Readme

Table Of Content


What's db.json

a light module to store data in easy way for beginners in JSON format, fast, simple and powerfull.


Features

  • Easy to use
  • Supports all datatypes < string | number | bigint | boolean | symbol | undefined | object | function | array >
  • Dot notation support
  • Key value based

Get Started

Installation

npm install db.json

Import json.db

const Database = require("db.json"); // Constructor
const db = new Database("test.json"); // create new json storage file

new Database(filename)

  • passing file name or file path is required
  • file format must be json !
  • If the file dosen't exist, it will create it

Multiple Files

easly create multiple storage files

const Database = require("db.json");
const users = new Database("users.json");
const guilds = new Database("guilds.json");

users.set("user_1", {
  id: 1,
  name: "Jhon Doe",
  ownGuilds: [],
});

let userOne = users.get("user_1");
// -> { id: 1, name: "Jhon Doe", ownGuilds: [] }

guilds.push("guilds", {
  name: "new Guild",
  owner: userOne,
});

guilds.fetchAll("guilds");
// -> { guilds: [ { name: "new Guild", owner: { id: 1, name: "Jhon Doe" } } ] }

users.push("user_1.ownGuilds", "new Guild");

Properties

.entries

returns the db entries length

Example

console.log(`total entries: ${db.entries}`);

.size

returns the file size in bytes

db.set("foo", "bar");

console.log(db.size); // 13 bytes

Methods

set(key, value)

Sets a key-value to db!

| Name | Type | Required | Description | | ----- | ------ | -------- | ----------- | | Key | String | true | Key | | Value | Any | true | Data |

Example

// set any datatype !

db.set("userProfile", { username: "jhon doe", views: 10 });
db.set("userProfile.bigint", BigInt(9007199254740991));
db.set("userProfile.getViews", function getViews() {
  return this.views;
});

get(key)

returns the value of a key

| Name | Type | Required | Description | | ---- | ------ | -------- | ----------- | | Key | String | true | Key |

To complete the previous example

let userProfile = db.get("userProfile");
// -> { username: 'jhon doe', views: 10, bigint: 9007199254740991n, getViews: [Function: anonymous] }

console.log(userProfile.getViews());
// -> 10

random(n)

returns 2d array with n number of random unique entries, by default returns 1 entry

| Name | Type | Required | Default | Description | | ---- | ------ | -------- | ------- | ------------------------ | | n | Number | false | 1 | number of random entries |

Eample

db.set("one", 1);
db.set("two", 2);

let randomEntries = db.random(2); // array with 2 unique random entry

console.log(randomEntries);
// -> [["one", 1], ["two", 2]]

console.log(
  "First Random Entry",
  "key: " + randomEntries[0][0],
  "value: " + randomEntries[0][1]
);

typeOf(key)

returns the type of key,

datatypes : < "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" >

| Name | Type | Required | Description | | ---- | ------ | -------- | ----------- | | Key | String | true | Key |

Exmaple

db.set("myBigint", BigInt(9007199254740991));
db.set("mySymbol", Symbol("symboool"));
db.set("myFunction", (msg) => console.log(msg));

console.log(db.typeOf("bigint"));
// -> bigint

console.log(db.typeOf("mySymbol"));
// -> symbol

console.log(db.typeOf("myFunction"));
// -> function

has(key)

checks if the db has key, returns Boolean

| Name | Type | Required | Description | | ---- | ------ | -------- | ----------- | | Key | String | true | Key |

Exmaple

db.set("foo", "bar");

console.log(db.has("foo"));
// -> true

push(key, value)

Pushs to Array, if typeof key wasn't array, automatically convert it to array and append the new value. and returns array length

| Name | Type | Required | Description | | ----- | ------ | -------- | ----------- | | Key | String | true | Key | | Value | Any | true | Data |

Example

db.push("items", "new item");

console.log(db.get("items"));
// -> [ "new value" ]

db.set("myWepon", "Pistol");

console.log(db.get("myWepon"));
// -> Pistol

db.push("myWepon", "Riffle");

console.log(db.get("myWepon"));
// -> ["Pistol", "Riffle"]

Search keys with startsWith(str), endsWith(str)

search keys that starts with or ends with str, returns array with the matched keys otherwise returns empty array

| Name | Type | Required | Description | | ---- | ------ | -------- | ---------------- | | Str | String | true | string to search |

Example

db.set("user_a", "jhon 1");
db.set("user_b", "jhon 2");
db.set("abba", "someText");

let keysStartsWithUser = db.startsWith("user");
let keysEndsWithA = db.endsWith("a");

console.log(keysStartsWithUser);
// -> ["user_a", "user_b"]

console.log(keysEndsWithA);
// -> ["user_a", "abba"]

remove(key) or delete(key)

removes key from db, returns Boolean

no difference between remove() and delete()

| Name | Type | Required | Description | | ---- | ------ | -------- | ----------- | | Key | String | true | Key |

Exmaple

db.set("foo", "bar");

console.log(db.remove("foo"));
// -> true

console.log(db.remove("foo"));
// -> false

fetchAll()

returns everything from the db as a Javascript object

Exmaple

let allData = db.fetchAll();

console.log(allData);

clear()

clears everything in the db

Exmaple

db.clear();

console.log("Everything Has Been Cleared");