Type definitions for node-json-db (#12319)

* Type definitions for node-json-db

* declaration of module 'node-json-db' was removed
This commit is contained in:
Ilya Kuznetsov 2016-11-01 16:13:01 +03:00 committed by Masahiro Wakame
parent 788c758591
commit 5661e33ce1
3 changed files with 151 additions and 0 deletions

57
node-json-db/index.d.ts vendored Normal file
View File

@ -0,0 +1,57 @@
// Type definitions for node-json-db
// Project: https://github.com/Belphemur/node-json-db
// Definitions by: Ilya Kuznetsov <https://github.com/kuzn-ilya>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare class JsonDB {
/**
* Create the JSON database
* @param filename where to save the data base
* @param saveOnPush saving on modification of the data
* @param humanReadable is the json file humand readable
* @returns {JsonDB}
* @constructor
*/
constructor(filename: string, saveOnPush?: boolean, humanReadable?: boolean);
/**
* Get the deta stored in the data base
* @param dataPath path leading to the data
* @returns {*}
*/
getData(dataPath: string): any;
/**
* Pushing data into the database
* @param dataPath path leading to the data
* @param data data to push
* @param override overriding or not the data, if not, it will merge them
*/
push(dataPath: string, data: any, override?: boolean): void;
/**
* Delete the data
* @param dataPath path leading to the data
*/
delete(dataPath: string): void;
/**
* Reload the database from the file
*/
reload(): void;
/**
* Manually load the database
* It is automatically called when the first getData is done
*/
load(): void;
/**
* Manually save the database
* By default you can't save the database if it's not loaded
* @param force force the save of the database
*/
save(force?: boolean): void;
}
export = JsonDB;

View File

@ -0,0 +1,75 @@
import JsonDB = require('node-json-db');
// 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)
let db = new JsonDB("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();

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"node-json-db-tests.ts"
]
}