Populate the cookbook database

This introductory tutorial shows you how to create and populate a database, which instantiates the demo data used in subsequent tutorials.

Prerequisite

Start by getting a Fauna account, as described in the first step of the Dashboard quick start.

Remember to enable type checking in the Dashboard or through the driver if you are using a driver to run the examples.

Create an example database and collection

  1. In the dashboard explorer window, go to your Region Group and click the Create Database icon (+) on the right to create a database.

  2. In the Create Database dialog,

    • Enter a database Name. The cookbook examples use the Cookbook database but you can name the database anything you want.

    • Deselect Enable backups.

    • Leave all other settings at their default value.

  3. With the Cookbook or your preferred database name selected, create a Collection to hold the database documents. In the lower panel that lists database entities, choose Collections and click the CREATE COLLECTION button.

  4. The cookbook uses coffee grower data as an example database. In the Create Collection dialog, enter CoffeeBean as the collection Name and leave the History and TTL fields in their default state.

  5. Click the CREATE button.

    In the Shell window on the right, notice the CoffeeBean collection schema you created:

    collection CoffeeBean {
      history_days 0
    }

 

A best practice is to create schema for supported database entities, such as reference:fsl_reference/collection.adoc[], reference:fsl_reference/function.adoc[], reference:fsl_reference/role.adoc[], and reference:fsl_reference/access-provider.adoc[], using FSL instead of FQL queries.

You can create and update schema using the dashboard or CLI commands. This cookbook uses the Dashboard to create and change schema. The FQL language and API shows you how to define and manage schema using the CLI.

Verify database and collection creation

  1. In the upper panel, for your Region Group, choose the Cookbook database.

  2. In the Shell window, clear any existing content and display the collections in the Cookbook database using the following query:

    Collection.all()
    {
      data: [
        {
          name: "CoffeeBean",
          coll: Collection,
          ts: Time("2023-10-13T14:27:51.120Z"),
          history_days: 0,
          constraints: [],
          indexes: {}
        }
      ]
    }

    The CoffeeBean collection should be the only collection.

  1. You can also verify the existence of the CoffeeBean collection by referencing the collection by name:

    Collection.byName("CoffeeBean")
    {
      name: "CoffeeBean",
      coll: Collection,
      ts: Time("2023-10-13T14:27:51.120Z"),
      constraints: [],
      history_days: 0,
      indexes: {}
    }
  2. Now, clear the Shell window and display the Document in the CoffeeBean collection:

    CoffeeBean.all()
    {
      data: []
    }

    You can see the collection is not yet populated. The next step adds a document to the collection and subsequent tutorials add more documents and make changes to the collection and documents.

Add a document to a collection

CoffeeBean documents hold properties that describe an individual coffee bean grower.

  1. Add the first document to the CoffeeBean collection:

    CoffeeBean.create({
        "Species": "Arabica",
        "Owner": "metad plc",
        "Country_of_Origin": "Ethiopia",
        "Harvest_Year": 2014,
        "Quality_Parameters": {
            "Aroma": 8.67,
            "Flavor": 8.83,
            "Balance": 8.42
            },
        "Altitude": {
          "unit_of_measurement": "m",
          "mean": 2075
        }
    })
    {
      id: "378484096953745473",
      coll: CoffeeBean,
      ts: Time("2023-10-13T15:03:32.470Z"),
      Species: "Arabica",
      Owner: "metad plc",
      Country_of_Origin: "Ethiopia",
      Harvest_Year: 2014,
      Quality_Parameters: {
        Aroma: 8.67,
        Flavor: 8.83,
        Balance: 8.42
      },
      Altitude: {
        unit_of_measurement: "m",
        mean: 2075
      }
    }

    Notice that in addition to the properties provided when you created the document, the document schema includes a Fauna-generated document ID (id), the collection of which the document is a member (coll), and a document creation timestamp (ts).

  2. View all the documents in the collection with the following query, and there should be only one document:

    CoffeeBean.all()
    {
      data: [
        {
          id: "378484096953745473",
          coll: CoffeeBean,
          ts: Time("2023-10-13T15:03:32.470Z"),
          Species: "Arabica",
          Owner: "metad plc",
          Country_of_Origin: "Ethiopia",
          Harvest_Year: 2014,
          Quality_Parameters: {
            Aroma: 8.67,
            Flavor: 8.83,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 2075
          }
        }
      ]
    }

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!