Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

role.replace()

Learn: Roles

We recommend you use FSL to create and update user-defined roles. See FSL role schema.

Replace a user-defined role.

Signature

replace(data: { *: Any }) => Role

Description

Replaces all fields in a user-defined role, represented as an Role document, with fields from a provided data object. Fields not present in the data object, excluding the coll and ts metadata fields, are removed.

Role documents are FQL versions of a database’s FSL role schema. See Roles.

Metadata fields

You can’t use this method to insert or edit the following metadata fields:

  • coll

  • ts

Staged schema

If a database has staged schema, this method interacts with the database’s staged schema, not the active schema.

You can’t rename a role while a database has staged schema.

If the database has no staged schema, using this method is equivalent to making an unstaged schema change. Changes are applied immediately to the database’s active schema.

Avoid concurrent schema changes

Concurrent unstaged schema changes can cause contended transactions, even if the changes affect different resources. This includes unstaged changes made using:

A schema change triggers a transaction that validates the entire database schema. To avoid errors, do one of the following instead:

Parameters

Parameter Type Required Description

data

Object

true

Fields for the Role document. Fields not present, excluding the coll and ts metadata fields, in the object are removed.

For supported document fields, see Role collection.

The object can’t include the following metadata fields:

* coll * ts

Return value

Type Description

Role

Role document with replaced fields.

Examples

Role.byName("manager")?.replace({
  name: "manager",
  privileges: [
    {
      resource: "OrderItem",
      actions: {
        create: true,
        read: true,
        write: true,
        delete: true
      }
    },
    {
      resource: "Customer",
      actions: {
        read: true
      }
    },
    {
      resource: "Manager",
      actions: {
        read: "(doc) => Query.identity() == doc && Date.today().dayOfWeek < 6"
      }
    },
    {
      resource: "getOrCreateCart",
      actions: {
        call: true
      }
    },
    {
      resource: "checkout",
      actions: {
        call: <<-END
          (args) => {
            let order = Order.byId(args[0])!
            order?.customer == Query.identity()
          }
        END
      }
    }
  ],
  membership: [
    {
      resource: "Manager"
    },
    {
      resource: "User",
      predicate: "(user) => user.accessLevel == \"manager\""
    }
  ]
})
{
  name: "manager",
  coll: Role,
  ts: Time("2099-10-28T16:14:20.640Z"),
  privileges: [
    {
      resource: "OrderItem",
      actions: {
        create: true,
        read: true,
        write: true,
        delete: true
      }
    },
    {
      resource: "Customer",
      actions: {
        read: true
      }
    },
    {
      resource: "Manager",
      actions: {
        read: "(doc) => Query.identity() == doc && Date.today().dayOfWeek < 6"
      }
    },
    {
      resource: "getOrCreateCart",
      actions: {
        call: true
      }
    },
    {
      resource: "checkout",
      actions: {
        call: <<-END
          (args) => {
            let order = Order.byId(args[0])!
            order?.customer == Query.identity()
          }
        END
      }
    }
  ],
  membership: [
    {
      resource: "Manager"
    },
    {
      resource: "User",
      predicate: "(user) => user.accessLevel == \"manager\""
    }
  ]
}

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!