0

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 2

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! My use case is programmatically instantiating a local test database, yep. I think FSM does not support this yet (it's one of the things listed under "potential extensions"). I have a working solution for now just defining my schema in a .ts file.
"programmatically instantiating a local test database" --> Check out the tests in Fwitter, e.g. the fweet spec They're written in JavaScript, but do create child databases from the given key. For the "local" part - see Fauna Dev You'll need to point the domain property to your local instance when creating your client. See this example
1

The 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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.