# small-indexeddb

> Simple IndexedDB transactions with Promises

Latest version **2.0.0** (published 2019-01-14) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install small-indexeddb
pnpm add small-indexeddb
yarn add small-indexeddb
bun add small-indexeddb
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-01-14 |
| First published | 2017-12-15 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | TehShrike |
| Maintainers | tehshrike |
| Keywords | indexeddb, indexedb, idb, browser, transaction, promise |

## Links

- npm: https://www.npmjs.com/package/small-indexeddb
- npm.io page: https://npm.io/package/small-indexeddb

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-01-14
- 1.0.0 — 2019-01-12
- 0.0.0 — 2017-12-15

## README

# Goals

- simplify [`IDBObjectStore`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore) interactions
- promise-based API, avoiding events
- expose low-friction multi-query transactions
- small, like [idb-keyval](https://github.com/jakearchibald/idb-keyval) and [nanodb](https://github.com/lrlna/nanoidb)
- not large like [localForage](https://github.com/localForage/localForage/blob/master/src/localforage.js) or [Dexie](https://github.com/dfahlander/Dexie.js/blob/master/src/Dexie.js) or [JsStore](https://github.com/ujjwalguptaofficial/JsStore/blob/master/Code/JsStore/JsStoreInstance.ts)
- tests

# IndexedDB model

- The browser exposes databases, identified by a string name
- Databases contain object stores, identified by a string name
- Object stores can have transactions applied to them with any number of read/write/delete actions

For now, this library ignores some IndexedDB features (version numbers, multiple object stores per database) for simplicity's sake.

# API

```js
import smallIndexedDb from 'small-indexeddb'
```

## `promise = smallIndexedDb(databaseName)`

Returns a promise that resolves to a `transaction` function for the given database.  The store will be created if it does not exist yet.

Under the hood, the store name will be the same as the database name.

## `promise = transaction(mode, callbackFunction)`

[`mode` must be either `'readonly'`, `'readwrite'` or `'readwriteflush'`.](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/transaction#Parameters)

`callbackFunction` will be called immediately with a single [`IDBObjectStore`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore) parameter.

Your callback function must return either an [`IDBRequest`](https://developer.mozilla.org/en-US/docs/Web/API/IDBRequest) or an array of `IDBRequests`.

The returned promise will return the result of the single `IDBRequest`, or an array containing the results of all the requests you returned.

```js
async function main() {
	const transaction = await smallIndexedDb('sweetness')

	await transaction(`readwrite`, idbStore => idbStore.put(`totally a`, `a`))

	const [ a, b ] = await transaction(`readonly`, idbStore => [
		idbStore.get(`a`),
		idbStore.get(`b`),
	])

	console.log(a, b)
}
```

# License

[WTFPL](http://wtfpl2.com)

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