How to query multiple records from storage API via Forge

Am working with Forge UI KIT

I saved two records to storage

await storage.set('mykey1', {first_name: 'Frank', last_name: 'James', department: 'chemistry' });
await storage.set('mykey2', {first_name: 'john', last_name: 'carrot', department: 'chemistry' });

       //const data = await storage.get('mykey');

Here is my issue:

I need to select the two records by department fields using where clause Eg

 select * from storage where department = 'chemistry';

To this effect, I have tried something like

const data = await storage
  .where('department', startsWith('chemistry'))

Here is the error that it showed

Error: The field “department” is not supported for filters.

Please what is the best way to query the records.

Hey @EsedoFredrick,

You currently can only query storage by the key. You can’t query the payload itself.

Like @danielwinterw said, you can only query the storage by key right now.

If possible and if it makes sense to your usage, you could append a department acronym or “key” to your records’ keys.
Quick example of a key from the top of my head: CHEM-myKey1.
The query could be something like…

// await storage.query().where('key', startsWith('CHEM')).getMany();
await storage.query().where('key', startsWith(departmentKey)).getMany();

This comes with a load of extra management and issues (so be certain to manage edge cases if you’re going that route) but it is a way to circumvent the current very limited APIs provided with Forge Storage.

Best of luck,

1 Like

Thanks for getting back to me @danielwinterw @PascalPerreault .

query code below not working at all
const data = await storage.query().where('key', startsWith('SHAGG')).getMany();

Here is what I have done again.
I have save the data as arrays as per code below

await storage.set('SHAGG-myKey4', [{first_name: 'rahul', last_name: 'jack', age: '21'}]);
await storage.set('SHAGG-myKey5', [{first_name: 'vasu', last_name: 'vijah', age: '18'}]);

query the record as per below

const data = await storage.query().where('key', startsWith('SHAGG')).getMany();
return data;

Here is how I try to map the array result

{ => {
          return (
<Text>First Name :  {con.first_name}</Text>
<Text>Last Name pp:  {con.last_name}</Text>

I need to display all the results but it throws error

map undefined.

it seems the line of code below has an issue
const data = await storage.query().where('key', startsWith('SHAGG')).getMany();

if I try using code below
const data = await storage.get('SHAGG-myKey4');

it will work but only display one result where key is SHAGG-myKey4

please help me out. Thanks

Hi again,

Using getMany() actually returns an object with an array named results in which each result is a key-value pair of the obtained record.

Try something like (but don’t take my code for granted):

const data = await storage.query().where('key', startsWith('SHAGG')).getMany();
return => storageFound.value);
1 Like