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.

Object.assign()

Copies properties from a source Object to a destination Object.

Signature

Object.assign(destination: { *: A }, source: { *: B }) => { *: A | B }

Description

Object.assign() copies properties from a source Object to a destination Object.

If the source and destination Object have a property with the same key, the value from the source Object is used.

If a property in the source Object doesn’t exist in the destination Object, the property is added to the destination Object.

Object.assign() doesn’t remove null properties.

Convert documents to Objects

You can use Object.assign() to convert a document into an Object. This can be useful for transforming results and manipulating document data without mutating the underlying document.

Parameters

Parameter Type Required Description

destination

Object containing fields of Any type.

Yes

The destination Object that the source’s properties are copied to.

source

Object containing fields of Any type.

Yes

Source Object containing properties to copy to the destination Object.

Return value

Type Description

Any

Updated destination Object.

Examples

Basic example

The following examples copies properties from a source Object to an empty destination Object:

Object.assign({ }, { a: 0, b: 'x' })
{
  a: 0,
  b: "x"
}

Merge properties

You can use Object.assign() to merge the properties of the source and destination Object:

Object.assign({ a: 0 }, { b: 'x' })
{
  a: 0,
  b: "x"
}

Convert a document to an Object

You can use Object.assign() to convert a document type into an Object:

// Get a Category collection document.
let category: Any = Category.byName("frozen").first()

// Convert the Category document to an Object.
Object.assign({ }, category)

The query returns an Object, not a document type:

{
  id: "456",
  coll: Category,
  ts: Time("2099-10-02T22:37:39.583Z"),
  products: {
    data: [
      {
        id: "333",
        coll: Product,
        ts: Time("2099-10-02T22:37:39.583Z"),
        name: "pizza",
        description: "Frozen Cheese",
        price: 499,
        stock: 100,
        category: Category("456")
      }
    ]
  },
  name: "frozen",
  description: "Frozen Foods"
}

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!