getSchema

The getSchema method is responsible for returning models and locales in Stackbit format.

The getSchema method should fetch the content models from the underlying content source and convert them to an array of Stackbit models. If the underlying content source supports locales, this method may also fetch the available locales and convert them to an array of Stackbit locales.

You can extend the Stackbit Schema type with custom data using the SchemaContext generic type and store it in the schema's context property. Stackbit will cache this data along with the rest of the Schema. You can retrieve the cached schema using the cache.getSchema method, which is passed to the init method.

Return Value

The getSchema method should return a Promise that resolves with a Schema object containing the following properties:

  • model: an array of Stackbit models represented by the Model[] type.
  • locales (optional): an array of Stackbit locales represented by the Locale[] type, if the underlying content source supports locales.
  • context: a custom object as defined by the SchemaContext generic type. This object can store additional data and will be cached by Stackbit along with the Schema.

Example

Continuing with the example for the init method, you can use the apiClient to fetch the content source models and locales, convert them to Stackbit models, and then return them as a Schema object.

  • 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
import type { ContentSourceInterface } from '@stackbit/types'

interface SchemaContext {
  customProp: string
}

export class MyContentSource
  implements ContentSourceInterface<UserContext, SchemaContext, DocumentContext, AssetContext>
{
  async getSchema(): Promise<Schema<SchemaContext>> {
    const models = await this.apiClient.getModels()
    const locales = await this.apiClient.getLocales()
    const stackbitModels = convertToStackbitModels(models)
    const stackbitLocales = convertToStackbitLocales(locales)
    return {
      models: stackbitModels,
      locales: stackbitLocales,
      context: {
        customProp: 'foo',
      },
    }
  }

  // other methods ...
}

The convertToStackbitModels and convertToStackbitLocales are examples of utility methods that receive arrays of content source models and locales and convert them to Stackbit models and locales