big-fsdb
v1.0.5
Published
__fsdb__ stands for `File System as a DataBase.`
Downloads
18
Readme
simple-fsdb
fsdb stands for File System as a DataBase.
It uses your local disk as a database. big-fsdb is designed to be used for large databases. Unlike simple-fsdb, it does not parse the whole database, instead splitting it into chunks and clusters to process, leading to less process time, less disk read/write cycles, and faster response time.
How to use:
Installing
In your project terminal, run
npm install big-fsdbThen, at the beginning of your main js file, require it.
const fsdb = require("big-fsdb")Setting up a database
let db = new fsdb(path, init, clustersize)path:- Accepts:
string - Serves as the identifier of the db. Also, the db file is saved at the path
- Ex.
myFirstDatabasewill lead to the db being stored atmyFirstDatabase.json
- Accepts:
init(optional, defaults to{}):- Accepts:
object - If no such database file exists, one will be created, with the contents of
init - Ex,
{bar:"foo", number:5}will lead to the database being created withbar=fooandnumber=5
- Accepts:
clustersize- Accepts:
integer - Number of JSON key/value pairs per file. Smaller number leads to more JSON files, but faster and less compute time.
- Defaults to
100
- Accepts:
- Multiple databases can be set up this way. Make sure not to use the same
pathor variable name twice.
Setting values
Setting a key to a value in the database
db.set(key, value)key- Accepts:
string - Acts as the key in the key/pair value.
- Accepts:
value- Accepts:
string,number,object,array - Value to link to the key
- Accepts:
- Ex.
db.set("numberOfCars", 5)Reading values
Retrieves a value from the database, using the key.
db.get(key)key- Accepts:
string - Acts as the key in the key/pair value.
- Accepts:
- Returns
- Value that links to the key. If not found, returns
undefined
- Value that links to the key. If not found, returns
- Ex.
db.get("numberOfCars")Deleting values
Deletes a key/value pair from the database.
db.delete(key)key- Accepts:
string - Acts as the key in the key/pair value.
- Accepts:
- Deletes the key/pair value
- Ex.
db.delete("numberOfCars")Prefix
Filter through all data in the dataabase using a prefix
db.prefix(pre)pre- Accepts:
string - The prefix to look for
- Accepts:
- Returns
- All the key/value pairs where the key starts with
pre, in a js object
- All the key/value pairs where the key starts with
Examples:
let fsdb = require("big-fsdb")
let db =new fsdb("db1", {hello:"hi"})
//If db1/ does not exist, one will be created with data {hello:"hi"}
db.set("cars", 4)
console.log(db.get("cars"))
// logs 4
db.set("car1", "1999 old car")
console.log(db.prefix("car"))
// logs {cars:4, car1:"1999 old car"}
db.delete("cars")
db.set("cereal", "frosted flakes")