# indexed-db-as-promised

> A thin Promise-like wrapper around IndexedDB

Latest version **0.1.0** (published 2018-07-03) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install indexed-db-as-promised
pnpm add indexed-db-as-promised
yarn add indexed-db-as-promised
bun add indexed-db-as-promised
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2018-07-03 |
| First published | 2018-05-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 430 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Justin Ridgewell |
| Maintainers | jridgewell |
| Keywords | indexed, db, promise |

## Links

- npm: https://www.npmjs.com/package/indexed-db-as-promised
- Repository: https://github.com/google/indexed-db-as-promised
- Homepage: https://github.com/google/indexed-db-as-promised#readme
- Issues: https://github.com/google/indexed-db-as-promised/issues
- npm.io page: https://npm.io/package/indexed-db-as-promised

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2018-07-03
- 1.0.0 — 2018-05-28

## README

# indexed-db-as-promised [![Build Status](https://travis-ci.org/google/indexed-db-as-promised.svg)](https://travis-ci.org/google/indexed-db-as-promised)
[![npm version](https://badge.fury.io/js/indexed-db-as-promised.svg)](http://badge.fury.io/js/indexed-db-as-promised)

A thin wrapper around IndexedDB, making it much more pleasant to use by
returning Promise-likes. Inspired by ideas in
[indexeddb-promised](https://github.com/jakearchibald/indexeddb-promised),
but written with the `SyncPromise` library to avoid issues with the transaction
lifetime.

This is not an official Google product.

## Installation

```
npm install --save indexed-db-as-promised
```

## Usage

The APIs mirror native IndexedDB's, just returning a promise-like everywhere
you would want one.

```javascript
import indexedDBP from "indexed-db-as-promised";

indexedDBP.open('database', 1, {
  upgrade(db, { transaction, oldVersion, newVersion }) {
    const people = db.createObjectStore('people', { autoIncrement: true, keyPath: 'id' });
    people.createIndex('ssn', 'ssn', { unique: true });
    people.createIndex('lastName', 'last');

    people.add({ first: 'Jane', last: 'Smith', ssn: '111-11-1111' });
  }
}).then((db) => {
  // Get the Jane's record
  return db.transaction('people').run((tx) => {
    // #run returns a Promise like that will resolve to whatever we return in
    // this block.
    const ssn = tx.objectStore('people').index('ssn');
    return ssn.get('111-11-1111');
  }).then((record) => {
    console.log(record); // => { first: 'Jane', last: 'Smith', ssn: '111-11-1111' }

    // Let's add someone, then get the total number of Smiths.
    return db.transaction('people', 'readwrite').run((tx) => {

      const store = tx.objectStore('people');
      return store.put({ first: 'John', last: 'Smith', ssn: '111-11-1112' })
        .then((key) => {
          console.log(key); // => 2

          return store.index('lastName').count(IDBKeyRange.only('Smith'));
        });
    });
  }).then((count) => {
    console.log(count); // => 2

    // Let's iterate over everyone.
    return db.transaction('people').run((tx) => {

      // Let's gather all our SSNs with a cursor.
      const open = tx.objectStore('people').openCursor();
      return open.iterate((cursor) => {
        const record = cursor.value;

        // Continue/advance (if you want to), or don't and early-exit iteration.
        cursor.continue();

        return record.ssn;
      }).then((ssns) => {
        console.log(ssns); // => ['111-11-1111', '111-11-1112']
      });
    });
  }).then(() => {
    // Let's close our connection now.
    db.close();
  });
});
```

## License

Apache 2.0

---
_Source: https://npm.io/package/indexed-db-as-promised · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
