Skip to content

Instantly share code, notes, and snippets.

@dfahlander
Last active January 28, 2021 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfahlander/9db0cd1476ea61782aaafb75191e8b14 to your computer and use it in GitHub Desktop.
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

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

@dfahlander
Copy link
Author

const friends = useLiveQuery(
    () => db.friends.toArray()
  );

@shrutikhetanvakt
Copy link

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
Copy link
Author

dfahlander commented Jan 27, 2021

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

@shrutikhetanvakt
Copy link

Yes I am using the same version.

@dfahlander
Copy link
Author

dfahlander commented Jan 27, 2021

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

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
Copy link
Author

dfahlander commented Jan 28, 2021

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