Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node-json-db example

import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/dist/lib/JsonDBConfig'

// The first argument is the database filename. If no extension, '.json' is assumed and automatically added.
// The second argument is used to tell the DB to save after each push
// If you put false, you'll have to call the save() method.
// The third argument is to ask JsonDB to save the database in an human readable format. (default false)
// The last argument is the separator. By default it's slash (/)
var db = new JsonDB(new Config("myDataBase", true, false, '/'));

// Pushing the data into the database
// With the wanted DataPath
// By default the push will override the old value
db.push("/test1","super test");

// It also create automatically the hierarchy when pushing new data for a DataPath that doesn't exists
db.push("/test2/my/test",5);

// You can also push directly objects
db.push("/test3", {test:"test", json: {test:["test"]}});

// If you don't want to override the data but to merge them
// The merge is recursive and work with Object and Array.
db.push("/test3", {
    new:"cool",
    json: {
        important : 5
    }
}, false);
/*
This give you this results :
{
   "test":"test",
   "json":{
      "test":[
         "test"
      ],
      "important":5
   },
   "new":"cool"
}
*/

// You can't merge primitive.
// If you do this:
db.push("/test2/my/test/",10,false);

// The data will be overriden

// Get the data from the root
var data = db.getData("/");

// From a particular DataPath
var data = db.getData("/test1");

// If you try to get some data from a DataPath that doesn't exists
// You'll get an Error
try {
    var data = db.getData("/test1/test/dont/work");
} catch(error) {
    // The error will tell you where the DataPath stopped. In this case test1
    // Since /test1/test does't exist.
    console.error(error);
};

// Deleting data
db.delete("/test1");

// Save the data (useful if you disable the saveOnPush)
db.save();

// In case you have a exterior change to the databse file and want to reload it
// use this method
db.reload();

// You can also easily append new item to an existing array
// This set the next index with {obj: 'test'}
db.push("/arraytest/myarray[]", {
    obj:'test'
}, true);


// The append feature can be used in conjuction with properties
// This will set the next index as an object {myTest: 'test'}
db.push("/arraytest/myarray[]/myTest", 'test', true);

// You can have the current index of an object
db.push("/arraytest/myarray", {id: 65464646155, name: "test"}, true);
db.getIndex("/arraytest/myarray", 65464646155);
// By default, the property is 'id'
// You can add another property instead
db.getIndex("/arraytest/myarray", "test", "name");

// It's useful if you want to delete some object
db.delete("/arraytest/myarray[" + db.getIndex("/arraytest/myarray", 65464646155) + "]");
Comment

node-json-db

npm install node-json-db
// OR
yarn add node-json-db
Comment

PREVIOUS NEXT
Code Example
Javascript :: vue js default prop 
Javascript :: last item in object javascript 
Javascript :: React Redux reducer combineReducers exemple 
Javascript :: how to send emoji as ID discord.js 
Javascript :: jquery find id with string at end 
Javascript :: inarray javascript 
Javascript :: javascript number pyramid 
Javascript :: javascript regex extract url from string 
Javascript :: regex for counting characters 
Javascript :: angular string contains 
Javascript :: how to cheack if a number is an integer or float in javascript 
Javascript :: retour à la ligne react native 
Javascript :: tailwind in react 
Javascript :: js json groupby prop 
Javascript :: jquery remove click event 
Javascript :: jquery get multiple input values by name 
Javascript :: fs.unlink 
Javascript :: how to remove element from array in javascript 
Javascript :: open new tab with angular router 
Javascript :: array left rotation javascript 
Javascript :: padend method javascript 
Javascript :: js string array convert to int 
Javascript :: javascript trim spaces 
Javascript :: how to get custom attribute value in react 
Javascript :: loop array and check if value matches in js 
Javascript :: how to see chrome sync storage and local storage 
Javascript :: javascript game loop 
Javascript :: router navigatebyurl 
Javascript :: how to right plain text format file in node js 
Javascript :: set drain docker node 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =