Giter Site home page Giter Site logo

feat7 / client-persist Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 3.0 308 KB

Offline storage for your web client. Supports IndexedDB, WebSQL, localStorage and sessionStorage with an easy to crawl with API.

License: Other

JavaScript 93.27% HTML 3.64% TypeScript 3.09%
localforage indexeddb websql localstorage sessionstorage

client-persist's Introduction

client-persist

This is fork of localForage with support of sessionStorage

Offline storage for your web client. Supports IndexedDB, WebSQL, localStorage and sessionStorage with an easy to crawl with API.

Installation

yarn add client-persist

or, if you use npm

npm install client-persist

Usage

import clientPersist from 'client-persist';

// to use sessionStorage
clientPersist.setDriver(clientPersist.SESSIONSTORAGE)

A minimal example with React

import React from "react";
import clientPersist from "client-persist";

clientPersist.setDriver(clientPersist.SESSIONSTORAGE);

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      value: "Client Persist"
    };
    clientPersist.getItem("value").then(value => {
      if (value !== null) this.setState({ value });
    });
  }
  storeValue(event) {
    clientPersist.setItem("value", event.target.value)
    .then(() => console.log('--saved--'));
    this.setState({
      value: event.target.value
    });
  }
  render() {
    return (
      <div>
        <h1>Hello {this.state.value}!<h1>
        <h2>Edit and reload the browser!</h2>
        <input
          type="text"
          onChange={event => this.storeValue(event)}
          value={this.state.value}
        />
      </div>
    );
  }
}

Edit k983rnkl3v

Documentation

Documentation of localForage

client-persist provides additonal driver for sessionStorage on localForage.

sessionStorage driver can be selected by setDrvier method.

clientPersist.SESSIONSTORAGE is driver for sessionStorage compatibility.

Store and Retrive data

import clientPersist from 'client-persist';

//Set sessionStorage driver
clientPersist.setDriver(clientPersist.SESSIONSTORAGE);

// Driver options
// clientPersist.LOCALSTORAGE, clientPersist.WEBSQL,
// clientPersist.INDEXEDDB, clientPersist.SESSIONSTORAGE

clientPersist.setItem('keyName', 'This is some data')
.then(() => console.log('Data saved');

clientPersist.getItem('keyName')
.then(value => console.log(value));

License

See LICENSE file.

More

See localForage for more details.

client-persist's People

Contributors

feat7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

client-persist's Issues

Add more examples

There is a lack of documentation right now.
Need to provide more usage examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.