amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

[Question] Is it possible to use react-native-mmkv with gunjs in react native? #1194

Closed madhav-madhusoodanan closed 2 years ago

madhav-madhusoodanan commented 2 years ago

First off, thank you for building this awesome project. I've been using gun in my recent projects for the times I've needed decentralised storage

Okay so I am building a react native app and i am just curious if I could use react-native-mmkv for it.

The docs say:

Also, you can pass any storage plugin to the Store that implements the AsyncStorage setItem/getItem API.

[Edit]: Should I add a getitem()/setItem() manually to the MMKV instance?

Can someone be kind enough to clarify? Thank you in advance

amark commented 2 years ago

Not an RN person, but my assumption is (???) AsyncStorage.getItem(key, cb) and .setItem(key, data, cb) so guessing you'd do something like (pseudo) myWrapper = {setItem: function(key, data, cb){ mmkv.write(key, data); cb(err, 1) } ... no?

Please please please add whatever you cook up to the https://github.com/amark/gun/wiki/React-Native docs !

3210jr commented 2 years ago

Hey @madhav-madhusoodanan & @amark It is possible, and it is as simple as your pseudo example. Here is a snippet of roughly what I have done:

import "gun/lib/mobile.js"; // most important!
import GUN from "gun/gun";
import "gun/lib/radix.js";
import "gun/lib/radisk.js";
import "gun/lib/store.js";

import { MMKV } from "react-native-mmkv";

const storage = new MMKV();

function Store(opt: { storage: MMKV }) {
    opt = opt || {};
    const store = function () {};
    const as = opt.storage;

    store.put = function (key, data, cb) {
        // console.log("Setting Key: ", key);
        // console.log("setting data: ", data);
        try {
            const res = as.set("" + key, data);
            cb(null, 1);
            console.log("ok put");
        } catch (error) {
            console.error(`failed saving to asyncstorage`, { key, data });
            cb(null, 0);
        }
    };

    store.get = (key, cb) => {
        // console.log("Getting key: ", key);
        try {
            const res = as.getString("" + key);
            cb(null, res || null);
            // console.log("ok get");
        } catch (error) {
            console.error(`failed fetching from asyncstorage`, { key });
            cb(null, 0);
        }
    };

    return store;
}

const store = Store({ storage });

Just a note: If you are trying to improve the performance of your queries, this approach is limited. Gun access individual items one at a time (i.e: one node at a time in a set). And there lies the bottle neck. You are better off really thinking about the structure of your data (small, relevant clusters of nodes). I wish there was some recommended reading on graphs and graph design dos and donts before people jump in!

amark commented 2 years ago

@3210jr 👏 🔥 👏 🔥 .

Mind submitting this as like a lib/rmmkv.js meanwhile? Then update https://github.com/amark/gun/wiki/React-Native please.

@madhav-madhusoodanan closing!