createDocument

The createDocument method is responsible for creating a document in the underlying content source.

Netlify Create calls this method when the user creates a new documents using the Netlify Create UI.

Parameters

This method receives an options object with the following properties:

  • updateOperationFields: Record<string, UpdateOperationField>: A map of update operation fields by field names. Please introspect the UpdateOperationField in the @stackbit/types package for more information.
  • model: Model: A model representing the document to be created.
  • locale?: string: A locale id for the document to be created.
  • userContext?: UserContext: An object that contains the user's email, name, and optional OAuth access tokens defined by the UserContext generic type.

Return Value

This method should return a Promise that resolves with an object containing the following properties:

  • documentId: string: The ID of the created document.

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
  • 37
  • 38
import type {
  ContentSourceInterface,
  UpdateOperationField,
  Model
} from '@stackbit/types';

interface UserContext {
  accessToken: string;
}

export class MyContentSource implements ContentSourceInterface<UserContext> {
  async createDocument(options: {
    updateOperationFields: Record<string, UpdateOperationField>;
    model: Model;
    locale?: string;
    defaultLocaleDocumentId?: string;
    userContext?: UserContext;
  }): Promise<{ documentId: string }> {
    // convert stackbit operation fields to apiDocument
    const apiDocument = convertOperationFieldsToAPIDocument(
      options.updateOperationFields,
      options.model
    );

    // create the document
    const result = await this.apiClient.createDocument({
      accessToken: options.userContext?.accessToken
      document: apiDocument
    });

    // return the id of the new document
    return {
      documentId: result.id
    };
  }

  // other methods ...
}

The convertOperationFieldsToAPIDocument is an example of a utility method that takes an array of Netlify Create update operation fields and converts them to the format expected by the content source API.