UNPKG

3.4 kBTypeScriptView Raw
1import { Config } from "./lib/JsonDBConfig";
2export declare type FindCallback = (entry: any, index: number | string) => boolean;
3export declare class JsonDB {
4 private loaded;
5 private data;
6 private readonly config;
7 /**
8 * JSONDB Constructor
9 * @param filename where to save the "DB". Can also be used to give the whole configuration
10 * @param saveOnPush save the database at each push command into the json file
11 * @param humanReadable the JSON file will be readable easily by a human
12 * @param separator what to use as separator
13 */
14 constructor(filename: string | Config, saveOnPush?: boolean, humanReadable?: boolean, separator?: string);
15 /**
16 * Process datapath into different parts
17 * @param dataPath
18 */
19 private processDataPath;
20 private retrieveData;
21 private getParentData;
22 /**
23 * Get the wanted data
24 * @param dataPath path of the data to retrieve
25 */
26 getData(dataPath: string): any;
27 /**
28 * Same as getData only here it's directly typed to your object
29 * @param dataPath path of the data to retrieve
30 */
31 getObject<T>(dataPath: string): T;
32 /**
33 * Check for existing datapath
34 * @param dataPath
35 */
36 exists(dataPath: string): boolean;
37 /**
38 * Returns the number of element which constitutes the array
39 * @param dataPath
40 */
41 count(dataPath: string): number;
42 /**
43 * Returns the index of the object that meets the criteria submitted.
44 * @param dataPath base dataPath from where to start searching
45 * @param searchValue value to look for in the dataPath
46 * @param propertyName name of the property to look for searchValue
47 */
48 getIndex(dataPath: string, searchValue: (string | number), propertyName?: string): number;
49 /**
50 * Find all specific entry in an array/object
51 * @param rootPath base dataPath from where to start searching
52 * @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
53 */
54 filter<T>(rootPath: string, callback: FindCallback): T[] | undefined;
55 /**
56 * Find a specific entry in an array/object
57 * @param rootPath base dataPath from where to start searching
58 * @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
59 */
60 find<T>(rootPath: string, callback: FindCallback): T | undefined;
61 /**
62 * Pushing data into the database
63 * @param dataPath path leading to the data
64 * @param data data to push
65 * @param override overriding or not the data, if not, it will merge them
66 */
67 push(dataPath: string, data: any, override?: boolean): void;
68 /**
69 * Delete the data
70 * @param dataPath path leading to the data
71 */
72 delete(dataPath: string): void;
73 /**
74 * Only use this if you know what you're doing.
75 * It reset the full data of the database.
76 * @param data
77 */
78 resetData(data: any): void;
79 /**
80 * Reload the database from the file
81 */
82 reload(): void;
83 /**
84 * Manually load the database
85 * It is automatically called when the first getData is done
86 */
87 load(): void;
88 /**
89 * Manually save the database
90 * By default you can't save the database if it's not loaded
91 * @param force force the save of the database
92 */
93 save(force?: boolean): void;
94}