Collection schema

Learn: Schema

Defines the structure and behavior of a user-defined collection and its documents.

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 Document history.

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 Set a default TTL.

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.

Examples

collection Product {
  name: String
  description: String
  price: Number
  category: Ref<Category>
  stock: Int
  creationTime: Time = Time.now()
  typeConflicts: { *: Any }?

  *: Any

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

  compute stockValue: Number = (.stock * .price)

  unique [.name]
  check stockIsValid (product => product.stock >= 0)
  check priceIsValid (product => product.price >= 0)

  index byCategory {
    terms [.category]
  }

  index sortedByCategory {
    values [.category]
  }

  index byName {
    terms [.name]
  }

  index sortedByPriceLowToHigh {
    values [.price, .name, .description, .stock]
  }

  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!