How to isolate a Gunjs database?
Asked Answered
U

1

6

I've been trying out GunJs for a couple of days now and I'm really enjoying it. As a starter project I've followed the Fireship chat dapp video aimed at building your own chat.

Here's the issue, now that I've finished the tutorial I would like to create my own chat. However, for some reason if I get a 'chat' node within my own app it seems to pick up on the same 'chat' node as the tutorial one that is online.

onMount(() => {

    // Get Messages in large chat
    db.get('chat')
    .map()
    .once(async (data, id) => {
        if (data) {
            // key for E2E - to do: change for web3
            const key = '#foo';

            var message = {
                //transform the data
                who: await db.user(data).get('alias'),
                what: (await SEA.decrypt(data.what, key)) + '',
                when: GUN.state.is(data, 'what'), 
            };

            if (message.what) {
                messages = [...messages.slice(-100), message]
            }
        }
    })
})

This is also the case if I change the encryption key (then the messages just become undefined). Multiple questions arise from this:

  • Are graph node names unique within the whole of GunDb?
  • How do you handle conflicts where two gun-based apps call on the same node name?
  • Is this problem generally solved through filtering using 'header' props?
  • How do I make it pick up on only my data?

Even if I've read most of the docs, there seems to be something I'm missing in my comprehension of how the graph is generally seperated between apps. Any insight on how this works would be much appreciated.

Unbelievable answered 25/9, 2021 at 10:5 Comment(0)
E
6

Are graph node names unique within the whole of GunDb?

Yes.

How do you handle conflicts where two gun-based apps call on the same node name?

You don't. The expected result will be, they will overwrite each other.

Is this problem generally solved through filtering using 'header' props?

I don't think it's the right way to do it.

How do I make it pick up on only my data?

Use your own relay server.

Conclusion : gunDB doesn't really care about the who fetch / put the data. If you want to protect your data, use your own relay server (not a public one), and put data in your user space. user space is readonly to the public, but read/write for the owner.

Encomium answered 25/9, 2021 at 11:2 Comment(3)
thank you for your quick response! that makes a lot of sense.Unbelievable
This is not a correct answer, necessarily. The reason @Fredaroo's app was picking up the data from the tutorial's instance is also because he was retrieving the same document database with db.get('chat'). He could simply change the database name to something other than 'chat'.Nourishment
Still not answer the isolation on server. I just included this code down below, still connects to "wss://guntest.herokuapp.com/gun" const gun = Gun({ peers: [ 'http://127.0.0.1:8765/gun', // 'https://bitfinicon.com/gun', }); export {gun}Buddha

© 2022 - 2024 — McMap. All rights reserved.