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

New Storage docs #902

Merged
merged 8 commits into from
Feb 20, 2025
Merged

New Storage docs #902

merged 8 commits into from
Feb 20, 2025

Conversation

nezaj
Copy link
Contributor

@nezaj nezaj commented Feb 19, 2025

What it says on the tin!

Preview Link

Copy link

View Vercel preview at instant-www-js-storage-docs-jsv.vercel.app.


Storage is still in **beta**, but you can request access [here](https://docs.google.com/forms/d/e/1FAIpQLSdzInffrNrsYaamtH_BUe917EOpcOq2k8RWcGM19XepJR6ivQ/viewform?usp=sf_link)!
Here's a full example of how to upload and display a grid of images
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: perhaps we add an intro sentence:

"Let's start with a fresh NextJS app:" or something like this?


// Types
// ----------
export type Image = {
Copy link
Contributor

@stopachka stopachka Feb 19, 2025

Choose a reason for hiding this comment

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

nit: if we are using schema, i.m.o we can just extract the type from it.

For example:

type InstantFile = InstaQLEntity<AppSchema, '$files'>

});

if (isLoading) {
return <div>Fetching data...</div>;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would return null for isLoading.

Reasoning: Otherwise this will have a bit of a jarring flash when you first load the page


// The result of a $files query will contain objects with
// metadata and a download URL you can use for serving files!
const { $files: images } = data as { $files: Image[] };
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't need to write this if you use schema.

);
const [isUploading, setIsUploading] = useState(false);

if (isLoadingAvatar) return <div>Loading...</div>;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would return null


return <img src={imageUrl} />;
function Wrapper() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would call this App or something

Copy link
Contributor

@stopachka stopachka left a comment

Choose a reason for hiding this comment

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

Some minor comments, but LGTM! Love the detail

@nezaj nezaj changed the title [WIP] New Storage docs New Storage docs Feb 20, 2025
);
}

function Wrapper() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would write this as export default function Page or something

Copy link
Contributor

@stopachka stopachka left a comment

Choose a reason for hiding this comment

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

Great work!


{% callout type="warning" %}
Now open `instant.perms.ts` and add the following permissions
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you mean instant.schema.ts?

$files: i.entity({
"content-disposition": i.string().indexed(),
"content-type": i.string().indexed(),
"key-version": i.number(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope! Will remove!

Now open `instant.perms.ts` and add the following permissions

```javascript {% showCopy=true %}
import type { InstantRules } from "@instantdb/react";
Copy link
Contributor

Choose a reason for hiding this comment

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

InstantRules are not needed here

@@ -5,287 +5,743 @@ title: Storage
Instant Storage makes it simple to upload and serve files for your app.
You can use Storage to store images, videos, documents, and any other file type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
You can use Storage to store images, videos, documents, and any other file type.
You can store images, videos, documents, and any other file type.


Storage is still in **beta**, but you can request access [here](https://docs.google.com/forms/d/e/1FAIpQLSdzInffrNrsYaamtH_BUe917EOpcOq2k8RWcGM19XepJR6ivQ/viewform?usp=sf_link)!
Let's use a fresh Next JS app to build a full example of how to upload and
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Let's use a fresh Next JS app to build a full example of how to upload and
Let's build a full example of how to upload and

contentType: file.type,
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
// Default: 'inline'
contentDisposition: 'attachment; filename="moop.jpg"',
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: did we mean the filename to be moop.jpg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did want to show an example of setting a custom filename but perhaps we should be more descriptive!

```

Then, in your `ImageViewer` component, you can use the `cachedUrl` by default, and handle the expiration when necessary:
Make sure to update this line with the app id in your `.env` file.
Copy link
Contributor

Choose a reason for hiding this comment

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

If they have a .env file, perhaps it makes sense to simply use process.env.NEXT_PUBLIC_INSTANT_APP_ID in the example?

const path = `${user.id}/orders/${orderId}.pdf`;
await db.storage.uploadFile(path, file, {
contentType: 'application/pdf',
contentDisposition: 'attachment; filename="confirmation.pdf"',
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the filename should be dynamic no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was actually intending not but we could do that!

},
});


Copy link
Contributor

Choose a reason for hiding this comment

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

nit: perhaps these should be two separate code blocks?


The Admin SDK offers the same API for managing storage on the server, plus a few extra convenience methods for scripting.
Here's a more detailed example showing how you may implement an avatar upload feature:
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels a bit jarring -- it's a pretty big example, but feels like it's said nonchalantly. Perhaps we can give this a full subheader? We can show the example, and link out to the full repo? I am not sure but just felt a bit off scrolling here. At the least it's weird that with such a large codeblock there is no copy button

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

@nezaj nezaj merged commit 82ecacc into main Feb 20, 2025
28 checks passed
@nezaj nezaj deleted the storage-docs branch February 20, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants