Node.js

Documentation for the npm package 'perma.db'

PermaDB

PermaDB is a versatile and easy to use database management library that aims to be fast, and save space.

v1.0.9 API

class PermaDB

Importing the class from the module.

const { PermaDB } = require("perma.db")

new PermaDB(db_name, [options])

Returns database

By creating a new instance of the PermaDB class, you create and open a new sqlite3 database. You can customize the options of the database with arguments.


db_name

String

The database name to be created. Default is perma.db.

options

Object

memory Boolean

options.memory specifies if the db works both with the database file and on memory. Default is false.

minimize Boolean

options.minimize specifies if the db will automatically perform vacuum operations. Default is false.

preload Boolean

options.preload specifies if all database should be loaded to the memory. Only considered if the options.memory is true. Default is false.

database

Creating the instance of class PermaDB imported from perma.db.

const database = new PermaDB();

new PermaDB(db_name, [options]): database

Synchronous Methods

setSync(key, value)

Defined in perma.db/lib/index.js#setSync

setSync(key, value): value

Stores the value in the database, so you can access it with the key after.

key String

value Buffer || String || Date || Number || null

returns Buffer || String || Date || Number || null

getSync(key)

Defined in perma.db/lib/index.js#getSync

getSync(key): value

Gets the value with the given key in the database.

key String

returns Buffer || String || Date || Number || null

fetchSync(key)

Defined in perma.db/lib/index.js#fetchSync

fetchSync(key): value

Gets the value with the given key in the database.

returns Buffer || String || Date || Number || null

key String returns Buffer || String || Date || Number || null

updateSync(key, value)

Defined in perma.db/lib/index.js#updateSync

updateSync(key, value): changed

If the given key exists in the database, changes its value and returns true. If it doesn't exist, returns false.

key String

value Buffer || String || Date || Number || null returns Boolean

hasSync(key)

Defined in perma.db/lib/index.js#hasSync

hasSync(key) : exists

Checks if the given key exists in the database. If it does, returns true. Else returns false.

key String returns Boolean

deleteSync(key)

Defined in perma.db/lib/index.js#deleteSync

deleteSync(key): success

Checks if the given key exists in the database. If it does, deletes it and returns true. Else returns false.

key String

returns Boolean

removeSync(key)

Defined in perma.db/lib/index.js#removeSync

removeSync(key): success

Checks if the given key exists in the database. If it does, deletes it and returns true. Else returns false.

key String

returns Boolean

typeSync(key)

Defined in perma.db/lib/index.js#typeSync

typeSync(key): typeof value

Checks if the given key exists in the database. If it does, returns the type of the value. Else returns null.

key String returns String

allSync()

Defined in perma.db/lib/index.js#allSync

allSync(): [{key, value}]

Gets all key-value pairs in the database inside an array.

returns Array

fetchAllSync()

Defined in perma.db/lib/index.js#fetchAllSync

fetchAllSync(): [{key, value}]

Returns all key-value pairs in the database inside an array.

returns Array

deleteAllSync()

Defined in perma.db/lib/index.js#deleteAllSync

deleteAllSync(): success

Deletes all key-value pairs in the database.

returns Boolean

removeAllSync()

Defined in perma.db/lib/index.js#removeAllSync

removeAllSync(): success

Deletes all key-value pairs in the database.

returns Boolean

clearSync()

Defined in perma.db/lib/index.js#clearSync

clearSync(): success

Deletes all key-value pairs in the database.

returns Boolean

deleteDBSync()

Defined in perma.db/lib/index.js#deleteDBSync

deleteDBSync(): success

Unlinks(removes) the database after closing it.

returns Boolean

removeDBSync()

Defined in perma.db/lib/index.js#removeDBSync

removeDBSync(): success

Unlinks(removes) the database after closing it.

returns Boolean

vacuumSync()

Defined in perma.db/lib/index.js#vacuumSync

vacuumSync(): success

Executes SQLite 'Vacuum' command on the database. returns Boolean

backupSync(path)

Defined in perma.db/lib/index.js#backupSync

backupSync(path): success

Closes the database. If the path already exists, returns false. If it doesn't, creates a backup.

path String

returns Boolean

querySync(sql, ...params)

Defined in perma.db/lib/index.js#querySync

querySync(sql, ...params): results

Execute an sql query.

sql String

params Any returns Any

sizeSync()

Defined in perma.db/lib/index.js#sizeSync

sizeSync(): size

Get the total size of the database contents.

returns Number

fileSizeSync()

Defined in perma.db/lib/index.js#fileSizeSync

fileSizeSync(): filesize

Get the file size of the database SQLite file.

returns Number

keysSync()

Defined in perma.db/lib/index.js#keysSync

keysSync(): [...keys]

Returns the array from the keys of the database.

returns Array

valuesSync()

Defined in perma.db/lib/index.js#valuesSync

valuesSync(): [...values]

Returns the array of the values stored in the database. returns Array

objectSync()

Defined in perma.db/lib/index.js#objectSync

objectSync(): {key: value}

Returns the key-value pairs as a Javascript Object.

returns Object

execSync(cmd)

Defined in perma.db/lib/index.js#execSync

execSync(cmd): output

Executes the given SQLite command, and returns the output.

cmd String

returns String?

closeSync()

Defined in perma.db/lib/index.js#closeSync

closeSync(): success

Closes the database. returns Boolean

expireSync(key, time)

Defined in perma.db/lib/index.js#expireSync

expireSync(key, time): success

Waits for the given time(ms). If exists, deletes the given key along with it's value from the database, and returns true. Else returns false. returns Boolean

lengthSync()

Defined in perma.db/lib/index.js#lengthSync

lengthSync(): length

Gets the number of existing key-value pairs in the database. returns Boolean

Asynchronous Methods

set(key, value)

async set(key, value): Promise<Buffer || String || Date || Number || null>

Stores the value in the database, so you can access it with the key after. Returns a promise which resolves with the value.

key String

value Buffer || String || Date || Number || null

get(key)

async get(key): Promise<value || null>

Returns a promise which resolves with the value for the given key in the database

key String

fetch(key)

async fetch(key): value || null

Stores the value in the database, so you can access it with the key after. Returns a promise which resolves with the value.

key String

update(key, value)

async update(key, value) : Promise<true || false>

If the given key exists in the database, changes its value and resolves with true. If it doesn't exist, resolves false.

key String

value Buffer || String || Date || Number || null

has(key)

async has(key) : Promise<true || false>

Checks if the given key exists in the database. If it does, resolves with true. Else resolves false.

key String

delete(key)

async delete(key): Promise<true || false>

Checks if the given key exists in the database. If it does, deletes it and returns true. Else returns false.

key String

remove(key)

remove(key): Promise<true || false>

Checks if the given key exists in the database. If it does, deletes it and returns true. Else returns false.

key String

type(key)

async type(key): Promise<typeof value || null>

Checks if the given key exists in the database. If it does, returns the type of the value. Else returns null.

key String

all(key)

async all(): Promise<[{key, value}]>

Resolves all key-value pairs in the database inside an array.

fetchAll()

async fetchAll(): Promise<Array [{key, value}]>

Returns all key-value pairs in the database inside an array.

deleteAll()

async deleteAll(): Promise<true>

Deletes all key-value pairs in the database, and returns true

removeAll()

async removeAll(): Promise<true>

Deletes all key-value pairs in the database, and returns true

clear()

async clear(): Promise<true>

Deletes all key-value pairs in the database, and returns true

deleteDB()

async deleteDB(): Promise<true>

Unlinks(removes) the database after closing it and returns true.

removeDB()

async removeDB(): Promise<true>

Unlinks(removes) the database after closing it and returns true.

vacuum()

async vacuum(): Promise<true>

Vacuums the database and returns true.

backup(path)

async backup(path): Promise<true || false>

Closes the database. If the path already exists, returns false. If it doesn't, creates a backup.

path String

query(sql, ...params)

async query(sql, ...params): Promise<results>

Execute an sql query.

sql String

params Any

size()

async size(): Promise<Number || null>

Returns the total size of the database contents.

fileSize()

async fileSize(): Promise<Number>

Resolves with the database file size in bytes

keys()

async keys(): Promise<Array [keys]>

Resolves with the array from the keys of the database.

values()

async values(): Promise<Array [values(Buffer || String || Date || Number || null)]>

Resolves with the array of database values

object()

async object(): Promise<{key, value} (Object)>

Resolves with key-value pairs as a Javascript Object.

exec(cmd)

async exec(cmd): Promise<output(String)>

Executes the given sqlite command, and returns the output.

cmd String

close()

async close(): Promise<true>

Closes the database and resolves with true.

expire(key, time)

async expire(key, time): Promise<success>

If exists, deletes the given key along with it's value from the database, and returns true.

Else returns false.

key String

time Number

resolves with Boolean

length()

length(): Promise<Number>

Resolves with the number of existing key-value pairs in the database.

Last updated