Skip to content

Instantly share code, notes, and snippets.

@dfahlander
Last active January 28, 2021 09:34
Show Gist options
  • Select an option

  • Save dfahlander/9db0cd1476ea61782aaafb75191e8b14 to your computer and use it in GitHub Desktop.

Select an option

Save dfahlander/9db0cd1476ea61782aaafb75191e8b14 to your computer and use it in GitHub Desktop.
Example of a React component using useLiveQuery()
function FriendsComponent({minAge = 75}) {
const friends = useLiveQuery(
() => db.friends
.where('age').aboveOrEqual(minAge)
.toArray()
);
return friends && <ul>{
friends.map(friend =>
<li key={friend.id}>
{friend.name}, {friend.age}
</li>
)
}</ul>;
}
@shrutikhetanvakt

Copy link
Copy Markdown

I want to use a useLiveQuery on db.friends.toArray() ??

@dfahlander

Copy link
Copy Markdown
Author
const friends = useLiveQuery(
    () => db.friends.toArray()
  );

@shrutikhetanvakt

Copy link
Copy Markdown

i did this and this gets called but the liveQuery() does not and updates are not picked up am not sure why? in console in Application tab I delete a row from indexed db useliveQuery gets called but it does not show the updated data

@dfahlander

dfahlander commented Jan 27, 2021

Copy link
Copy Markdown
Author

Are you using dexie@3.1.0-alpha.6? (npm install dexie@next)

@shrutikhetanvakt

Copy link
Copy Markdown

Yes I am using the same version.

@dfahlander

dfahlander commented Jan 27, 2021

Copy link
Copy Markdown
Author

I just read your previous comment more carefully - Dexie's new observability will only observe changes performed programatically. It's not possible to get noticed about manual changes using the application tab in devtools. There is simply no native support for that in the DOM apis. However, changes made on workers or other tabs do propagate cross tabs/workers as long as they were changed using dexie@3.1.0-alpha.6 or later.

@shrutikhetanvakt

Copy link
Copy Markdown

Thanks .. understood.. we are trying to make a POC where we are using indexedDB (Dexie) as a wrapper and checking if it is feasible to use it. Our application is very data heavy and it keeps increasing .. do we know the limit we can save in indexed db and if we can increase it how...

@dfahlander

dfahlander commented Jan 28, 2021

Copy link
Copy Markdown
Author

Limits of indexedDB storage depends on platform and whether you get persist permission for you app. If you are building a PWA where your users "install" the webapp (user adds the web app to their start screen), you will get more limit than if you are just using it on a web page. See also StorageManager. That page doesn't yet cover how Chromium (Chrome, Opera, Edge) / WebKit (Safari) and SpiderMonkey (Firefox) are handling the limits differently.

Regarding useLiveQuery(), queries will remain efficient on large datasets as long as they are using indices to query subsets of the data. Observing an entire large table without filtering on index will of course be too heavy for the app, but adding a .limit(X) on the query will keep the queries light

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment