simple-properties-db
v1.1.0
Published
A simple file-based key-value properties database for Node.js
Downloads
42
Maintainers
Readme
Simple Properties DB
A lightweight Node.js library for managing key-value properties in a file-based database. It supports parsing, retrieving, updating, and deleting properties while preserving comments and formatting.
Features
- File-Based Storage: Uses a
.propertiesfile for storing key-value pairs. - Supports Multiple Data Types: Handles strings, numbers, and booleans.
- Comment Preservation: Keeps comments intact when modifying the file.
- Simple API: Easy-to-use methods for managing properties.
Installation
npm install simple-properties-dbUsage
Import the Library
const SimplePropertiesDB = require('simple-properties-db');Initialize the Database
const db = new SimplePropertiesDB('./config'); // Path to the directory where the file will be createdThis will create a db.properties file in the specified directory if it doesn't already exist.
Set a Value
db.set('username', 'johndoe');
db.set('age', 30);
db.set('isAdmin', true);Get a Value
const username = db.get('username'); // 'johndoe'
const age = db.get('age'); // 30
const isAdmin = db.get('isAdmin'); // trueGet All Values
const allValues = db.getAll();
console.log(allValues);
// Output: { username: 'johndoe', age: 30, isAdmin: true }Delete a Value
db.delete('username');
console.log(db.get('username')); // undefinedPreserve Comments
If the .properties file contains comments, they will be preserved when modifying the file:
# This is a comment
username=johndoe
# Another comment
age=30API Reference
constructor(dbPath, options)
dbPath: Path to the directory where the.propertiesfile will be stored. Defaults to the current working directory.options: (Optional) Additional configuration options.
set(key, value)
Sets a key-value pair in the database.
key: The key to set.value: The value to associate with the key (string, number, or boolean).
get(key)
Retrieves the value associated with a key.
key: The key to retrieve.
getAll()
Retrieves all key-value pairs as an object.
delete(key)
Deletes a key-value pair from the database.
key: The key to delete.
Example .properties File
# User configuration
username=johndoe
age=30
isAdmin=trueTesting
Run the tests using Jest:
npm testLicense
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
Author
Developed by sjh.
