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> |
Document field names and definitions. See Field definitions. |
||
migrations |
See Migrations block. |
||
history_days |
Number of days of document history to retain for all documents in the
collection. Defaults to |
||
document_ttls |
If If the collection schema contains
field definitions,
|
||
ttl_days |
Number of days that documents in the collection should be retained. See Set a default TTL. |
||
index |
See Index definitions. |
||
unique |
See Unique constraints. |
||
check |
See Check constraints. |
||
compute |
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!