import { defaults } from 'lodash' ; import { SavedObjectsClientContract } from '../../saved_objects/types' ; import { SavedObjectsErrorHelpers } from '../../saved_objects/' ; import { Logger } from '../../logging' ; import { getUpgradeableConfig } from './get_upgradeable_config' ; interface Options { savedObjectsClient : SavedObjectsClientContract ; version : string ; buildNum : number ; log : Logger ; handleWriteErrors : boolean ; } export async function createOrUpgradeSavedConfig ( options ) { const { savedObjectsClient , version , buildNum , log , handleWriteErrors } = options ; const upgradeableConfig = await getUpgradeableConfig ( { savedObjectsClient , version , } ) ; const attributes = defaults ( { buildNum } , upgradeableConfig ? ( upgradeableConfig . attributes as any ) : { } ) ; try { await savedObjectsClient . create ( 'config' , attributes , { id : version } ) ; } catch ( error ) { if ( handleWriteErrors ) { if ( SavedObjectsErrorHelpers . isConflictError ( error ) ) { return ; } if ( SavedObjectsErrorHelpers . isNotAuthorizedError ( error ) || SavedObjectsErrorHelpers . isForbiddenError ( error ) ) { return attributes ; } } throw error ; } if ( upgradeableConfig ) { log . debug ( `Upgrade config from ${ upgradeableConfig . id } to ${ version }` , { prevVersion : upgradeableConfig . id , newVersion : version , } ) ; } }