Search code examples
nearprotocol

How to store an arrays within a PersistentVector item?


I'm attempting the Bucket List Design challenge here:

https://github.com/near-examples/workshop--exploring-assemblyscript-contracts

In the notes it says there is one model Activity, and this should include a PersistentVector for a list of friends.

My activities are stored as a PersistentVector, so this would mean a nested PersistentVector with the Activity model - it seems this would not be possible. Am I missing something, or is this maybe designed to create a bit of thinking?


Solution

  • It’s certainly possible to have a PersistentVector<Activity>, and inside each activity have a PersistentVector<string> with friends. However, using this datastructure, I think you end up with a shared list of friends for all activites (unless you use a unique prefix). If you use PersistentVector for your activities, then it’s possible to use a regular list (string[]) for your friends to isolate it inside one activity. Otherwise, you would need to store the PersistentVector of friends using the id of your activity (maybe as the unique prefix like below)

    friends: PersistentVector<string> = new PersistentVector<string>(uniqueIdOfActivity);
    

    I haven’t tested my hypothesis yet, but as far as I understand, a PersistentVector is just a helper to use the storage