simple-fsdb
v1.0.1
Published
__fsdb__ stands for `File System as a DataBase.`
Downloads
5
Readme
simple-fsdb
fsdb stands for File System as a DataBase.
It uses your local disk as a small database. simple-fsdb is designed to be used for small databases, as every time a database is initialized or created or a value set, a read/write cycle is used. As a daatabase grows larger, the native JSON interpreter we use will need more time to execute, and so slow down read/writes of large databases made with simple-fsdb.
How to use:
Installing
In your project terminal, run
npm install simple-fsdbThen, at the beginning of your main js file, require it.
const fsdb = require("simple-fsdb")Setting up a database
let db = new fsdb(path, init)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:
- 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")Read all data
Reads all the data in a database
db.all()- Returns
- All the key/value pairs in the database, in a js object.
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
List all keys
List all the keys of the data
db.keys()- Returns
- A js array of all the keys in the database
Examples:
let fsdb = require("simple-fsdb")
let db =new fsdb("db1", {hello:"hi"})
//If db1.json 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")
console.log(db.keys())
// logs ["car1", "cereal"]
console.log(db.all())
// logs {car1: "1999 old car", cereal:"frosted flakes"}