mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 17:42:57 +00:00
feat: copy connector
This commit is contained in:
82
backend/copy-connector/src/copy.ts
Normal file
82
backend/copy-connector/src/copy.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<void> {
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user