npm install merodb
Create MeroDB Instance
const MeroDB = require("merodb");
var myDb = new MeroDB();
<script src="./dist/merodb.min.js"></script>Create MeroDB Instance
<script>
document.addEventListener('DOMContentLoaded', function () {
var myDb = new merodb();
});
</script>
Look here for SAMPLE.
(It works on almost all latest browsers.)
parameter | type | description |
---|---|---|
collection name | string | name of collection ot be created. Similar to "Table" in RDBMS. |
is activate | boolean (optional) | will activate the collection if set to true |
var isCreated = myDb.createCollection("continent");
if(isCreated === false){
console.log("Failed to create collection");
}
It has a method called getCollections which will return the list of collections found in a
database.
var myCollections = myDb.getCollections();
parameter | type | description |
---|---|---|
collection name | string(optional) | it can be omitted for active collection |
data | object | it is the data to be inserted which is called document |
var data1 = {id:1,place:"Asia",color:"red"};
var data2 = {id:3,place:"Europe",color:"green"};
var collectionName = "continent";
myDb.insert(collectionName,data1);
myDb.insert(collectionName,data2);
myDb.use("continent");
var data1 = {id:5,place:"Africa",color:"blue"};
var data2 = {id:7,place:"America",color:"yellow"};
myDb.insert(data1);
myDb.insert(data2);
"use" & "insert" methods will return false if failed, Otherwise returns true. if second
parameter is passed as true while creating collection, it will automatically activate the given
collection.
myDb.createCollection("continent", true);
myDb.insert({id:7,place:"Antartica",color:"pink"});
var myObj = [
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 1,
"id": 3,
"title": "fugiat veniam minus",
"completed": false
},
{
"userId": 1,
"id": 4,
"title": "et porro tempora",
}
];
myDb.loadFromJsonObject("user", myObj);
parameter | type | description |
---|---|---|
object (1 param) | search condition | returns all the document satisfiying this condition |
object, array(2 params) | search condition, column list | similar to case 1 but only the columns listed will be returned |
string, object, array(3 params) | collection name, search condition, column list | name of collection can be explicitly applied |
var searchTableString = "user";
var searchConditionObject = { place: "Asia" };
var resultColumnArray = ["id"];
var searchResult = myDb.find(searchTableString, searchConditionObject);
var searchResult = myDb.find(searchTableString, searchConditionObject, resultColumnArray);
var searchResult = myDb.find(searchConditionObject);
var searchResult = myDb.find(searchConditionObject, resultColumnArray);
name | meaning | usage |
---|---|---|
$e | equals | { id: { $e: 200 } } |
$ne | not equal | { id: { $ne: 200 } } |
$lt | less then | { id: { $lt: 200 } } |
$lte | less then or equal | { id: { $lte: 200 } } |
$gt | greater then | { id: { $gt: 200 } } |
$gte | greater then or equal | { id: { $gte: 200 } } |
$bt | between | { id: [2, 5] } |
$cont | contains | { name: { $cont: "a" } } |
var searchResult = myDb.find("user",{ id: { $gte: 4 } });
var searchResult = myDb.find("user", {
$or: [{ id: 1 }, { name: "kk" }],
salary: 15
});
var searchResult = myDb.find({
$or: [
{ isbn: { $gte: 2000 } },
{ writer: { $ne: "df" } }
],
name: { $ne: "df" },
price:{$lt:5000}
});
paramet | type | description |
---|---|---|
collection name | string | it can be omitted for active collection |
condition | object | documents fulfilling this condition will be updated with new value |
new value | object | this value will be the new value |
if the
// create instance of MeroDB
var myDb = new MeroDB();
// create collection with name "user" and activate
myDb.createCollection("user", true);
// insert two documents in the active collection
myDb.insert({ id: 1, name: "hbeauty", enroll: "2018-01-20", sex: "M", salary: 15 });
myDb.insert({ id: 2, name: "dakc", enroll: "2017-02-28", sex: "F", salary: 35 });
// update active collection having its documents id = 2
myDb.update({ id: 2 }, { id: 1111, name: "tom" });
// get current data from active collection
var newData = myDb.find({});
console.log(newData[0], newData[1]);
// Object {id: 1, name: "hbeauty", enroll: "2018-01-20", sex: "M", salary: 15}
// Object {name: "tom"}
// the result will be as following where second document is completely replaced.
If
// update active collection having its documents id = 2
myDb.update({ id: 2 }, { $set: { id: 1111, name: "tom" } });
// get current data from active collection
var newData = myDb.find({});
console.log(newData[0], newData[1]);
//Object {id: 1, name: "hbeauty", enroll: "2018-01-20", sex: "M", salary: 15}
//Object {id: 1111, name: "tom", enroll: "2017-02-28", sex: "F", salary: 35}
// only id,name were updated.
Condition is object which is exactly similar to the condition for find method.
Search Condition
var searchCondition = { id: { $lt: 2 } };
var newData = { $set: { name: "harilarl" } };
myDb.update(searchCondition, newData);
// Get number of documents updated
var updatedNumber = myDb.collAffected();
console.log("number of updated documents is " + updatedNumber);
var searchCondition = { id: { $lt: 2 } };
var documentName = "user"
var isDelete = myDb.delete(documentName, searchCondition);
if (isDelete == true) {
console.log("Deleted.");
}
// Get number of documents deleted
var deletedNumber = myDb.collAffected();
console.log("number of deleted documents is " + deletedNumber);
// get document count for "user" collection
var collectionName = "user";
var numberRows = myDb.getDocumentCount(collectionName);
// get active collection's document count
var numberRowsActiveCollection = myDb.getDocumentCount();
// delete from collection user
var searchCondition = { id: { $lt: 2 } };
var collectionName = "user"
var isDelete = myDb.delete(documentName, searchCondition);
// get the number of documents deleted
var numberRowsDeleted = myDb.getDocumentNumAffected();
// get name of active collection
var activeCollection = myDb.getActiveCollection();
// get the error message
var errorMessage = myDb.getError();
// get the names of collections existing in a database
var arrayCollection = myDb.getCollections();
parameter | type | description |
---|---|---|
collection name | string | name of collection to hold data |
data | array of json object | data to be loaded in a database |
// create data to be inserted
let userData = [];
userData.push({ id: 1, name: "pk", salary: 10 });
userData.push({ id: 2, name: "ck", age: 20, salary: 20, sex: "M" });
userData.push({ id: 3, name: "dk", create: "2018-01-20", sex: "M", salary: 35 });
userData.push({ id: 4, name: "kk", salary: 25, sex: "M" });
userData.push({ id: 5, name: "kk", salary: 15 });
// load to database with collection name user
myDb.loadFromJsonObject("user", userData);
// write contents to "my.db"
myDb.save("./my.db");
// write contents to "my.db" on each change
myDb.updateAlways("./my.db");
// load from file
myDb.loadFromFile("./my.db");