DEV Community

Hilton Meyer
Hilton Meyer

Posted on • Originally published at hiltonmeyer.com on

Fauna with netlify functions

To be able to develop fauna we need the sdk library that you can then use FQL (Fauna Query Language) to interact with the database, this is the SQL for Fauna basically. There is an option of using graphQL which I want to explore and might come back and edit this post. The encoding package is used by netlify and causes an error at the time of writing if not installed. It might be a peer dependency and fixed in future versions but at the moment this is the work around I have found to be working.

npm init -y
npm install faunadb encoding

Enter fullscreen mode Exit fullscreen mode

Now we want to be able to connect to the fauna db so there is a key that should be used. You create is in Fauna Console in the Security tab with the NEW KEY

Fauna Key

The important thing is to save the given secret, this will be your connection to the DB and I store this in a .env file and make sure that this is in the .gitignore so that the key is not uploaded in my repo. As this is used in Netlify you should create a key there too. I've actually used this little feature to setup a test db for local development and a production db and just generate a key for each and save that.

Fauna Key

The cool thing about having a test db is that you can set the collection to clean itself up with the History and TTL setting. I set this to 1 and that should in theory keep the testing data clean. One thing to keep in mind is that there are limits to the free account so this might be something to check when you are doing heavy testing. But again you could have a personal account where you create the DB and develop against the API like that.

Fauna Collection Settings

This is how I setup the local environment

touch .env

## Add the following to the .env file
FAUNADB_SECRET=fnADldk43FDdfdsffxlw08N2dh73d

Enter fullscreen mode Exit fullscreen mode

My .gitignore has the following added to it so that the build directories are not saved to the repo. They are built on the fly so not required to be in the git repo.

# Netlify Functions
/dist
/functions

Enter fullscreen mode Exit fullscreen mode

In development netlify has provided a cool way of being able to test and run the functions locally. There is also Netlify Dev which gives more features but for basic function testing an nothing else this lightweight package is just the thing you need.

npm install -D netlify-lambda 

Enter fullscreen mode Exit fullscreen mode

To be able to use the functions in Netlify you can either do this through the web interface but I prefer to have it in a file in the repo so that I know exactly what is being used without having to open the console. We need a netlify.toml file with some basic config inside:

[build]
  # This will be your default build command.
  command = "npm run build"
  # This is where Netlify will look for your lambda functions.
  functions = "functions"
  # This is the directory that you are publishing from.
  publish = "dist"

Enter fullscreen mode Exit fullscreen mode

In the file setup the following in the package.json file. The build command is what Netlify will use and the functions command is what I use locally to develop the functions and see the output in my console.

"scripts": {
    "build": "netlify-lambda build src/functions",
    "functions": "netlify-lambda serve src/functions"
  },

Enter fullscreen mode Exit fullscreen mode

Time to create our first function

mkdir -p src/functions
touch src/functions/test-connection.js

Enter fullscreen mode Exit fullscreen mode

In Fauna create a collection called test_connections

New Collection

And you can add a document for some basic testing.

New Document

The function itself will use the secret you previously created to connection to the DB and fetch this document

require('dotenv').config();
import faunadb from 'faunadb';

const q = faunadb.query;
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET,
});

exports.handler = (event, context, callback) => {
  console.log(`${event.httpMethod}: ${event.path}`);
  return client
    .query(q.Paginate(q.Match(q.Ref('classes/test_connections'))))
    .then((response) => {
      const references = response.data;
      console.log('references', references);
      const getAllDataQuery = references.map((ref) => {
        console.log(ref);
        return q.Get(ref);
      });
      // then query the refs
      return client.query(getAllDataQuery).then((ret) => {
        return callback(null, {
          statusCode: 200,
          body: JSON.stringify(ret),
        });
      });
    })
    .catch((error) => {
      console.log('error', error);
      return callback(null, {
        statusCode: 400,
        body: JSON.stringify(error),
      });
    });
};

Enter fullscreen mode Exit fullscreen mode

Now you can run the function with npm run functions and go to http://localhost:9000/.netlify/functions/test-connection to see the result. I prefer using Postman for this type of testing but you can also use curl.

Top comments (0)