erquhart
erquhart•15mo ago

Sending metadata to convex functions

I'm about to start sending user timezone info to my convex functions, and I think it's going to be required for over half of them. I can put together a decent pattern for this, but it would be cool if Convex at some point supported sending a group of values (ideally metadata) with every request, and they could be centrally configured.
10 Replies
erquhart
erquhartOP•15mo ago
Eg., configured right in the provider, and maybe available at a _metadata key in the args.
ian
ian•15mo ago
You're in luck my friend! I have a helper for "sessions" where it passes up a sessionId on every request, and you can set per-session data, that is available on every function that uses the queryWithSession or mutationWithSession functions (provided the client uses useSessionQuery and the like) https://stack.convex.dev/sessions-wrappers-as-middleware
Sessions: Wrappers as "Middleware"
How to implement session storage with Convex, using some helper functions we wrote. While most of your data is associated with a user or another docum...
erquhart
erquhartOP•15mo ago
Sweet! I'll check it out now
ian
ian•15mo ago
I have a newer version of that "middleware" that I'd love for you to try out - that will soon be in the convex-helpers repo. Want to try the newer version?
erquhart
erquhartOP•15mo ago
Just looking at the docs, I'm a bit hesitant on the helpers. Quietly holding out for some of this to get baked in 😅 Or at least distributed as a lib on npm (by docs I mean the article you pasted) Have y'all considered distributing? I'm super on board for not prematurely gunking up your core api's (or maybe ever doing so), and npm feels like a nice compromise.
ian
ian•15mo ago
yeah I've added a couple things to https://www.npmjs.com/package/convex-helpers -
npm
convex-helpers
A collection of useful code to complement the official convex package.. Latest version: 0.1.1, last published: 2 days ago. Start using convex-helpers in your project by running npm i convex-helpers. There are no other projects in the npm registry using convex-helpers.
erquhart
erquhartOP•15mo ago
oh I didn't know this was available, nice!
ian
ian•15mo ago
When I add it to convex-helpers npm it'll look like this to use server-side: https://github.com/a16z-infra/ai-town/blob/72137611b01a315191357b0d960644cdf22269f7/convex/util/withSession.ts
GitHub
ai-town/convex/util/withSession.ts at 72137611b01a315191357b0d96064...
A MIT-licensed, deployable starter kit for building and customizing your own version of AI town - a virtual town where AI characters live, chat and socialize. - a16z-infra/ai-town
ian
ian•15mo ago
GitHub
ai-town/src/hooks/useSession.ts at 72137611b01a315191357b0d960644cd...
A MIT-licensed, deployable starter kit for building and customizing your own version of AI town - a virtual town where AI characters live, chat and socialize. - a16z-infra/ai-town
erquhart
erquhartOP•15mo ago
Awesome, will definitely give it a go!

Did you find this page helpful?