/
push-data.js
44 lines (37 loc) · 1.51 KB
/
push-data.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
39
40
41
42
43
44
const { getApifyStorageClient, getDefaultStorageId, APIFY_STORAGE_TYPES } = require('../../lib/actor');
const { ApifyCommand } = require('../../lib/apify_command');
class PushDataCommand extends ApifyCommand {
async init() {
await super.init();
// Read data from stdin of the command
this.stdin = await this.readStdin(process.stdin);
}
async run() {
const { args } = this.parse(PushDataCommand);
const { item } = args;
const apifyClient = getApifyStorageClient();
const defaultStoreId = getDefaultStorageId(APIFY_STORAGE_TYPES.DATASET);
const data = item || this.stdin;
let parsedData;
try {
parsedData = JSON.parse(data);
} catch (err) {
throw new Error(`Failed to parse data as JSON string: ${err.message}`);
}
await apifyClient.dataset(defaultStoreId).pushItems(parsedData);
}
}
PushDataCommand.description = 'Stores an object or an array of objects to the default dataset of the actor run.\n'
+ 'It is possible to pass data using item argument or stdin.\n'
+ 'Passing data using argument:\n'
+ '$ apify actor:push-data {"foo": "bar"}\n'
+ 'Passing data using stdin with pipe:\n'
+ '$ cat ./test.json | apify actor:push-data\n';
PushDataCommand.args = [
{
name: 'item',
required: false,
description: 'JSON string with one object or array of objects containing data to be stored in the default dataset.',
},
];
module.exports = PushDataCommand;