Skip to content

Commit d4dae7f

Browse files
authored
refactor: set storage object on dataset open (#3116)
Following up on [this recent update](#2837) where we started storing the full storageObject when opening a key-value store, this PR applies the same logic when opening a dataset. This change prepares the ground for adding a new method: Dataset.createItemsPublicUrl, which will need access to the dataset's urlSigningSecretKey.
1 parent c75c4f9 commit d4dae7f

File tree

8 files changed

+42
-27
lines changed

8 files changed

+42
-27
lines changed

packages/core/src/storages/dataset.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export class Dataset<Data extends Dictionary = Dictionary> {
232232
id: string;
233233
name?: string;
234234
client: DatasetClient<Data>;
235+
readonly storageObject?: Record<string, unknown>;
235236
log: Log = log.child({ prefix: 'Dataset' });
236237

237238
/**
@@ -244,6 +245,7 @@ export class Dataset<Data extends Dictionary = Dictionary> {
244245
this.id = options.id;
245246
this.name = options.name;
246247
this.client = options.client.dataset(this.id) as DatasetClient<Data>;
248+
this.storageObject = options.storageObject;
247249
}
248250

249251
/**
@@ -733,6 +735,7 @@ export interface DatasetOptions {
733735
id: string;
734736
name?: string;
735737
client: StorageClient;
738+
storageObject?: Record<string, unknown>;
736739
}
737740

738741
export interface DatasetContent<Data> {

test/e2e/kv-open-return-storage-object/test.mjs

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/e2e/kv-open-return-storage-object/actor/.actor/actor.json renamed to test/e2e/storage-open-return-storage-object/actor/.actor/actor.json

File renamed without changes.

test/e2e/kv-open-return-storage-object/actor/.gitignore renamed to test/e2e/storage-open-return-storage-object/actor/.gitignore

File renamed without changes.

test/e2e/kv-open-return-storage-object/actor/Dockerfile renamed to test/e2e/storage-open-return-storage-object/actor/Dockerfile

File renamed without changes.

test/e2e/kv-open-return-storage-object/actor/main.js renamed to test/e2e/storage-open-return-storage-object/actor/main.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Actor, KeyValueStore } from 'apify';
1+
import { Actor, KeyValueStore, Dataset } from 'apify';
22

33
const mainOptions = {
44
exit: Actor.isAtHome(),
@@ -10,5 +10,9 @@ const mainOptions = {
1010

1111
await Actor.main(async () => {
1212
const kv = await KeyValueStore.open();
13-
await kv.setValue('storageObject', { storeObject: kv.storageObject });
13+
const dataset = await Dataset.open();
14+
await kv.setValue('storageObject', {
15+
keyValueStorageObject: kv.storageObject,
16+
datasetStorageObject: dataset.storageObject,
17+
});
1418
}, mainOptions);

test/e2e/kv-open-return-storage-object/actor/package.json renamed to test/e2e/storage-open-return-storage-object/actor/package.json

File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { initialize, expect, getActorTestDir, runActor } from '../tools.mjs';
2+
3+
/* This test verifies that the storageObject is correctly returned when the KeyValueStore or Dataset is opened.
4+
* The storageObject is the result of the KeyValueStoreClient.get() or Dataset.get() methods,
5+
* containing properties such as name, id, and other custom attributes.
6+
*/
7+
8+
const testActorDirname = getActorTestDir(import.meta.url);
9+
await initialize(testActorDirname);
10+
11+
const { defaultKeyValueStoreItems: items } = await runActor(testActorDirname);
12+
13+
await expect(items !== undefined, 'Key value store exists');
14+
15+
const item = items.find((kvItem) => kvItem.name === 'storageObject');
16+
17+
const parsed = JSON.parse(item.raw.toString());
18+
19+
await expect(
20+
typeof parsed === 'object' && parsed !== null,
21+
'Key-value contains key "storageObject" and it\'s value is a non-nullable object',
22+
);
23+
24+
const datasetStorageObject = parsed.datasetStorageObject;
25+
const keyValueStorageObject = parsed.keyValueStorageObject;
26+
27+
await expect(datasetStorageObject.id !== null, 'datasetStorageObject contains id');
28+
await expect(datasetStorageObject.name !== null, 'datasetStorageObject contains name');
29+
await expect(datasetStorageObject.userId !== null, 'datasetStorageObject contains userId');
30+
31+
await expect(keyValueStorageObject.id !== null, 'keyValueStorageObject contains id');
32+
await expect(keyValueStorageObject.name !== null, 'keyValueStorageObject contains name');
33+
await expect(keyValueStorageObject.userId !== null, 'keyValueStorageObject contains userId');

0 commit comments

Comments
 (0)