updateDocument

The updateDocument method is responsible for updating a document in the underlying content source.

Netlify Create calls this method when the user updates one of the document fields using the Netlify Create UI.

Parameters

This method receives an options object with the following properties:

  • document:Document: The existing document from the Netlify Create cache before any updates are applied.
  • operations: UpdateOperation[]: An array of UpdateOperation. Please introspect the UpdateOperation in the @stackbit/types package for more information.
  • userContext?: UserContext: An object that contains the user's email, name, and optional OAuth access tokens defined by the UserContext generic type.

Netlify Create may delay and group several changes into a single method call. In this case, the operations array will contain multiple operations, each representing a value change of a different field.

Return Value

This method should return a Promise that resolves with void, indicating that the document has been successfully updated.

Example

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
import type { ContentSourceInterface, UpdateOperation } from '@stackbit/types'

interface UserContext {
  accessToken: string
}

export class MyContentSource
  implements
    ContentSourceInterface<
      UserContext,
      SchemaContext,
      DocumentContext,
      AssetContext
    >
{
  async updateDocument(options: {
    document: Document<DocumentContext>
    operations: UpdateOperation[]
    userContext?: UserContext
  }): Promise<void> {
    // convert the update operations to API fields
    const updateFields = convertOperationsToUpdateAPI(
      options.operations,
      options.document,
    )

    // update the document
    await this.apiClient.updateDocument({
      accessToken: options.userContext?.accessToken,
      documentId: options.document.id,
      updateFields,
    })
  }

  // other methods ...
}

The convertOperationsToUpdateAPI method is an example of a utility method that receives an array of Netlify Create update operations and converts them to the updated fields expected by the content source API.