UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

68 lines (56 loc) 2.47 kB
import { functions, bigquery, HttpsError } from '@limebooth/atlas-cloud-functions'; import { ifElse, isString } from '@limebooth/atlas-cloud-utils'; import { logger } from '@limebooth/atlas-cloud-logger'; import * as collections from './collections/index.js'; export const manageAtlasExport = functions.https.onRequest( { timeoutSeconds: 600 }, async (request, response) => { const { collection, mode, orderOrId } = request.body; if (!isString(collection)) { throw new HttpsError( 'failed-precondition', 'Collection must be provided and should be a string pointing the a predefined config entry or a firestore collection.' ); } try { const config = await ifElse( collection in collections, () => Promise.resolve(collections[collection]), () => bigquery.generateDefaultConfig(collection, orderOrId) ).catch(error => { logger.error('Error generating config:', { error }); throw new HttpsError('internal', 'Error generating configuration.', { original: error, trace: error.stack }); }); if (mode === 'config') { return response.status(200).send({ results: { config } }); } const result = await bigquery.initializeFirestore(config, request.body); response.status(200).send({ result }); } catch (error) { response.status(500).send({ error }); } } ); export const bucketUpsertRequest = functions.https.onRequest(async (request, response) => { const { collection, orderOrId } = request.body; const config = await ifElse( collection in collections, () => Promise.resolve(collections[collection]), () => bigquery.generateDefaultConfig(collection, orderOrId) ).catch(error => { throw new HttpsError('internal', 'Error generating configuration.', { original: error, trace: error.stack }); }); logger.info('firebase-export', 'bucket-upsert-start', { config }); try { const result = bigquery.upsertFromBucket([config]); response.status(200).send({ result }); } catch (error) { response.status(500).send({ error }); } });