Any Code Examples for a Small User Database Using Stable Data Structures?

Hello Fellow Developers,

We are building a Note application similar to Evernote, or OneNote but ours runs in your desktop computer with special features to leverage the IC.

There are two key features, and both need a canister that will hold user data, a small database for users: uploading data to the IC, and using ckBTC for selling notes or getting donations.

Our architecture is like this:
1- Client software written in Electron
2- IC Canisters in Motoko and Rust
3- Each User will Own their Own Canister for Storage

Now as you can guess #3 requires that we run a separate canister that holds a small database of our users with:

  • Principal ID, email, name, StorageCanisterID, etc.

I am not an expert in either Motoko or Rust, it is all a learning experience. Do you know of any example code, or open source DAPP for the IC that implements a similar small database canister? If you do please share.

Any advice on the best way, or best tools to build this please let me know. We are considering the usual suspects:

But these are just building blocks, a small implementation would be far more useful to see how the pieces can actually be connected in a small simple database like structure.

Any advice and especially links to example code are welcome!

Joseph

1 Like

I think the superheroes example may be a good starting point. You can easily replace the fields with the ones you’re using and swapping the data structure to the one that best fits your purposes shouldn’t be hard either

2 Likes

Thanks a lot @Severin I had not seen this example!

It uses a functional Trie data structure for a hash map, a novel way to do this data structure. Would this survive an upgrade? I suppose not.

Would you recommend upgrading the logic to use another data structure, like for example the Motoko HashMap by @ZhenyaUsenko which can survive an upgrade? Or do you think is better to just stick with it, and do the pre-upgrade, post-upgrade process?

Thanks, and have a great weekend!

P.S. Copying @claudio since he is the author of the code. Claudio any advice on using this as a basis of a little canister to save user state?

Using a stable data structure (like the one you linked) is a good idea. Otherwise you get stuck at ~2GB of data

“Stable data structure” as most people use the term just means that it can be declared stable so the Motoko compiler does the (de)serialization for you at upgrade time. The data structure still lives on the heap and is subject to a 4 GB limit (or 2GB, depending on the type of GC used). That is also the case for the datastructure that he linked. But probably not a problem because he said his database is small.

To be able to answer the original question, what are your search (lookup) keys? Is only one of the above the search key or do you need lookup for multiple of them?

Side question: Is the notes or user data in the canisters going to be encrypted?

Timo,

The lookup key will be the email of the user, which will link to the rest of the data structure, most likely a Rust BTreeMap.

About your follow up question:

Is the notes or user data in the canisters going to be encrypted?

The user notes are physically on his own laptop or desktop computer, however, if he wishes to share the note, or back it up on the IC, on a one by one basis he can do so by using the IC Storage option. Each user will own his own data canister.

In version 1 we are not using encryption, but in later upgrades we are considering using VetKeys: VETKeys Primer | Internet Computer.

This data canister we are asking about is to hold the information of the many customers, so we can display their data, and use their canisters to store their own data there.

1 Like