Switching to FaunaDB on Netlify

I’m a newbie working on a project and trying to use a basic database on a small personal webdev project. I want to include a database and am looking at the documentation for FaunaDB. My project currently uses node.js, express and MongoDB/mongoose. I would use MongoDB on Netlify but it seems like, because you can’t whitelist individual IP addresses between Netlify and Atlas/MongoDB, FaunaDB is the more secure choice - is that right? I’m not sure how to set that up on FaunaDB since most of the documentation uses react. For example, the section of my server.js file where I call in the MongoDB database looks like this:

> // configuration ===============================================================
> mongoose.Promise = global.Promise;
> const dotenv = require('dotenv').config()
> 
> // Initialize connection to database
> const dbUrl = process.env.DB_URL,
>       dbOptions = {
>         useNewUrlParser: true,
>         useFindAndModify: false
>       }
> mongoose.connect(dbUrl, dbOptions);

Can anyone point me toward resources for how to set up a basic FaunaDB? I don’t know how much reconfiguring this will take.

PS I hope this was the right section to post this in - please feel free to move if not. Thanks!

Hi,

While you can’t whitelist a serverless function’s IP, as long as your user password is secure, IMO it’s safe to use. In terms of faunaDB, they have their own documentation for using it on their site: Welcome to the Fauna documentation - Fauna Documentation

Note that if your intention is to use faunaDB with referrer/IP whitelisting then won’t be using it via netlify functions, but isntead right through the JS client.

Hi there, at Fauna, we have several different guides and examples for getting you going.

Here is a link to the drivers README

Below is a VERY basic example of a lambda node function connecting to FaunaDB:

const faunadb = require('faunadb')

// your secret hash
const secret = process.env.FAUNADB_SECRET_KEY
const q = faunadb.query
const client = new faunadb.Client({ secret })

module.exports = async (req, res) => {
  try {
    const dbs = await client.query(
      q.Map(
        // iterate each item in result
        q.Paginate(
          // make paginatable
          q.Match(
            // query index
            q.Index('all_customers') // specify source
          )
        ),
        ref => q.Get(ref) // lookup each result by its reference
      )
    )
    // ok
    res.status(200).json(dbs.data)
  } catch (e) {
    // something went wrong
    res.status(500).json({ error: e.message })
  }
}
1 Like