Building on prior explorations with CRUD, this demo adds functionality for photo uploads. Uploaded image files are resized to fit a defined size, and then stored using Vercel Blob Storage. This results in a public URL which in turn is saved to MongoDB as an ordinary string.
The default UI for uploads (i.e. <input type="file">) has been hidden; instead we add a custom UI via input label. Drag-and-drop has been implemented, but only where this feature makes sense. Before enabling drag-and-drop we use matchMedia with a pointer: fine media query to ensure the user actually has a mouse (i.e. NOT a tablet or phone)
On the backend, the BusBoy library handles file uploads by parsing the relevant headers that the frontend creates (i.e. multipart/form-data).
We use the Sharp JavaScript library to scale down images to a predictable and performant size. This helps to prevent abuse of the system.
Assuming you are using a copy of this template: Prior to local development, deploy your repo directly to Vercel, using an environment variable DATABASE_URL containing your MongoDB connection string (be sure to include a database name on the end of the string). After the initial Vercel deployment, we can setup the Blob Storage as follows:
- Go to your project on the Vercel Dashboard
- Navigate to the Storage tab
- Click Create Database and select Blob
- Follow the prompts to enable Blob storage for your project
- Vercel will automatically generate a
BLOB_READ_WRITE_TOKENenvironment variable - For local development, copy this token from the Environment Variables section
- Add it to your
.envfile:BLOB_READ_WRITE_TOKEN=your_token_here
The Blob storage is now ready to accept image uploads both in production (on Vercel) and locally.
- Add your MongoDB connection string to the
.envfile (see.env.examplefor an example). - Be sure to include the name of the Database you want to connect to. For example, if your connection string is
mongodb+srv://username:password@cluster.abc.mongodb.net/MyDatabase, you would changeMyDatabaseto any database name of your choosing. - Add your Vercel Blob storage token string to the
.envfile (see above, and also see.env.examplefor an example). - Run
npm installto install express and prisma. NOTE:prisma generateruns automatically as apostinstallscript (seepackage.jsonfor details).
- Can you create a system map to describe the architecture of this web app?
- Having added an image upload capability, what new use cases emerge?
Reminder that in this demo, as with out prior exploration, it's possible to iterate on the prisma schema and form elements to accomplish new use cases. A typical iteration pattern here might be as follows:
- create form elements that fit your concept, each with a given
name(index.html) - add the new element to the display template (
script.js) using its propername. - add the corresponding
nametoschema.prismawith relevant a data type and save - re-generate the Prisma Client from the schema using
npx prisma generate - re-start the app using
npm run start - test that all CRUD operations work as expected (Create, Read, Update, Delete)
- verify that data changes are reflected in MongoDB Compass.