import uuid from 'uuid' ; import { decodeVersion , encodeVersion } from '../version' ; import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry' ; import { SavedObjectsRawDoc , SavedObjectSanitizedDoc } from './types' ; /**
 * A serializer that can be used to manually convert  SavedObjectsRawDoc or
 *  SavedObjectSanitizedDoc documents to the other kind.
 *
 *  remarks Serializer instances should only be created and accessed by calling  SavedObjectsServiceStart . createSerializer public export class SavedObjectsSerializer { private readonly registry : ISavedObjectTypeRegistry ; internal constructor ( registry ) { this . registry = registry ; } param { SavedObjectsRawDoc } rawDoc public isRawSavedObject ( rawDoc ) { const { type , namespace } = rawDoc . _source ; const namespacePrefix = namespace && this . registry . isSingleNamespace ( type ) ? `${ namespace }:` : '' ; return Boolean ( type && rawDoc . _id . startsWith ( `${ namespacePrefix }${ type }:` ) && rawDoc . _source . hasOwnProperty ( type ) ) ; } param { SavedObjectsRawDoc } doc public rawToSavedObject ( doc ) { const { _id , _source , _seq_no , _primary_term } = doc ; const { type , namespace , namespaces , originId } = _source ; const version = _seq_no != null || _primary_term != null ? encodeVersion ( _seq_no ! , _primary_term ! ) : undefined ; return { type , id : this . trimIdPrefix ( namespace , type , _id ) , ... ( namespace && this . registry . isSingleNamespace ( type ) && { namespace } ) , ... ( namespaces && this . registry . isMultiNamespace ( type ) && { namespaces } ) , ... ( originId && { originId } ) , attributes : _source [ type ] , references : _source . references || [ ] , ... ( _source . migrationVersion && { migrationVersion : _source . migrationVersion } ) , ... ( _source . updated_at && { updated_at : _source . updated_at } ) , ... ( version && { version } ) , } ; } param { SavedObjectSanitizedDoc } savedObj public savedObjectToRaw ( savedObj ) { const { id , type , namespace , namespaces , originId , attributes , migrationVersion , updated_at , version , references , } = savedObj ; const source = { [ type ] : attributes , type , references , ... ( namespace && this . registry . isSingleNamespace ( type ) && { namespace } ) , ... ( namespaces && this . registry . isMultiNamespace ( type ) && { namespaces } ) , ... ( originId && { originId } ) , ... ( migrationVersion && { migrationVersion } ) , ... ( updated_at && { updated_at } ) , } ; return { _id : this . generateRawId ( namespace , type , id ) , _source : source , ... ( version != null && decodeVersion ( version ) ) , } ; } param { string } namespace param { string } type param { string } id public generateRawId ( namespace , type , id ? ) { const namespacePrefix = namespace && this . registry . isSingleNamespace ( type ) ? `${ namespace }:` : '' ; return `${ namespacePrefix }${ type }:${ id || uuid . v1 ( ) }` ; } private trimIdPrefix ( namespace , type , id ) { assertNonEmptyString ( id , 'document id' ) ; assertNonEmptyString ( type , 'saved object type' ) ; const namespacePrefix = namespace && this . registry . isSingleNamespace ( type ) ? `${ namespace }:` : '' ; const prefix = `${ namespacePrefix }${ type }:` ; if ( ! id . startsWith ( prefix ) ) { return id ; } return id . slice ( prefix . length ) ; } } function assertNonEmptyString ( value , name ) { if ( ! value || typeof value !== 'string' ) { throw new TypeError ( `Expected "${ value }" to be a ${ name }` ) ; } }