mrousavy / react-native-mmkv

⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!
https://mrousavy.com
MIT License
5.76k stars 249 forks source link

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

Open DorianMazur opened 2 months ago

DorianMazur commented 2 months ago

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:

maintenance-hans[bot] commented 2 months ago

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 commented 1 month ago

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 commented 1 month ago

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

DorianMazur commented 1 month ago

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

mrousavy commented 1 month ago

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..