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.
Why not Web SQL?
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.
When to use the IndexedDB API?
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.
© 2022 - 2024 — McMap. All rights reserved.
localStorage
is not sufficient – Arsphenamine