Skip to content

Commit ee9cbcc

Browse files
committed
feat: datasets create / key-value-stores create
1 parent 0a21ebe commit ee9cbcc

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

src/commands/datasets/create.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Args } from '@oclif/core';
2+
import chalk from 'chalk';
3+
4+
import { ApifyCommand } from '../../lib/apify_command.js';
5+
import { tryToGetDataset } from '../../lib/commands/storages.js';
6+
import { error, success } from '../../lib/outputs.js';
7+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
8+
9+
export class DatasetsCreateCommand extends ApifyCommand<typeof DatasetsCreateCommand> {
10+
static override description = 'Creates a new Dataset on your account';
11+
12+
static override args = {
13+
datasetName: Args.string({
14+
description: 'Optional name for the Dataset',
15+
required: false,
16+
}),
17+
};
18+
19+
static override enableJsonFlag = true;
20+
21+
async run() {
22+
const { datasetName } = this.args;
23+
24+
const client = await getLoggedClientOrThrow();
25+
26+
if (datasetName) {
27+
const existing = await tryToGetDataset(client, datasetName);
28+
29+
if (existing) {
30+
error({ message: 'Cannot create a Dataset with the same name!' });
31+
return;
32+
}
33+
}
34+
35+
const newDataset = await client.datasets().getOrCreate(datasetName);
36+
37+
if (this.flags.json) {
38+
return newDataset;
39+
}
40+
41+
success({
42+
message: `Dataset with ID ${chalk.yellow(newDataset.id)}${datasetName ? ` (called ${chalk.yellow(datasetName)})` : ''} was created.`,
43+
stdout: true,
44+
});
45+
46+
return undefined;
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Args } from '@oclif/core';
2+
import chalk from 'chalk';
3+
4+
import { ApifyCommand } from '../../lib/apify_command.js';
5+
import { tryToGetKeyValueStore } from '../../lib/commands/storages.js';
6+
import { error, success } from '../../lib/outputs.js';
7+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
8+
9+
export class KeyValueStoresCreateCommand extends ApifyCommand<typeof KeyValueStoresCreateCommand> {
10+
static override description = 'Creates a new Key-value Store on your account';
11+
12+
static override hiddenAliases = ['kvs:create'];
13+
14+
static override args = {
15+
keyValueStoreName: Args.string({
16+
description: 'Optional name for the Key-value Store',
17+
required: false,
18+
}),
19+
};
20+
21+
static override enableJsonFlag = true;
22+
23+
async run() {
24+
const { keyValueStoreName } = this.args;
25+
26+
const client = await getLoggedClientOrThrow();
27+
28+
if (keyValueStoreName) {
29+
const existing = await tryToGetKeyValueStore(client, keyValueStoreName);
30+
31+
if (existing) {
32+
error({ message: 'Cannot create a Key-value Store with the same name!' });
33+
return;
34+
}
35+
}
36+
37+
const newStore = await client.keyValueStores().getOrCreate(keyValueStoreName);
38+
39+
if (this.flags.json) {
40+
return newStore;
41+
}
42+
43+
success({
44+
message: `Key-value Store with ID ${chalk.yellow(newStore.id)}${keyValueStoreName ? ` (called ${chalk.yellow(keyValueStoreName)})` : ''} was created.`,
45+
stdout: true,
46+
});
47+
48+
return undefined;
49+
}
50+
}

src/lib/commands/storages.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { ApifyClient, Dataset, DatasetClient, KeyValueStore, KeyValueStoreClient } from 'apify-client';
2+
3+
import { getLocalUserInfo } from '../utils.js';
4+
5+
type ReturnTypeForStorage<T extends 'dataset' | 'keyValueStore'> = T extends 'dataset'
6+
? {
7+
dataset: Dataset;
8+
datasetClient: DatasetClient;
9+
}
10+
: { keyValueStore: KeyValueStore; keyValueStoreClient: KeyValueStoreClient };
11+
12+
async function tryToGetStorage<T extends 'dataset' | 'keyValueStore'>(
13+
client: ApifyClient,
14+
id: string,
15+
storageType: T,
16+
): Promise<ReturnTypeForStorage<T> | null> {
17+
const byIdOrName = await client
18+
.dataset(id)
19+
.get()
20+
.catch(() => undefined);
21+
22+
if (byIdOrName) {
23+
return {
24+
[storageType]: byIdOrName,
25+
[`${storageType}Client`]: client[storageType](byIdOrName.id),
26+
} as ReturnTypeForStorage<T>;
27+
}
28+
29+
const info = await getLocalUserInfo();
30+
31+
const byName = await client[storageType](`${info.username!}/${id}`)
32+
.get()
33+
.catch(() => undefined);
34+
35+
if (byName) {
36+
return {
37+
[storageType]: byName,
38+
[`${storageType}Client`]: client[storageType](byName.id),
39+
} as ReturnTypeForStorage<T>;
40+
}
41+
42+
return null;
43+
}
44+
45+
export async function tryToGetDataset(
46+
client: ApifyClient,
47+
datasetId: string,
48+
): Promise<{ dataset: Dataset | undefined; datasetClient: DatasetClient } | null> {
49+
return tryToGetStorage(client, datasetId, 'dataset');
50+
}
51+
52+
export async function tryToGetKeyValueStore(
53+
client: ApifyClient,
54+
keyValueStoreId: string,
55+
): Promise<{ keyValueStore: KeyValueStore | undefined; keyValueStoreClient: KeyValueStoreClient } | null> {
56+
return tryToGetStorage(client, keyValueStoreId, 'keyValueStore');
57+
}

0 commit comments

Comments
 (0)