Nick Cardoso
Nick Cardoso

Reputation: 21783

How can I operate on a subset of chrome.storage data

I'm storing data as an object in sync storage for my extension. Sometimes I need direct access to one 'leaf' of my tree and sometimes I need the whole of the data. It looks like this:

{
    someNode: ...,
    otherNode: { ... },
    exclusions: {
         'someName': [
             item1Constant,
             item5Constant,
         ],
         'someOtherName': [ ... ]
    }
}

How can I change the array of constants under someName, without using the entire exclusions tree? Putting all 'someName' nodes at the top level without the exclusions wrapper does not seem like a good solution, as there is other data at that level too.

Does the following update just someName, or would it replace all of exclusions?

 chrome.storage.sync.set({ 'exclusions' : { [someNameVar] : [...] } }, () => { ... });

What about this alternative - does it create a new node on the exclusions document, or just a new node at the top level, with a dot in its name?

 chrome.storage.sync.set({ ['exclusions.' + [someNameVar]] : [...] } }, () => { ... });

How can I access just the someName data in a get? I know for the top level I can specify just one property, but I don't know what works for just a 'sub node':

chrome.storage.sync.get(['exclusions'], (result) => { ... });

Would object notation do what I expect here? My suspicion is, no

chrome.storage.sync.get({ 'exclusions' : { 'someNameVar' } }, () => { ... });

Or

chrome.storage.sync.get(['exclusion.someNameVar'], () => { ... });

Exclusions can grow to a large size so I do not want to use the full data (and chrome has a limit to message size)

Upvotes: 0

Views: 129

Answers (1)

Nick Cardoso
Nick Cardoso

Reputation: 21783

Thanks to the following points offered by wOxxOm:

  • You can't "operate on a subset of chrome.storage data". It's like a database: one key equals one entry
  • Built-in sync storage is extremely limited (100kB) so you'll never exceed the message limit (~128MB), FWIW you might want to use compression or a different cloud provider altogether.
  • P.S. foo.bar is not a hierarchical path, it's just a single string key for a single entry in the storage

And because here it confirms that I can't get all keys with a prefix (without loading whole document) it seems the only viable solution is to do the following:

{
    someNode: ...,
    otherNode: { ... },
    exclusions: [
         'someName',
         'someOtherName'
    ],
    'someName': [
         item1Constant,
         item5Constant,
     ],
     'someOtherName': [ ... ]
}

It means doing things in two steps and duplicating some data, but it allows editing of each item (someName/someOtherName, etc.) directly, while still allowing retrieval of all exclusion data without loading unrelated items (otherNode, etc.)

Note: sync storage still has a limit of 512 items max too. So there is a limit how many of these 'exclusion keys' you can create

Upvotes: 1

Related Questions