Skip to content

Commit

Permalink
feat: Add schema parameter to getOrCreate for kvs, dataset (apify#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jirimoravcik authored Feb 16, 2022
1 parent 8f7eeed commit 032ecae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.2.1 / 2022/02/XX
===================
- Added support for `schema` parameter in key-value store and dataset `getOrCreate` function

2.2.0 / 2022/02/XX
===================
- Added support for the (for now experimental) `view` parameter to the dataset items endpoints
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apify-client",
"version": "2.2.0",
"version": "2.2.1",
"description": "Apify API client for JavaScript",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion src/base/resource_collection_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export class ResourceCollectionClient extends ApiClient {
return parseDateFields(pluckData(response.data)) as R;
}

protected async _getOrCreate<R>(name?: string): Promise<R> {
protected async _getOrCreate<D, R>(name?: string, resource?: D): Promise<R> {
const response = await this.httpClient.call({
url: this._url(),
method: 'POST',
params: this._params({ name }),
data: resource,
});
return parseDateFields(pluckData(response.data)) as R;
}
Expand Down
10 changes: 8 additions & 2 deletions src/resource_clients/dataset_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export class DatasetCollectionClient extends ResourceCollectionClient {
/**
* https://docs.apify.com/api/v2#/reference/datasets/dataset-collection/create-dataset
*/
async getOrCreate(name?: string): Promise<Dataset> {
async getOrCreate(name?: string, options?: DatasetCollectionClientGetOrCreateOptions): Promise<Dataset> {
ow(name, ow.optional.string);
return this._getOrCreate(name);
ow(options?.schema, ow.optional.object); // TODO: Add schema validatioon

return this._getOrCreate(name, { schema: options?.schema });
}
}

Expand All @@ -45,4 +47,8 @@ export interface DatasetCollectionClientListOptions {
desc?: boolean;
}

export interface DatasetCollectionClientGetOrCreateOptions {
schema?: Record<string, unknown>;
}

export type DatasetCollectionClientListResult = PaginatedList<Dataset>
9 changes: 7 additions & 2 deletions src/resource_clients/key_value_store_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export class KeyValueStoreCollectionClient extends ResourceCollectionClient {
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/store-collection/create-key-value-store
*/
async getOrCreate(name?: string): Promise<KeyValueStore> {
async getOrCreate(name?: string, options?: KeyValueStoreCollectionClientGetOrCreateOptions): Promise<KeyValueStore> {
ow(name, ow.optional.string);
ow(options?.schema, ow.optional.object); // TODO: Add schema validatioon

return this._getOrCreate(name);
return this._getOrCreate(name, { schema: options?.schema });
}
}

Expand All @@ -46,4 +47,8 @@ export interface KeyValueStoreCollectionClientListOptions {
desc?: boolean;
}

export interface KeyValueStoreCollectionClientGetOrCreateOptions {
schema?: Record<string, unknown>;
}

export type KeyValueStoreCollectionListResult = Omit<KeyValueStore, 'stats'> & { username?: string; };

0 comments on commit 032ecae

Please sign in to comment.