Create a collection and documents using the Fauna Dashboard

This tutorial is the second part of a series that shows you how to quickly set up and run a Fauna database using the Fauna Dashboard. For an overview of the series, see Get started with the Fauna Dashboard.

In the previous tutorial, you used the Dashboard to create a database with demo data. Now, you’ll use the Dashboard to create a new collection and populate it with documents.

Create a collection

Fauna Schema Language (FSL) lets you define and manage database schema as code.

Fauna’s demo dataset includes FSL schema for several resources, including collections. You can create and manage your database’s FSL schema using the Dashboard.

Next, create a new collection named Review in the database. Then update the collection’s FSL schema to define its structure.

  1. In the left navigation, under your database, click + beside Collections.

  2. Enter Review as the collection Name. Then click Create.

  3. Click the Schema tab.

  4. Replace the existing FSL collection schema with the following:

    collection Review {
      // Field definitions.
      // Define the structure of the collection's documents.
      title: String
      rating: Int
      // `?` is equivalent to `| Null`
      comment: String?
      // Contains a reference to a `Customer` collection document.
      customer: Ref<Customer>
      // Contains a reference to a `Product` collection document.
      product: Ref<Product>
    
      // Check constraint.
      // Requires that `rating` values be `1` to `5`.
      check isValidRating(.rating >= 1 && .rating <= 5)
    
      // Index definition.
      // Use indexes to filter and sort documents
      // in a performant way. The `sortedByRating()`
      // index sorts `Review` documents by:
      // * Ascending `rating`, then ...
      // * Ascending `title`, then ...
      // * Ascending `comment`, then ...
      // * Ascending `customer`, then ...
      // * Ascending `product`, then ...
      // * Ascending `id` (default)
      index sortedByRating {
        // Sort `Review` documents by ascending `rating` value.
        values [.rating, .title, .comment, .customer, .product]
      }
    }
  5. Click the Save. Then click Confirm Save.

Create a collection in the Fauna Dashboard

You can manage your database schema as .fsl files using the Fauna CLI or the Fauna Core HTTP API’s Schema endpoints. See Manage schema as .fsl files.

You can also manage schema using FQL queries. See FQL schema methods.

Create documents

Next, add some documents to the Review collection. As defined in the collection schema, each Review document must reference a Customer and Product document. These references model relationships between the data.

  1. In the left navigation, click your database.

  2. In the Shell tab, run the following query to add documents to the Review collection:

    // Declare the `customer` variable. The variable
    // contains a reference to a single `Customer` document.
    // Retrieve the document using the `Customer`
    // collection's `byEmail()` index.
    let alice = Customer.byEmail("alice.appleseed@example.com").first()
    
    // Get a reference to a `Product` document
    // with a `name` field value of `limes`.
    let limes = Product.byName("limes").first()
    
    // Creates a `Review` document that references
    // the alice `Customer` document and the limes
    // `Product` document.
    Review.create({
      title: "Delicious limes!",
      rating: 5,
      comment: "Incredibly ripe and juicy.",
      customer: alice,
      product: limes
    })
    
    // Get a reference to a `Product` document
    // with a `name` field value of `pizza`.
    let pizza = Product.byName("pizza").first()
    
    // Creates a `Review` document that references
    // the alic `Customer` document and the pizza
    // `Product` document/
    Review.create({
      title: "Great NYC-style pizza",
      rating: 4,
      customer: alice,
      product: pizza
    })
    
    // FQL returns the result of the last statement.
    // Calls the `Review` collection's `sortedByRating()`
    // index and projects the covered `title` and `rating`
    // fields. For collections with more than 16 documents,
    // you need to paginate results using `pageSize()` or
    // `paginate()`.
    Review.sortedByRating() {
      title,
      rating,
      comment,
      // Projecting a field that contains a document reference
      // resolves the references. This retrieves the `email`
      // field of the referenced `Customer` document.
      customer {
        email
      },
      // Retrieves the `name` field of the referenced
      // `Product` document.
      product {
        name
      }
    }

Create a collection in the Fauna Dashboard

Want to learn how to perform other document operations in FQL? Check out CRUD and basic operations.

Next steps

Congratulations! You’ve created a collection and added some documents.

In the next tutorial, you’ll create a basic app that connects to your database using a Fauna client driver. The drivers let you pass FQL queries to Fauna using an FQL template string. When you’re ready, head over to Connect Fauna to an app.

 

Connect your database to an app using a client driver.

\