1const Apify = require('apify');
2const firebase = require('firebase');
3
4Apify.main(async () => {
5 const input = await Apify.getInput();
6
7 const { data } = input;
8 let islegacyPhantomJSTask = false;
9 if (data && typeof data === 'string') {
10
11 const legacyInput = JSON.parse(data);
12 Object.assign(input, legacyInput);
13 islegacyPhantomJSTask = true;
14 }
15
16 const { datasetId, apiKey, authDomain, projectId, collectionName } = input;
17
18 if (!datasetId) {
19 throw new Error('DatasetId is required on input.');
20 }
21
22 firebase.initializeApp({
23 apiKey,
24 authDomain,
25 projectId,
26 });
27
28
29 const db = firebase.firestore();
30 console.log(`Start importing dataset ${datasetId} to firestore.`);
31 const dataset = await Apify.openDataset(datasetId, { forceCloud: true });
32 const datasetInfo = await dataset.getInfo();
33
34 const limit = 1000;
35 let counter = 0;
36 for (let offset = 0; offset < datasetInfo.itemCount; offset += limit) {
37 const pagination = await dataset.getData({
38 simplified: islegacyPhantomJSTask,
39 clean: !islegacyPhantomJSTask,
40 limit,
41 offset,
42 });
43 console.log(`Get dataset items offset: ${pagination.offset}`);
44 for (const item of pagination.items) {
45 try {
46 await db.collection(collectionName).add(item);
47 counter++;
48 } catch (err) {
49 console.log(`Cannot import item ${JSON.stringify(item)}: ${err.message}`);
50 }
51 }
52 }
53
54 console.log(`Imported ${counter} from dataset ${datasetId}.`);
55
56 console.log('Done!');
57});