Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dexie.js always uses WebSQL even when IndexedDB is fully supported #342

Closed
bsautel opened this issue Oct 15, 2016 · 9 comments
Closed

Dexie.js always uses WebSQL even when IndexedDB is fully supported #342

bsautel opened this issue Oct 15, 2016 · 9 comments

Comments

@bsautel
Copy link

bsautel commented Oct 15, 2016

I am developing a Cordova application and I need the application to be able to work offline.

Dexie.js offers a great and well documented API to interact with IndexedDB. As Dexie.js is able to use IndexedDBShim for devices that do not support IndexedDB, it seems to be a very good tool given my constraints.

I did some experiments with Dexie.js and IndexedDBShim and I realized that Dexie.js always uses the shim. It even tries to use the shim on Firefox that supports IndexedDB but not WebSQL. In Chrome it works but it uses WebSQL (it would be probably better to use IndexedDB) and in Firefox it does not work at all since it does not support WebSQL.

Here is how I load both libraries:

<script src="lib/IndexedDBShim/dist/indexeddbshim.min.js"></script>
<script src="lib/dexie/dist/dexie.min.js"></script>

I create a Dexie object with default options:

new Dexie('my-db-name');

Note that I do not call window.shimIndexedDB.__useShim().

I would expect Dexie.js to use shimIndexedDB only when the device does not support IndexedDB.

I probably misused either Dexie.js or shimIndexedDB.

Does anybody else encounter this behavior? Can anybody help me?

Thanks!

@dfahlander
Copy link
Collaborator

If the shim is there, Dexie has assumed you'd prefer using it. This has been due to the incomplete indexedDB implementation in Safari 9 that has been practically almost unusable. Now with Safari 10, this need to change, so thanks for bringing it up.

You can still force using the native indexedDB by setting :

Dexie.dependencies.indexedDB = indexedDB;
Dexie.dependencies.IDBKeyRange = IDBKeyRange;

@bsautel
Copy link
Author

bsautel commented Oct 16, 2016

Thanks @dfahlander.

Now I understand why I get this behavior.

The shim's documentation tells that it does nothing in browsers that natively support IndexedDB but it does not seem to be the case since in Firefox the window.shimIndexedDB is defined. Or maybe when they say it will do nothing they just mean it will not replace the native implementation?

I noticed a difference between the shim's documentation and Dexie.js code regarding the use of the shim. The shim suggests to use IndexedDB when it is available whereas Dexie.js uses the shim first if it is available. Is this aimed at fixing the issue with Safari 9 you are mentionning?

I expected the shim or maybe Dexie.js to provide always the most appropriate implementation. I can force the use of IndexedDB as you suggested but who better than the shim knows that the current browser supports IndexedDB?

I am going to investigate this issue and maybe discuss about that in the shim's project.

@bsautel
Copy link
Author

bsautel commented Oct 17, 2016

I just come to report an issue in the shim project to get more precisions regarding its behavior: indexeddbshim/IndexedDBShim#263.

@bsautel bsautel changed the title Dexie.js always uses shim even when IndexedDB is fully supported Dexie.js always uses WebSQL even when IndexedDB is fully supported Oct 17, 2016
@dpogue
Copy link
Contributor

dpogue commented Oct 17, 2016

See also #148.

@dfahlander
Copy link
Collaborator

There's also a historical reason of Dexie's behavior : the shim suffered from not being able to shim Safari 9 once it had indexedDB already. It was not possible to useShim because Safari complained about replacing indexedDB. There were simply no way to use the shim through window.indexedDB. Had to check for window.shimIndexedDB.

Now we have the opportunity to change Dexie's behavior in 2.0 beta. Would that be good or bad? Apps that rely on the current behavior would suddenly go over to using the real indexedDB instead.

I need advice here ;)

@bsautel
Copy link
Author

bsautel commented Oct 18, 2016

Thanks for your responses!

I am getting gradually a better picture of the issue.

I could solve my issue by telling Dexie.js to use the shim or not. As I am in an ionic / Cordova application running on Android and iOS only, I can detect easily the OS type and version thanks to an ionic API. Then I configure Dexie.js according to what Can I use says. I will test that on various devices to confirm it works.

If I change this heuristic later, in some devices the database used may change and that will result in a total data loss. Indeed, it will be the same if Dexie.js changes its behavior regarding the shim in version 2. In my case I don't care because I use IndexedDB as a cache storage but I assume it is not the case for all Dexie.js users. Maybe if that has to change it would be nice to let the ability to choose which IndexedDB implementation to use at it is currently the case and provide the configuration that corresponds to the 1.x behavior in the migration guide?

If that has to change in Dexie.js 2, in my own opinion, it would be nice to find a way to use always the most accurate implementation:

  • Use the native IndexedDB implementation in browsers that fully support it (Firefox, Chrome, Safari 10, ...)
  • Use a fixed native IndexedDB implementation in cases the support is partially broken and can be fixed (it sounds like the shim is able to do that)
  • Use the binding to WebSQL in the case IndexedDB is not supported at all or too much broken and WebSQL is supported.

As far as I know, the shim doesn't offer any way to get this more accurate implementation without detecting yourself the browser type:

  • If you use the browser implementation first (window.indexedDB || window.shimIndexedDB), it will not work on Safari since window.indexedDB is broken in Safari 8 and 9 the shim cannot replace it.
  • If you use the shim implementation first, it sounds like it is as if you always use the shim since the shim seems to always define window.shimIndexedDB. But it will not work in Firefox since it does not support WebSQL. It also makes Chrome use WebSQL whereas it natively supports IndexedDB. I assume it would be better to use the real implementation instead of an emulated one based on a deprecated technology and it should be faster.

I will try to find a solution thanks to this ticket: indexeddbshim/IndexedDBShim#263.

@dpogue
Copy link
Contributor

dpogue commented Oct 18, 2016

I am 👍 on making Dexie 2.0 prefer native IndexedDB.

@brettz9
Copy link

brettz9 commented Apr 16, 2017

You may be interested in my reply at indexeddbshim/IndexedDBShim#263 (comment)

@dfahlander
Copy link
Collaborator

@brettz9 Thanks for the detailed explanation. I'm removing the faulty behavior in Dexie 2.0.0-beta.11 - (won't no more use shimIndexedDB just because it is present). Will just use window.indexedDB and assume the shim does the job to check if it is needed. This may cause disturbance for Dexie users that assumes Dexie's previous behavior. People that have included the shim may expect Dexie to pick it up without calling __useShim() also for browsers where the shim is optional (as with IE/Edge). So I might need to be clear on this in the release notes and the deprecations page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants