Create mutable data once, not each time the page is refreshed

#1

Please forgive my ignorance, I’ve been sifting through the docs but can’t find the info i’m looking for. I’m playing with an app based on the SAFE Web App Tutorial. When the page is refreshed, a public MutableData object is created on the (mock) network. I don’t want a new MutableData object to be stored each time I (or the user) reloads the page. If it were a WordPress site, the tables are created when WordPress is installed on the server, and the clients send API requests to the server to read/write data to those tables. Can someone please clarify how this works with SAFE? I guess i will need to store the MutableData at a custom location. Would I then just check if the data exists?

0 Likes

SAFE Network Dev Update - January 17, 2019
#2

hi @Glen_Simister - do you have the code on GitHub or anywhere that people can view it?
I’m going to ask for some help from some of our dev guys but I suspect that they’ll want a starting point.

0 Likes

#3

Pretty much yep. If you create an MD at a known (or deterministic xor location), you can grab that same MD again by providing the xor address when initing the MD instance on page load.

(https://docs.maidsafe.net/safe_app_nodejs/mutabledatainterface#newPublic)

5 Likes

#4

OK thanks. I was able to create the MD at a custom location but i’m not sure how to retrieve it. So when i create the new public MD it works the first time, and fails when i reload (as it should). I then want to catch the error and load the MD from the key but i’m not sure how to do this. For example, lets say that the key for the MD is: ‘160, 215, 133, 125, 250, 127, 0, 0, 1, 0, 0, 0’. How would I retrieve the MD on page load? Do you have any sample code i could look at?

0 Likes

#5

The code in the docs will retrieve the same MD each time:

const asyncFn = async () => {
    try {
        const name = await app.crypto.sha3Hash('1010101010101'); 
        const mData = await app.mutableData.newPublic(name, 15002);
    } catch (err) {
        throw err;
    }
};

The string used (above '1010101010101') can be anything, and using the sha3Hash generates a valid xor address.

So as long as you use the same hashed name string and typeTag (15002 above) each time, you’d get the same MD. (This is how the public name system on safe works eg.)


What you perhaps want is to use your application’s own container. (Which is a specific MD that can be created for you application) https://docs.maidsafe.net/safe_app_nodejs/authinterface#getOwnContainer (note the extra options you need to pass when requesting auth).


Hope this helps! If you need more examples / detail just let me know and I’ll get back the morn :+1:

8 Likes

#6

Ah i see. I thought newPublic() was only used to create a public MD. I didn’t know it could be used to get an existing MD. I was expecting it to be something like…

const name = await app.crypto.sha3Hash(‘1010101010101’)
md = await app.getPublic(name)

Anyway, thanks for your help. I am able to retrieve the data now :wink:

6 Likes

#7

Yes, you wouldn’t be the first to think so :slight_smile: that method name is unnecessarily confusing. It had me wonder for a while, what was going on there, when I started with it.

Usually with that behaviour, the naming is something along the line of GetOrAdd.

8 Likes

#8

Yeh that’s a fair point. And something we should address.

I’ve made this issue to track such suggestions.

It’d be amazing if you come across any more confusing naming conventions (or you have a list, @oetyng) if you went over there to add suggestions to the issue, @Glen_Simister.

[Not sure who all else might have suggestions, but just incase: @bzee, @happybeing, @nigel :point_up_2: :point_up_2: ]

8 Likes

closed #9

This topic was automatically closed after 60 days. New replies are no longer allowed.

0 Likes