Worklight v6: use multiple JSON stores concurrentl

2019-08-08 10:23发布

问题:

Is it possible to use two or more JSON stores in a Worklight app at the same time (without switching back and forth)? When I initialize a second JSON store with a username/password, collections in the "default" JSON store that were initialized properly become inaccessible.

Given that many functions in the JSON store api does not let you specify a target store, I am guessing that using multiple stores concurrently is not possible. If this is true, then how does one address the use case where it is necessary to:

  1. Encrypt sensitive user data, and
  2. Need access to non-sensitive data before user is authenticated.

回答1:

The username field you pass to init is basically the file name for the store, for example:

WL.JSONStore.init(..., {username: 'store1'})

You will have store1.sqlite on disk, no encryption. If you want to switch to another store simply call:

WL.JSONStore.closeAll()

The closeAll function will kill all database accessors. Then you can start a second store with a password, for example:

WL.JSONStore.init(..., {username: 'store2', password: '123'})

That will create a store2.sqlite file encrypted with 256-bit AES encryption.

If you want to switch back to store1, simply call WL.JSONStore.closeAll() and then WL.JSONStore.init(..., {username: 'store1'}).

Currently you can not access store1 and store2 at the same time. You can open a feature request here.

The .sqlite files are mentioned here if you want to see them on the file system, and a bit of their internal structure is mentioned here. The code snippets above don't show it, but make sure you take into account that most JSONStore API functions are async, read more here.