{"name":"idb","version":"4.0.0-beta.1","description":"A small wrapper that makes IndexedDB usable","main":"build/cjs/index.js","module":"build/esm/index.mjs","typings":"build/esm/index.d.ts","scripts":{"clean":"rm -r build test-build","build":"npm run clean && rollup -c && gzip-size build/iife/index-min.js && gzip-size build/iife/with-async-ittr-min.js","dev":"npm run clean && rollup -c --watch"},"repository":{"type":"git","url":"git://github.com/jakearchibald/idb.git"},"author":{"name":"Jake Archibald"},"license":"ISC","devDependencies":{"@types/chai":"^4.1.7","@types/mocha":"^5.2.6","chai":"^4.2.0","conditional-type-checks":"^1.0.0","gzip-size-cli":"^3.0.0","mocha":"^6.0.2","rollup":"^1.6.0","rollup-plugin-commonjs":"^9.2.1","rollup-plugin-copy":"^0.2.3","rollup-plugin-node-resolve":"^4.0.1","rollup-plugin-terser":"^4.0.4","rollup-plugin-typescript2":"^0.19.3","tslint":"^5.13.1","tslint-config-airbnb":"^5.11.1","tslint-react":"^3.6.0","typescript":"^3.3.3333"},"readme":"# IndexedDB with usability.\n\nThis is a tiny (~1.17k) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.\n\n1. [Installation](#installation)\n1. [Changes](#changes)\n1. [API](#api)\n    1. [`openDB`](#opendb)\n    1. [`deleteDB`](#deletedb)\n    1. [`unwrap`](#unwrap)\n    1. [`wrap`](#wrap)\n    1. [General enhancements](#general-enhancements)\n    1. [`IDBDatabase` enhancements](#idbdatabase-enhancements)\n    1. [`IDBTransaction` enhancements](#idbtransaction-enhancements)\n    1. [`IDBCursor` enhancements](#idbcursor-enhancements)\n    1. [Async iterators](#async-iterators)\n1. [Examples](#examples)\n1. [TypeScript](#typescript)\n\n# Installation\n\n```sh\nnpm install idb\n```\n\nThen, assuming you're using a module-compatible system (like Webpack, Rollup etc):\n\n```js\nimport { openDB, deleteDB, wrap, unwrap } from 'idb';\n\nasync function doDatabaseStuff() {\n  const db = await openDB(…);\n}\n```\n\n# Changes\n\nThis is a rewrite from 3.x. [See changes](changes.md).\n\n# API\n\n## `openDB`\n\nThis method opens a database, and returns a promise for an enhanced [`IDBDatabase`](https://w3c.github.io/IndexedDB/#database-interface).\n\n```js\nconst db = await openDB(name, version, {\n  upgrade(db, oldVersion, newVersion, transaction) {\n    // …\n  },\n  blocked() {\n    // …\n  },\n  blocking() {\n    // …\n  }\n});\n```\n\n* `name`: Name of the database.\n* `version`: Schema version.\n* `upgrade` (optional): Called if this version of the database has never been opened before. Use it to specify the schema for the database. This is similar to the `upgradeneeded` event in plain IndexedDB.\n    * `db`: An enhanced `IDBDatabase`.\n    * `oldVersion`: Last version of the database opened by the user.\n    * `newVersion`: Whatever new version you provided.\n    * `transaction`: The transaction for this upgrade. This is useful if you need to get data from other stores as part of a migration.\n* `blocked` (optional): Called if there are older versions of the database open on the origin, so this version cannot open.\n* `blocking` (optional): Called if this connection is blocking a future version of the database from opening.\n\n## `deleteDB`\n\nDelete a database.\n\n```js\nawait deleteDB(name);\n```\n\n* `name`: Name of the database.\n\n## `unwrap`\n\n```js\nconst unwrapped = unwrap(wrapped);\n```\n\nIf for some reason you want to drop back into plain IndexedDB, give one of the enhanced objects to `unwrap` and you'll get the unmodified version back.\n\nPromises will also be converted back into `IDBRequest` objects.\n\n## `wrap`\n\n```js\nconst wrapped = wrap(unwrapped);\n```\n\nUse this to convert a plain IDB object to one enhanced by this library. This is useful if some third party code gives you an `IDBDatabase` object and you want it to have the features of this library.\n\nThis doesn't work with `IDBCursor`, [due to missing primitives](https://github.com/w3c/IndexedDB/issues/255). Also, if you wrap an `IDBTransaction`, `tx.store` and `tx.objectStoreNames` will not work in Edge. To avoid these issues, wrap the `IDBDatabase` object, and use the wrapped object to create a new transaction.\n\n## General enhancements\n\nOnce you've opened the database the API is the same as IndexedDB, except for a few changes to make things easier.\n\nAny method that usually returns an `IDBRequest` object will now return a promise for the result.\n\n```js\nconst store = db.transaction(storeName).objectStore(storeName);\nconst value = await store.get(key);\n```\n\n## `IDBDatabase` enhancements\n\n### Shortcuts to get/set from an object store\n\nIt's common to create a transaction for a single action, so helper methods are included for this:\n\n```js\n// Get a value from a store:\nconst value = await db.get(storeName, key);\n// Set a value in a store:\nawait db.put(storeName, value, key);\n```\n\nThe shortcuts are: `get`, `getKey`, `getAll`, `getAllKeys`, `count`, `put`, `add`, `delete`, and `clear`. Each method takes a `storeName` argument, the name of the object store, and the rest of the arguments are the same as the equivalent `IDBObjectStore` method.\n\nThese methods depend on the same methods on `IDBObjectStore`, therefore `getKey`, `getAll`, and `getAllKeys` are missing in Edge as it lacks support.\n\n### Shortcuts to get from an index\n\nThe shortcuts are: `getFromIndex`, `getKeyFromIndex`, `getAllFromIndex`, `getAllKeysFromIndex`, and `countFromIndex`.\n\n```js\n// Get a value from an index:\nconst value = await db.getFromIndex(storeName, indexName, key);\n```\n\nEach method takes `storeName` and `indexName` arguments, followed by the rest of the arguments from the equivalent `IDBIndex` method. Again, these methods depend on the equivalent methods on `IDBIndex`, so Edge does not support `getKeyFromIndex`, `getAllFromIndex`, or `getAllKeysFromIndex`.\n\n## `IDBTransaction` enhancements\n\n### `tx.store`\n\nIf a transaction involves a single store, the `store` property will reference that store.\n\n```js\nconst tx = db.transaction('whatever');\nconst store = tx.store;\n```\n\nIf a transaction involves multiple stores, `tx.store` is undefined.\n\n### `tx.done`\n\nTransactions have a `.done` promise which resolves when the transaction completes successfully, and otherwise rejects.\n\n```js\nconst tx = db.transaction(storeName, 'readwrite');\ntx.store.put('foo', 'bar');\nawait tx.done;\n```\n\n## `IDBCursor` enhancements\n\nCursor advance methods (`advance`, `continue`, `continuePrimaryKey`) return a promise for the cursor, or null if there are no further values to provide.\n\n```js\nconst store = db.transaction(storeName).objectStore(storeName);\nlet cursor = await store.openCursor();\n\nwhile (cursor) {\n  console.log(cursor.key, cursor.value);\n  cursor = await cursor.continue();\n}\n```\n\n## Async iterators\n\nAsync iterator support isn't included by default (Edge doesn't support them). To include them, import `idb/with-async-ittr.js` (~1.4k) instead of `idb`:\n\n```js\nimport { openDB } from 'idb/with-async-ittr.js';\n```\n\nNow you can iterate over stores, indexes, and cursors:\n\n```js\nconst tx = db.transaction(storeName);\n\nfor await (const cursor of tx.store) {\n  // …\n}\n```\n\nEach yielded object is an `IDBCursor`. You can optionally use the advance methods to skip items (within an async iterator they return void):\n\n```js\nconst tx = db.transaction(storeName);\n\nfor await (const cursor of tx.store) {\n  console.log(cursor.value);\n  // Skip the next item\n  cursor.advance(2);\n}\n```\n\nIf you don't manually advance the cursor, `cursor.continue()` is called for you.\n\nStores and indexes also have an `iterate` method which has the same signiture as `openCursor`, but returns an async iterator:\n\n```js\nconst tx = db.transaction('books');\nconst index = tx.store.index('author');\n\nfor await (const cursor of index.iterate('Douglas Adams')) {\n  console.log(cursor.value);\n}\n```\n\n# Examples\n\n## Keyval Store\n\nThis is very similar to `localStorage`, but async. If this is *all* you need, you may be interested in [idb-keyval](https://www.npmjs.com/package/idb-keyval), you can always upgrade to this library later.\n\n```js\nconst dbPromise = openDB('keyval-store', 1, {\n  upgrade(db) {\n    db.createObjectStore('keyval');\n  }\n});\n\nconst idbKeyval = {\n  async get(key) {\n    return (await dbPromise).get('keyval', key);\n  },\n  async set(key, val) {\n    return (await dbPromise).put('keyval', val, key);\n  },\n  async delete(key) {\n    return (await dbPromise).delete('keyval', key);\n  },\n  async clear() {\n    return (await dbPromise).clear('keyval');\n  },\n  async keys() {\n    return (await dbPromise).getAllKeys('keyval');\n  },\n};\n```\n\n# TypeScript\n\nThis library is fully typed, and you can improve things by providing types for your database:\n\n```ts\nimport { openDB, DBSchema } from 'idb';\n\ninterface MyDB extends DBSchema {\n  'favourite-number': {\n    key: string,\n    value: number,\n  },\n  'products': {\n    value: {\n      name: string,\n      price: number,\n      productCode: string,\n    },\n    key: string,\n    indexes: { 'by-price': number },\n  }\n}\n\nasync function demo() {\n  const db = await openDB<MyDB>('my-db', 1, {\n    upgrade(db) {\n      db.createObjectStore('favourite-number');\n\n      const productStore = db.createObjectStore('products', { keyPath: 'productCode' });\n      productStore.createIndex('by-price', 'price');\n    }\n  });\n\n  // This works\n  await db.put('favourite-number', 7, 'Jen');\n  // This fails at compile time, as the 'favourite-number' store expects a number.\n  await db.put('favourite-number', 'Twelve', 'Jake');\n}\n```\n\nTo define types for your database, extend `DBSchema` with an interface where the keys are the names of your object stores.\n\nFor each value, provide an object where `value` is the type of values within the store, and `key` is the type of keys within the store.\n\nOptionally, `indexes` can contain a map of index names, to the type of key within that index.\n\nProvide this interface when calling `openDB`, and from then on your database will be strongly typed. This also allows your IDE to autocomplete the names of stores and indexes.\n\n## Opting out of types\n\nIf you call `openDB` without providing types, your database will use basic types. However, sometimes you'll need to interact with stores that aren't in your schema, perhaps during upgrades. In that case you can cast.\n\nLet's say we were renaming the 'favourite-number' store to 'fave-nums':\n\n```ts\nimport { openDB, DBSchema, IDBPDatabase, IDBPTransaction } from 'idb';\n\ninterface MyDBV1 extends DBSchema {\n  'favourite-number': { key: string, value: number },\n}\n\ninterface MyDBV2 extends DBSchema {\n  'fave-num': { key: string, value: number },\n}\n\nconst db = await openDB<MyDBV2>('my-db', 2, {\n  async upgrade(db, oldVersion) {\n    // Cast a reference of the database to the old schema.\n    const v1Db = db as unknown as IDBPDatabase<MyDBV1>;\n\n    if (oldVersion < 1) {\n      v1Db.createObjectStore('favourite-number');\n    }\n    if (oldVersion < 2) {\n      const store = v1Db.createObjectStore('favourite-number');\n      store.name = 'fave-num';\n    }\n  }\n});\n```\n\nYou can also cast to a typeless database by omiting the type, eg `db as IDBPDatabase`.\n","readmeFilename":"README.md","gitHead":"c8ebc5e165034b1bc47e0cafb7737c856ef08a76","bugs":{"url":"https://github.com/jakearchibald/idb/issues"},"homepage":"https://github.com/jakearchibald/idb#readme","_id":"idb@4.0.0-beta.1","_nodeVersion":"10.15.1","_npmVersion":"6.8.0","dist":{"integrity":"sha512-J+GnxL1LxEnSOD+y+HE8lPH+NbE899QMPWMMyX/7jNv8e3BXkbOMyM/+V3zgSlDf5EoCaU+FIZL4DcwkLwEcbQ==","shasum":"5a257ef764deefec975c2d92ced3173e7ca19f6f","tarball":"http://registry-npm.ywwl.com/idb/download/idb-4.0.0-beta.1.tgz","fileCount":37,"unpackedSize":190959,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJciMTSCRA9TVsSAnZWagAAuNUP/1E6rPDhzhsMF5y3S0mV\n0KUUzZwPYDx0s+PjgxzQ2cVSi9k28POaTBsLns+tLudn/x7UooJIT2BxhA2d\n4EaE5VzPOPrVo0kroalXIGkI4IGdD9Nn845t+8kJ8hOjbQwD3oOAsiOkK4xP\ncjseqbJwHRLPbPMWs5O0tJuRgTIC9Fr24Lb6vHBCV0OCiEXsTajuLOqUfDsQ\nQyTQFUnUft9SNnFi7xnRb0ZD0TiOZqvwUkaD8DM/N1NZOXrfa/dk9EBZ8Vdw\n5BLpCsxgw52KsJdoaxYxy20zJUrwWljiko2y96rQVy0cb7n9CPst9DQ05EEI\nl97GegdqwsU0XvIcaDz/w8ZfVbXZlPGFq5QNwhFe6JnZqvGrj1T8mmtu8p2a\naSS2vfAlRokPR6mBVJPOxzgrO+t0E+SwoOsKOvRRZPtt7STNUnAiRK0fWjjB\ngKfKIKAaxizt22oO6oeGOVSa3StJJJbnbJI/8tiGIJywggptBuccOEpAkXa0\nDPGYWEPO/UVQ7+PRg43+lcq640oFqiQvMt3htWgmVr0fyE6KX5Zkln9QPoPf\nrUtxYVeaVkaEIofgafd9KhtLZ1+2/Q6C/wLUejN5OAc8kfowIjs6x2gyuoGm\nX+xuBnrAp84L+oBqhF3QjTf7acNCbZlZT2RrqyHs7lQWnbIlW93Oj0hANdmn\nifJJ\r\n=Fzyt\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAN30/4WBptjPriFDMWpj8WXYevRuo2ka8UoGcegU2LIAiARMciissPBTYg7npTCLSZyWGQNkzmQyr831YuiPiDY3A=="}],"size":29872,"noattachment":false,"key":"/idb/-/idb-4.0.0-beta.1.tgz"},"maintainers":[{"name":"jaffathecake","email":"jaffathecake@gmail.com"}],"_npmUser":{"name":"jaffathecake","email":"jaffathecake@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/idb_4.0.0-beta.1_1552467154088_0.09475496747532763"},"_hasShrinkwrap":false,"publish_time":1552467154241,"_cnpm_publish_time":1552467154241,"dist-tags":{"beta":"7.0.0-beta","latest":"8.0.4","with-src":"7.0.1-with-src"}}