createData()

Create a document in the collection with a data field.

Signature

createData(properties: {*: any}): <Document>

Description

Creates a document in the collection with the provided property values in the data field. If the properties data field isn’t provided, the data field isn’t instantiated.

Returns the created document.

Parameters

Parameter Type Required Description

properties

Object

Yes

Object describing the document properties and values to include in the data field.

properties fields

Name Type Required Description

ttl

Time

Timestamp indicating when to remove the document. When the document is removed it ceases to exist and temporal queries can’t recover the document.
Default = null Causes the document to persist indefinitely.

id

Number

The document id value is assigned by Fauna but can be set to a user-defined value with this method. The id is immutable after it is set.

data

Any

The key:value pairs to populate the data field. If omitted, the data field isn’t instantiated.

Return value

Type Description

Document

New document with a data field populated with properties if provided.

Examples

  1. Create a document with a data field:

    Books.createData({
      name: "Then and Now",
      author: "W. Somerset Maugham",
      id: "007571098"
    })
    {
      id: "368118064668999714",
      coll: Books,
      ts: Time("2023-06-21T04:59:53.710Z"),
      name: "Then and Now",
      author: "W. Somerset Maugham",
      data: {
        id: "007571098"
      }
    }
  2. Access the data field:

    Books.byId("368118064668999714")!.data.id
    "007571098"

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!