-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (31 loc) · 1007 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// import { config } from 'dotenv';
// config();
const fetch = require('node-fetch');
const unzipper = require('unzipper');
const { DATA_URL } = require('./config/download.js');
const bqConfig = require('./config/bigquery.js');
const { getTable, insertData } = require('./bigquery.js');
const downloadFile = async () => {
if (!DATA_URL) {
throw 'No url';
}
const res = await fetch(DATA_URL);
const json = await res?.json();
const url = json?.result?.resources?.[0]?.url;
if (!url) {
throw 'Error: no url for download';
}
const fileReq = await fetch(url);
fileReq.body.pipe(unzipper.Parse()).on('entry', function (entry) {
const table = 'erb';
if (entry.type !== 'File') {
throw `Entry type is ${entry.type}, File expected`;
}
if (!entry?.path?.includes('erb')) {
throw `Unknown file name ${entry.path}`;
}
const tableConfig = bqConfig[table];
getTable(tableConfig).then(() => insertData(entry, tableConfig));
});
};
downloadFile();