Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add static Dataset.exportToValue #1564

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/examples/export_entire_dataset.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
id: export-entire-dataset
title: Export entire dataset to one file
---

import CodeBlock from '@theme/CodeBlock';
import ApiLink from '@site/src/components/ApiLink';
import CrawlSource from '!!raw-loader!./export_entire_dataset.ts';

This `Dataset` example uses the `exportToValue` function to export the entire default dataset to a single CSV file into a key-value store named "my-data".

<CodeBlock className="language-js">
{CrawlSource}
</CodeBlock>
20 changes: 20 additions & 0 deletions docs/examples/export_entire_dataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Dataset } from 'crawlee';

// Retrieve or generate two items to be pushed
const data = [
{
id: 123,
name: 'foo',
},
{
id: 456,
name: 'bar'
},
];

// Push the two items to the default dataset
await Dataset.pushData(data);

// Export the entirety of the dataset to a single file in
// a key-value store named "my-data" under the key "OUTPUT"
await Dataset.exportToValue('OUTPUT', { contentType: 'text/csv', keyValueStoreName: 'my-data' });
11 changes: 11 additions & 0 deletions packages/core/src/storages/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ export class Dataset<Data extends Dictionary = Dictionary> {
await kvStore.setValue(key || 'OUTPUT', await this.client.listItems(), recordOptions);
}

/**
* Save the entirety of the default dataset's contents into one file within a key-value store.
*
* @param key The name of the value to save the data in, defaults to `OUTPUT`.
* @param options An optional options object where you can provide a `keyValueStoreName` and a `contentType` for the output.
*/
static async exportToValue(key?: string, options: RecordOptions & { keyValueStoreName?: string } = {}) {
const dataset = await this.open();
await dataset.exportToValue(key, options);
}

/**
* Returns an object containing general information about the dataset.
*
Expand Down
37 changes: 26 additions & 11 deletions test/core/storages/dataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,20 +473,35 @@ describe('dataset', () => {
expect(chunkBySize([...triple, ...triple], (2 * size) + 3)).toEqual([`[${json},${json}]`, `[${json},${json}]`, `[${json},${json}]`]);
});

test('exportToValue', async () => {
const dataset = await Dataset.open('test-123-abc-123');
describe('exportToValue', () => {
const dataToPush = [
{
hello: 'world',
},
{
foo: 'bar',
},
];

it('Should work', async () => {
const dataset = await Dataset.open(Math.random().toString(36));
await dataset.pushData(dataToPush);
await dataset.exportToValue('HELLO');

const kvData = await KeyValueStore.getValue<{ items: [] }>('HELLO');

expect(kvData.items).toEqual(dataToPush);
});

const dataToPush = [{
hello: 'world',
}, {
foo: 'bar',
}];
it('Should work as a static method for the default dataset', async () => {
await Dataset.pushData(dataToPush);

await dataset.pushData(dataToPush);
await dataset.exportToValue('HELLO');
await Dataset.exportToValue('TEST-123-123');

const kvData = await KeyValueStore.getValue<{ items: [] }>('HELLO');
expect(kvData.items).toEqual(dataToPush);
const kvData = await KeyValueStore.getValue<{ items: [] }>('TEST-123-123');

expect(kvData.items).toEqual(dataToPush);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
mstephen19 marked this conversation as resolved.
Show resolved Hide resolved
id: export-entire-dataset
title: Export entire dataset to one file
---

import CodeBlock from '@theme/CodeBlock';
import ApiLink from '@site/src/components/ApiLink';
import CrawlSource from '!!raw-loader!./export_entire_dataset.ts';

This `Dataset` example uses the `exportToValue` function to export the entire default dataset to a single CSV file into a key-value store named "my-data".

<CodeBlock className="language-js">
{CrawlSource}
</CodeBlock>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Dataset } from 'crawlee';

// Retrieve or generate two items to be pushed
const data = [
{
id: 123,
name: 'foo',
},
{
id: 456,
name: 'bar'
},
];

// Push the two items to the default dataset
await Dataset.pushData(data);

// Export the entirety of the dataset to a single file in
// a key-value store named "my-data" under the key "OUTPUT"
await Dataset.exportToValue('OUTPUT', { contentType: 'text/csv', keyValueStoreName: 'my-data' });