Usage Guidelines
pritamsoni-hsr opened this issue · 13 comments
Hi @jepiqueau , so my app has like lots of components which will be in the DOM at any time (Ionic application) and where I need to share data, some of them needs to fetch and update very quickly. I'm not sure if the approach you mentioned is great/efficient considering that when it will run on native platform. for instance say I've 10 components, and I've loaded initial state from storage, but when I'll get new values from server or user input I need to update sqlite db, doing same thing at multiple places, is this gonna open as much (10 here) simultaneous connections to the database.
I'm using the method at only places where I need to access data very frequently.
at other places where I only need key value kinda values, I'm using this
const storage = new StorageAPIWrapper();
export const getIndexedItem = async (key: string, table = 'default', db = 'App') => {
let connection = await storage.openStore({ database: db, table: table });
return connection && (await storage.getItem(key));
};
I'm also designing something similar to use as orm with capacitor/sqlite
.
@pritamsoni-hsr i do not really understand what you try to achieve and what is the issue. i didn't advise any approach i was just answering to your issue .In native apps, the datastore for security access is opened and closed after each commands. The openStore should only be call once when you create the datastore and the table. if you want to add a table in the datastore you can use the setTable. if you want to set a value you just do a set(key, value) internally the set
method create the connection in write mode to the datastore, look for the existence of the key to decide if it is an insert or an update, close the connection. so you do not need to do a call to get
before to do a call to set
.
Now when you have several components, you can design them to have the same datastore and each of them store their state in a different key
, or in a different table
, or in a different datastore
.
different key
let result = await storage.openStore({ database: db, table: table});
if( result ) {
await storage.set({key: "componentA", value: JSON.stringify({foo: "foo",foo1: "foo1)})
await storage.set({key: "componentB", value: JSON.stringify({foo: "foo",foo1: "foo1)})
}
...
different table
var result = await storage.openStore({ database: db, table: "componentA"});
if( result ) {
await storage.set({key: "state", value: JSON.stringify({foo: "foo",foo1: "foo1)})
result = await storage.setTable({table: "componentB")
if ( result ) {
await storage.set({key: "state", value: JSON.stringify({foo: "foo",foo1: "foo1)})
}
...
}
different datastore
let result = await storage.openStore({ database: dbA, table: table});
if( result ) {
await storage.set({key: "state", value: JSON.stringify({foo: "foo",foo1: "foo1)})
}
let result = await storage.openStore({ database: dbB, table: table});
if( result ) {
await storage.set({key: "state", value: JSON.stringify({foo: "foo",foo1: "foo1)})
}
// now to update component A you must do
let result = await storage.openStore({ database: dbA, table: table});
if( result ) {
await storage.set({key: "state", value: JSON.stringify({foo: "foo",foo1: "foo1)})
}
Hope this clarify
@pritamsoni-hsr Any news ?
Hi @jepiqueau I read the instructions obviously, my question was when multiple multiple components are accessing db, it might be slightly inefficient, but that's not the actual problem, thanks for mentioning usage again.
The actual problem comes when there is no method like getAll values, like sql filtering based on keys.
capacitor-sqlite
is much powerful in these terms but I like the simplicity of capacitor-data-storage-sqlite
.
It'd be great to have key based filtering options here.
Tbh, this is not a big deal when data is less, i can read entire state on startup and save using background task.
Having something like filter would be great.
@pritamsoni-hsr Can you be more specific with this like sql filtering based on keys
by giving an example and/or detailing the specification of that new method. I do not have any problem to add a functionality if it is required
startsWith
, contains
, or you know just regex based filtering
@pritamsoni-hsr Do you mean return all keys where the value
contains or startsWith a string or return all keys where the key
contains or startsWith a string?
Key based, I'm not sure if indexedDB allows such thing (for web), but this is available in sql.
@pritamsoni-hsr Key based means for you return all keys where the key contains or startsWith a string
?
values where key either startsWith contains a string
@pritamsoni-hsr Ok now that i have understood i might have a look for iOS, Android and Electron platforms only
@pritamsoni-hsr The new release 2.4.2 fix this issue by adding by new method filterValues(filter)
where filter is a string.
- filter = "%foo" to filter keys starting with "foo"
- filter = "foo" to filter keys containing "foo"
- filter = "foo%" to filter keys ending with "foo"
Awesome Thanks @jepiqueau