CreateClass

CreateClass( param_object )
CreateClass( param_object )
CreateClass( param_object )
CreateClass( param_object )
create_class( param_object )
CreateClass( param_object )

Description

This function is deprecated as of Fauna 2.7.0. Use CreateCollection instead.

A Class was renamed to a Collection in Fauna 2.7.0. This function continues to exist for compatibility with older drivers.

The CreateClass function is used to create a class (now, collection) which groups instance objects (now, documents). Once the class has been created, it is possible to create instances in the class. You cannot create a class and insert instances into that class in the same transaction.

Unique name required

The class name must be unique within the database. If you try to lookup a class by name and only create it if it does not exist, the operation might fail if another transaction created the class in the meantime.

Parameters

Argument Type Definition and Requirements

param_object

The param_object fields are described below.

param_object

Field Name Field Type Definition and Requirements

name

The name of a class. Classes (now, collections) cannot be named any of the following reserved words: events, set, self, documents, or _.

data

Optional - This is user-defined metadata for the class (now, collection). It is provided for the developer to store information at the class (now, collection) level.

history_days

Optional - The number of days that document history is retained for in this class (now, collection). The default is 30 days.

Setting history_days to null retains this class' (now, collection’s) history forever. Setting history_days to 0 retains only the current version of each document in this class (now, collection); no history is retained.

ttl_days

Optional - The number of days documents are retained for this class. Instances which have not been updated within the configured TTL duration are removed. Setting ttl_days to null retains documents forever. The default is null.

Removal is handled by a background task, so once a document (including collections, databases, indexes, keys, roles, and tokens) "expires" due to the setting in the ttl_days field, it could be some time (hours or days) before the removal occurs. There is no guarantee that removal actually occurs.

permissions

Optional - Provides the ability to enable permissions at the class level. See class permissions for additional details.

Returns

An object containing the fields returned by the CreateClass function is described below.

Field Name Field Type Definition and Requirements

ref

Reference

The reference is an automatically generated identifier within the database to uniquely identify the class that was created.

name

The name of the class (now, collection) that was created.

ts

The timestamp, with microsecond resolution, associated with the creation of the class (now, collection).

history_days

The number of days to retain history. 0 means that no history is retained for any document in this class (now, collection); only the current version is retained. null means that history is retained indefinitely.

Examples

The following query creates a class called "boons" with defaults:

client.Query(CreateCollection(Obj("name", "boons")));
{
  "ref": { "@ref": "classes/boons" },
  "class": { "@ref": "classes" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "create_class": { "object": { "name": "boons" } } }'
HTTP/1.1 201 Created
{
  "resource": {
    "ref": { "@ref": "classes/boons" },
    "class": { "@ref": "classes" },
    "ts": 1509244539971619,
    "history_days": 30,
    "name": "boons"
  }
}
result, _ := client.Query(f.CreateCollection(f.Obj{"name": "boons"}))

fmt.Println(result)
map[ref:{boons 0xc4201f2fa0 <nil>} ts:1527277025406385 history_days:30 name:boons]
System.out.println(
      client.query(
            CreateCollection(Obj("name", Value("boons")))
      ).get());
{
  ref: ref(id = "boons", collection = ref(id = "collections")),
  ts: 1526674566802938,
  history_days: 30,
  name: "boons"
}
client.query(q.CreateCollection({ name: 'boons' }))
.then((ret) => console.log(ret))
{ ref: Ref(id=boons, collection=Ref(id=collections)),
  ts: 1527274777496292,
  history_days: 30,
  name: 'boons' }
client.query(q.create_collection({"name": "boons"}))
{
  "ref": { "@ref": "classes/boons" },
  "class": { "@ref": "classes" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}
client.query(CreateCollection(Obj("name" -> "boons")))
{
  "ref": { "@ref": "classes/boons" },
  "class": { "@ref": "classes" },
  "ts": 1509244539971619,
  "history_days": 30,
  "name": "boons"
}

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!