# Astro (/docs/guides/frameworks/astro) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Set up Prisma ORM in an Astro app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute. Location: Guides > Frameworks > Astro ## Introduction [#introduction] This guide shows you how to use Prisma ORM in an Astro site. You scaffold a project that queries users during server rendering and exposes a JSON API route, initialize the schema, see your data render, and deploy the site to [Prisma Compute](https://www.prisma.io/docs/compute). Every command below was run end to end against a live [Prisma Postgres](https://www.prisma.io/docs/postgres) database. ## Quick start [#quick-start] One command scaffolds the project with Prisma ORM wired in: #### npm ```bash npm create prisma@latest -- my-astro-app --template astro --provider postgres --no-deploy ``` #### pnpm ```bash pnpm create prisma@latest my-astro-app --template astro --provider postgres --no-deploy ``` #### yarn ```bash yarn create prisma@latest my-astro-app --template astro --provider postgres --no-deploy ``` #### bun ```bash bun create prisma@latest my-astro-app --template astro --provider postgres --no-deploy ``` Answer the prompts, then export `DATABASE_URL` in your shell before running the database scripts. If you do not have a database yet, `npx create-db@latest` prints a Prisma Postgres connection string. ## Prerequisites [#prerequisites] - [Node.js](https://nodejs.org) 22.18 or newer on the 22 line, 24.11 or newer on the 24 line, or 26 or newer (24 recommended) - A PostgreSQL connection string, or nothing at all: `npx create-db@latest` can create a Prisma Postgres database for you To use MongoDB instead of PostgreSQL, scaffold with `--provider mongo` and follow [Create a new app with MongoDB](https://www.prisma.io/docs/prisma-orm/quickstart/mongodb) for the database steps. Neither `npm run dev:composer` (the local run in step 3) nor a deploy creates a MongoDB database for you, so export `MONGODB_URL` with your connection string before you run either. The first deploy copies the value to your project on Prisma Compute, and the deployed app reads it from there. Later deploys keep that stored value even if you export a different one. To change it, export the new connection string as `MONGODB_URL` and run `npx prisma project env update MONGODB_URL --project my-astro-app --role production`, which copies the exported value to the project (see [`prisma project`](https://www.prisma.io/docs/cli/project)); the app gets the new value on its next deploy. If you renamed the module or deployed with `--name`, pass that name to `--project`. ## 1. Scaffold and enter the project [#1-scaffold-and-enter-the-project] #### npm ```bash npm create prisma@latest -- my-astro-app --template astro --provider postgres --no-deploy cd my-astro-app ``` #### pnpm ```bash pnpm create prisma@latest my-astro-app --template astro --provider postgres --no-deploy cd my-astro-app ``` #### yarn ```bash yarn create prisma@latest my-astro-app --template astro --provider postgres --no-deploy cd my-astro-app ``` #### bun ```bash bun create prisma@latest my-astro-app --template astro --provider postgres --no-deploy cd my-astro-app ``` The scaffold generates the Astro app with Prisma ORM wired in, installs dependencies, and emits the contract your queries are type-checked against. Next, set the database connection for the local steps. Use your own PostgreSQL connection string, or create a Prisma Postgres database with `npx create-db@latest`, which prints a connection string and a claim URL. Claim the database with a free Prisma account if you want to keep it. Export the variable in the shell you work in; the generated scripts read the environment variable, not `.env`: ```bash export DATABASE_URL="" ``` ## 2. Initialize the database [#2-initialize-the-database] #### npm ```bash npm run db:init ``` #### pnpm ```bash pnpm run db:init ``` #### yarn ```bash yarn db:init ``` #### bun ```bash bun run db:init ``` ```text no-copy ✔ Applied 5 operation(s) across 1 contract space App space ├─ Create table "Post" ├─ Create table "User" ├─ Add unique constraint on "User" (email) ├─ Create index "Post_authorId_idx_e47547ed" on "Post" ├─ Add foreign key "Post_authorId_fkey" on "Post" └─ marker 920a87b50729a867e21706dd0fa5c3739e273a84ed89eb2f3525633b2d6ef85c ``` If `db:init` stops with `Connection terminated unexpectedly`, a database you just created is still starting; wait a few seconds and run it again. The command is safe to repeat: when there is nothing left to do, it prints `✔ Database already matches contract across 1 contract space`. `db:init` applies your schema (`src/prisma/contract.prisma`) to the database and signs it. With TypeScript authoring (`--authoring typescript`) the schema is `src/prisma/contract.ts` and the emitted files land in `src/prisma/generated/`. Sample users are seeded automatically the first time the app queries the database. ## 3. Run and verify [#3-run-and-verify] #### npm ```bash npm run dev ``` #### pnpm ```bash pnpm run dev ``` #### yarn ```bash yarn dev ``` #### bun ```bash bun run dev ``` When you open [http://localhost:4321](http://localhost:4321), the page lists the seeded users, and `GET /api/users` returns them as JSON. To run the app the way Prisma Compute runs it, with no `DATABASE_URL` to set, run `npm run dev:composer` instead. It builds the app, starts a local Prisma Postgres database, applies the committed migrations to it, and prints the app's local URL. ## Where things live [#where-things-live] - `src/pages/index.astro`: queries users in the frontmatter and renders them - `src/pages/api/users.ts`: a JSON API route backed by the same query - `src/prisma/users.ts`: the `listUsers` query helper both import - `src/prisma/db.ts`: the Prisma ORM client behind that helper On PostgreSQL, you reach a model through the database schema it is in: `db.orm.public.User` is the `User` model in the `public` schema. The [Prisma ORM overview](https://www.prisma.io/docs/orm) explains the contract that defines your models. ## 4. Deploy to Prisma Compute [#4-deploy-to-prisma-compute] Astro is supported on [Prisma Compute](https://www.prisma.io/docs/compute), which runs the site as a Node server. The scaffold configures that already: `astro.config.mjs` sets `output: "server"` with `adapter: node({ mode: "standalone" })`, and `module.ts` with `service.ts` declare the app for [Prisma Composer](https://www.prisma.io/docs/composer), pointing at the built `dist/server/entry.mjs`. Keep those settings: `output: "server"` renders the page and the API route per request, so the live site reads current data instead of freezing the build-time snapshot into static files, and without the standalone adapter there is no server entrypoint to deploy. Sign in once (it opens a browser): #### npm ```bash npx prisma auth login ``` #### pnpm ```bash pnpm prisma auth login ``` #### yarn ```bash yarn prisma auth login ``` #### bun ```bash bunx prisma auth login ``` Then build and deploy from the project directory: #### npm ```bash npm run build npx prisma deploy ``` #### pnpm ```bash pnpm run build pnpm prisma deploy ``` #### yarn ```bash yarn build yarn prisma deploy ``` #### bun ```bash bun run build bunx prisma deploy ``` ```text no-copy my-astro-app ├─ database postgres-database db_abc123 └─ app compute-service cps_abc123 https://xyz.ewr.prisma.build ``` The first deploy creates a project in your workspace named after your module, which is the first argument to `module()` in `module.ts` (`my-astro-app` here). The project gets its own Prisma Postgres database, declared in `module.ts`. The deploy does not use the `DATABASE_URL` from your local steps, and the deployed database seeds on the app's first query. Running `npx prisma deploy` again updates that same project. The deploy finds the project by that name, so a deploy from any app whose module has the same name, such as two apps both scaffolded as `my-app`, goes to that same project. Give each app its own name in `module.ts`. If a project with that name exists but the CLI cannot identify or verify its stored state (one deployed by an older version of Composer, for example), the deploy stops with `HostedStateBootstrapError`: rename the module in `module.ts`, or deploy under another name with `--name ` and pass the same `--name` on every later deploy. Verify the live API route returns the seeded users: ```bash curl https://xyz.ewr.prisma.build/api/users ``` The live URL's root page renders the same users server-side. For previews per Git branch and deploy on push, see [Deploy on push](https://www.prisma.io/docs/compute/deploy-on-push). ## Change the schema [#change-the-schema] The scaffold also writes a first migration to the `migrations/` directory. A deploy changes the hosted database only by applying the migrations in that directory, so every schema change needs a migration of its own. `migration:plan` writes each new migration as a step after the migration that `migrations/app/refs/db.json` names. That file is the `db` ref, and `db:init` creates it. If you have not run `db:init` in this checkout, for example because you only use `npm run dev:composer`, point the `db` ref at the first migration once. Pass the directory name only, without `migrations/app/` in front: `ls migrations/app` lists it, and its name ends in `_init`, such as `20261120T1011_init`. A path such as `migrations/app/20261120T1011_init` fails with `MIGRATION.REF_NOT_FOUND`. #### npm ```bash npx prisma migration ref set db ``` #### pnpm ```bash pnpm prisma migration ref set db ``` #### yarn ```bash yarn prisma migration ref set db ``` #### bun ```bash bunx prisma migration ref set db ``` After you edit `src/prisma/contract.prisma`, for example to add a `bio String?` field to `User`, run: #### npm ```bash npm run contract:emit npm run migration:plan -- --name add-user-bio ``` #### pnpm ```bash pnpm run contract:emit pnpm run migration:plan --name add-user-bio ``` #### yarn ```bash yarn contract:emit yarn migration:plan --name add-user-bio ``` #### bun ```bash bun run contract:emit bun run migration:plan --name add-user-bio ``` `contract:emit` updates the files your queries are type-checked against, and `migration:plan` writes the change as a new migration in `migrations/app/`. Then apply the migration to your local database, and point the `db` ref at it so that your next `migration:plan` starts from this migration. Without the ref step, the next plan would include this change a second time. If your development database is in `DATABASE_URL`, one command does both: #### npm ```bash npm run migrate -- --advance-ref db ``` #### pnpm ```bash pnpm run migrate --advance-ref db ``` #### yarn ```bash yarn migrate --advance-ref db ``` #### bun ```bash bun run migrate --advance-ref db ``` If you use `npm run dev:composer`, restart it: it applies new migrations when it starts. It does not update the `db` ref, so once it is running, set the ref yourself. Pass the last part of the path that `migration:plan` printed on its `app space:` line: for `migrations/app/20261120T1124_add_user_bio`, that is `npx prisma migration ref set db 20261120T1124_add_user_bio`. #### npm ```bash npx prisma migration ref set db ``` #### pnpm ```bash pnpm prisma migration ref set db ``` #### yarn ```bash yarn prisma migration ref set db ``` #### bun ```bash bunx prisma migration ref set db ``` Astro builds on Vite, so the [Vite plugin](https://www.prisma.io/docs/cli/contract-emit#run-it-automatically) can do the `contract:emit` step for you whenever the contract changes while the dev server runs: import `prismaVitePlugin` from `@prisma/orm-postgres/vite-plugin-contract-emit` and add `vite: { plugins: [prismaVitePlugin()] }` to `astro.config.mjs`. Commit the `migrations/` directory with your change, including `migrations/app/refs/db.json`, then deploy again with `npm run build` and `npx prisma deploy`. The deploy applies the new migration to the hosted database before it starts the new version of your app. If you skip `migration:plan`, the deploy stops with `MIGRATION_PATH_NOT_FOUND`, because no migration in `migrations/` leads to your new schema. If you changed your database with `db:update`, which makes the database match the schema without writing a migration, `migration:plan` stops with `MIGRATION.HASH_NOT_IN_GRAPH`. Pass the newest migration with `--from`: it is the first migration name in the graph that `npx prisma migration list` prints, such as `20261120T1124_add_user_bio`. Then apply the migration as above: #### npm ```bash npm run migration:plan -- --from --name add-user-bio ``` #### pnpm ```bash pnpm run migration:plan --from --name add-user-bio ``` #### yarn ```bash yarn migration:plan --from --name add-user-bio ``` #### bun ```bash bun run migration:plan --from --name add-user-bio ``` ## Use with your agent [#use-with-your-agent] To delegate this guide to your coding agent, copy the prompt below and hand it over: ```text Create a new Astro app with Prisma ORM, seed it, and deploy it to Prisma Compute. 1. Scaffold: `npm create prisma@latest -- my-astro-app --template astro --provider postgres --yes`. The scaffold installs the Prisma agent skills. Get a database connection string: use the one I give you, or create a Prisma Postgres database with `npx create-db@latest` and show me the claim URL it prints. Export it as `DATABASE_URL` in the shell; the generated scripts read the environment variable, not `.env`. 2. In `my-astro-app`, run `npm run db:init` with `DATABASE_URL` exported. The app seeds sample users on its first query, from `src/prisma/seed.ts`; there is no seed command to run. 3. Start `npm run dev` in the background, wait until it reports ready, verify http://localhost:4321 lists the seeded users and `curl http://localhost:4321/api/users` returns them as JSON, then stop the dev server. 4. Confirm `astro.config.mjs` still sets `output: "server"` and `adapter: node({ mode: "standalone" })` (the template sets both). 5. Deploy: check `npx prisma auth whoami`; if I am not signed in, stop and ask me to run `npx prisma auth login`. Then run `npm run build` followed by `npx prisma deploy` and verify the live URL's /api/users endpoint with curl. The deployed app provisions and seeds its own Prisma Postgres database; do not pass the local DATABASE_URL. If the deploy fails with `HostedStateBootstrapError`, a project with the module's name exists in my workspace but its hosted state cannot be verified; re-run the deploy with `--name `. Use the installed Prisma ORM skills. ``` ## Next steps [#next-steps] - [Learn the fundamentals](https://www.prisma.io/docs/orm/fundamentals/reading-data): filtering, sorting, pagination, and writes. - [Read the Prisma ORM overview](https://www.prisma.io/docs/orm) for the concepts behind contracts and typed queries. ## Related pages - [Elysia](https://www.prisma.io/docs/guides/frameworks/elysia): Build an Elysia API on Prisma ORM with the elysia template and deploy it to Prisma Compute. - [Hono](https://www.prisma.io/docs/guides/frameworks/hono): Build a Hono API on Prisma ORM with the hono template, add your own routes, and deploy it to Prisma Compute. - [NestJS](https://www.prisma.io/docs/guides/frameworks/nestjs): Set up Prisma ORM in a NestJS app with create-prisma, from scaffold to seeded API to a live deploy on Prisma Compute. - [Next.js](https://www.prisma.io/docs/guides/frameworks/nextjs): Set up Prisma ORM in a Next.js app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute. - [Nuxt](https://www.prisma.io/docs/guides/frameworks/nuxt): Set up Prisma ORM in a Nuxt app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute.