-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpush-items.ts
85 lines (67 loc) · 2.32 KB
/
push-items.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Args } from '@oclif/core';
import type { ApifyApiError } from 'apify-client';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/apify_command.js';
import { readStdin } from '../../lib/commands/read-stdin.js';
import { tryToGetDataset } from '../../lib/commands/storages.js';
import { error, success } from '../../lib/outputs.js';
import { getLoggedClientOrThrow } from '../../lib/utils.js';
export class DatasetsPushDataCommand extends ApifyCommand<typeof DatasetsPushDataCommand> {
static override description = 'Adds data items to specified dataset. Accepts single object or array of objects.';
static override args = {
nameOrId: Args.string({
required: true,
description: 'The dataset ID or name to push the objects to',
ignoreStdin: true,
}),
item: Args.string({
description: 'The object or array of objects to be pushed.',
}),
};
async run() {
const { nameOrId, item: _item } = this.args;
const client = await getLoggedClientOrThrow();
const existingDataset = await tryToGetDataset(client, nameOrId);
if (!existingDataset) {
error({
message: `Dataset with ID or name "${nameOrId}" not found.`,
});
return;
}
const { datasetClient, dataset } = existingDataset;
let parsedData: Record<string, unknown> | Array<Record<string, unknown>>;
const item = _item || (await readStdin(process.stdin));
if (!item) {
error({ message: 'No items were provided.' });
return;
}
try {
parsedData = JSON.parse(item.toString('utf8'));
} catch (err) {
error({
message: `Failed to parse data as JSON string: ${(err as Error).message}`,
});
return;
}
if (Array.isArray(parsedData) && parsedData.length === 0) {
error({
message: 'No items were provided.',
});
return;
}
const idMessage = dataset.name
? `Dataset named ${chalk.yellow(dataset.name)} (${chalk.gray('ID:')} ${chalk.yellow(dataset.id)})`
: `Dataset with ID ${chalk.yellow(dataset.id)}`;
try {
await datasetClient.pushItems(parsedData);
success({
message: `${this.pluralString(Array.isArray(parsedData) ? parsedData.length : 1, 'Object', 'Objects')} pushed to ${idMessage} successfully.`,
});
} catch (err) {
const casted = err as ApifyApiError;
error({
message: `Failed to push items into ${idMessage}\n ${casted.message || casted}`,
});
}
}
}