Skip to content

JongHyeonSong/simple-properties-db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 .properties file 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-db

Usage

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 created

This 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'); // true

Get 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')); // undefined

Preserve Comments

If the .properties file contains comments, they will be preserved when modifying the file:

# This is a comment
username=johndoe
# Another comment
age=30

API Reference

constructor(dbPath, options)

  • dbPath: Path to the directory where the .properties file 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=true

Testing

Run the tests using Jest:

npm test

License

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.

About

simple nodejs filebase db

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published