Skip to content

Opensearch dashboards.savedobjectmigrationfn

Home > opensearch-dashboards > SavedObjectMigrationFn

SavedObjectMigrationFn type

A migration function for a saved object type used to migrate it to a given version

Signature:

export declare type SavedObjectMigrationFn<InputAttributes = unknown, MigratedAttributes = unknown> = (doc: SavedObjectUnsanitizedDoc<InputAttributes>, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc<MigratedAttributes>;

References: SavedObjectUnsanitizedDoc, SavedObjectMigrationContext

Example

interface TypeV1Attributes {
  someKey: string;
  obsoleteProperty: number;
}

interface TypeV2Attributes {
  someKey: string;
  newProperty: string;
}

const migrateToV2: SavedObjectMigrationFn<TypeV1Attributes, TypeV2Attributes> = (doc, { log }) => {
  const { obsoleteProperty, ...otherAttributes } = doc.attributes;
  // instead of mutating `doc` we make a shallow copy so that we can use separate types for the input
  // and output attributes. We don't need to make a deep copy, we just need to ensure that obsolete
  // attributes are not present on the returned doc.
  return {
    ...doc,
    attributes: {
      ...otherAttributes,
      newProperty: migrate(obsoleteProperty),
    },
  };
};