David AlonsoD
Convex Community2y ago
8 replies
David Alonso

How to wrap the database writer so that `ctx.db.patch` and `ctx.db.insert` run side effects

Firebase has a concept of trigger functions which trigger when documents are created, updated, deleted, etc. I found this really useful since I didn't have to worry about where or how documents were updated to still be sure that a cleanup or side effect would be run. What would be a robust way to do this in Convex?

I was trying to do it inside rules but quickly realized that's not possible...

This is what my mental model wanted to do:
export const dbRules: Rules<AuthQueryCtx | AuthMutationCtx, DataModel> = {
  fields: {
    insert: async (ctx, field) => {
      // TODO: Make sure we update the table views
      await _backfillCollectionViewsForField(ctx, field._id);
      return true;
    },
  },
};


Problems:
1. cannot do mutations inside the handlers
2. field._id is not yet available
Was this page helpful?