Skip to content

Emmo00/persisty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

persisty 💾

Simple Browser Local storage (and Session Storage) Abstraction.

Helps you work with browser localStorage (and sessionStorage) as a regular JavaScript object. It does this by allowing you use regular javascript object syntax.

Installation

To use it just install from npm with the following command:

npm install persisty

Usage

Working with localStorage

When working with localStorage, you should import it as default like below:

import persisty

// import persisty
import persisty from 'persisty';

or as a named import:

import persistLocal

import { persistLocal } from 'persisty';

Note

You have the flexibility to use either the import of persisty or persistLocal, as they refer to the same functionality.

store

const orders = [
  { id: 1, quantity: 2 },
  { id: 2, quantity: 1 },
];

persisty.orders = orders // Also saved in you browser localStorage

// or use persistLocal from named import:
persistLocal.orders = orders

get

console.log(persisty.orders);
// Output:
// [
//   { id: 1, quantity: 2 },
//   { id: 2, quantity: 1 },
// ];

delete

delete persisty.orders

console.log(persisty.orders); // null

Working with sessionStorage

Import persistSession to interact with sessionStorage.

import persistSession

// import persisty
import { persistSession } from 'persisty';

store

const orders = [
  { id: 1, quantity: 2 },
  { id: 2, quantity: 1 },
];

persistSession.orders = orders // Also saved in you browser sessionStorage

get

console.log(persistSession.orders);
// Output:
// [
//   { id: 1, quantity: 2 },
//   { id: 2, quantity: 1 },
// ];

delete

delete persistSession.orders

console.log(persisty.orders); // null

Note

persisty does not support deep mutation (yet).

For example:

console.log(persisty.orders[0]) // { id: 1, quantity: 2 }
persisty.orders[0].id = 123
console.log(persisty.orders[0]) // { id: 1, quantity: 2 }

Won't work.

Contribute

Issues/Feature request and PRs are welcome.

License

MIT License