Simple file based database in nodejs. Data is stored as json files.
SQL | FileDB |
---|---|
Table | Collection |
Row | Document |
currently not available in npm you have to clone manually.
var db = require("./db") //change accordingly
var database = new db();
Existing database is referenced or if it doesnot exsist new database is created
default location of database current directory (
./
)
default name of database is
.data
to define custom location and name for database use
var database = new db(dbname, dbpath);
var userTable = database.collection("users")
Existing table is referenced or if it doesnot exsist new table is created
row with autogenerated ID
userTable.add({name: "John", age: 25}).then(doc => console.log(doc));
row with custom ID
userTable.doc('john').set({name: "John", age: 23}).then(doc => console.log(doc));
get all data from table
userTable.get().then(docs => console.log(docs));
get data based on condition
userTable.where("name", "==", "John").get().then(docs => console.log(docs));
multiple conditions
userTable.where("name", "==", "John").where("age", "==", 25).get().then(docs => console.log(docs));
replace entire data of row
userTable.doc('john').set({name: "John Doe"}).then(doc => console.log(doc));
//here only the name field will be remaining in the saved all other data in the row is lost
change existing data or add fields
userTable.doc('john').set({age: 30, location: "Canada"},{merge: true}).then(doc => console.log(doc));
//here name will remain unchanged age will be updated and location field will be added
- Currently only = condition is checked in where more conditions need to be added