📖Methods

All of these methods support JSON objects.

.get(key)

Returns the value for that key.

const db = require("lol.db")()

console.log(db.get('money')) // returns undefined because that key still does not exist

.set(key, value)

Sets the value for that key.

const db = require("lol.db")()

db.set('money', 100)
console.log(db.get('money')) // returns 100

//With objects
db.set('user.money', 100)
console.log(db.get('user')) // returns { money: 100 }
console.log(db.get('user.money')) // returns 100

.add(key, value)

Adds a value for that key, but this time you can ONLY use numbers.

const db = require("lol.db")()

db.set('money', 100)
db.add('money', 50)
console.log(db.get('money')) // returns 150

//With objects
db.set('user.money', 100)
db.add('user.money', 50)
console.log(db.get('user')) // returns { money: 150 }
console.log(db.get('user.money')) // returns 150

.subtract(key, value)

The opposite of the .add method. Instead of adding to the value of that key, it subtracts. No need of examples as you can imagine.

.push(key, value)

Pushes a value on the array of that key. If that key does not exist, it will create one with an array and the value inside already. If that key exists and there is an array, it will push into that array and if that key exists but it's value is not an array, it will simply not push.

const db = require("lol.db")()

db.push('thiskeydoesnotexist', 100)
console.log(db.get('thiskeydoesnotexist')) // returns [100]
db.push('thiskeydoesnotexist', 100)
console.log(db.get('thiskeydoesnotexist')) // returns [100,100]

// As all of the methods above, it supports objects.

.removeFromArray(key, value)

Title says it all, removes the value from the key's array.

const db = require("lol.db")()

db.push('thiskeydoesnotexist', 100)
console.log(db.get('thiskeydoesnotexist')) // returns [100]
db.removeFromArray('thiskeydoesnotexist', 100)
console.log(db.get('thiskeydoesnotexist')) // returns []

.delete(key)

Deletes a key.

const db = require("lol.db")()

db.set('thiskeydoesnotexist', 100)
console.log(db.get('thiskeydoesnotexist')) // returns 100
db.delete('thiskeydoesnotexist')
console.log(db.get('thiskeydoesnotexist')) // returns undefined because that key does not exist anymore

.clear()

Be carefull about this method, it clears the ENTIRE database. Not just a key. But the ENTIRE database.

const db = require("lol.db")()

db.set('thiskeyexists', 100)
db.set('thiskeyexists2', 300)
db.set('anotherkey', 200)
/* 
The entire database will look like:
{"thiskeyexists":100,"thiskeyexists2":300,"anotherkey":200}
*/

db.clear()

/* 
The database now looks like... {}
*/

.size(beautify)

Returns the total size of the database (in BYTES). However, you can set the parameter beautify to true and it will return a converted amount of bytes to whatever it is.

const db = require("lol.db")()

db.set('ok', '?')
db.set('BigObject.first', 'idk')

console.log(db.size()) // returns 38 (as a number)
console.log(db.size(true)) // returns "38 Bytes"

.keys()

Returns the quantity of MAIN keys on the database.

const db = require("lol.db")()

/* Imagine that your data looks like: {"mykey":"something","anotherkey":"somethingelse"}*/
console.log(db.keys()) // returns 2 (because there are 2 keys in the database. Keys inside objects inside keys though DO NOT count)

.setAll(data)

sets the entire database to one single JSON object.

const db = require("lol.db")()

db.setAll({ firstkey: "my data", secondkey: "more data" })
console.log(db.raw.d) // returns the ENTIRE database, so it will return { firstkey: "my data", secondkey: "more data" }

.raw | Raw Functions And Parameters

This seccion is basically raw functions that we used to build some major functions like set, delete or get. The object looks like this btw:

{
    "data": "entire database data",
    "d": "same as data, but it's a cool shortcut",
    "assign": "THIS IS A FUNCTION AND IT IS USED TO SET NESTED KEYS TO OBJECTS (or normal keys too ig).",
    "deletePath": "The opposite of assign, it's still a function, HOWEVER it DELETES the keys.",
    "path": "The path for your database file",
    "size": "This will be a number and it represents the size of your database",
    "exists": "true or false (if the database exists)"
}

btw this is implemented in the database, so once you defined the database, you will have access to this object doing db.raw.nameofthekeyabove

Quick Syntax for assign and deletePath functions:

db.assign(object, key, value) - Example: db.assign(db.raw.d, "some.key", 69)

db.deletePath(object, key) - Example: db.deletePath(db.raw.d, "some.key")

DO NOT remove the {} on the start and end of your database file. Otherwise the database will NOT work.

Last updated