async function migrateSavedObjects()

in inline.js [306:366]


async function migrateSavedObjects(subFolder) {
  const visPath = `${folderPath}/${subFolder}`;
  const exists = fs.existsSync(visPath);
  if (!exists) return new Map();
  const visualizationPaths = fs.readdirSync(visPath);
  const visualizations = visualizationPaths.map((vis) =>
    JSON.parse(fs.readFileSync(`${visPath}/${vis}`, { encoding: "utf8" }))
  );

  let response;

  try {
   response = await axios.post(
    `${baseUrl}/api/saved_objects/_bulk_create?overwrite=true`,
    visualizations.map(
      ({ type, id, attributes, references, migrationVersion }) => ({
        type,
        id,
        attributes: cleanupAttributes(attributes),
        references,
        // sometimes the migration version is not set
        migrationVersion: migrationVersion || { [subFolder]: "7.0.0" },
      })
    ),
    {
      headers: {
        "kbn-xsrf": "abc",
      },
    }
  );
  } catch (e){
    // Some machines are converting localhost to IPv6 address that doesn't work with axios
    // so providing some helpful message here to help debug the issue
    // on the first axios call
    if(/ECONNREFUSED ::1/.test(e.message)){
      console.log('Either Kibana is not running or try to use a IPv4 address (i.e. 127.0.0.1)');
    }
    throw Error(e)
  }
  if (response.data.saved_objects.some((s) => s.error)) {
    throw new Error(`error loading ${subFolder}`);
  }
  const response2 = await axios.post(
    `${baseUrl}/api/saved_objects/_bulk_get`,
    visualizations.map((v) => ({ type: subFolder, id: v.id })),
    {
      headers: {
        "kbn-xsrf": "abc",
      },
    }
  );
  const migratedVisualizations = new Map();
  response2.data.saved_objects.forEach((s) => {
    if (s.error) throw new Error(s.error);
    migratedVisualizations.set(s.id, s);
  });
  console.log(
    `Prepared ${response2.data.saved_objects.length} ${subFolder}s to be inlined`
  );
  return migratedVisualizations;
}