Basic Usage

You can find an auto-generated documentation website to use as a reference here.

Initializing

Wherever you need to use quick.db, you need to initialize the db variable so it can be used. You can either do this in the file using the below example, or somehow pass it to the file so it can be accessed.

const { QuickDB } = require("quick.db");
const db = new QuickDB();

async/await

Since quick.db’s methods are asynchronous, the surrounding function where quick.db is being used has to be async.

async function myMethod() {
  // your code goes here...
}

Set & Get

Quick.db has a multitude of methods that you can find on the API page. The two most commonly used ones are get and set.

// Setting the string "myValue" to the key "myKey"
await db.set("myKey", "myValue"); // Returns -> "myValue"

// Getting the value of the key "myKey"
await db.get("myKey"); // Returns -> "myValue"

// In addition to strings, you can store objects as well.
await db.set("myUser", { balance: 500 }); // Returns -> { balance: 500 }

// Getting the value of the key "myUser"
await db.get("myUser"); // Returns -> { balance: 500 }

Delete

How to delete keys you have previously created.

// Deleting the key "myKey"
await db.delete("myKey"); // Returns -> "myValue"

Dot Notation

You can use dot notation in the key parameter to modify the associated object’s properties. Notice below how “myUser” is the key, although the property “balance” is being set and get.

await db.set("myUser.balance", 700); // Returns -> { balance: 700 }
await db.get("myUser.balance"); // Returns -> 700

Arrays

In addition to primitive types and objects, quick.db can also store arrays in the database.

await db.set("myArray", [1, 2, 3]); // Returns -> [1, 2, 3]
await db.get("myArray"); // Returns -> [1, 2, 3]

// Using the myUser object from earlier
await db.set("myUser.items", ["Sword", "Shield", "Health Potion"]); // Returns -> { balance: 700, items: [ 'Sword', 'Shield', 'Health Potion' ] }

// Some additional helper methods for arrays include push, pull, and has
await db.push("myUser.items", "Armor"); // Returns -> { balance: 700, items: [ 'Sword', 'Shield', 'Health Potion', 'Armor' ] }
await db.pull("myUser.items", "Health Potion"); // Returns -> { balance: 700, items: [ 'Sword', 'Shield', 'Armor' ] }

Add & Subtract

There are some helper methods for adding and subtracting values as well.

await db.add("myUser.balance", 100); // Returns -> { balance: 800, items: [ 'Sword', 'Shield', 'Armor' ] } }
await db.sub("myUser.balance", 50); // Returns -> { balance: 750, items: [ 'Sword', 'Shield', 'Armor' ] } }