Abstraction around IndexedDB storage to provide the same simple interface as
localStorage. Because I'm lazy and I don't want to deal with real database
magic. It will create a database with a single table. Both the database and
the table will have the name you pass to connect
. The key-value pairs will
be stored in that table.
Main differences with the localStorage API: this is async and it can store complicated objects, not just strings.
- Source:
Example
import IndexedDB from 'indexedDB.js';
const db = await IndexedDB.connect("my-database");
await db.setItem('some key', 'some value');
console.log(await db.getItem('some key'));
await db.removeItem('some key');
Methods
(async) getItem(key) → {Promise.<any>}
Get the value that belongs to this key from the database
Parameters:
Name | Type | Description |
---|---|---|
key |
string | What to retrieve |
- Source:
Returns:
A promise that resolves to the value
- Type
- Promise.<any>
(async) removeItem(key) → {Promise}
Delete the value that belongs to this key from the database
Parameters:
Name | Type | Description |
---|---|---|
key |
string | What to delete |
- Source:
Returns:
A promise that resolves if the value is deleted
- Type
- Promise
(async) setItem(key, value) → {Promise}
Store or overwrite the value
for the given key
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Where to store the value |
value |
any | The value to store |
- Source:
Returns:
A promise that resolves if the value is stored
- Type
- Promise
(async, static) connect(name) → {Promise.<IndexedDB>}
Connect to an IndexedDB database
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of the database and the table to use |
- Source:
Returns:
A promise to a connected database
- Type
- Promise.<IndexedDB>