Store with product images
Product images for a Next.js store. lib/vaif.ts exports a browser client, and lib/vaif-server.ts exports createVaifServer(), a server client with the secret key. lib/storage.ts uses the server client to keep each product’s images in a product-images bucket, in a folder named after the product:
uploadProductImage()uploads a file and returns its path and URLs;listProductImages()lists a product’s images;getSignedImageUrl()returns a link that expires, one hour by default;deleteProductImage()removes one.
The secret key skips every row rule, so call them from Route Handlers and Server Actions only, after checking that the signed-in user may manage products (getCurrentUser() in lib/vaif-server.ts). Both server files import server-only, so a build that pulls them into browser code fails.
Create the app
Section titled “Create the app”vaif init --template ecommerce-apiRun it in your app's folder. It writes vaif.config.json and the template's files there. It stops if the folder already has a vaif.config.json, and leaves any other file that already exists alone; --force overwrites both.
It asks which features to include. This template has files for Database and Functions; Database is selected to start with. To skip the question, name them: vaif init --template ecommerce-api --features database,functions. Add one later with vaif init --template ecommerce-api --add-features <feature>.
What it writes
Section titled “What it writes”| File | Written |
|---|---|
lib/vaif.ts | Always |
lib/vaif-server.ts | Always |
lib/storage.ts | Always |
.env.example | Always |
README.md | Always |
drizzle/0001_initial.sql | With Database |
drizzle.config.ts | With Database |
src/db/schema.ts | With Database |
functions/hello.ts | With Functions |
Install
Section titled “Install”This template adds files to an app you already have, and writes no package.json. Install the packages its files import:
npm install @vaifstudio/client @vaifstudio/auth server-onlyConfigure
Section titled “Configure”Copy .env.example to .env and fill it in. The project ID and the keys are on Security > Credentials in the console.
| Variable | Value |
|---|---|
NEXT_PUBLIC_VAIF_API_URL | The API address. Keep the value in the file, https://api.vaif.studio. |
NEXT_PUBLIC_VAIF_PROJECT_ID | Your project ID, for the app. |
NEXT_PUBLIC_VAIF_PUBLISHABLE_KEY | The project's publishable key (vk_pub_). Row rules apply to it, so it is safe in an app. |
VAIF_SECRET_KEY | The project's secret key (vk_srv_), for server code only. It bypasses row rules; never ship it in a browser or mobile app. |
VAIF_PROJECT_ID | Your project ID again, without the prefix, for the server client. |
Create the bucket
Section titled “Create the bucket”Create a bucket named product-images on the Storage page in the console. Make it private and serve images with getSignedImageUrl(), or make it public if every image may be seen at a plain URL. See Buckets and policies.
Set up the database
Section titled “Set up the database”The template writes one SQL file for your database. The comment line above each CREATE TABLE sets that table's row rules.
drizzle/0001_initial.sql, written with the Database feature, is an example: edit it to describe your own tables before you apply it.
-- Initial migration. Customize this schema for your project; vaif db push-- applies it. The comment directly above each CREATE TABLE sets that table's-- row rules: here each signed-in user reads and changes only their own rows,-- and the owner column is filled in from their sign-in on insert. Keep a rule-- comment on every table you add: a table with no row rules is closed to-- signed-out visitors but open to every signed-in user.
-- vaif:owner idCREATE TABLE IF NOT EXISTS "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), "email" text NOT NULL UNIQUE, "name" text, "created_at" timestamp with time zone NOT NULL DEFAULT now());
-- vaif:owner user_idCREATE TABLE IF NOT EXISTS "posts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), "user_id" uuid NOT NULL REFERENCES "users"("id"), "title" text NOT NULL, "content" text, "published" boolean NOT NULL DEFAULT false, "created_at" timestamp with time zone NOT NULL DEFAULT now());users(-- vaif:owner id): each signed-in user reads and changes only their own rows, andidis filled in from their sign-in.posts(-- vaif:owner user_id): each signed-in user reads and changes only their own rows, anduser_idis filled in from their sign-in.
Put your project ID in vaif.config.json ("projectId"), sign in, and apply it. With no --dir, vaif db push finds the template's SQL folder, or a ./migrations folder, and applies its .sql files in name order.
vaif loginvaif db push --dry-run # what each file would changevaif db pushNext steps
Section titled “Next steps”- Storage overview for uploads, downloads and signed links.
- Row rules for the product and order tables you add.