Dataset <Data>
Index
Properties
backend
readonlyconfiguration
id
log
optionalname
Accessors
stats
Backend-independent usage counters tracked for this dataset (read / write operations issued to the underlying storage backend). Counted per backend call.
Returns DatasetStats
Methods
[asyncIterator]
Default async iterator for the dataset, iterating over items. Allows using the dataset directly in a
for await...ofloop.Example usage:
const dataset = await Dataset.open('my-results');for await (const item of dataset) {console.log(item);}Returns AsyncGenerator<Data, void, undefined>
drop
Removes the dataset either from the Apify cloud storage or from the local directory, depending on the mode of operation.
Returns Promise<void>
entries
Returns dataset entries (index-value pairs).
When awaited (
await dataset.entries()), returns all entries as a flat[index, item][]array. When used as an async iterable (for await...of), iterates over all entries across pages without loading everything into memory at once.Example usage:
const dataset = await Dataset.open('my-results');// Iterate over all entriesfor await (const [index, item] of dataset.entries()) {console.log(`Item at ${index}: ${JSON.stringify(item)}`);}// Or fetch all at onceconst entries = await dataset.entries();console.log(entries);Parameters
options: DatasetIteratorOptions = {}
Options for the iteration.
Returns AsyncIterable<[number, Data], any, any> & Promise<[number, Data][]>
export
Returns all the data from the dataset. This will iterate through the whole dataset via the
listItems()client method, which gives you only paginated results.Parameters
options: DatasetExportOptions = {}
Returns Promise<Data[]>
exportTo
Save the entirety of the dataset's contents into one file within a key-value store.
Parameters
key: string
The name of the value to save the data in.
optionaloptions: DatasetExportToOptions
An optional options object where you can provide the dataset and target KVS name.
optionalcontentType: string
Only JSON and CSV are supported currently, defaults to JSON.
Returns Promise<Data[]>
exportToCSV
Save entire default dataset's contents into one CSV file within a key-value store.
Parameters
key: string
The name of the value to save the data in.
optionaloptions: Omit<DatasetExportToOptions, fromDataset>
An optional options object where you can provide the target KVS name.
Returns Promise<void>
exportToJSON
Save entire default dataset's contents into one JSON file within a key-value store.
Parameters
key: string
The name of the value to save the data in.
optionaloptions: Omit<DatasetExportToOptions, fromDataset>
An optional options object where you can provide the target KVS name.
Returns Promise<void>
forEach
Iterates over dataset items, yielding each in turn to an
iterateefunction. Each invocation ofiterateeis called with two arguments:(item, index).If the
iterateefunction returns a Promise then it is awaited before the next call. If it throws an error, the iteration is aborted and theforEachfunction throws the error.Example usage
const dataset = await Dataset.open('my-results');await dataset.forEach(async (item, index) => {console.log(`Item at ${index}: ${JSON.stringify(item)}`);});Parameters
iteratee: DatasetConsumer<Data>
A function that is called for every item in the dataset.
optionaloptions: DatasetIteratorOptions = {}
All
forEach()parameters.optionalindex: number = 0
Specifies the initial index number passed to the
iterateefunction.
Returns Promise<void>
getData
Returns DatasetContent object holding the items in the dataset based on the provided parameters.
Parameters
options: DatasetDataOptions = {}
Returns Promise<DatasetContent<Data>>
getInfo
Returns an object containing general information about the dataset.
Example:
{id: "WkzbQMuFYuamGv3YF",name: "my-dataset",createdAt: new Date("2015-12-12T07:34:14.202Z"),modifiedAt: new Date("2015-12-13T08:36:13.202Z"),accessedAt: new Date("2015-12-14T08:36:13.202Z"),itemCount: 14,}Returns Promise<DatasetInfo>
Throws - If the underlying storage no longer exists (e.g. it was deleted externally).
map
Produces a new array of values by mapping each value in list through a transformation function
iteratee(). Each invocation ofiteratee()is called with two arguments:(element, index).If
iterateereturns aPromisethen it's awaited before a next call.Parameters
iteratee: DatasetMapper<Data, R>
optionaloptions: DatasetIteratorOptions = {}
All
map()parameters.
Returns Promise<R[]>
purge
pushData
Stores an object or an array of objects to the dataset. The function returns a promise that resolves when the operation finishes. It has no result, but throws on invalid args or other errors.
IMPORTANT: Make sure to use the
awaitkeyword when callingpushData(), otherwise the crawler process might finish before the data is stored!Parameters
data: Data | Data[]
Object or array of objects containing data to be stored in the default dataset. The objects must be serializable to JSON.
Returns Promise<void>
reduce
Reduces a list of values down to a single value.
The first element of the dataset is the initial value, with each successive reductions should be returned by
iteratee(). Theiteratee()is passed three arguments: thememo,valueandindexof the current element being folded into the reduction.The
iterateeis first invoked on the second element of the list (index = 1), with the first element given as the memo parameter. After that, the rest of the elements in the dataset is passed toiteratee, with the result of the previous invocation as the memo.If
iteratee()returns aPromiseit's awaited before a next call.If the dataset is empty, reduce will return undefined.
Parameters
iteratee: DatasetReducer<Data, Data>
Returns Promise<undefined | Data>
values
Returns dataset items.
When awaited (
await dataset.values()), returns all items as a flatData[]array. When used as an async iterable (for await...of), iterates over all items across pages without loading everything into memory at once.Example usage:
const dataset = await Dataset.open('my-results');// Iterate over all items (memory-efficient for large datasets)for await (const item of dataset.values()) {console.log(item);}// Or fetch all items at onceconst items = await dataset.values();console.log(items);Parameters
options: DatasetIteratorOptions = {}
Options for the iteration.
Returns AsyncIterable<Data, any, any> & Promise<Data[]>
staticexportToCSV
Save entire default dataset's contents into one CSV file within a key-value store.
Parameters
key: string
The name of the value to save the data in.
optionaloptions: DatasetExportToOptions
An optional options object where you can provide the dataset and target KVS name.
Returns Promise<void>
staticexportToJSON
Save entire default dataset's contents into one JSON file within a key-value store.
Parameters
key: string
The name of the value to save the data in.
optionaloptions: DatasetExportToOptions
An optional options object where you can provide the dataset and target KVS name.
Returns Promise<void>
staticgetData
Returns DatasetContent object holding the items in the dataset based on the provided parameters.
Parameters
options: DatasetDataOptions = {}
Returns Promise<DatasetContent<Data>>
staticopen
Opens a dataset and returns a promise resolving to an instance of the Dataset class.
Datasets are used to store structured data where each object stored has the same attributes, such as online store products or real estate offers. The actual data is stored either on the local filesystem or in the cloud.
For more details and code examples, see the Dataset class.
Parameters
optionalidentifier: null | string | StorageIdentifier
ID or name of the dataset to be opened. If a string is provided, it will first be looked up as an ID; if no such storage exists, it will be treated as a name. If
nullorundefined, the function returns the default dataset associated with the crawler run.optionaloptions: StorageOpenOptions = {}
Storage manager options.
Returns Promise<Dataset<Data>>
The
Datasetclass represents a store for structured data where each object stored has the same attributes, such as online store products or real estate offers. You can imagine it as a table, where each object is a row and its attributes are columns. Dataset is an append-only storage - you can only add new records to it but you cannot modify or remove existing records. Typically it is used to store crawling results.Do not instantiate this class directly, use the Dataset.open function instead.
Datasetstores its data either on local disk or in the Apify cloud, depending on whether theAPIFY_LOCAL_STORAGE_DIRorAPIFY_TOKENenvironment variables are set.If the
APIFY_LOCAL_STORAGE_DIRenvironment variable is set, the data is stored in the local directory in the following files:Note that
{DATASET_ID}is the name or ID of the dataset. The default dataset has ID:default, unless you override it by setting theAPIFY_DEFAULT_DATASET_IDenvironment variable. Each dataset item is stored as a separate JSON file, where{INDEX}is a zero-based index of the item in the dataset.If the
APIFY_TOKENenvironment variable is set butAPIFY_LOCAL_STORAGE_DIRnot, the data is stored in the Apify Dataset cloud storage. Note that you can force usage of the cloud storage also by passing theforceCloudoption to Dataset.open function, even if theAPIFY_LOCAL_STORAGE_DIRvariable is set.Example usage: