NodeDB is a simple key-value store for Node.js, built on top of SQLite.
You can install NodeDB using npm:
npm install @ajayos/nodedb- Install NodeDB using npm:
npm install @ajayos/nodedb- Import and create an instance of the DB class with a custom database file path:
const DB = require('@ajayos/nodedb');
const customDBPath = '/path/to/custom/database/file.db';
const nodedb = new DB(customDBPath);- Use the available functions to interact with the database, as demonstrated in the examples below.
NodeDB provides an efficient way to manage key-value data in your Node.js applications by leveraging the power of SQLite. It offers a lightweight and embedded database solution suitable for small to medium-sized projects. Whether you need to store user profiles, configuration settings, or any other structured data, NodeDB simplifies the process by providing easy-to-use functions for storing, retrieving, and deleting data.
Inserts or updates a row in the specified table with the specified key and data.
await nodedb.setDB('users', 'ajay', { name: 'Ajay o s', age: 20 });Retrieves a row from the specified table with the specified key, or all rows if no key is specified.
const ajay = await nodedb.getDB('users', 'ajay');
console.log(ajay); // { name: 'Ajay o s', age: 20 }const allUsers = await nodedb.getDB('users');
console.log(allUsers); // [ { rowName: 'ajay', data: { name: 'Ajay o s', age: 20 } }, ... ]Deletes a row from the specified table with the specified key, or the entire table if no key is specified.
await nodedb.deleteDB('users', 'ajay');await nodedb.deleteDB('users');Similar to setDB, but stores data without JSON stringifying it.
await nodedb.setDATA('metadata', 'version', 1.2);Similar to getDB, but retrieves data without JSON parsing it.
const version = await nodedb.getDATA('metadata', 'version');
console.log(version); // 1.2const allVersions = await nodedb.getDATA('metadata');
console.log(allVersions); // [ { rowName: 'version', data: 1.2 }, ... ]Similar to deleteDB, but deletes data without JSON parsing it.
await nodedb.deleteDATA('metadata', 'version');await nodedb.deleteDATA('metadata');Example Here's a complete example of how to use NodeDB to interact with a database:
const DB = require('@ajayos/nodedb');
// Define a custom database file path
const customDBPath = 'mydatabase.sql';
// Create an instance of the DB class
const nodedb = new DB(customDBPath);
async function main() {
// Set data using setDB
await nodedb.setDB('users', 'ajay', { name: 'Ajay o s', age: 20 });
// Get data using getDB
const ajay = await nodedb.getDB('users', 'ajay');
console.log('Retrieved Data:', ajay);
// Delete a specific row using deleteDB
await nodedb.deleteDB('users', 'ajay');
}
// Run the main function
main().catch((error) => {
console.error('An error occurred:', error);
});For functions like deleteDB and deleteDATA, if you provide only the tableName, the entire table will be deleted. To delete a specific row, provide both tableName and rowName.
NodeDB is licensed under the Apache License 2.0. See the LICENSE file for details.
You can find the repository for NodeDB on GitHub at https://github.com/Ajayos/nodedb