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

getItems does not work with prefixStorage #396

Open
harlan-zw opened this issue Feb 7, 2024 · 5 comments · May be fixed by #455
Open

getItems does not work with prefixStorage #396

harlan-zw opened this issue Feb 7, 2024 · 5 comments · May be fixed by #455
Labels
bug Something isn't working

Comments

@harlan-zw
Copy link
Contributor

harlan-zw commented Feb 7, 2024

Environment

1.10.1

Reproduction

https://stackblitz.com/edit/stackblitz-starters-rrstqp?file=index.js

Describe the bug

Working

useStorage().getItems(['namespace:key'])

Not Working

prefixStorage(useStorage(), 'namespace').getItems(['key'])
useStorage('namespace').getItems(['key'])

// { key: "key", value: null }

It seems to switch back to the default driver (memory). I'm guessing this is because the mount is being discovered from the input key, without the base being applied first.

Additional context

No response

Logs

No response

@pi0
Copy link
Member

pi0 commented Feb 7, 2024

Can you please make a sandbox ideally with unstorage only? 🙏🏼

@harlan-zw
Copy link
Contributor Author

Yes, sorry thought it was clear enough. Here is a repro: https://stackblitz.com/edit/stackblitz-starters-rrstqp?file=index.js

Would be great to add a starter to readme for quickly spinning one up.

@harlan-zw harlan-zw changed the title getItems does not work with a storage base getItems does not work with prefixStorage Feb 7, 2024
@dimasxp
Copy link

dimasxp commented Apr 9, 2024

useStorage('namespace').clear() also not working with namespace

@pi0 pi0 added the bug Something isn't working label May 1, 2024
@AQian-Cup
Copy link

AQian-Cup commented Jun 19, 2024

I think I found the bug, the cause of the issue is the function prefixStorage.

export function prefixStorage<T extends StorageValue>(
  storage: Storage<T>,
  base: string
): Storage<T> {
  base = normalizeBaseKey(base);
  if (!base) {
    return storage;
  }
  const nsStorage: Storage = { ...storage };
  for (const property of storageKeyProperties) {
    // @ts-ignore
    nsStorage[property] = (key = "", ...args) =>
      // @ts-ignore
      storage[property](base + key, ...args);
  }
  nsStorage.getKeys = (key = "", ...arguments_) =>
    storage
      .getKeys(base + key, ...arguments_)
      // Remove Prefix
      .then((keys) => keys.map((key) => key.slice(base.length)));

  return nsStorage;
}

In this code it tries to iterate through the storageKeyProperties to splice the base for each key, but in fact the storageKeyProperties does not contain the getItems and setItems strings, and this splice step is not implemented in runBatch. This means that when traversing the asyncCall in getItems, the key in the argument does not actually contain a namespace.

Additionally, since the traversal code here can't handle more complex cases, the function getKeys is also handled separately, which may be another reason why the issue arose.
#336

All in all, I'm trying to deal with the current issue and will bring up a pr when I'm done.

(The above is from a translation tool, please understand that it may be misleading)

@AQian-Cup AQian-Cup linked a pull request Jun 19, 2024 that will close this issue
8 tasks
@Mahdhir
Copy link

Mahdhir commented Aug 27, 2024

Same issue here.
prefix storage clear doesn't work
Also clear with base doesn't work for in-memory storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants