Ning KuangN
Convex Community16mo ago
3 replies
Ning Kuang

Best practice to store "callbacks" in database?

For example, let's say I have a JS interface for game achievement:

type Achievement = {
  title: string;

  // based on user's game data, return whether the criteria of this achievement is met or not
  isCriteriaMet: (ctx: UserGameData) => boolean;
  
  // if criteria is met, reward user by running this function to mutate user's data
  reward: () => void;
}


What is the best practice to represent this in Convex DB? Or in any DB?

My thought is to store reference to a query function (in my code) in isCriteriaMet field; store reference to a mutation function in reward field. Or maybe just references to helper functions with context?

If implemented this way, is there a Convex internal API where I can sync the list of query/mutation functions I defined in my code to the Convex DB, so I can safely reference them in another table?
Was this page helpful?