Skip to content

Address Book App Quickstart

Let’s take 5 minutes to create a stateful-serverless backend for a simple Address Book. It's going to run globally distributed with local read-write latencies around 50ms.

We are going to walk through 4 steps:

  • Create a collection and add some data to it.
  • Query that data.
  • Save the query (A saved query is called a Query Worker).
  • Execute the Query Worker!

First things first, if you don't already have a Macrometa account go create a free one and mosey on back.

Step 1: Create a Collection

Let’s start by clicking the COLLECTIONS tab in the left nav and then New Collection on the right. Select the Document option, give it the name addresses, and save it.

Note: The collection addresses that you just created is now distributed to every location in the fabric!

create-collection

Step 2: Add some data to your new collection and query it

Now, click the QUERIES tab in the left nav to open the C8QL query editor. Copy and paste the query below into the editor and click Run Query a couple times.

// Query to insert addresses
FOR persons IN [ 
  { firstname: "Captain", lastname: "Nemo", email: "cpnemo@gnautilus.com" },
  { firstname: "Pierre", lastname: "Aronnax", email: "pierre@asoc.org" },
  { firstname: "Ned", lastname: "Land", email: "ned@nature.org" },
  { firstname: "Cyrus", lastname: "Smith", email: "cycy@laborrights.org" },
  { firstname: "Tom", lastname: "Ayrton", email: "tommy@water.org" },
  { firstname: "Jules", lastname: "Verne", email: "j.garcia@en-julesverne.nantesmetropole.fr" } 
  ]
  INSERT persons INTO addresses

Your Query Result will be empty brackets, but if you click on Profile button you can checkout what just happened along with some performance details. Your data was just written to the location you're currently logged into and replicated across all of the nodes in your fabric.

Now, let's query the data you just added to your collection. Copy the below query and replace the INSERT query currently in the editor with it.

FOR docs IN addresses RETURN docs 

You should see the data you just saved returned.

Ok, now we are going to save this query. Saving it will turn it into a Query Worker.

Step 3: Save the Query

Click the Save Query button and name the saved query getAddresses

Now, in around 50ms, you have a globally distributed stateful-serverless endpoint.

dashboard

Step 4: Execute the Query Worker

Now that you've saved the query click on the API Usage button, and we automatically generate a Query Worker (Exactly like a serverless function, but a save query instead of a function)!

create-query-worker

Next Steps

Nice job you totally rocked that quickstart!

Now let's build out the REST of our CRUD API. Just create a Query Worker for each of these queries.

SaveContact

INSERT {firstname:@firstName,
        lastname:@lastName,
        email:@email} 
INTO addresses

ReadContact

FOR entry 
IN addresses 
RETURN entry

RemoveContact

REMOVE @_key 
IN addresses

UpdateContact

UPDATE @_key WITH { firstname:@firstName, 
                    lastname:@lastName, 
                    email:@email} 
IN addresses

Sweet, so now you have a full functional API for your app. We made a front-end for you to take your new backend for a spin.

Now that you've build your app, full stack, you can dig into the docs and see all the rad things Macrometa can help you build >> Essentials guide.