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. |
Collection.create()
Learn: Collection schema |
---|
We recommend you use FSL to create and update user-defined collections. See FSL collection schema. |
Create a collection.
Description
Creates a collection with a provided collection definition, represented as a
Collection
document.
Collection
documents are FQL versions of a database’s FSL
collection schema. Collection
documents
have the CollectionDef type. See
Collections.
You can’t create a collection and use it in the same query. Use separate queries instead.
Staged schema
If a database has staged schema, this method adds a collection to the staged schema, not the active 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.
Unstaged schema changes that trigger an index build may result in downtime where the index is not queryable.
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:
-
The Fauna CLI
-
The Fauna Dashboard
-
The Fauna Core HTTP API’s Schema endpoints
A schema change triggers a transaction that validates the entire database schema. To avoid errors, do one of the following instead:
-
Perform unstaged schema changes sequentially
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
data |
Yes |
Document fields for the new
For supported document fields, see
|
Return value
Type | Description |
---|---|
The new
|
Examples
Create a collection named Inventory
without defining fields for the
collection:
Collection.create({ name: "Inventory" })
{
name: "Inventory",
coll: Collection,
ts: Time("2099-02-18T20:49:36.680Z"),
history_days: 0,
indexes: {},
constraints: []
}
Fields can be added and changed with the
document.update()
and
document.replace()
methods.
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!