1.2.4 • Published 4 years ago

react-indexed-db-hooks v1.2.4

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

Install

Several quick start options are available:

  • Install with npm: npm install react-indexed-db-hooks
  • Install with yarn: yarn add react-indexed-db-hooks

Reqirement

  • Need a higher react 16.8 version

Development and demo

How to use

  • Project root init provide IndexedDBProvider.
  • Init initialState in databaseName, version
  • Init initialState in schemas indexedDB objectStores
import {IndexedDBProvider, IndexedDBReducer} from './indexedDBProvider';

<IndexedDBProvider
  initialState={{databaseName: 'user', version: 1, schemas: [
      {
        schema: 'info',
        autoIncrement: {keyPath: 'id', autoIncrement: true},
        indexes: [
          {name: 'name', keypath: 'name', options: {unique: false}},
          {name: 'phone', keypath: 'phone', options: {unique: true}},
          {name: 'gender', keypath: 'gender', options: {unique: false}}
        ],
      },
      {
        schema: 'auth',
        autoIncrement: {keyPath: 'id', autoIncrement: true},
        indexes: [
          {name: 'userId', keypath: 'userId', options: {unique: true}},
          {name: 'password', keypath: 'password', options: {unique: false}},
        ],
      },
    ]}}
  reducer={IndexedDBReducer}
>
  // child component
</IndexedDBProvider>
  • Use ObserveTransaction component (only insert, delete, clear observation)
import { ObserveTransaction } from 'react-indexed-db-hooks';

/*
  scheme : require (string)
  index : require (string)
  value : default null (string | number)
  loading : deafault false (boolean)
  loadingComponent default null (Component)
 */

<ObserveTransaction
  schema="info"
  index="gender"
  value="male">
  {({ result }) => {
    if (!!result) {
      return result.map((item, idx) => <div>
        <p>name: {item.name}</p>
        <p>phone: {item.phone}</p>
      </div>);
    } else {
      return <div>empty users..</div>;
    }
  }}
</ObserveTransaction>
  • import useIndexedDB in your project.
import { useIndexedDB } from 'react-indexed-db-hooks';

const {
  insert,
  findById,
  findByKey,
  findByValue,
  findAll,
  findAllKeys,
  count,
  update,
  deleteByKey,
  claer
} = useIndexedDB();

insert :

insert('info', {name: 'Hudson', phone: '010-1111-1111', gender: 'male' })
 .then(success => {
   consolel.log('success = ', success)
 })
 .catch(error => alert(error));

findById :

findById('info', 1)
   .then(success => console.log('success = ', success))
   .catch(error => alert(error));

findByKey :

findByKey('info', 'phone', '010-0000-0000')
 .then(success => console.log('success = ', success))
 .catch(error => alert(error));

findByValue :

findByValue('info', 'phone', '010-0000-0000')
  .then(success => console.log('success = ', success))
  .catch(error => alert(error));

findAll :

findAll('info')
 .then(success => console.log('success = ', success))
 .catch(error => alert(error));

findAllKeys :

findAllKeys('info')
  .then(success => console.log('success = ', success))
  .catch(error => alert(error));

count :

count('info')
  .then(success => console.log('success = ', success))
  .catch(error => alert(error));

update :

update('info', {
  name: 'kims-acount',
  phone: '010-0000-0000',
  gender: 'female',
  id: 3,
})
  .then(success => console.log('update kims-acount success!! = ', success))
  .catch(error => alert(error));

deleteByKey :

deleteByKey('info', 1)
  .then(success => console.log('key 1 is delete = ', success))
  .catch(error => alert(error));

deleteByValue :

deleteByKey('info', 'phone', '010-0000-0000')
  .then(success => console.log('kims-acount delete success!! = ', success))
  .catch(error => alert(error));

deleteAllIndexMatchValue:

deleteAllIndexMatchValue('info', 'gender', 'male')
  .then(success => console.log('male data all delete = ', success))
  .catch(error => alert(error));

clear :

clear('info')
  .then(success => console.log('info clear success!! = ', success))
  .catch(error => alert(error));

TODOS

  • cursor
  • create index, delete index
  • mutation
  • error, abort, loading, success
1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.2

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago