I’m trying to use FaunaDB as a backend for comments on my website. I’m storing each page as a collection. I need to dynamically create a collection and its index whenever the first time a page tries to load or post comments. Since FaunaDB will throw an error when a collection or index with a specified name already exists, I have to check for that.
So, I created a function which will first check if a collection with a specific name exists, if it does, I directly call my addComment or getComment function. However, if it doesn’t exist, I create a collection, then create an index and then call the functions I mentioned.
This is how I was using it till now:
From my client-side code, I was calling setupComments function (severless function) which would check the existence and once that’s done, I was calling addComment and getComments (again, serverless functions), according to the context. This was creating 2 problems:
Calling one serverless function, waiting for its response, then calling another function was taking time. It was taking just a second, but still.
In serveless functions, each invocation is counted. So, I didn’t want to increase costs by calling setupComments function just to check the existence of the required elements (collection and index).
Thus, I created created a module setupComments which handles the setting up part for me and required this module in both, addComment as well as getComment function. So now, I am able to save the invocation of an additional function by bundling it. But this has put me in a different trouble. Seems like there’s a delay between an index being created and it being available to use.
console.log() statements, I can see that the collection and index are ready.
However, when my
getComment function actually looks for that index immediately after it’s done creating, it fails.
The same thing will now work if I refresh the page.
Here’s my repo if it helps: OpinionJS/src at main · Hrishikesh-K/OpinionJS · GitHub. There you can see the setupComments and the other two functions. From my understanding, I’ve written the code correctly, especially since it works after a page refresh, but still, I won’t mind correcting it if I’ve made a mistake.