npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

persist-it

v0.0.1

Published

A persist-it library

Downloads

1

Readme

persist-it

LICENSE Node CodeFactor

Coverage Status Snyk Vulnerabilities for GitHub Repo

Version Downloads

GitHub Stars GitHub Forks GitHub Discussions GitHub closed pull requests GitHub closed issues

"persist-it" is a Node.js library that provides a flexible and easy-to-use persistence solution for storing key-value pairs in a persistent storage system. It allows you to store and retrieve data persistently, making it suitable for various use cases where persistent data storage is required.

Features

  • Store key-value pairs persistently
  • Create separate storage instances for general use or app-specific use
  • Ability to initialize storage with customizable options
  • Automatic caching of frequently accessed data for improved performance
  • Asynchronous API using Promises for non-blocking operations

Installation

You can install "persist-it" via npm or yarn. Run the following command to install it:

NPM

npm install persist-it

Yarn

yarn add --dev persist-it

Adding to your project

There are 3 ways to include and use it in your project, most projects will only use the first way (singleton instance).

Singleton Instance

For easy of use persist-it provides an instance that you can initialize once and can be imported/required anywhere.

import { persistIt } from 'persist-it';
persistIt.init({ directory: 'user', appName: 'MyApp' }); // init once in your app

// use it anywhere
persistIt.set('namespace', {x:1,y:[1,2,3]});
persistIt.setValue('namespace', 'foo.bar', 'value');
const result = await persistIt.getValue('namespace', 'foo.bar');
console.log(result);

Storage for App

You can still create your own instances if you need multiple scoped instances, or just want to have more control of life cycle. Each instance keeps its own caching.

import { PersistItApp } from 'persist-it';

const appStorage = new PersistItApp();
appStorage.init({ directory: 'user', appName: 'MyApp' });
appStorage.set('key', 'value');
const result = appStorage.get('key');
console.log(result); // Output: value

General Storage

If you don't need the multiplatform, appDirs directory resolution, or automatically determine the name, author, and version. You can create your own general purpose storagge instance directly, just passing a specific path to where to store files.

import { PersistIt } from 'persist-it';

const storage = new PersistIt('~/custom/dir/foo');

storage.set('namespace', {x:1,y:[1,2,3]});
storage.setValue('namespace', 'foo.bar', 'value');
const result = await storage.getValue('namespace', 'foo.bar');
console.log(result);

Options

  • directory: Specifies the mode for selecting the storage location (user | shared | custom).
  • path: The path to use when the directory is set to custom.
  • appName: The custom app name (defaults to package.json name).
  • appAuthor: The custom app author (defaults to package.json author).
  • appVersion: The version to append (defaults to package.json major version v#). Set to false to disable versioning.
  • folder: The folder name inside the directory.
  • readPackageJson: Specifies whether to attempt to determine appName, appAuthor, and appVersion from package.json.
  • preload: Preloads data from disk to memory during initialization.

Methods

Initialization

init(options: InitializationOptions): void

Initializes the persistence mechanism with the specified options.

  • options: An object containing the initialization options.
    • directory (string, required): The directory where the data will be stored.
    • preload (boolean, optional, default: true): Determines whether to preload data from disk to memory during initialization.

Throws an error if the directory option is not specified.

Asynchronous Methods

get(key: string): Promise

Retrieves the value associated with the specified key from the persistence mechanism.

  • key (string): The key to retrieve the value for.

Returns a Promise that resolves to the value associated with the key. If the key is not found, undefined is returned.

set(key: string, value: any): Promise

Sets a value for the specified key in the persistence mechanism.

  • key (string): The key to set.
  • value (any): The value to associate with the key.

Returns a Promise that resolves once the value is successfully set.

delete(key: string): Promise

Deletes the key-value pair associated with the specified key from the persistence mechanism.

  • key (string): The key to delete.

Returns a Promise that resolves once the key-value pair is successfully deleted.

getValue(key: string, path: string, defaultValue?: any): Promise

Retrieves a nested value from an object stored with the specified key in the persistence mechanism.

  • key (string): The key to retrieve the object from.
  • path (string): The path to the nested value within the object.
  • defaultValue (any, optional): The default value to return if the nested value is not found.

Returns a Promise that resolves to the retrieved nested value or the defaultValue if the nested value is not found.

setValue(key: string, path: string, value: any): Promise

Sets a nested value within an object stored with the specified key in the persistence mechanism.

  • key (string): The key to retrieve the object from.
  • path (string): The path to the nested value within the object.
  • value (any): The value to set for the nested path.

Returns a Promise that resolves once the nested value is successfully set.

flush(): Promise

Writes any pending changes in the write queue to the persistent storage.

Returns a Promise that resolves once the pending changes are successfully written.

Synchronous Methods

The synchronous methods provide the same functionality as their asynchronous counterparts but operate synchronously without returning Promises. These methods are suitable for cases where synchronous execution is desired.

getSync(key: string): any

Retrieves the value associated with the specified key from the persistence mechanism synchronously.

  • key (string): The key to retrieve the value for.

Returns the value associated with the key. If the key is not found, undefined is returned.

setSync(key: string, value: any): void

Sets a value for the specified key in the persistence mechanism synchronously.

  • key (string): The key to set.
  • value (any): The value to associate with the key.

deleteSync(key: string): void

Deletes the key-value pair associated with the specified key from the persistence mechanism synchronously.

  • key (string): The key to delete.

getValueSync(key: string, path: string): any

Retrieves a nested valuefrom an object stored with the specified key in the persistence mechanism synchronously.

  • key (string): The key to retrieve the object from.
  • path (string): The path to the nested value within the object.

Returns the retrieved nested value or undefined if the nested value is not found.

setValueSync(key: string, path: string, value: any): void

Sets a nested value within an object stored with the specified key in the persistence mechanism synchronously.

  • key (string): The key to retrieve the object from.
  • path (string): The path to the nested value within the object.
  • value (any): The value to set for the nested path.

flushSync(): void

Writes any pending changes in the write queue to the persistent storage synchronously.


Contributing

Contributions to "persist-it" are welcome! If you find any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request.

Before contributing, please make sure to read the CONTRIBUTING.md.

License

The code is available under the MIT license.