json.handling v2.1.6
Installation
Node.js 16.6.0 or newer is required.
npm i json.handling
Features & Index
Example usage
Importing
Importing in Common JavaScript Files
const {} = require('json.handling');
Importing in ESM JavaScript Files
import {} from ('json.handling');
Set Response Time
Importing from 'json.handling/responseBridge';
Deprecated: and SetGlobalResponse
is no longer required as queues are implemented in this section as of package version SetResponseTime
2.1.6
// For ModuleJs (ESM)
import { SetGlobalResponse, SetResponseTime } from 'json.handling/responseBridge';
// For CommonJs
const { SetGlobalResponse, SetResponseTime } = require('json.handling/responseBridge');
SetResponseTime method.
// set response time for some path.
SetResponseTime('./tests/database.json', 100); // SetResponseTime(local, response);
// parameter: `local` for local module and package.
// parameter: `response` for set timeout.
SetGlobalResponse method.
// set global response time.
SetGlobalResponse(1500); // SetGlobalResponse(response);
// parameter: `response` for set timeout.
Creating Json Handling
- example is json database
// import local modules.
import { create } from 'json.handling';
// find 'json' file from path as database.
const database = new create('./database.json');
Write method.
import { randomInt, randomUUID } from 'crypto';
// create data as object.
let data = { [randomUUID()]: `${randomInt(999999)}` };
// and writing object as database.
database.write(data);
output for write method. example filename is randomized.json
{"dcd15169-7a67-4196-9513-0f467123f053":"289511"}
// {"uuid from randomly": "int from randomly"}
Update method.
use the data of the file randomized.json
.
import { randomInt, randomUUID } from 'crypto';
// create data as object.
let data = { [randomUUID()]: `${randomInt(999999)}` };
// and update object as database.
database.update("dcd15169-7a67-4196-9513-0f467123f053", data);
output for update method.
{"31620958-4825-4f81-b7d5-3207e30c6065":"48411"}
// {"uuid from randomly updated": "int from randomly updated"}
Erase method.
use the data of the file randomized.json
.
// erase object key as database.
database.erase("dcd15169-7a67-4196-9513-0f467123f053");
output for erase method. example filename is randomized.json
{}
// {} All data in that key will be deleted.
Get method.
use getValue method and use the data of the file randomized.json
.
// getValue() method for get value from key.
const value = database.getValue("dcd15169-7a67-4196-9513-0f467123f053");
console.info(value);
console output for getValue method
"289511"
use getData method and use the data of the file randomized.json
.
// getData() method for get value from key.
const data = database.getData();
console.info(data);
console output for getValue method
{"dcd15169-7a67-4196-9513-0f467123f053":"289511"}
Simple Success File.
In methods.js
from tests
folder
// import others modules.
import { randomInt, randomUUID } from 'crypto';
const uuid = randomUUID();
// import local modules.
import { create } from '../libs/handling.js';
import { SetGlobalResponse, SetResponseTime } from '../libs/responseBridge.js';
// set response time for some path.
SetResponseTime('./tests/database.json', 100);
// set global response time.
SetGlobalResponse(1500);
// find 'json' file from path to handling. | example is json database
const database = new create('./tests/database.json');
//** A simple usage example is below. **//
1. // example: `write` method | use 'data' type as 'Object' to write database.
// create 'data' as 'object'.
let a = { [uuid]: `${randomInt(999999)}` };
// write 'data' on database.
database.write(a);
2. // example: `update` method | update 'key' and 'data' to update at database and using as current.
// create 'data' as 'object'.
let b = { [uuid] :`${randomInt(999999)}` };
// update 'data' with 'key' on database.
database.update(uuid, b);
3. // example: `erase` method | erase 'data' with 'key' on database.
database.erase(uuid);
In database-handling.json
from tests
folder.
{"dataA":21, "dataB": 56, "dataC": "instant"}
In handing.js
from tests
folder and use database-handling.json
in this example.
// import local modules
import { create, SetResponseTime } from '../JsonHandling.js';
const JsonHandling = new create('./tests/database-handling.json');
SetResponseTime('./tests/database-handling.json', 100);
// Use async function
async function AwaitFunction () {
const v = await JsonHandling.getValue("dataA");
const object = await JsonHandling.getData(({ dataA, dataC }) => ({ dataA, dataC }));
console.info(v, object); // console output: 21 {"dataA":21, "dataB": 56, "dataC": "instant"}
};
AwaitFunction();
// Or use then
JsonHandling.getValue("dataA").then(v => { console.info(a) }); // console output: 21
JsonHandling.getData(({ dataA, dataC }) => ({ dataA, dataC })).then(object => { console.info(object) }); // console output: { "dataA":21, "dataC": "instant" }