'use strict'; const assert = require('assert'); const path = require('path'); const {BigQuery} = require('@google-cloud/bigquery'); const {Storage} = require('@google-cloud/storage'); const bigquery = new BigQuery(); const storage = new Storage({ projectId: 'jsonsuploading', }); const tableschema = [ { "name": "date", "type": "DATE", "mode": "NULLABLE" }, { "name": "location", "type": "STRING", "mode": "NULLABLE" }, { "name": "purchasedItems", "type": "RECORD", "mode": "REPEATED", "fields": [ { "name": "price", "type": "INTEGER", "mode": "NULLABLE" }, { "name": "quantity", "type": "INTEGER", "mode": "NULLABLE" }, { "name": "description", "type": "STRING", "mode": "NULLABLE" }, { "name": "sku", "type": "STRING", "mode": "NULLABLE" } ] }, { "name": "custType", "type": "STRING", "mode": "NULLABLE" }, { "name": "custID", "type": "STRING", "mode": "NULLABLE" }, { "name": "orderID", "type": "STRING", "mode": "NULLABLE" } ]; exports.bqload = (event) => { const file = event; console.log(`File ${file.name} start procesing.`); const datasetId = 'uploading'; const tableId = path.parse(file.name).name; const ext = path.parse(file.name).ext const jobMetadata = { schema: { fields: tableschema }, sourceFormat : 'NEWLINE_DELIMITED_JSON', createDisposition : 'CREATE_IF_NEEDED', writeDisposition: 'WRITE_TRUNCATE' }; if (ext == '.json') { return bigquery .dataset(datasetId) .table(tableId) .load(storage.bucket(file.bucket).file(file.name), jobMetadata) .then(results => { const job = results[0]; assert.equal(job.status.state, 'DONE'); console.log(`File ${file.name} processed. Job ${job.id} completed.`); const errors = job.status.errors; if (errors && errors.length > 0) { throw errors; } }) .catch(err => { console.error('ERROR:', err); }); } else { return console.error(`File ${file.name} uploaded has ${ext} extension`); } };