Skip to content

Commit

Permalink
Fix db.get for multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
lr0pb committed Jan 13, 2023
1 parent 5e6bb20 commit 4394f7c
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 55 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# v.2.0.3 (13-01-2023)
## 🛠️ Patch db.get call with multiple keys
- fix that `db.get` when call with multiple keys but it is actually 1 key in array return requested item instead return it as alone item in array
- rename package's iife build in [`/out`](./out/) from `IDB.worker.js` to `IDB.iife.js`

# v.2.0.2 (11-01-2023)
## 🛠️ Patch onDataUpdate not call listeners after deleting events
## 🛠️ Patch db.onDataUpdate not call listeners after deleting events
- fix that `db.onDataUpdate` not call listeners when `delete` and `deleteAll` events happened in the listened store

# v.2.0.1 (02-10-2022)
Expand Down
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
# Install
Version with `es` export and as `iife` available in the [`out` directory](https://github.com/lr0pb/IDB.js/tree/main/out/)

Typescript and types definition files can be found in the [`src` directory](https://github.com/lr0pb/IDB.js/tree/main/src/)
Typescript source and types definition files can be found in the [`src` directory](https://github.com/lr0pb/IDB.js/tree/main/src/)
### Main thread
```js
import { IDB } from './out/IDB.module.js'
```
### Worker thread
You can use IDB.js in **worker thread** via the same `import` statement, but it works not in [all](https://caniuse.com/mdn-javascript_statements_import_worker_support) browsers. In this case you can use `importScripts`
```js
importScripts('./out/IDB.worker.js');
importScripts('./out/IDB.iife.js');
```

# How to use
Expand All @@ -46,18 +46,22 @@ const db = new IDB('library', 1, [
You can also provide fourth argument `options` described in [`new IDB`](https://lr0pb.github.io/IDB.js/classes/IDB#constructor)
### Set items in stores
Add item to store via simple `db.set` method
> `db.set` support second argument to be one item or array of items
> `db.set` support second argument to be one item or an array of items
```js
async function addAuthor(books) {
await db.set('authors', {
name: 'Agatha Christie',
books: []
});

await db.set('authors', [
author1, author2, ...
]);
}
```
### Check is item are in store and update them
Check if store have certain item via `db.has` and update them via `db.update`
> `db.has` and `db.update` support second argument to be one item key or array of item keys
> `db.has` and `db.update` support second argument to be one item's key or an array of item keys
```js
async function addBook() {
const book = {
Expand All @@ -78,15 +82,37 @@ async function addBook() {
```
> `UpdateCallback` function in [`db.update`](https://lr0pb.github.io/IDB.js/classes/IDB#update) can be **async**
> If you provide multiple keys, `UpdateCallback` will be called for each item. If you want to use separate `UpdateCallback` function for each item, provide array of `UpdateCallback`'s same length as item keys array length
> If you provide multiple keys, `UpdateCallback` will be called for each item. If you want to use separate `UpdateCallback` functions for each item, provide array of `UpdateCallback` functions **same length** as item keys array length
> `db.has` also can return count of items in store: just call it only with store name `db.has('books'); // 1`
### Read store entries
Get one item via `db.get` and all store items via `db.getAll`
> `db.get` support second argument to be one item key or array of item keys
> `db.get` support second argument to be one item's key or an array of item keys
```js
async function renderAuthor() {
const author = await db.get('author', 'Agatha Christie');
// {
// name: 'Agatha Christie',
// birth: 1920,
// death: 1976,
// books: [12345, 67890, ...]
// }
...
const authorsBooks = await db.get('books', author.books);
// [
// {
// id: 12345,
// title: `Hercule Poirot's Christmas`,
// ...
// },
// {
// id: 67890,
// title: `Murder on the Orient Express`,
// ...
// },
// ...
// ]
}

async function renderBooks() {
Expand All @@ -108,7 +134,7 @@ async function renderBooksProgressive() {
### Item and stores deletion
Delete whole store simply via upgrading database version and remove relevant `StoreDefinition` object from `stores` array. Delete one item via `db.delete` and clear all store entries via `db.deleteAll`
> `db.delete` support second argument to be one item key or array of item keys
> `db.delete` support second argument to be one item's key or an array of item keys
```js
const db = new IDB('library', 2, [
{ name: 'books', index: { keyPath: 'id' } },
Expand All @@ -118,6 +144,9 @@ const db = new IDB('library', 2, [

async function deleteBooks() {
await db.delete('books', 12345);
await db.delete('books', [
67890, 34567, ...
]);
await db.deleteAll('author'); // authors store still available but have no items
}
```
Expand All @@ -126,9 +155,11 @@ You can register multiple callbacks to spot if items added, updated or deleted f
To unregister callback, call returned from `db.onDataUpdate` function [`UnregisterListener`](https://lr0pb.github.io/IDB.js/classes/IDB#onDataUpdate)
```js
async signForUpdates() {
const unregister = await db.onDataUpdate('books', async ({store, item}) => {
const unregister = await db.onDataUpdate('books', async ({store, type, item}) => {
// item argument not presented when it was a deleting operation
if (item) await notifyUserAboutNewBookAdded(item);
if (type === 'set' && isNewBook(item)) {
await notifyUserAboutNewBookAdded(item);
}
});
...
unregister();
Expand All @@ -145,4 +176,4 @@ View whole API documentation [on docs site](https://lr0pb.github.io/IDB.js/class
# Develop
Clone this repo on your machine and run `npm i` in a root

Write tests to your code in [`www/mocha.test.js`](https://github.com/lr0pb/IDB.js/tree/main/www/mocha.test.js) file and run its via `npm run dev` (will be open default browser window with tests page hosted by local server)
Write tests to your code in [`www/mocha.test.js`](https://github.com/lr0pb/IDB.js/tree/main/www/mocha.test.js) file and run its via `npm start` (will be open default browser window with tests page hosted by local server)
Loading

0 comments on commit 4394f7c

Please sign in to comment.