Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ To check if an MMKV instance exists, use `existsMMKV(...)`:
import { existsMMKV } from 'react-native-mmkv'

const exists = existsMMKV('my-instance')

// For instances created with a custom path:
const existsAtPath = existsMMKV('my-instance', '/custom/mmkv/path')
```

### Delete an MMKV instance
Expand All @@ -251,6 +254,19 @@ To delete an MMKV instance, use `deleteMMKV(...)`:
import { deleteMMKV } from 'react-native-mmkv'

const wasDeleted = deleteMMKV('my-instance')

// For instances created with a custom path:
const wasDeletedAtPath = deleteMMKV('my-instance', '/custom/mmkv/path')
```

### Get the default MMKV base directory

To get the default native MMKV base directory, use `getBaseDirectory()`:

```ts
import { getBaseDirectory } from 'react-native-mmkv'

const baseDirectory = getBaseDirectory()
```

### Log Level
Expand Down
58 changes: 57 additions & 1 deletion example/__tests__/MMKV.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
afterEach,
} from 'react-native-harness';
import { Platform } from 'react-native';
import { MMKV, createMMKV, deleteMMKV, existsMMKV } from 'react-native-mmkv';
import {
MMKV,
createMMKV,
deleteMMKV,
existsMMKV,
getBaseDirectory,
} from 'react-native-mmkv';

const skipOnWeb = (reason: string): boolean => {
if (Platform.OS === 'web') {
Expand All @@ -20,6 +26,12 @@ const waitForNextTick = async () => {
await new Promise<void>((resolve) => setTimeout(resolve, 0));
};

const getNativeCustomPath = (name: string): string | undefined => {
if (skipOnWeb('custom paths are not supported on Web')) return undefined;
const baseDirectory = getBaseDirectory();
return `${baseDirectory}/${name}`;
};

describe('MMKV Core Functionality', () => {
let storage: MMKV;

Expand Down Expand Up @@ -1186,6 +1198,27 @@ describe('Deleting instances and checking if they exist', () => {
const exists = existsMMKV('some-non-existing-instance');
expect(exists).toStrictEqual(false);
});

it('should accept an explicit undefined path argument', () => {
const id = `some-location-instance-${Date.now()}`;
const storage = createMMKV({ id });
storage.set('value', 'stored');

const exists = existsMMKV(id, undefined);
expect(exists).toStrictEqual(true);
});

it('should check custom path instances with their path', () => {
const id = `some-path-instance-${Date.now()}`;
const path = getNativeCustomPath(`path-aware-exists-${Date.now()}`);
if (path == null) return;

const storage = createMMKV({ id, path });
storage.set('value', 'stored');

expect(existsMMKV(id)).toStrictEqual(false);
expect(existsMMKV(id, path)).toStrictEqual(true);
});
});

describe('Deleting an instance', () => {
Expand Down Expand Up @@ -1214,6 +1247,29 @@ describe('Deleting instances and checking if they exist', () => {
const wasDeleted = deleteMMKV('some-non-existing-instance');
expect(wasDeleted).toStrictEqual(false);
});

it('should accept an explicit undefined path argument', () => {
const id = `some-location-instance-${Date.now()}`;
const storage = createMMKV({ id });
storage.set('value', 'stored');

const wasDeleted = deleteMMKV(id, undefined);
expect(wasDeleted).toStrictEqual(true);
expect(existsMMKV(id, undefined)).toStrictEqual(false);
});

it('should delete custom path instances with their path', () => {
const id = `some-path-instance-${Date.now()}`;
const path = getNativeCustomPath(`path-aware-delete-${Date.now()}`);
if (path == null) return;

const storage = createMMKV({ id, path });
storage.set('value', 'stored');

expect(existsMMKV(id, path)).toStrictEqual(true);
expect(deleteMMKV(id, path)).toStrictEqual(true);
expect(existsMMKV(id, path)).toStrictEqual(false);
});
});
});

Expand Down
12 changes: 8 additions & 4 deletions packages/react-native-mmkv/cpp/HybridMMKVFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ std::shared_ptr<HybridMMKVSpec> HybridMMKVFactory::createMMKV(const Configuratio
return std::make_shared<HybridMMKV>(configuration);
}

bool HybridMMKVFactory::deleteMMKV(const std::string& id) {
return MMKV::removeStorage(id);
bool HybridMMKVFactory::deleteMMKV(const std::string& id, const std::optional<std::string>& path) {
std::string rootPath = path.value_or("");
std::string* rootPathPtr = rootPath.empty() ? nullptr : &rootPath;
return MMKV::removeStorage(id, rootPathPtr);
}

bool HybridMMKVFactory::existsMMKV(const std::string& id) {
return MMKV::checkExist(id);
bool HybridMMKVFactory::existsMMKV(const std::string& id, const std::optional<std::string>& path) {
std::string rootPath = path.value_or("");
std::string* rootPathPtr = rootPath.empty() ? nullptr : &rootPath;
return MMKV::checkExist(id, rootPathPtr);
}

} // namespace margelo::nitro::mmkv
4 changes: 2 additions & 2 deletions packages/react-native-mmkv/cpp/HybridMMKVFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class HybridMMKVFactory final : public HybridMMKVFactorySpec {
void initializeMMKV(const std::string& rootPath) override;

std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) override;
bool deleteMMKV(const std::string& id) override;
bool existsMMKV(const std::string& id) override;
bool deleteMMKV(const std::string& id, const std::optional<std::string>& path) override;
bool existsMMKV(const std::string& id, const std::optional<std::string>& path) override;
};

} // namespace margelo::nitro::mmkv
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace margelo::nitro::mmkv { struct Configuration; }
#include <memory>
#include "HybridMMKVSpec.hpp"
#include "Configuration.hpp"
#include <optional>

namespace margelo::nitro::mmkv {

Expand Down Expand Up @@ -56,8 +57,8 @@ namespace margelo::nitro::mmkv {
// Methods
virtual void initializeMMKV(const std::string& rootPath) = 0;
virtual std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) = 0;
virtual bool deleteMMKV(const std::string& id) = 0;
virtual bool existsMMKV(const std::string& id) = 0;
virtual bool deleteMMKV(const std::string& id, const std::optional<std::string>& path) = 0;
virtual bool existsMMKV(const std::string& id, const std::optional<std::string>& path) = 0;

protected:
// Hybrid Setup
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-mmkv/src/deleteMMKV/deleteMMKV.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getMMKVFactory } from '../getMMKVFactory'
import { isTest } from '../isTest'

export function deleteMMKV(id: string): boolean {
export function deleteMMKV(id: string, path?: string): boolean {
if (isTest()) {
return true
}

const factory = getMMKVFactory()
return factory.deleteMMKV(id)
return factory.deleteMMKV(id, path)
}
6 changes: 5 additions & 1 deletion packages/react-native-mmkv/src/deleteMMKV/deleteMMKV.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
LOCAL_STORAGE_KEY_WILDCARD,
} from '../web/getLocalStorage'

export function deleteMMKV(id: string): boolean {
export function deleteMMKV(id: string, path?: string): boolean {
if (path != null) {
throw new Error("MMKV: 'path' is not supported on Web!")
}

const storage = getLocalStorage()
const prefix = id + LOCAL_STORAGE_KEY_WILDCARD
let wasRemoved = false
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-mmkv/src/existsMMKV/existsMMKV.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getMMKVFactory } from '../getMMKVFactory'
import { isTest } from '../isTest'

export function existsMMKV(id: string): boolean {
export function existsMMKV(id: string, path?: string): boolean {
if (isTest()) {
return true
}

const factory = getMMKVFactory()
return factory.existsMMKV(id)
return factory.existsMMKV(id, path)
}
6 changes: 5 additions & 1 deletion packages/react-native-mmkv/src/existsMMKV/existsMMKV.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
LOCAL_STORAGE_KEY_WILDCARD,
} from '../web/getLocalStorage'

export function existsMMKV(id: string): boolean {
export function existsMMKV(id: string, path?: string): boolean {
if (path != null) {
throw new Error("MMKV: 'path' is not supported on Web!")
}

const storage = getLocalStorage()
const prefix = id + LOCAL_STORAGE_KEY_WILDCARD
const keys = Object.keys(storage)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getPlatformContext } from '../getMMKVFactory'

/**
* Get the default native MMKV base directory.
*/
export function getBaseDirectory(): string {
const platformContext = getPlatformContext()
return platformContext.getBaseDirectory()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Get the default native MMKV base directory.
*/
export function getBaseDirectory(): string {
throw new Error("MMKV: 'getBaseDirectory()' is not supported on Web!")
}
3 changes: 3 additions & 0 deletions packages/react-native-mmkv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export type { Configuration, Mode } from './specs/MMKVFactory.nitro'
// The create function
export { createMMKV } from './createMMKV/createMMKV'

// Path utilities
export { getBaseDirectory } from './getBaseDirectory/getBaseDirectory'

// Exists + Delete
export { existsMMKV } from './existsMMKV/existsMMKV'
export { deleteMMKV } from './deleteMMKV/deleteMMKV'
Expand Down
13 changes: 7 additions & 6 deletions packages/react-native-mmkv/src/specs/MMKVFactory.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ export interface MMKVFactory extends HybridObject<{
createMMKV(configuration: Configuration): MMKV

/**
* Deletes the MMKV instance with the
* given {@linkcode id}.
* Deletes the MMKV instance with the given `id`.
* If the instance was created with a custom `path`, pass the same path here.
*/
deleteMMKV(id: string): boolean
deleteMMKV(id: string, path?: string): boolean

/**
* Returns `true` if an MMKV instance with the
* given {@linkcode id} exists, `false` otherwise.
* Returns `true` if an MMKV instance with the given `id` exists, `false`
* otherwise. If the instance was created with a custom `path`, pass the same
* path here.
*/
existsMMKV(id: string): boolean
existsMMKV(id: string, path?: string): boolean

/**
* Get the default MMKV instance's ID.
Expand Down
Loading