Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gxlmyacc/indexed-db-async
opera indexed-db with promise
https://github.com/gxlmyacc/indexed-db-async
async indexed-db promise
Last synced: about 4 hours ago
JSON representation
opera indexed-db with promise
- Host: GitHub
- URL: https://github.com/gxlmyacc/indexed-db-async
- Owner: gxlmyacc
- License: mit
- Created: 2022-08-25T15:08:30.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-09T03:22:16.000Z (almost 2 years ago)
- Last Synced: 2024-11-13T17:54:38.038Z (3 days ago)
- Topics: async, indexed-db, promise
- Language: JavaScript
- Homepage:
- Size: 183 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# indexed-db-async
oprea indexed-db with promise.
[![NPM version](https://img.shields.io/npm/v/indexed-db-async.svg?style=flat)](https://npmjs.com/package/indexed-db-async)
[![NPM downloads](https://img.shields.io/npm/dm/indexed-db-async.svg?style=flat)](https://npmjs.com/package/indexed-db-async)## install
```bash
npm install --save indexed-db-async
```
or
```bash
yarn add indexed-db-async
```## usage
1. open db
```js
// a filter.js
import IndexedDBAsync from 'indexed-db-async';const db = new IndexedDBAsync('indexed-db-test', 1, [
{
name: 'table1',
key: 'url',
// autoIncrement: true,
indexes: [
{ name: 'timestamp', unique: false }
]
}
]);// add/update data
db.put('table1', { url: 'url1', timestamp: (new Date()).now() });
```2. add/update data
```js
for (let i = 1; i < 10; i++) {
const count = await db.put('table1', { url: `url${i}`, data: 'data' + i, timestamp: Date.now() });
console.log('put count: ' + count);
}const count = await db.add(
'table1',
[
{ url: `url1`, data: 'data1', timestamp: Date.now() },
{ url: `url2`, data: 'data2', timestamp: Date.now() },
{ url: `url3`, data: 'data3', timestamp: Date.now() }
]
);const count = await db.put(
'table1',
[
{ url: `url1`, data: 'data1', timestamp: Date.now() },
{ url: `url2`, data: 'data2', timestamp: Date.now() },
{ url: `url3`, data: 'data3', timestamp: Date.now() }
]
);
```3. get data
```js
const item = await db.get('table1', `url1`);
console.log(`get item`, item);// get all data
const itemList = await db.all('table1');
console.log(`get itemList`, itemList);
```4. query data
```js
// query data by key
const itemList = await db.query('table1', `url1`);
console.log(`query item list `, itemList);// query keys
const keys = await db.queryKeys('table1', '> "url1"');
console.log(`query keys `, keys);// query data by index
const itemList = await db.query('table1', 'timestamp > 12314234234');
console.log(`query item list `, itemList);// query data by index using IDBKeyRange
const itemList = await db.query('table1', IDBKeyRange.lowerBound(0));
console.log(`query item list `, itemList);
```4. query table count
```js
const count = await db.count('table1');
console.log(`table count`, count);
```5. clear table
```js
await db.clear('table1');
console.log(`table cleared, current count:`, await db.count(table1));
```6. delete table item
```js
await db.delete('table1', 'url1');// delete datas
await db.delete('table1', ['url1', 'url2', 'url3']);// delete data using IDBKeyRange
await db.deleteRange('table1', '> "url1"');// delete data by index
await db.deleteRange('table1', 'timestamp = 12314234234');// delete data by index using IDBKeyRange
await db.deleteRange('table1', 'timestamp > 0');await db.deleteRange('table1', `timestamp >= 0 && < ${new Date().now()}`);
```
7. create IDBKeyRange by text
```js
db.range('<= x') // => IDBKeyRange.upperBound('x')
db.range('< x') // => IDBKeyRange.upperBound('x', true)
db.range('>= x') // => IDBKeyRange.lowerBound('x')
db.range('> x') // => IDBKeyRange.lowerBound('x', true)
db.range('>= x && <= y') // => IDBKeyRange.bound('x', 'y')
db.range('> x && < y') // => IDBKeyRange.bound('x', 'y', true, true)
db.range('> x && <= y') // => IDBKeyRange.bound('x', 'y', true)
db.range('>= x && < y') // => IDBKeyRange.bound('x', 'y', false, true)
db.range('= x') // => IDBKeyRange.only('x')
```
range text format: `[indexName] >|<|>=|<= value1 [&& >|<|>=|<= value2]`