Skip to content

lazaromx/keyvalues-storage

Repository files navigation


KeyValues Storage

DeepScan grade CodeFactor CodeQL Publish

lodash write-file-atomic jest

paypal url kofi url liberapay url releases url license url

Summary

The KeyValues class is responsible for managing key-value pairs and storing them in a JSON file. It provides methods for setting, getting, checking existence, and removing key-value pairs. The class uses the Functions class to handle file operations and data manipulation.

Example Usage

// Create a new instance of KeyValues with custom options
const keyValues = new KeyValues({
  fileName: 'config.json',
  prettify: true
});

-- or --

/**
 * Default Options
 * 
 * {
 *   atomicSave: true,
 *   fileName: 'keyvalues.json',
 *   prettify: false,
 *   numSpaces: 2,
 * }
 */
const keyValues = new KeyValues()

// Set a key-value pair
await keyValues.set('color.name', 'sapphire');

// Get the value at a specific key path
const value = await keyValues.get('color.name');

// Check if a key path exists
const exists = await keyValues.has('color.name');

// Remove a key-value pair
await keyValues.unset('color.name');

Code Analysis

Main functionalities

  • Manage key-value pairs and store them in a JSON file
  • Set and get values at specific key paths
  • Check if a key path exists
  • Remove key-value pairs

Methods

  • constructor(options?: Partial<Options>): Initializes a new instance of the KeyValues class with optional custom options.
  • file(): string: Returns the path to the JSON file.
  • reset(): void: Resets the configuration of the KeyValues instance to default options.
  • has(keyPath: KeyPath): Promise<boolean>: Checks if a key path exists asynchronously.
  • hasSync(keyPath: KeyPath): boolean: Checks if a key path exists synchronously.
  • get<T extends valueTypes>(keyPath?: KeyPath): Promise<T>: Gets the value at a specific key path asynchronously.
  • getSync<T extends valueTypes>(keyPath?: KeyPath): T: Gets the value at a specific key path synchronously.
  • set<T extends valueTypes>(...args: [Types<T>] | [KeyPath, T]): Promise<void>: Sets a value at a specific key path asynchronously.
  • setSync<T extends valueTypes>(...args: [Types<T>] | [KeyPath, T]): void: Sets a value at a specific key path synchronously.
  • unset(keyPath?: KeyPath): Promise<void>: Removes a key-value pair at a specific key path asynchronously.
  • unsetSync(keyPath?: KeyPath): void: Removes a key-value pair at a specific key path synchronously.

Fields

  • options: Options: The configuration options for the KeyValues instance.
  • fnc: Functions: An instance of the Functions class used for file operations and data manipulation.

Dependencies

  • lodash: The Lodash library exported as Node.js modules.
  • write-file-atomic: Atomically and asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer

Contributing

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to project!

Made with contrib.rocks.

That said, there's a bunch of ways you can contribute to this project, like by:

  • 🪲 Reporting a bug
  • 📄 Improving this documentation
  • 🚨 Sharing this project and recommending it to your friends
  • 💵 Supporting this project on GitHub Sponsors or Ko-fi
  • 🌟 Giving a star on this repository

Donate

If you appreciate that, please consider donating to the Developer.

paypal url kofi url liberapay url github sponsors url

License

MIT © Heliomar P. Marques 🔝

About

A simple and robust KeyValues Storage's library

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 86.1%
  • JavaScript 13.9%