Giter Site home page Giter Site logo

Comments (11)

imhoffd avatar imhoffd commented on April 28, 2024 4

Okay, the latest for this is: If you use the storage group "NativeStorage", it will be backwards-compatible with cordova-plugin-nativestorage. For web this means you can access the root level of localStorage, fulfilling the original feature request in this issue. We'd like devs to migrate their data from NativeStorage, because the clear() of the NativeStorage plugin on web seems to be very destructive (but please correct me if I'm mistaken).

On iOS and web, the Capacitor Storage plugin uses the storage group (default of "CapacitorStorage") as a prefix to separate key/values from other data. On Android, it uses the group as an entirely separate SharedPreferences file.

Please take a look at this PR and let me know your thoughts! I'll update this issue when dev releases are made for Capacitor 3 and the new storage plugin.

from capacitor-plugins.

yacut avatar yacut commented on April 28, 2024 2

@dwieeb Thank you, I can't wait for Capacitor 3 👍

from capacitor-plugins.

Nasicus avatar Nasicus commented on April 28, 2024 1

@jcesarmobile In my opinion it should be possible to set a custom KEY_PREFIX (obviously it can unofficially be done, by just setting KEY_PREFIX now, but it's not nice...). This would not only allow to seamlessly solve situations like this, but also to have capacitor/plugin independent keys (without the prefix).

from capacitor-plugins.

yacut avatar yacut commented on April 28, 2024 1

@jcesarmobile @dwieeb would you please explain that? I don't quite understand why it's not possible to configure a prefix for the storage key (remove prefix)? If capacitor is a good replacement for the old cordova project, then most likely cordova-plugin-nativestorage was used in the old project, which has no default prefix. With capacitor you should use the core storage plugin, which has _cap_ default prefix. So if you need to migrate application from cordova to capacitor and data in local storage is important, then it is not possible with capacitor core plugin and you have to stay with cordova-plugin-nativestorage.

NativeStorage.setItem('key1', 'value1'); // key1=value1

const res = await Plugins.Storage.get({ key: 'key1' });
console.log(res.value); // <== undefined

from capacitor-plugins.

jcesarmobile avatar jcesarmobile commented on April 28, 2024

The plugin is a wrapper around localStorage, so if you have data already in localStorage you will need to migrate it by checking current data, add it as Capacitor data and remove you copy of localStorage, or keep using localStorage as you already do. The only problem with that is it won't use the native implementation on iOS and Android.

Maybe we can add a migrate function for web implementation that does it for you, what do you thing @mlynch?

from capacitor-plugins.

jcesarmobile avatar jcesarmobile commented on April 28, 2024

Changed the issue to be a feature request since it doesn't really break anything, it has worked like that since the beginning.

Issues tagged with feature request are closed but tracked for reactions to gauge interest

from capacitor-plugins.

agroves333 avatar agroves333 commented on April 28, 2024

This is indeed an unexpected behavior that I thought for sure would be configurable. I would be willing to create a PR for the feature if necessary.

My current workaround is to just wrap the Capacitor Storage with a util class that removes the prefix from the get method. Also, support auto serialization like the old Cordova/Ionic Storage.

// app/utils/Storage.ts
import { Plugins } from '@capacitor/core';
const { Storage: CapStorage } = Plugins;

export default class Storage {
  static get(key): Promise<any> {
    return CapStorage.get({ key: key.replace('_cap_', '') }).then(item => {
      const value = item.value;
      return typeof value === 'object' ? JSON.parse(value) : value;
    });
  }

  static set(key, value): Promise<any> {
    const setValue = typeof value === 'object' ? JSON.stringify(value) : value;
    return CapStorage.set({ key, value: setValue });
  }

  static remove(key): Promise<any> {
    return CapStorage.remove({ key });
  }
}

Usage:

import Storage from 'app/utils';

NativeStorage.setItem('somekey', 'somevalue');
// or
Storage.set('somekey', 'somevalue');
// would both be retrievable by
Storage.get('somekey');

But this just seems ridiculous to have to do.

from capacitor-plugins.

imhoffd avatar imhoffd commented on April 28, 2024

@yacut For example on iOS, NativeStorage uses a custom UserDefaults group called "NativeStorage", and we use the standard UserDefaults at the moment (with a key prefix). To me, it doesn't make sense to share their group, but I could be convinced otherwise. Any changes to this behavior would result in a breaking change for apps unless we add in migration logic. I would like to exclude the ability to extract values from NativeStorage from the scope of this issue and instead focus on the original request: the ability to change the key prefix. This is a completely reasonable request and will be addressed in Capacitor 3. If you want, you can create a separate feature request for extracting values from the NativeStorage plugin.

from capacitor-plugins.

imhoffd avatar imhoffd commented on April 28, 2024

And I just found out the Android implementation doesn't even use the prefix to begin with, so this will be very difficult to implement in a cross-platform way. We may need to provide a way to migrate data.

from capacitor-plugins.

imhoffd avatar imhoffd commented on April 28, 2024

I am going to rewrite the iOS implementation to use a non-shared UserDefaults and rewrite the web implementation to use IndexedDB. This will remove the need for a prefix, and instead promote the idea of a "storage group". There will be a way to migrate data from the Capacitor 2 implementation.

from capacitor-plugins.

ionitron-bot avatar ionitron-bot commented on April 28, 2024

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.

from capacitor-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.