Socket
Socket
Sign inDemoInstall

paolodelfino-store

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    paolodelfino-store

A powerful and easy-to-use storage manager for managing `localStorage`, `sessionStorage`, and in-memory storage in web. With built-in support for expiring keys, advanced search features, undo/redo capabilities, and easy-to-understand API, `paolodelfino-st


Version published
Maintainers
1
Created

Readme

Source

paolodelfino-store

OLD PROJECT

A powerful and easy-to-use storage manager for managing localStorage, sessionStorage, and in-memory storage in web. With built-in support for expiring keys, advanced search features, undo/redo capabilities, and easy-to-understand API, paolodelfino-store provides a seamless solution for all your storage needs.

🕒 5 Minutes Read but Worth It

Features

  • Manage localStorage, sessionStorage, and in-memory storage with a single API
  • Powerful, complete and easy-to-use DOT Notation
  • Set expiry dates for keys in the storage
  • Advanced search capabilities, including searching by path, query, and options (case sensitivity, searching keys or values, and more) and an easier implementation for simple searches
  • Undo and redo capability to track changes and revert them when needed
  • Built-in event system to listen for changes in the storage
  • Import and export storages for easy data migration and backup
  • Fluent and easy-to-understand API

Installation

<script src="store.js"></script>

or

<script src="store.min.js"></script>

Usage

Create a new Store

const myStore = new Store("myStorage", "local");

Set a value in the Store

myStore.set("user.name", "John Doe");

Get a value from the Store

const username = myStore.get("user.name");

Reconnect to the storage

const anotherInstance = new Store("myStorage", "local");

or Create a new storage

const newStorage = new Store("newStorage", "local");

and Comeback to the other one whenever you want

const myStorage = new Store("myStorage", "local");

Check if a key exists in the Store

const hasUsername = myStore.has("user.name"); // true

Set expiry date for a key

// Expires in an hour
const expiresDate = new Date().getTime() + 60 * 60 * 1000;
myStore.expires("user.name", expiresDate);

Clear the Store

myStore.clear();

Undo and Redo

myStore.undo(); // Revert last change
myStore.redo(); // Redo last reverted change

Search within the Store (with various options)

const results = myStore.search("john", "user", {
  caseSensitive: false,
  searchKeys: ["name"],
  retrieveParentObject: true,
  exactMatch: true,
  matchChars: false,
  byPage: {
    howManyResultsPerPage: 10,
    pageNumber: 1,
  },
});

and Search using Regular Expressions

const results = myStore.search("jo.+");

and Much More

Try

<script src="store.js"></script>

to discover more.

Events

myStore.onContentChanged.push(($key, $newValue, $oldValue) => {
  console.log(`Key "${$key}" changed from "${$oldValue}" to "${$newValue}".`);
});

myStore.onContentCleared.push(() => {
  console.log("Content cleared.");
});

Import and Export

// Export the store data
const exportedData = myStore.export(true); // true for exporting as a string, or false/omitted for an object

// Import data into the store
myStore.import(exportedData);

Dot Notation

paolodelfino-store supports dot notation for accessing nested keys in the storage. This allows you to access and manipulate nested objects and arrays easily.

Set a value using dot notation

To set a value using dot notation, simply pass the nested key as a string parameter in the set function. For example:

// Set a nested key using dot notation
myStore.set("user.name", "John Doe");

This will create the following structure in the storage:

{
  "user": {
    "name": "John Doe"
  }
}

So it also complete the path when setting a value. In this case it had to create user object, than created name and assign the value to it.

Get a value using dot notation

To get a value using dot notation, simply pass the nested key as a string parameter in the get function. For example:

// Get a nested key using dot notation
const username = myStore.get("user.name");

This will return the value of the user.name key from the storage.

Check if a key exists using dot notation

To check if a key exists using dot notation, simply pass the nested key as a string parameter in the has function. For example:

// Check if a nested key exists using dot notation
const hasUsername = myStore.has("user.name");

This will return true if the user.name key exists in the storage and false otherwise.

Search using dot notation

paolodelfino-store also supports dot notation for searching for keys in the storage. To search for a nested key in the storage, simply pass the nested key as a string parameter in the search function. For example:

// Search for a nested key using dot notation
const results = myStore.search("john", "users", {
  searchKeys: ["name"],
});

This will return an array of all the keys that contain the string john in their value, under the user.name key. It also order by the keys position in the array if provided.

Accessing arrays using dot notation

paolodelfino-store also supports accessing arrays using dot notation. To access an array element using dot notation, simply use the array index in the key. For example:

// Set an array element using dot notation
myStore.set("users.0.name", "John Doe");

This will create the following structure in the storage:

{
  "users": [
    {
      "name": "John Doe"
    }
  ]
}

You can then access the array element using dot notation in the same way:

// Get an array element using dot notation
const firstUsername = myStore.get("users.0.name");

This will return the value of the users[0].name key from the storage.

Conclusion

Using dot notation in paolodelfino-store allows you to easily access and manipulate nested objects and arrays in the storage. This feature makes it easier to work with complex data structures in your application.

Understand How-It-Works

masterDatabase (localStorage or sessionStorage, or an object for memory storages){
   paolodelfino-store: {
     storageUserCreated: {
       content: {},
       history: [],
       redoHistory: [],
       expiringDates: {}
     },
     storageUserCreated2: {
       content: {},
       history: [],
       redoHistory: [],
       expiringDates: {}
     }
   }
 }

Contributing

We welcome contributions, bug reports, and feature requests. Please feel free to open an issue or submit a pull request on the GitHub repository.

License

paolodelfino-store is released under the MIT License.

Support

If you find this library helpful or want to support the development, please consider giving it a star on GitHub. If you have any questions, feel free to reach out to the author or open an issue on the repository.

FAQs

Last updated on 09 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc