Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ljans/idb
Promise-based wrapper around the IndexedDB API.
https://github.com/ljans/idb
idb indexeddb promise wrapper
Last synced: 11 days ago
JSON representation
Promise-based wrapper around the IndexedDB API.
- Host: GitHub
- URL: https://github.com/ljans/idb
- Owner: ljans
- License: mit
- Created: 2018-11-04T13:26:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T09:44:03.000Z (over 1 year ago)
- Last Synced: 2024-11-23T15:33:24.941Z (2 months ago)
- Topics: idb, indexeddb, promise, wrapper
- Language: JavaScript
- Homepage: https://ljans.github.io/idb
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# IDB
Promise-based wrapper around the [IndexedDB](https://developer.mozilla.org/de/docs/IndexedDB) API.### Getting started
You would mostly want to make use of the IDB inside a [ServiceWorker](https://developers.google.com/web/fundamentals/primers/service-workers/):
```javascript
self.importScripts('idb.js');
```
Open a new IDB connection and setup the tables you need for your project:
```javascript
var idb = new IDB({
table1: {},
table2: {keyPath: 'id'},
table3: {autoIncrement: true},
}, config);
```
The following settings can be passed in the optional `config` object:
- `name` sets the Database name (defaults to "IDB")
- `version` provides the version of the table structure (defaults to 1)
- An increment in version triggers a database upgrade
- `upgrade` defines a function to perform when a database upgrade was performedYou can pass the follwing options for each table:
- `keyPath` specifies a "primary key" of inserted objects
- `autoIncrement` determines, whether inserted objects get an incrementing numeric keyThe defined tables will be available for operations in the newly created `idb` object like `idb.table1`. Remember that all operations return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
#### Inserting data
- `idb..put({id: 'foo', a: 'b'})`
- Inserts the object with key "foo" when using `keyPath: 'id'`
- Inserts the object with an automatically incremented key when using `autoIncrement: true`
- `idb..put('value')`
- Inserts the value with an automatically incremented key when using `autoIncrement: true`
- `idb..put({id: 'foo', a: 'b'}, 'bar')`
- Inserts the object with key "bar" independent of `autoIncrement` but only without `keyPath`#### Selecting data
- `idb..get('foo')`
- Selects the object or value with key "foo" (also works with numeric keys)
- `idb..all(filter)`
- Applies a provided filtering function on all entries to decide which ones will be selected
- Example: `idb..all(item => item.amount > 5)`#### Deleting data
- `idb..delete('foo')`
- Deletes the object or value with key "foo" (also works with numeric keys)
- `idb..clear()`
- Deletes all entries in the table