Collection.create()

Create a collection.

Signature

Collection.create(data: {*: any}): CollectionDef

Description

The Collection.create() method creates a collection definition document with the provided data, which must include a unique collection name.

Collection.create({ name: "Inventory" })

You can’t create a collection and use it in the same query. Use separate queries instead.

Parameters

Parameter Type Required Description

data

Object

Yes

Object describing the collection.

data fields

Name Type Required Description

name

String

Yes

Name of the collection to create.

  • The name must match the following regular expression: [a-zA-Z_]+[a-zA-Z0-9_]*.

  • The name can’t be a single underscore (_).

  • The name can’t be a reserved word.

  • Because collections are similar to traditional objects and because of chaining indexes to collections, Fauna recommends using a singular name that follows your class and struct formatting convention.

  • See the aliasing section about renaming existing entities.

history_days

Number

Number of days to retain the document in the collection.
Default = 0

ttl_days

Number

Delete documents ttl_days number of days after their last write.
Default = null, documents are retained indefinitely.

data

Object

User-defined metadata.

Return value

Type Description

CollectionDef

New collection definition document.

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"),
  indexes: {},
  constraints: []
}

 

Fields can be added and changed with the update() and replace() methods.

See also

Collection document definition to delete a collection.

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!