Best Practices to store user owned items

Hello,

I want to start implementing this with the right foot so looking for some advice here on best practices.

My use case:

  • game currently contains about 300 different items
  • each user can have unlimited items (but realistically up to 1000)

What is the best way to store these items held by each user?

Option 1:
Each item is stored in an ITEMS collection with its ID for a key for each user.

Option 2:
items are stored in a USERDATA collection with ITEMS key as:
{
“items”: […]
}

Are there other options than this? I really hope there are and that I misunderstand the framework because:

  • Option 1: you end up with thousands of records in the storage engine, but makes it easier to lookup individual items for each user
  • Option 2: it isn’t very granular and you have to read entire collection to find one item and the write the whole collection back to the server even if only one was changed. But it keeps the db records to a lower number.

What is the “Nakama way” of implementing the storage for this use case?

Many thanks in advance for your time.

Hello @Antviss

I think this thread provides enough information on how to approach your objective (link). In case you opt for a more fine-grain control over read and/or writes using custom SQL with json specific functions, I would recommend that you change the items from a list to an object (alternative 2). That way you can add and remove elements easily O(1) from the data structure without iterating over the elements to find them O(n).