DEV Community

Jarod Peachey
Jarod Peachey

Posted on

FaunaDB in 5 Minutes

Originally posted on Five Minute Developer

The serverless ecosystem is growing more than ever before, and there are a lot of new tools built around it. In this post, we'll be overviewing FaunaDB, a serverless database built for scalability and ease of use.

What is Fauna?

FaunaDB is a global cloud database created to integrate with the JAMstack and modern serverless architecture. According to their site, FaunaDB "transforms the traditional DBMS into a Data API that gives you all the capabilities of an old-guard database, without sacrificing flexibility, scale, and performance".

That's a pretty cool sentence.

FaunaDB also allows you to query your data how you want. It supports relational data, document-based data, and graph-based data. It's got a ton of cool features, which we'll list below.

Fauna + GraphQL

FaunaDB recently added support for GraphQL, which allows you to create custom schemas and access your data using GraphQL. Fauna allows access to a GraphQL endpoint for each database you create, which allows for easy access to your data.

It's great for integrating with SSGs like Gatsby and Hugo. It also integrates perfectly with Apollo.

FQL

For more advanced functionality, Fauna also comes with the Fauna Query Language (FQL). FQL is a function-based querying language, built for advanced data manipulation. FQL comes with at least 50 built-in functions that you can use depending on your needs.

An FQL statement looks like this:

Query(
  Create(
    Collection('posts'),
    {
      data: {
        title: "New Post",
        author: "Jarod Peachey",
        date: "10/08/2019"
      },
    },
  )
)

Executing this FQL statement will create a new document in the posts collection, which we can access through one of the many other FQL functions.

Functions

FaunaDB allows you to create reusable queries in FQL using functions. This is useful for, well, repeating queries without writing more code.

A function is defined like this.

CreateFunction({
  name: "create_post",
  body: Query(
    Lambda(
      "data",
      Create(
        Collection('posts'),
          {
            data: {
            title: Select("title", Var("data")),
            author: Select("author", Var("data")),
            date: Select("date", Var("data"))
          },
        },
      )
    )
  )
})

The first argument to the CreateFunction query is the name. For this example, we named it "create_post".

The second argument is the query to run when the function is called. This is placed inside of a Lambda() function, which lets us access the data passed to the function call.

To call this function, simply execute this query.

Call(Function("create_post"), {
  title: "New Post",
  author: "Jarod Peachey",
  date: "10/08/2019"
})

Indexes

Fauna also allows you to create indexes, which let you retrieve data based on specific attributes, rather than the document ref (or id). You can use indexes to get a single post or multiple posts.

For example, the simplest index gets all the documents in a single collection.

Query(
  CreateIndex({
    name: "all_posts",
    source: Collection("posts")
  })
)

This allows you to access all posts, via a call to the index.

Match(Index('all_posts'))

Examples

FaunaDB is growing in popularity, so there are plenty of examples to choose from, as well as Fauna's own documentation.

My personal favorite tutorial is Rethinking Twitter as a Serverless App on CSS Tricks. It was immensely helpful when I started using FaunaDB, and I highly recommend reading it.

Conclusion

In my opinion, FaunaDB is one of the front-runners in the serverless world, and it's an amazing platform. I would definitely recommend using it for your next project requiring a database.

Top comments (0)