LiteJsonDB is a lightweight local JSON database for Node.js, designed to simplify JSON data management in your projects. This package is a fork of the Python LiteJsonDB project, adapted for Node.js. Currently, this project is about 45% complete, meaning it's already super functional but still has room for more cool features.
LiteJsonDB is like a nifty little vault for your JSON data. It provides a simple and intuitive API for adding, modifying, retrieving, and deleting data. You don't need to worry about the complexities of heavier database systems. With LiteJsonDB, you can focus on what really matters: your data.
To get started with LiteJsonDB, you need to install it via npm. It's super easy! Just run the following command in your terminal:
npm install litejsondb-node
Once installed, you can import JsonDB and initialize your database like this:
const JsonDB = require('litejsondb-node');
// Initialize a basic database
const db = new JsonDB();
// Initialize with a specific filename
const dbWithFilename = new JsonDB('my_database.json');
// Initialize with options
const dbWithOptions = new JsonDB('my_database.json', {
crypted: true, // Enables data encryption with base64 (default)
encryptionMethod: 'crypto', // Specify the encryption method ('base64', 'crypto'). If crypted = true, the default method will be base64.
secretKey: '12345678901234567890123456789012', // Secret key for encryption (required if crypted is true and encryptionMethod is 'crypto') must be 32-byte
enableLog: true, // Enables logging to file `LiteJsonDb.log` in `database` directory
autoBackup: true, // Enables automatic backup of the database before save
});
// If you want only crypted
const cryptedDb = new JsonDB('crypted_db.json',{crypted:true,secretKey: 'secret'});
Options Description:
filename: (Optional) Astringrepresenting the name of the JSON file used for storing the database. If no filename is provided, the default name isdatabase.jsonis used. The file is located indatabasedirectory.crypted: (Optional) Abooleanindicating whether the database should be encrypted or not. Default tofalse.encryptionMethod: (Optional) Astringrepresenting the method used for encryption, can bebase64orcrypto. Ifcryptedistrueand you don't provide an encryption method, the default will be 'base64'.secretKey: (Optional) Astringrepresenting the secret key used for encryption. Required ifcryptedistrueandencryptionMethodis set tocrypto. Must be a 32-byte string.enableLog: (Optional) Abooleanindicating whether to log database operations. Default totrue. Logs are saved indatabase/LiteJsonDb.log.autoBackup: (Optional) Abooleanindicating if auto backup is enabled. Default tofalse.
Adding data is a breeze with LiteJsonDB. Use the setData method to store information in the database:
db.setData('users/1', { name: 'Aliou', age: 30 });
db.setData('products/1', { name: 'Laptop', price: 999.99 });
Explanation:
- Key/Value Storage: Use the
setData(key, value)method to store your data in the database. The data is stored as a JSON object. - Data Format: The
valuemust be anobject, other types are not supported. - Key Structure: The
keyis a string representing the path for the new entry. The path is created recursively if it does not exist. If thekeyalready exists, the data is not added, you should useeditDatato modify it.
To update existing data, use editData. You can modify information without erasing whatβs already there:
db.editData('users/1', { age: 31 });
db.editData('products/1', {price: 1200})
Explanation:
- Merging Objects: The
editData(key, value)method merges existing data at the given path with the new data you provide. - Key Path: The
keyis a string representing the path for the entry to be edited. - Value Format: The
valuemust be an object. Non-object types are not supported. - Key Check: If the
keydoesn't exists an error will be returned. You should usesetDatato create a new entry.
The getData method allows you to retrieve data from the database. You can specify the exact path of the data you want to access. Hereβs how you can use it:
console.log('User 1:', db.getData('users/1'));
console.log('Product 1:', db.getData('products/1'));
Explanation:
- Path Specification: You use a key path like
'users/1'or'products/1'to retrieve specific entries from the database. The key path is a string that denotes the location of the data in the hierarchical structure of the database. - Default Behavior: If the specified path does not exist,
getDatawill returnnull. Ensure you handle such cases in your code to avoid errors. - Data Format: The returned data will be the value of the key or subcollection.
Need to delete data? No problem! The deleteData method allows you to remove specific information. You have two options:
-
Delete Specific Entry: If you want to delete a specific entry, provide the exact key path:
db.deleteData('products/1');This command will remove only the data located at
'products/1', leaving other data intact. -
Delete All Data Under a Path: If you want to delete all data under a specific key path, you can use a nested key path:
db.deleteData('products');This will remove the entire
'products'section, including all entries within it, such as'products/1'and any other nested products.
Explanation:
- Specific Entry Deletion: By providing a precise path like
'products/1', you delete only that particular entry. - Path-Wide Deletion: Using a broader path like
'products'deletes everything under that path. This is useful for clearing out all related data but should be used carefully to avoid unintentional data loss. - Key Check: An error is returned if the specified path does not exist.
You can also search your data with searchData. This helps you find specific values anywhere in the database:
const searchResults = db.searchData(db.showDb(), 'Aliou');
const searchResultsWithPath = db.searchData(db.showDb(), 30, 'users/1');
console.log(searchResults);
console.log(searchResultsWithPath);
Explanation:
- Search Value: The
searchData(data, searchValue, key)searches for thesearchValuein thedata. - Data: The first parameter
datais the dataset to be searched,db.showDb()for the entire database ordb.getData('your/path')for a specific data part. - Key: The third parameter
keyis optional, if provided the search will be done only in the given path. - Return: The method will return the matching values in
objectwith the path as the key. - Not Found: If no matches found an error will be displayed.
You can use setSubcollection, editSubcollection and getSubcollection methods to handle nested objects
-
setSubcollection(parentKey, subcollectionKey, subcollectionData): This allows you to add or update a sub-collection within a specified parent keydb.setSubcollection('users/1', 'address', { street: '123 Main St', city: 'Anytown' }); -
editSubcollection(parentKey, subcollectionKey, subcollectionData): This allows you to update an existing sub-collection within a specified parent keydb.editSubcollection('users/1', 'address', { country: 'USA' }); -
getSubcollection(parentKey, subcollectionKey): You can use this to retrieve either the entire subcollection or a specific part of itconsole.log('sub collection address : ', db.getSubcollection('users/1', 'address')); console.log('sub collections user 1 : ', db.getSubcollection('users/1')); -
deleteSubcollection(parentKey, subcollectionKey): Use this method to remove a specific sub-collection.db.deleteSubcollection('users/1','address')
LiteJsonDB provides functions for backing up and restoring the database:
-
backupDb(backupFile): Creates a backup of the database to the specifiedbackupFilein thedatabasedirectory.db.backupDb('backup.json') -
restoreDb(backupFile): Restores the database from the specifiedbackupFile.db.restoreDb('backup.json')
You can use setRegex to set a regex for the value of an entry and validateData to check if the values respect the regex that is previously set using setRegex :
-
setRegex(key, regexPattern): Allows you to define a regex pattern for a value of givenkeydb.setRegex('age', '^[0-9]+$'); -
validateData(data): Allows you to check if the data is valid according to the defined regex.db.validateData({age:22})- If a value does not respect the regex pattern an error will be returned.
-
showDb(): Returns the entire database object.console.log(db.showDb()); -
convertToDateTime(key): Converts a timestamp to a date time string.db.setData('date', new Date().getTime()) console.log(db.convertToDateTime('date'))- If the
keydoesn't exists or isn't valid anullvalue will be returned.
- If the
-
flattenJson(key): Flattens a nested JSON object to a one-level objectdb.setData('settings',{theme: {color: 'dark', size: 'large'},notifications:{email: true, sms:false}}) console.log(db.flattenJson('settings')); console.log(db.flattenJson(''));- If
keyis empty, it will flatten the entire database - If the
keydoesn't exists or isn't valid anullvalue will be returned.
- If
-
keyExists(key): Used to check if the key exists or notconsole.log("key exists:", db.keyExists('products/1'))
LiteJsonDB includes some handy utility functions:
hashPassword(password): Hashes the given password using SHA256.checkPassword(storedHash, password): Checks if the given password matches the stored hash.getOrDefault(data, key, defaultValue): Returns the value for the key if it exists in thedata, or thedefaultValueif the key doesn't exist.keyExistsOrAdd(data, key, defaultValue): Checks if the givenkeyexists in thedataand returnstrue. If not, it adds the key with the specifieddefaultValueand returnsfalse.searchData(data, searchValue, key): Searches for thesearchValuein the provideddata, and return the matches if it found. Thekeyis optional, if it's provided the search will be done only in the given pathsanitizeOutput(data): Returns a stringified JSON with a pretty print format.prettyPrint(data): Prints a pretty format of the JSON object.
Hereβs how you can use them:
const hashedPassword = db.hashPassword('myPassword123');
const isPasswordValid = db.checkPassword(hashedPassword, 'myPassword123');
const defaultValue = db.getOrDefault({test: 1}, 'country', 'Unknown');
const keyExists = db.keyExistsOrAdd({test:1}, 'country', 'Unknown');
const searchResult = db.searchData({test:1, test2: 'test'}, 'test');
const output = db.sanitizeOutput({test: 1});
db.prettyPrint({test: 1});
Hereβs a small example to show you how everything works together:
const JsonDB = require('litejsondb-node');
const db = new JsonDB();
// Set initial data
db.setData('users/1', { name: 'Aliou', age: 30 });
db.setData('products/1', { name: 'Laptop', price: 999.99 });
// Edit data
db.editData('users/1', { age: 31 });
// Get data
console.log('User 1:', db.getData('users/1'));
console.log('Product 1:', db.getData('products/1'));
// Delete Data
db.deleteData('products/1');
// Show the entire database content
console.log('Database Content:', db.showDb());
// Backup and restore the database
db.backupDb('backup.json');
db.restoreDb('backup.json');
LiteJsonDB is a fork of our Python LiteJsonDB package. Iβve managed to reproduce about 45% of the work so far, and it's already functional with features like adding, editing, and deleting data. However, thereβs still a lot to be done!
If you have skills in both Node.js and Python, you might want to dive into the code and contribute to the Node.js project. Currently, Iβm focused more on Python development and may not have enough time to add all the desired features to this package.
Your contributions could help move the project forward and make it even better. Feel free to explore the code and get involved!
If you want to contribute to the project, feel free to open issues or pull requests. Every contribution is welcome to help make this project even better!