Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Fauna: Select from pure JS

I'm interested in using Fauna from browser from pure JS in read-only mode. Either official documentation lacks understandable information for this case or I can't figure it out. Please help me to understand.

I'm trying to run such query:

Map(Paginate(Documents(Collection('spells'))), Lambda('x', Get(Var('x'))));

Running this query from the shell gives me the result.

I want to push this query to JavaScript variable. But I don't understand what to do. Here is my code:

var client = new faunadb.Client({
  secret: '320438826900127936',
  keepAlive: false,
  domain: 'db.eu.fauna.com',
  // NOTE: Use the correct domain for your database's Region Group.
  port: 443,
  scheme: 'https',
});

var helper = client.paginate(
  q.Match(
    q.Index('spells'),
    '101'
  )
)

paginate.then(function(response) {
  console.log(response.ref) // Would log the ref to console.
})

Please help me to get output from DB using pure JavaScript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using the JavaScript driver, all of the FQL functions are in faunadb.query namespace. Typically, developers would do this at the top of their scripts:

const faunadb = require('faunadb')
const q = faunadb.query

After that, the FQL functions can be access using the q object. For example: q.Add(1, 2).

Otherwise, you can use destructuring to import FQL functions into the script's namespace, like so:

const faunadb = require('faunadb')
const {
  Documents,
  Collection,
  Get,
  Index,
  Lambda,
  Match,
  Paginate
} = faunadb.query

and then you can call the FQL functions "directly", e.g. Add(1, 2).

If you use destructuring, be sure to import all of the FQL functions that you intend to use in your queries. Not all FQL functions can be imported "as-is", since some of their names would collide with built-in JavaScript names, such as Function. For those, you have to assign them directly:

const FaunaFunction = faunadb.query.Function

And then use the local name that you specified (FaunaFunction in this case) when composing queries.

Either way, the query itself isn't sent to Fauna until the client.query(), or in your case, client.paginate(), function is called.

That means that you can assign the FQL function calls to JavaScript variables. With the desired query, that could look like this:

const spellsQuery = q.Map(
  q.Paginate(
    q.Documents(q.Collection('spells'))
  ),
  q.Lambda('x', q.Get(q.Var('x')))
)

Then you can do this:

client.query(spellsQuery)
.then(res => console.log(res))

You can use the client.paginate if you are only performing pagination. Your desired query calls Map, so the client.pagination helper gets in your way. My example uses client.query.

Note that when you configure your client connection object, you don't need to specify the port and scheme options; you're using the default values already. And you likely shouldn't change the keepAlive option unless your workflow specifically requires it.

Plus, I see that you didn't include an actual secret in your example, which is a good thing: if you did include an actual secret, anyone with that secret could access your database just like you can.

If you want to see a complete (but simple) Fauna-based application that runs in a web page, see: https://github.com/fauna-labs/todo-vanillajs

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!