DEV Community

Cover image for Netlify Forms with FaunaDB
Vishang
Vishang

Posted on

1

Netlify Forms with FaunaDB

It's nice to see the form data to be stored in netlify.


Now, what's next. I am going to store my form data to FaunaDB.

Create a db-schema

  • Create a Db-schema folder with registration.gql
    type Registration {
    name: String!
    email: String!
    }
    type Query {
    allRegistration: [Registration!]!
    }

Create DB with FaunaDB

  • Login to FaunaDB Console
  • Create new DB with FaunaDB
  • Import schema (DB Console -> GraphQL -> Import Schema)
  • Create a new FUANA_DB_SECRET with security tab in console

Create Environment Variable

  • In netlify console.
    • Go to Deploy Settings -> Environment -> Edit variable Add FAUNA_DB_SECRET -> Copy paste your secret here. This is used for authentication purpose.

Create Nelify Functions

Create netlify functions manually

  • create a functions folder at root level
  • within functions folder create your netlify functions i.e for the form submission create submission-created.js under the functions folder. The functions name also needs to match the specific event name with netlify events. The available event triggers available here on netlify docs
const fetch = require('node-fetch');
exports.handler = async (event) => {
console.log(event);
const body = JSON.parse(event.body);
const { firstname, lastname, email} = body.payload.data;
const response = await fetch(
'https://graphql.fauna.com/graphql',
{
method:'POST',
headers:{
Authorization: `Bearer ${process.env.FAUNA_API_SECRET}`,
},
body: JSON.stringify({
query: `
mutation($name: String!, $email: String!) {
createRegistration(data: { name: $name, email: $email }) {
_id
}
}
`,
variables: { name: `${firstname} ${lastname}`, email },
}),
})
.then(res => res.json())
.catch(err => console.log(err));
return {
statusCode: 200,
body: 'Success',
};
};

Folder structure will look something like this.
Folder Structure after creating functions

Create netlify functions with netlify-cli

$ netlify functions:create submission-created

$ To deploy netlify function via netlify-cli

$ netlify deploy --prod

Now, in netlify console in functions tab all available netlify functions will be listed. Click on function for debug purposes.

Netlify Config

Functions to run in netlify we need a netlify.toml (configuration) file

[build]
command = "#"
publish = "src"
functions = "functions"
view raw netlify.toml hosted with ❤ by GitHub

Now the data which is pushed on netlify forms will also be available in FuanaDB for use.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay