collection

Define a Collection schema, including index and unique and check constraint fields.

Syntax

[@alias(<aliasId>]
collection <collName> {
  [<fieldName>: <field definition> . . .]
  [migrations <migrations block>]
  [history_days <history days>]
  [document_ttls <Boolean | Null>]
  [ttl_days <time to live days>]
  [index <index name> <index config block> . . .]
  [unique <unique constraint fields> . . .]
  [check <name> <predicateBody> . . .]
  [compute <name> [: <type>] <anonymousFunction> . . .]
}

Name

collName String Required

Name of the collection. The collName must match the following regular expression but can’t be a single underscore or reserved word: [a-zA-Z_]+[a-zA-Z0-9_]*. A collName should be singular and PascalCased.

Properties

Parameter Type Required Description

<fieldName>

String

Document field names and definitions. See Field definitions.

migrations

String

history_days

Int

Number of days of document history to retain for all documents in the collection. Defaults to 0 (retain no history). See Temporality.

document_ttls

Boolean | Null

If true, you can write to the ttl field of the collection’s documents.

If the collection schema contains field definitions, document_ttls defaults to false. Otherwise, document_ttls defaults to true.

document_ttls does not stop ttl-related deletions or affect ttl values set by the collection schema’s ttl_days field.

ttl_days

Int

Number of days that documents in the collection should be retained. See Temporality.

If the collection schema’s document_ttls field is false, Fauna ignores this field.

index

String

unique

String

check

String

compute

String

Annotations

aliasId String or Identifier

The optional @alias annotation defines a second identifier for the collection. The aliasId can be a String, "Foo", or an identifier, Foo.

Collection aliases are useful when migrating from FQL v4 and you have collection names that violate the FQL v10 naming rules. See reserved schema names.

Examples

collection Product {
  name: String?
  description: String?
  price: Double = 0.00
  quantity: Int = 0
  store: Ref<Store>?
  backorderLimit: Int?
  backordered: Boolean?
  categories: Array<String>?
  creationTime: Time = Time.now()
  creationTimeEpoch: Int?
  typeConflicts: { *: Any }?

  *: Any

  migrations {
    add .typeConflicts
    add .quantity
    backfill .quantity = 0
    drop .internalDesc
    move_conflicts .typeConflicts
    move .desc -> .description
    split .creationTime -> .creationTime, .creationTimeEpoch
  }

  unique [.name, .description, mva(.categories)]

  index byName {
    terms [.name]
    values [desc(.quantity), desc(mva(.categories))]
  }

  check posQuantity ((doc) => doc.quantity >= 0)
  compute InventoryValue: Number = (.quantity * .price)

  history_days 3
  document_ttls true
  ttl_days 5
}

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!