Developers documentation best Map/DB Motoko practices

In Motoko, there are stable data structures can be used with the stable keyword to automatically persist that data across upgrades, and class based data structures that are not stable, meaning that you have to write your own serialization logic to persist them across upgrades.

My recommendation is to use stable data structures in Motoko. Here are a few of the most popular that should get you pretty far:

  • vector - resizable array (more performant buffer)
  • stableheapbtreemap - BTree - a balanced, sorted tree based data structure. Been running in production for a year and half with no issues. Use it if you want to be able to search & scan/paginate. This library sits underneath and powers CanDB.
  • map - performant hash map/set. Stable for over a year now?

For general logic inside of a function, I’d still recommend using the Motoko base library for primitives (Nat, Int, Iter, Option, etc.) and temporary logic, but most of the data structures in the Motoko base library were written a long time ago and haven’t been updated in awhile. Many of the libraries in mops are now more fully featured and performant than those in base.

Mops makes it super easy to install specific modules, so I’d recommend starting at mops.one and searching for a specific package/use case.

Have a question about a library? Ask it in these forums or on the library’s GitHub repo and that should help provide answers with respect to its current support.

1 Like