From 5661e33ce14d477b7e49bfdbefffeca41ce3623a Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Tue, 1 Nov 2016 16:13:01 +0300 Subject: [PATCH] Type definitions for node-json-db (#12319) * Type definitions for node-json-db * declaration of module 'node-json-db' was removed --- node-json-db/index.d.ts | 57 +++++++++++++++++++++++ node-json-db/node-json-db-tests.ts | 75 ++++++++++++++++++++++++++++++ node-json-db/tsconfig.json | 19 ++++++++ 3 files changed, 151 insertions(+) create mode 100644 node-json-db/index.d.ts create mode 100644 node-json-db/node-json-db-tests.ts create mode 100644 node-json-db/tsconfig.json diff --git a/node-json-db/index.d.ts b/node-json-db/index.d.ts new file mode 100644 index 0000000000..9a6dfa6160 --- /dev/null +++ b/node-json-db/index.d.ts @@ -0,0 +1,57 @@ +// Type definitions for node-json-db +// Project: https://github.com/Belphemur/node-json-db +// Definitions by: Ilya Kuznetsov +// 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; diff --git a/node-json-db/node-json-db-tests.ts b/node-json-db/node-json-db-tests.ts new file mode 100644 index 0000000000..ac7d3e37e0 --- /dev/null +++ b/node-json-db/node-json-db-tests.ts @@ -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(); \ No newline at end of file diff --git a/node-json-db/tsconfig.json b/node-json-db/tsconfig.json new file mode 100644 index 0000000000..410b9e0bcd --- /dev/null +++ b/node-json-db/tsconfig.json @@ -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" + ] +} \ No newline at end of file