-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdelete-value.ts
75 lines (60 loc) · 1.99 KB
/
delete-value.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
import { Args } from '@oclif/core';
import type { ApifyApiError } from 'apify-client';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/apify_command.js';
import { tryToGetKeyValueStore } from '../../lib/commands/storages.js';
import { error, info } from '../../lib/outputs.js';
import { confirmAction } from '../../lib/utils/confirm.js';
import { getLoggedClientOrThrow } from '../../lib/utils.js';
export class KeyValueStoresDeleteValueCommand extends ApifyCommand<typeof KeyValueStoresDeleteValueCommand> {
static override description = 'Delete a value from a key-value store.';
static override hiddenAliases = ['kvs:delete-value'];
static override args = {
storeId: Args.string({
description: 'The key-value store ID to delete the value from.',
required: true,
}),
itemKey: Args.string({
description: 'The key of the item in the key-value store.',
required: true,
}),
};
async run() {
const { storeId, itemKey } = this.args;
const apifyClient = await getLoggedClientOrThrow();
const maybeStore = await tryToGetKeyValueStore(apifyClient, storeId);
if (!maybeStore) {
error({
message: `Key-value store with ID or name "${storeId}" not found.`,
});
return;
}
const { keyValueStoreClient: client } = maybeStore;
const existing = await client.getRecord(itemKey);
if (!existing) {
error({
message: `Item with key "${itemKey}" not found in the key-value store.`,
});
return;
}
const confirm = await confirmAction({
type: 'record',
});
if (!confirm) {
info({ message: 'Key-value store record deletion aborted.', stdout: true });
return;
}
try {
await client.deleteRecord(itemKey);
info({
message: `Record with key "${chalk.yellow(itemKey)}" deleted from the key-value store.`,
stdout: true,
});
} catch (err) {
const casted = err as ApifyApiError;
error({
message: `Failed to delete record with key "${itemKey}" from the key-value store.\n ${casted.message || casted}`,
});
}
}
}