Collection()

Dynamically get the Collection by name.

Signature

Collection(name: String): Any

Description

The Collection() method gets the name collection.

Calling Collection() is similar to accessing <collectionName> directly, except:

  • Collection() returns an Any value

  • <collectionName> is a Collection value

This difference only affects static typing, not runtime behavior. Both Collection() and <collectionName> support the same collection instance methods.

In most cases, you should use <collectionName>. However, Collection() is useful if you need to iterate through a list of collection names.

Parameters

Parameter Type Required Description

name

String

Yes

Collection name

Return value

Type Description

Any

The name collection.

Examples

Collection() returns an Any value. For example:

Collection("Product")

Returns an Any value:

Product

Difference with <collectionName>

Accessing <collectionName> directly returns a similar value, typed as a Collection. For example:

Product

Returns the Product Collection value:

Product

Method chaining

In most cases, you’ll chain other collection instance methods to Collection(). For example:

Collection("Product").create({
  name: "pinata",
  price: 20.99
})

Returns an Any value:

{
  id: "393431480156553282",
  coll: Product,
  ts: Time("2024-03-26T14:45:47.893Z"),
  name: "pinata3",
  price: 20.99
}

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!