List storage objects in reverse order

For each multiplayer match, we store a record of the results. Then a user can look back through previous matches. We’re just using the storage system for this, in go:

nk.StorageWrite(ctx, []*runtime.StorageWrite{
		{
			Collection:      "matches",
			Key:             matchId,
			Value:           string(payload),
			PermissionRead:  0,
			PermissionWrite: 0,
			UserID:          userId,
		},
	})

Then when we want to show match history, we use:

records, nextCursor, err := nk.StorageList(ctx, "", userId, "matches", 10, cursor)

However, while we’d like to show the most recent 10 matches, this shows the first 10. Looking through the server runtime code in core_storage.go, it looks like there is no way to reverse the order of the cursors and the list queries.

This seems like a straightforward use case, so I thought I’d ask here if we’re missing something simple. If not, would the Heroic Labs team be open to a PR?

Hello @thegoldenmule,

The StorageList API doesn’t guarantee an ordering of the results - if you’re seeing an ordering it’s coincidental and that expectation may break sooner or later.

An option would be to create a Storage Search index and list ordering by create_time.

Otherwise you’d have to store all the entries in a single storage object per user and manually update it with consistent ordering every time you add to it- but you’d have to cap it at some amount of entries otherwise it could grow too large.

Best.

Whoops, just saw this. Thanks for the info! Yah, strange, they have always returned ordered but I see in the SQL that it’s only ordering by key.