Let's say I have a string like const fql = "CreateCollection({ name: "users" })". How do I turn it into a faunadb Expr in JS?
2 Answers
You landed on the correct answer yourself. FQL is not a string-based language. This approach avoids problems like SQL injection, but it does mean you need to compose queries somewhere using a driver, the console, or a tool like Fauna Schema Migrate (FSM).
const fql = "CreateCollection({ name: "users" })"
The example you give leans toward schema/resource management. If that's your actual need here, consider FSM or the Fauna Serverless Framework plugin.
If you're building a front-end app using JavaScript, FSM is probably the right approach as it drops right into your app. It might also give you some more hints at how to transform strings into FQL. You would do the above in a single FQL file, e.g., fauna/resources/collections/users.fql as:
CreateCollection({ name: "users" })
If you're doing infrastructure as code in a separate pipeline or are already building with the Serverless Framework, the plugin might be a better fit.
If you want to see something else like a Pulumi or Terraform provider plugins, please submit a feature request in the forums!
2 Comments
domain property to your local instance when creating your client. See this exampleThe only way to do this at the moment seems to be evaluating the FQL as JavaScript.
The fauna-shell eval command is implemented using esprima to parse the FQL as ECMAScript, then running it through node's vm api with escodegen.
It's likely easier to just rewrite FQL files as JS files if you have the option!