Options
All
  • Public
  • Public/Protected
  • All
Menu

@quintype/migration-helpers

Index

Type aliases

MappingFunction

MappingFunction: function

Type declaration

    • (entries: ReadonlyArray<any>, batchNumber: number): Promise<ReadonlyArray<any>>
    • Parameters

      • entries: ReadonlyArray<any>
      • batchNumber: number

      Returns Promise<ReadonlyArray<any>>

Private MetadataStream

MetadataStream: Writable & object

Story

Represents a Story in the Editor. Please See Individual Parts of the Story.

Variables

Const endAuthorStream

endAuthorStream: endMetadataStream = endMetadataStream

Async function that Closes the author stream. See createAuthorStream for usage

Const endSectionStream

endSectionStream: endMetadataStream = endMetadataStream

Async function that Closes the section stream. See createSectionStream for usage

Const endStoryAttributeStream

endStoryAttributeStream: endMetadataStream = endMetadataStream

Async function that Closes the author stream. See createStoryAttributeStream for usage

Functions

Private asyncToStream

  • asyncToStream<T>(source: AsyncIterableIterator<T> | Readable): Readable
  • Type parameters

    • T

    Parameters

    • source: AsyncIterableIterator<T> | Readable

    Returns Readable

batchStream

  • Batch records in the stream. It will call your transform with a batch, and expect a batch of results to be returned Downstream from the pipe will see individual items, not a batch.

    Example

    import { batchStream, Story } from '@quintype/migration-helpers';
    
    async function mapRowToStories(rows: any, _batchNumber: number): Promise<ReadonlyArray<Story>> {
      const someRelatedData = fetchInfoForStories(rows.map(r => r.storyId));
      return rows.map(r => ({...rowToStory(r), ...someRelatedData(r.storyId)}));
    }
    
    writeStories(
      readStoriesFromDatabase(conn)
        .pipe(batchStream(100, mapRowToStories))
        .on('end', () => conn.destroy())
    );

    Type parameters

    • T

    Parameters

    • Default value size: number = 1000

      The maximum number of records in the batch

    • Default value mapping: MappingFunction = async x => x

      An optional function to transform the batch before being pushed onto the stream

    Returns Transform

createAuthorStream

  • Creates a writeable stream that can be used to pipe authors to. Authors that are piped to this stream are automatically writen to authors-*.txt.gz. The stream created with this function can be passed to writeStories

    Example

    import { Author, createAuthorStream, endAuthorStream, writeStories } from "@quintype/migration-helpers";
    
    async function main() {
      const authorStream = createAuthorStream(mapAuthorIdToAuthor, opts);
      await writeStories(getStoriesFromDB(), {...opts, authorStream});
      await endAuthorStream(authorStream);
    }

    Parameters

    • mapping: function

      An async function that maps from the external-ids given and the Author

        • Parameters

          Returns Promise<ReadonlyArray<Author>>

    • Default value opts: GenerateToFileOptions = {}

    Returns MetadataStream

Private createJSONStream

  • createJSONStream<T>(batch: ReadonlyArray<T>): Readable

Private createMetadataStream

createSectionStream

  • Creates a writeable stream that can be used to pipe sections to. Sections that are piped to this stream are automatically writen to sections-*.txt.gz. The stream created with this function can be passed to writeStories

    Example

    import { Section, createSectionStream, endSectionStream, writeStories } from "@quintype/migration-helpers";
    
    async function main() {
      const sectionStream = createSectionStream(mapSectionIdToSection, opts);
      await writeStories(getStoriesFromDB(), {...opts, sectionStream});
      await endSectionStream(sectionStream);
    }

    Parameters

    Returns MetadataStream

createStoryAttributeStream

  • Creates a writeable stream that can be used to pipe authors to. StoryAttributes that are piped to this stream are automatically writen to authors-*.txt.gz. The stream created with this function can be passed to writeStories

    Example

    import { StoryAttribute, createStoryAttributeStream, endStoryAttributeStream, writeStories } from "@quintype/migration-helpers";
    
    async function main() {
      const authorStream = createStoryAttributeStream(mapStoryAttributeIdToStoryAttribute, opts);
      await writeStories(getStoriesFromDB(), {...opts, authorStream});
      await endStoryAttributeStream(authorStream);
    }

    Parameters

    Returns MetadataStream

Private endMetadataStream

Private readFromGzipFile

  • readFromGzipFile(path: string): Promise<string>

Private teeStoryToMetadataStream

  • teeStoryToMetadataStream(f: function, stream?: Writable): Transform

writeStories

  • Takes a generator of stories, and writes the stories into .txt.gz files. Each file will have 1000 stories.

    You can provide a streams for various metadata to be written to. See createAuthorStream and createSectionStream for examples

    Example

    import { batchStream, Story, writeStories } from '@quintype/migration-helpers';
    
    async function* readStoriesFromDatabase(): AsyncIterableIterator<Story> {
     const txn = createDbTxn();
     const results = txn.runQuery("select * from stories");
     while(results.hasNext()) {
       yield rowToStory(results.next());
     }
     txn.close();
    }
    
    writeStories(readStoriesFromDatabase(), 'interviews')

    You may also use a stream to produce stories. Use batchStream to efficiently load stories.

    import { Story, writeStories } from '@quintype/migration-helpers';
    import { Transform } from 'stream'
    
    const stream = conn.query("select * from stories").stream();
    
    async function convertRowsToStories(rows: ReadonlyArray<any>): Promise<ReadonlyArray<Story>> {
      const relatedData = await loadSomeData(rows.map(row => row.storyId));
      return row.map(row => rowToStory(row, relatedData));
    }
    
    writeStories(stream.pipe(batchStream(100, convertRowsToStories)));

    Parameters

    • generator: AsyncIterableIterator<Story> | Readable
    • Default value source: string = "export"

      A string describing where the stories come from. ex: interviews

    • Default value opts: GenerateToFileOptions & MetadataStreamOptions = {}

      Control some fine grained tuning

    Returns Promise<void>

Private writeToFiles

  • writeToFiles<T>(source: AsyncIterableIterator<T> | Readable, __namedParameters: object): Promise<void>
  • Type parameters

    • T

    Parameters

    • source: AsyncIterableIterator<T> | Readable
    • __namedParameters: object
      • batchSize: undefined | number
      • directory: string
      • filePrefix: string

    Returns Promise<void>

Generated using TypeDoc