vector
vector•12mo ago

Sending a function as an argument to a function

yeah, I have these helpers in javascript, mainly generated to make it less syntactically heavy to generate backend functions. The above is a little bit more different. Essentially, I want to be writing all my database functions on the front end so I (theoritically) dont need to design custom calls for each new query I want to make. So the idea was to pass down a custom function that has the filtering and ordering built out on the front end already as a function that takes in a ctx and have it run that function on the back end. I assumed this was very security prone and Im wondering if thats correct. Regardless, my current system does things more traditionally and creates the backend query endpoints separately for each use case.
18 Replies
ballingt
ballingt•12mo ago
@vector it sounds like you understand the whole "functions can only be called from the frontend, or scheduled, or called from actions" thing: the arguments to a function call are always serialized. Then your problem is that a function can't be serialized. There are fancy things you can try to do like sending in a string and eval()-ing it (not currently supported in the Convex runtime) but there's no way to transfer a JavaScript function from teh frontend to the backend.
vector
vectorOP•12mo ago
understood, so basically it isn't possible to do filtering and ordering on the front end -- it all has to be done in the back
ballingt
ballingt•12mo ago
Yep, you'd have to build a data structure describing your filtering and ordering and then implement that on the backend.
vector
vectorOP•12mo ago
No description
vector
vectorOP•12mo ago
does this work it uses the filter helper function and simplifies syntax by auto-generating helpers and on the front end, there is a function in lib that takes in arguments needed and returns the query @Tom @Lee
lee
lee•12mo ago
the code you showed never calls getTweets with more than one argument, so there is no filter applied. can you describe how you're going to do filtering?
ballingt
ballingt•12mo ago
The function getMainFeedFrontEnd() isn't a React hook, but it calls a React hook. That's not allowed. Are you using React?
vector
vectorOP•12mo ago
oh yeah i realized that i did that wrong pass a filter function as a parameter
ballingt
ballingt•12mo ago
Into the the getTweets() helper, as an argument you haven't shown yet?
vector
vectorOP•12mo ago
yeah its an optional argument flter
ballingt
ballingt•12mo ago
cool, sure that's fine. It depends on the code for filter() but assuming it's basically an arr.filter() then sure and modulo some awaits since you type the optional filter as returning a promise
vector
vectorOP•12mo ago
it uses the helper filter function, that takes in a typescript lambda in the form of (row) => bool
ballingt
ballingt•12mo ago
@vector I'm not sure if you're all set yet, what you've described sounds fine but you're no longer passing anything up from the client.
vector
vectorOP•12mo ago
client uses the query hook as usual i pretty much just simplified the backend generation for my AI client just runs the backend stuff using the hook
ballingt
ballingt•12mo ago
I don't see any arguments in the code you posted, but it sounds like you do have some? Either way this seems fine, your big question earlier about passing up a function from the client to the server is the thing that won't work.
vector
vectorOP•12mo ago
yeah so pretty much keeping all the simplification on the backend gave up on trying to create filters on the front end cuz wont work
ballingt
ballingt•12mo ago
yeah, and that way nobody can send up an infinite loop or anything either
vector
vectorOP•12mo ago
cant get it done for the hackathon but AI generated convex backend coming soon 🤞

Did you find this page helpful?