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

Add method to check if MMKV instance with given ID exists #700

Open
DorianMazur opened this issue Jun 25, 2024 · 5 comments
Open

Add method to check if MMKV instance with given ID exists #700

DorianMazur opened this issue Jun 25, 2024 · 5 comments

Comments

@DorianMazur
Copy link
Contributor

DorianMazur commented Jun 25, 2024

Description:

Currently, there's no straightforward way to check if an MMKV instance with a specific ID already exists. This functionality would be useful for managing multiple MMKV instances and avoiding conflicts or unnecessary instance creation.

Feature request:

Add a static method to the MMKV class that allows checking for the existence of an instance with a given ID.

Proposed usage:

import { MMKV } from 'react-native-mmkv'

const instanceExists = MMKV.exists('some-id')
console.log(instanceExists) // Should return true or false

Benefits:

  • Simplifies instance management in apps using multiple MMKV stores
  • Helps prevent accidental overwriting of existing instances
Copy link

maintenance-hans bot commented Jun 25, 2024

Guten Tag, Hans here.

Note

New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time.
To support @mrousavy, please consider 💖 sponsoring him on GitHub 💖.
Sponsored issues will be prioritized.

@mrousavy
Copy link
Owner

mrousavy commented Jul 10, 2024

Well you can just load the MMKV instance and check if there are any keys in it, no?

const instance = new MMKV({ id: 'some-id' })
const instanceExists = instance.getAllKeys().length > 0
console.log(instanceExists) // Should return true or false

@mrousavy
Copy link
Owner

Or maybe instance.size > 0, this is size in bytes

@DorianMazur
Copy link
Contributor Author

I have encrypted instance, I need to check if instance exists before encrypting it. Initializing without encryption key will erase all data.

@mrousavy
Copy link
Owner

Initializing without encryption key will erase all data.

This is also a bit weird imo - that should throw an error instead of erasing all data..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants