Skip to content

feat: add support for arbitrary metadata #70

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

Merged
merged 5 commits into from
Oct 18, 2023
Merged

Conversation

eduardoboucas
Copy link
Member

Adds support for arbitrary metadata, supplied by users, to be stored alongside entries.

To write an entry with metadata, one can supply the metadata option to the set() method:

const store = getStore("my-store")

await store.set("some-key", "some-value", { metadata: { name: "Netlify", cool: true } })

To read an entry and also retrieve its metadata, a new getWithMetadata() method has been added:

const store = getStore("my-store")
const { data, etag, metadata } = await store.getWithMetadata("some-key")

assert.equal(await data.text(), "some-value")
assert.equal(typeof etag, "string")
assert.deepEqual(metadata, { name: "Netlify", cool: true })

Needs https://github.com/netlify/netlify-server/pull/3111.

Part of https://github.com/netlify/pillar-runtime/issues/751.

@eduardoboucas eduardoboucas requested a review from a team as a code owner October 18, 2023 13:39
@netlify
Copy link

netlify bot commented Oct 18, 2023

Deploy Preview for blobs-js ready!

Name Link
🔨 Latest commit 4f06edb
🔍 Latest deploy log https://app.netlify.com/sites/blobs-js/deploys/652ff984e18a5e00085086ad
😎 Deploy Preview https://deploy-preview-70--blobs-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Oct 18, 2023
@eduardoboucas eduardoboucas changed the title feat: add support for user-supplied metadata feat: add support for arbitrary metadata Oct 18, 2023
@@ -0,0 +1,70 @@
import { Client, ClientOptions, getClientOptions } from './client.ts'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods on this file haven't changed. They were just moved from store.ts.

This happened in its own commit, so reviewing each commit individually might help.

- `arrayBuffer`: Returns the entry as an
[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
- `blob`: Returns the entry as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
- `json`: Parses the entry as JSON and returns the resulting object

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if that parsing fails? do we just bubble up the SyntaxError? would it make sense to include a note about what happens?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the same error that you get when parsing an invalid Response will be thrown. I can add a note to the docs.

@@ -191,6 +191,29 @@ const entry = await blobs.get('some-key', { type: 'json' })
console.log(entry)
```

### `getWithMetadata(key: string, { type?: string }): Promise<{ data: any, etag: string, metadata: object }>`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we reference the Metadata type in the return type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon it's ok in the docs to just say object. It's just Record<string, unknown>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think there's much value in using the custom type here.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Do you think it would be possible to also implement getMetadata, which uses a HEAD request and returns just the metadata?

@eduardoboucas
Copy link
Member Author

Looks good. Do you think it would be possible to also implement getMetadata, which uses a HEAD request and returns just the metadata?

I think so, although maybe not for the beta since it'll require some work in nfserver.

@eduardoboucas eduardoboucas merged commit 9b2a4df into main Oct 18, 2023
@ascorbic
Copy link
Contributor

It could be a nice addition at some point, to cheaply check metadata before requesting potentially large objects

@eduardoboucas eduardoboucas deleted the feat/metadata branch October 24, 2023 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants