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: "User" })
User.create({})

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 Activities without defining fields for the collection:

Collection.create({ name: "Author" })
{
  name: "Author",
  coll: Collection,
  ts: Time("2023-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!