Skip to content

Database Examples

Before starting

Below are several examples, in case you don’t know how to start the class you can see:

Utility functions

Database Ping

Get the ping of the database

const database: Database<"main">
database
.
Database<"main">.ping(): Promise<number>

Get the ping of the db

ping
()

All database

Gets the contents of all tables and condenses them into a single object

const database: Database<"main">
database
.
Database<"main">.all(auth?: string): Promise<Record<"main", JSObjectN>>

Gets the contents of all tables and condenses them into a single object

all
(
const AUTH_KEY: string
AUTH_KEY
)

Sanitize string

Example content for the β€œmain” table

main.json
{
"foo": {
"bar": "πŸ‘ΈπŸ» You found me Mario!",
},
"foo.bar" : "πŸ„ The princess is in another castle!",
}
// This returns "πŸ„ The princess is in another castle!"
const database: Database<"main">
database
.
Database<"main">.get<JSONValues>({ table, key, auth }: KeyedOptions<"main">): Promise<JSONValues | undefined> (+1 overload)

Get a specific value of a key in the table

get
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
:
const database: Database<"main">
database
.
Database<"main">.sanitize(path: string): string

Sanitize an string to use.

Example: If you has {"foo.bar": null} you must use sanitize("foo.bar") to access it

sanitize
("foo.bar"),
SimpleOptions<T extends string>.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})
// This returns "πŸ‘ΈπŸ» You found me Mario!"
const database: Database<"main">
database
.
Database<"main">.get<JSONValues>({ table, key, auth }: KeyedOptions<"main">): Promise<JSONValues | undefined> (+1 overload)

Get a specific value of a key in the table

get
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo.bar",
SimpleOptions<T extends string>.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Example extracted from Dot-Prop

Basic usages

Get function

// Get an entire table
const database: Database<"main">
database
.
Database<"main">.get<JSObjectN>({ table, auth }: SimpleOptions<"main">): Promise<JSObjectN> (+1 overload)

You get an entire table

get
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})
// Get a specific value of a key in the table, or get the entire table
const database: Database<"main">
database
.
Database<"main">.get<JSONValues>({ table, key, auth }: KeyedOptions<"main">): Promise<JSONValues | undefined> (+1 overload)

Get a specific value of a key in the table

get
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
SimpleOptions<T extends string>.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Set function

// Overwrite a table completely
const database: Database<"main">
database
.
Database<"main">.set({ table, auth, value }: UnKeyedValuedOptions<"main">): Promise<JSObjectN> (+1 overload)

Overwrite a table completely

set
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
,
UnKeyedValuedOptions<"main">.value: JSObjectN

Value to be established

value
: {
foo: string
foo
: "bar"}
})
// Writes a specific property
const database: Database<"main">
database
.
Database<"main">.set({ table, key, auth, value }: KeyedValuedOptions<"main", JSONValues>): Promise<JSObjectN> (+1 overload)

Writes a specific property

set
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", JSONValues>.value: JSONValues

Value to be established

value
: "bar",
SimpleOptions<T extends string>.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Delete function

// Deletes a table completely
const database: Database<"main">
database
.
Database<"main">.delete({ table, auth }: SimpleOptions<"main">): Promise<JSObjectN> (+1 overload)

Deletes a table completely

delete
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
,
})
//Deletes a specific property
const database: Database<"main">
database
.
Database<"main">.delete({ table, key, auth }: KeyedOptions<"main">): Promise<JSObjectN> (+1 overload)

Deletes a specific property

delete
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
SimpleOptions<T extends string>.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Number Oriented

Sum function

const database: Database<"main">
database
.
Database<"main">.sum({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Adds a quantity to a key, if it does not exist it is created, or if it does exist and it is not a number it is overwritten with the value of the quantity to be added.

sum
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Sub function

const database: Database<"main">
database
.
Database<"main">.sub({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Subtract a quantity from a key, if it does not exist it is created, or if it does exist and it is not a number it is overwritten with the value of the quantity to be subtracted.

sub
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Multi function

const database: Database<"main">
database
.
Database<"main">.multi({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Multiplies a quantity to a key, if it does not exist it is created, or if it does exist and it is not a number it is overwritten with the value of the quantity to be multiplied.

multi
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Divide function

const database: Database<"main">
database
.
Database<"main">.divide({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Divides a quantity to a key, if it does not exist it is created, or if it does exist and it is not a number it is overwritten with the value of 0.

divide
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Pow function

const database: Database<"main">
database
.
Database<"main">.pow({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Raises a quantity to a power in a key, if it does not exist it is created, or if it does exist it is overwritten with the value of 0.

pow
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Sqrt function

const database: Database<"main">
database
.
Database<"main">.sqrt({ table, key, value, auth }: KeyedValuedOptions<"main", number>): Promise<JSObjectN>

Gets a value from a database based on a key. If the value does not exist or is not a number, it is stored as 0. If the value is a number, it is updated with nth root of the value.

sqrt
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", number>.value: number

Value to be established

value
: 2, //THIS SHOULD ALWAYS BE A NUMBER
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Array Oriented

Push function

const database: Database<"main">
database
.
Database<"main">.push({ table, key, value, auth }: KeyedValuedOptions<"main", JSONValues>): Promise<JSObjectN>

Pushs a value from the provided key. If the array does not exist or is not an array, then an array containing the given value will be created.

push
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", JSONValues>.value: JSONValues

Value to be established

value
: "bar",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Remove function

const database: Database<"main">
database
.
Database<"main">.remove({ table, key, value, auth }: KeyedValuedOptions<"main", JSONValues>): Promise<JSObjectN>

Removes a value from the provided key. If the array does not exist or is not an array, then an empty array will be created.

remove
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", JSONValues>.value: JSONValues

Value to be established

value
: "bar",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Unshift function

const database: Database<"main">
database
.
Database<"main">.unshift({ table, key, value, auth }: KeyedValuedOptions<"main", JSONValues>): Promise<JSObjectN>

Adds at the begging the provided values from the provided key. If the array does not exist or is not an array, then an array containing the given value will be created.

unshift
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
KeyedValuedOptions<"main", JSONValues>.value: JSONValues

Value to be established

value
: "bar",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Shift function

const database: Database<"main">
database
.
Database<"main">.shift({ table, key, auth }: KeyedOptions<"main">): Promise<JSONValues | undefined>

Removes and returns the first value from the provided key. If the array does not exist or is not an array, then an empty array will be created.

shift
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})

Pop function

const database: Database<"main">
database
.
Database<"main">.pop({ table, key, auth }: KeyedOptions<"main">): Promise<JSONValues | undefined>

Removes and returns the last value from the provided key. If the array does not exist or is not an array, then an empty array will be created.

pop
({
SimpleOptions<"main">.table: "main"

Table to use

table
: "main",
KeyedOptions<"main">.key: string

Dot path key inside a table

key
: "foo",
SimpleOptions<"main">.auth?: string

Authentication key

auth
:
const AUTH_KEY: string
AUTH_KEY
})