Skip to content

Commit

Permalink
chore: grammar fix, transcation to transaction (#344)
Browse files Browse the repository at this point in the history
Co-authored-by: Ammar Ahmed <[email protected]>
  • Loading branch information
giacomocerquone and ammarahm-ed authored Aug 16, 2024
1 parent ad6d247 commit 5f59b57
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
22 changes: 9 additions & 13 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ Hooks let's the storage update your app when a change takes place in storage.
Starting from `v0.5.5`, thanks to the power of JSI, we now have our very own `useMMKVStorage` Hook. Think of it like a persisted state that will always write every change in storage and update your app UI instantly. It doesn't matter if you reload the app or restart it.

```js
import {MMKVLoader, useMMKVStorage } from "react-native-mmkv-storage";
import { MMKVLoader, useMMKVStorage } from 'react-native-mmkv-storage';

const storage = new MMKVLoader().initialize();
const App = () => {
const [user, setUser] = useMMKVStorage("user", storage, "robert");
const [age, setAge] = useMMKVStorage("age", storage, 24);
const [user, setUser] = useMMKVStorage('user', storage, 'robert');
const [age, setAge] = useMMKVStorage('age', storage, 24);

return (
<View style={styles.header}>
Expand Down Expand Up @@ -106,8 +106,8 @@ Learn more about `useIndex` hook it in the [docs](https://rnmmkv.vercel.app/#/us
Listen to a value's lifecycle and mutate it on the go. Transactions lets you register lifecycle functions with your storage instance such as Read, Write and Delete. This allows for a better and more managed control over the storage and also let's you **build custom indexes** with a few lines of code.

```js
MMKV.transcations.register("object", "beforewrite", ({ key, value }) => {
if (key.startsWith("post.")) {
MMKV.transaction.register('object', 'beforewrite', ({ key, value }) => {
if (key.startsWith('post.')) {
// Call this only when the key has the post prefix.
let indexForTag = MMKV.getArray(`${value.tag}-index`) || [];
MMKV.setArray(indexForTag.push(key));
Expand All @@ -126,14 +126,9 @@ MMKV supports concurrent read-read and read-write access between processes. This
You can create many database instances. This helps greatly if you have seperate logics/modules in the same app that use data differently, It also helps in better performance since each database instance is small instead of a single bulky database which makes things slower as it grows.

```js
const userStorage = new MMKVLoader()
.withEncryption()
.withInstanceID("userdata")
.initialize();
const userStorage = new MMKVLoader().withEncryption().withInstanceID('userdata').initialize();

const settingsStorage = new MMKVLoader()
.withInstanceID("settings")
.initialize();
const settingsStorage = new MMKVLoader().withInstanceID('settings').initialize();
```

### **Full encryption support**
Expand All @@ -157,6 +152,7 @@ For each database instance, there is one global key index and then there are ind
Support for redux persist is also added starting from v0.3.2.

### **Supports expo**

You can use this library with expo [bare workflow](https://docs.expo.dev/workflow/customizing/).

## Consider supporting with a ⭐️ [star on GitHub](https://github.com/ammarahm-ed/react-native-mmkv-storage/)
Expand Down Expand Up @@ -187,4 +183,4 @@ Copyright © Ammar Ahmed ([@ammarahm-ed](https://github.com/ammarahm-ed))

<a href="https://notesnook.com" target="_blank">
<img style="align:center;" src="https://i.imgur.com/EMIqXNc.jpg" href="https://notesnook.com" alt="Notesnook Logo" width="50%" />
</a>
</a>
36 changes: 18 additions & 18 deletions docs/transactionmanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Allows you to register a lifecycle function for a given data type.
**Arguments**

| Name | Required | Type | Description |
|-----------------------------------|----------|------------------------------------------------------|---------------------------------------------------------|
| --------------------------------- | -------- | ---------------------------------------------------- | ------------------------------------------------------- |
| type | yes | "string" / "number" / "object" / "array" / "boolean" | The type of data you want to register the function for. |
| transcation | yes | "beforewrite" / "onwrite" /"onread" / "ondelete" | When should the function be called |
| transaction | yes | "beforewrite" / "onwrite" /"onread" / "ondelete" | When should the function be called |
| `mutator({key:string,value:any})` | no | function | The function that allows to mutate the value |

### `unregister`
Expand All @@ -21,9 +21,9 @@ Unregister a lifecycle function for a given data type.
**Arguments**

| Name | Required | Type | Description |
|-------------|----------|------------------------------------------------------|-----------------------------------------------------------|
| ----------- | -------- | ---------------------------------------------------- | --------------------------------------------------------- |
| type | yes | "string" / "number" / "object" / "array" / "boolean" | The type of data you want to unregister the function for. |
| transcation | yes | "beforewrite" / "onwrite" / "onread" / "ondelete" | type of transaction to unregister |
| transaction | yes | "beforewrite" / "onwrite" / "onread" / "ondelete" | type of transaction to unregister |

### `clear`

Expand All @@ -34,7 +34,7 @@ Clear all registered functions.
Import `MMKVStorage` and `useMMKVStorage` Hook.

```js
import { MMKVLoader, useMMKVStorage } from "react-native-mmkv-storage";
import { MMKVLoader, useMMKVStorage } from 'react-native-mmkv-storage';
```

Initialize the `MMKVStorage` instance.
Expand All @@ -48,8 +48,8 @@ const MMKV = new MMKVLoader().initialize();
Debug what is happening during the lifecycle of your app. Track every single change and have better control and understanding your storage.

```js
MMKV.transcations.register("object", "onwrite", ({ key, value }) => {
console.log(MMKV.instanceID, "object:onwrite: ", key, value);
MMKV.transactions.register('object', 'onwrite', ({ key, value }) => {
console.log(MMKV.instanceID, 'object:onwrite: ', key, value);
});
```

Expand All @@ -60,8 +60,8 @@ Building and storing custom indexes with a simple key/value storage can become d
Let's assume that we are storing user posts in storage and each post as a specific tag.

```js
MMKV.transcations.register("object", "beforewrite", ({ key, value }) => {
if (key.startsWith("post.")) {
MMKV.transactions.register('object', 'beforewrite', ({ key, value }) => {
if (key.startsWith('post.')) {
// Call this only when the key has the post prefix.
let indexForTag = MMKV.getArray(`${value.tag}-index`) || [];
MMKV.setArray(indexForTag.push(key));
Expand Down Expand Up @@ -93,16 +93,16 @@ This is a basic example but you can imagine the level of control you can achieve
Abstraction of data in your storage. Anything that is affected by multiple factors in your storage can be put here. This also allows for easier debugging since such data is mutated in one place instead of tens of different places in the app, you can know exactly what is happening.

```js
MMKV.transcations.register("object", "onwrite", ({ key, value }) => {
if (!key.startsWith("posts.")) return; // only look at "posts." prefix keys
MMKV.transactions.register('object', 'onwrite', ({ key, value }) => {
if (!key.startsWith('posts.')) return; // only look at "posts." prefix keys
// developer can update other fields based on this transaction
MMKV.setIntAsync("postsCount", oldValue + 1);
MMKV.setIntAsync('postsCount', oldValue + 1);
});

MMKV.transcations.register("object", "ondelete", ({ key }) => {
if (!key.startsWith("posts.")) return; // only look at "posts." prefix keys
MMKV.transactions.register('object', 'ondelete', ({ key }) => {
if (!key.startsWith('posts.')) return; // only look at "posts." prefix keys
// developer can update other fields based on this transaction
MMKV.setIntAsync("postsCount", oldValue - 1);
MMKV.setIntAsync('postsCount', oldValue - 1);
});
```

Expand All @@ -111,10 +111,10 @@ MMKV.transcations.register("object", "ondelete", ({ key }) => {
Mutate a value before read/write.

```js
const injectTimestamp = (record) => ({ ...record, timestamp: Date.now() });
const injectTimestamp = record => ({ ...record, timestamp: Date.now() });

MMKV.transcations.register("object", "beforewrite", ({ key, value }) => {
if (!key.startsWith("posts.")) return; // only look at "posts." namespace
MMKV.transactions.register('object', 'beforewrite', ({ key, value }) => {
if (!key.startsWith('posts.')) return; // only look at "posts." namespace
if (!!value.timestamp) return; // only run if timestamp is not in record already

// Setup new transaction with properly structured data
Expand Down
2 changes: 1 addition & 1 deletion src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type TransactionType = 'beforewrite' | 'onwrite' | 'onread' | 'ondelete';
*
* const MMKV = new MMKVStorage.Loader().initialize();
*
* MMKV.transcations.register("object", "onwrite", ({ key, value }) => {
* MMKV.transactions.register("object", "onwrite", ({ key, value }) => {
* console.log(MMKV.instanceID, "object:onwrite: ", key, value)
* });
* ```
Expand Down

0 comments on commit 5f59b57

Please sign in to comment.