Recently, I have come across the Web SQL and IndexedDB APIs that are provided by browsers. What are the use cases for Web SQL and IndexedDB and when should I use one over the other?
Web SQL is deprecated per https://www.w3.org/TR/webdatabase/.
You can use IndexedDB
if you need to store structured client specific data that you do not store in the server side or you do not want to request from the server every time.
Also as opposed to localStorage
, IndexedDB
is async so it is more performant. It supports indexing resulting in more efficient querying than localStorage
which is simply a key-value store. However, if your needs are simple, localStorage
might be a better option.
Here is a link that talks about different web storage options. Here is a tutorial about how to use IndexedDB
for a progressive web app.
The Web SQL database specification has been deprecated since November 2010. The browser vendors are not encouraged to support this technology and it's important that anyone reading this understands this. You can read more about Web SQL on its Wikipedia page. Now back to the other important segment of your question.
You can use IndexedDB to store data of any JavaScript type, such as an object or array, without having to serialize it. All requests against the database are asynchronous. The browser's implementation allows you to set callbacks for when successes or errors occur. Modern abstractions over this implementation allow you to use promises instead.
One of the major use cases for IndexedDB is creating an offline database that will be synchronized with the actual database once online. This allows an application to continue to work while offline and persist past reloads. A now-dead famous example of this is Wunderlist, you can add and edit tasks even when offline. These operations go in a synchronization queue which is processed and emptied when the network is available again. This is how a lot of to-do list applications work when offline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With