Skip to content

Commit

Permalink
Add events to upgrade, blocking, and blocked callbacks (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Sep 21, 2022
1 parent 04f4211 commit 204a7ea
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 1,366 deletions.
29 changes: 19 additions & 10 deletions README.md
@@ -1,6 +1,6 @@
# IndexedDB with usability.

This is a tiny (~1.05k brotli'd) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.
This is a tiny (~1.06kB brotli'd) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.

1. [Installation](#installation)
1. [Changes](#changes)
Expand Down Expand Up @@ -81,13 +81,13 @@ This method opens a database, and returns a promise for an enhanced [`IDBDatabas

```js
const db = await openDB(name, version, {
upgrade(db, oldVersion, newVersion, transaction) {
upgrade(db, oldVersion, newVersion, transaction, event) {
//
},
blocked() {
blocked(currentVersion, blockedVersion, event) {
//
},
blocking() {
blocking(currentVersion, blockedVersion, event) {
//
},
terminated() {
Expand All @@ -103,8 +103,15 @@ const db = await openDB(name, version, {
- `oldVersion`: Last version of the database opened by the user.
- `newVersion`: Whatever new version you provided.
- `transaction`: An enhanced transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration.
- `event`: The event object for the associated `upgradeneeded` event.
- `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open. This is similar to the [`blocked` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBOpenDBRequest/blocked_event) in plain IndexedDB.
- `currentVersion`: Version of the database that's blocking this one.
- `blockedVersion`: The version of the database being blocked (whatever version you provided to `openDB`).
- `event`: The event object for the associated `blocked` event.
- `blocking` (optional): Called if this connection is blocking a future version of the database from opening. This is similar to the [`versionchange` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event) in plain IndexedDB.
- `currentVersion`: Version of the open database (whatever version you provided to `openDB`).
- `blockedVersion`: The version of the database that's being blocked.
- `event`: The event object for the associated `versionchange` event.
- `terminated` (optional): Called if the browser abnormally terminates the connection, but not on regular closures like calling `db.close()`. This is similar to the [`close` event](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close_event) in plain IndexedDB.

## `deleteDB`
Expand All @@ -121,6 +128,8 @@ await deleteDB(name, {

- `name`: Name of the database.
- `blocked` (optional): Called if the database already exists and there are open connections that don’t close in response to a versionchange event, the request will be blocked until they all close.
- `currentVersion`: Version of the database that's blocking the delete operation.
- `event`: The event object for the associated 'versionchange' event.

## `unwrap`

Expand Down Expand Up @@ -261,7 +270,7 @@ while (cursor) {

## Async iterators

Async iterator support isn't included by default (Edge doesn't support them). To include them, import `idb/with-async-ittr` instead of `idb` (this increases the library size to ~1.28k brotli'd):
Async iterator support isn't included by default (Edge doesn't support them). To include them, import `idb/with-async-ittr` instead of `idb` (this increases the library size to ~1.29kB brotli'd):

```js
import { openDB } from 'idb/with-async-ittr';
Expand Down Expand Up @@ -320,19 +329,19 @@ const dbPromise = openDB('keyval-store', 1, {

export async function get(key) {
return (await dbPromise).get('keyval', key);
};
}
export async function set(key, val) {
return (await dbPromise).put('keyval', val, key);
};
}
export async function del(key) {
return (await dbPromise).delete('keyval', key);
};
}
export async function clear() {
return (await dbPromise).clear('keyval');
};
}
export async function keys() {
return (await dbPromise).getAllKeys('keyval');
};
}
```

## Article store
Expand Down
2 changes: 1 addition & 1 deletion lib/simple-ts.js
Expand Up @@ -15,7 +15,7 @@ import { relative, join, parse } from 'path';
import { promises as fsp } from 'fs';
import { promisify } from 'util';

import * as ts from 'typescript';
import ts from 'typescript';
import glob from 'glob';

const globP = promisify(glob);
Expand Down

0 comments on commit 204a7ea

Please sign in to comment.