/* * Copyright (C) 2018-2022 Open StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {SCSearchRequest, SCThingType} from '@openstapps/core'; import {Bar} from 'cli-progress'; import {Client, ConnectorClient, OutOfRangeError} from '@openstapps/api'; /** * Copy data for a StAppsCore type from one backend to another * @param apiIn The API for the backend to copy from * @param apiOut The API for the backend to copy to * @param source The source identifier for the bulk * @param type The SCThingType to copy * @param batchSize The batch size for the copy operation */ export async function copy( apiIn: Client, apiOut: ConnectorClient, source: string, type: SCThingType, batchSize: number, ): Promise { const bulk = await apiOut.bulk(type, source); let searchRequest: SCSearchRequest = { filter: { arguments: { field: 'type', value: type, }, type: 'value', }, size: 0, }; let searchResponse = await apiIn.search(searchRequest); searchRequest.size = batchSize; const progressBar = new Bar({}); progressBar.start(searchResponse.pagination.total, 0); let outOfRange = false; do { try { ({searchRequest, searchResponse} = await apiIn.searchNext(searchRequest, searchResponse)); const things = searchResponse.data; await Promise.all( Array.from({length: ConnectorClient.ITEM_CONCURRENT_LIMIT}).map(async () => { for (let item = things.shift(); item !== undefined; item = things.shift()) { progressBar.increment(1); await bulk.add(item); } }), ); } catch (error) { if (error instanceof OutOfRangeError) { outOfRange = true; } else { progressBar.stop(); throw error; } } } while (!outOfRange); await bulk.done(); progressBar.stop(); }