JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q31706F
  • License MIT

A leightweight easy to use local json database

Package Exports

  • dotdatabase
  • dotdatabase/src/dotdb.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dotdatabase) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

DotDB - Leightweight Local JSON Database

DotDB is a easy to use and leightweight local database that uses JSON to store data. It also supports different "directories" by usings dots. See this.
You can install it by running:

npm install dotdatabase

Getting started

To create a database, you need to import dotdb and create a new instance of the class.

import DotDB from "dotdatabase";

const database = new DotDB("./path-to-your-db.json");

Methods

There are ten different methods to use.
set, multiset, delete, multidelete, get, has, keys, values, all, clear

All the tutorials below will assume that the db is:

{
    "1": "2",
    "key": "value",
    "user_723": {
        "name": "John Doe",
        "age": 30,
        "email": "johndoe@example.com",
        "address": {
            "street": "123 Main St",
            "city": "Anytown",
            "state": "CA",
            "zip": "12345"
        }
    }
}

Set and Multiset

set allows you to set a key to a value in the database

database.set("k", "v")

multiset allows you to set multiple keys and values at once

database.multiset({ "k": "v", "k2": "v2" })

Delete and Multidelete

delete allows you to remove a key from the database

database.delete("key")

multidelete allows you to delete multiple keys at once

database.multidelete(["1", "key", "user_723"])

Get and Has

get allows you to get a value from a key from the database.
Make sure to use await before.

const value = await database.get("key")
console.log(value) // -> "value"

has allows you to check if a key-value pair exists in the database and returns true if it exists and false if not. Make sure to use await before.

console.log( await database.has("key") ); // -> true
console.log( await database.has("abc") ); // -> false

Keys and Values

keys allows you to get all keys from the database.
Make sure to use await before.

console.log( await database.keys() ); // ->
// [
//   '1',
//   'key',
//   'user_723.name',
//   'user_723.age',
//   'user_723.email',
//   'user_723.address.street',
//   'user_723.address.city',
//   'user_723.address.state',
//   'user_723.address.zip'
// ]

values allows you to get all values from the database.
Make sure to use await before

console.log( await database.values() ); // ->
// [
//   '2',
//   'value',
//   'John Doe',
//   30,
//   'johndoe@example.com',
//   '123 Main St',
//   'Anytown',
//   'CA',
//   '12345'
// ]

All and Clear

all returns the entire database as a Record.
Make sure to use await before

console.log( await database.all() ); // ->
// {
//   '1': '2',
//   key: 'value',
//   user_723: {
//     name: 'John Doe',
//     age: 30,
//     email: 'johndoe@example.com',
//     address: {
//       street: '123 Main St',
//       city: 'Anytown',
//       state: 'CA',
//       zip: '12345'
//     }
//   }
// }

clear clears the entire database and replaces it with {}.
You need to confirm it when writing the function.

await db.clear(true) // -> `true` is to confirm

What are "dot directories"?

Dot directories are kind of like paths. For example insead of doing this:

database.set("player", { health: 100 })

You can just simply do this:

database.set("player.health", 100)

Those two will have the same result.
You can use those "dot direcotries" on all functions.