Field accessors and method chaining

This section covers field accessors and method chaining.

Field access

Accessors can be viewed as keys for referencing fields or properties in an associative array, dictionary, or lookup table. In Fauna documents, fields can be accessed using dot notation or bracket notation.

Dot notation field accessor

The dot prefix (.) preceding the field name is used to access a field or property, as shown by the .aKey notation in this example:

let object = {
  aKey: "one",
  bKey: "two"
}
object.aKey
"one"

Providing a field that doesn’t exist returns a Type error: does not have field.

This group by UDF uses dot notation to group all customers by city, showing how to use dot notation to create anonymous functions:

let group_by = (set, selector) => set.fold({}, (result, value) => {
  let key = selector(value)
  let existing = result[key] // <-- dynamic field access
  let group = if (existing == null) {
    [value]
  } else {
    existing.append(value)
  }
  Object.assign(result, Object.fromEntries([[key, group]]))
})

group_by(Customer.all(), .address.city)
{
  Washington: [
    {
      id: "386968888588369988",
      coll: Customer,
      ts: Time("2024-01-15T06:45:40.390Z"),
      firstName: "Alice",
      lastName: "Appleseed",
      address: {
        street: "87856 Mendota Court",
        city: "Washington",
        state: "DC",
        zipCode: "20220"
      },
      telephone: "208-346-0715",
      creditCard: {
        network: "Visa",
        number: "4556781272473393"
      }
    },
    {
      id: "386968888589418564",
      coll: Customer,
      ts: Time("2024-01-15T06:45:40.390Z"),
      firstName: "Bob",
      lastName: "Brown",
      address: {
        street: "72 Waxwing Terrace",
        city: "Washington",
        state: "DC",
        zipCode: "20002"
      },
      telephone: "719-872-8799",
      creditCard: {
        network: "Visa",
        number: "4916112310613672"
      }
    },
    {
      id: "386968888590467140",
      coll: Customer,
      ts: Time("2024-01-15T06:45:40.390Z"),
      firstName: "Carol",
      lastName: "Clark",
      address: {
        street: "5 Troy Trail",
        city: "Washington",
        state: "DC",
        zipCode: "20220"
      },
      telephone: "907-949-4470",
      creditCard: {
        network: "Visa",
        number: "4532636730015542"
      }
    }
  ]
}

Bracket notation field accessor

The following example uses bracket notation ([ ]), passing the field name as a string to access the field or property:

let object = {
  aKey: "one",
  bKey: "two"
}
object["aKey"]
"one"

Providing a field that doesn’t exist returns null.

Using bracket notation allows you to dynamically access document fields as shown in this example:

let object = {
  aKey: "one",
  bKey: "two"
}
let x = "aKey"
object[x]
"one"

Method chaining

Methods can be chained using dot notation to compose complex queries where the output of the previous method is the input of the next method.

In this example, the query returns up to ten documents from all the documents in the Product collection:

Product.all().take(10)

Anonymous field and method access

A variant of dot notation, the optional chaining operator can be used to access a field or invoking a method, returning null instead of an error if the left side of the expression evaluates to null.

Access an anonymous field example:

let book = {
  name: 'Hamlet',
  author: {
    name: 'Shakespeare'
  }
}

book.writer?.name

The example returns null instead of an error provided type checking is disabled.

Invoke an anonymous method example:

let book = {
  name: 'Hamlet',
  author: {
    name: 'Shakespeare'
  }
}

book.author.customMethod?.()

The example returns null instead of an error provided type checking is disabled.

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!