
Important
With the project getting larger, you may encounter Your Worker exceeded the size limit of 1 MiB.
Actually when I start using Prisma to call D1, this happened.
So it is inevitable to Separate your project and connect them with RPC
If you just want have a glance in how Remix Prisma and Cloudflare interact and deploy on one Page, please refer to single-page branch.
You'll have to deploy Remix on Pages by specifying /remix as root
Prisma Worker should deploy separately to a worker
git clone https://github.com/GJC14/cloudflare-pages-remix-prisma-d1.git
cd cloudflare-pages-remix-prisma-d1
# In both remix and prisma-worker
npm install
binding services
YOUR_DATABASE_NAME and YOUR_DATABASE_ID, please refer to Dashboard > Workers & Pages > D1
Entrypoint in /remix/wrangler.toml
should match the export WorkerEntrypoint
in /prisma-worker/src/index
# /prisma-worker/wrangler.toml
[[ d1_databases ]]
binding = "DB"
database_name = "<YOUR_DATABASE_NAME>"
database_id = "<YOUR_DATABASE_ID>"
# /remix/wrangler.toml
[[service]]
binding = "DB"
service = "prisma-worker-test"
[[service]]
binding = "USER_SERVICE"
service = "prisma-worker"
entrypoint = "UserService"
You'll see worker-configuration.d.ts
in root level defining Env for use with @remix-run/cloudflare
# Remix
npm run typegen
# Worker (prisma-worker)
npm run cf-typegen
Change the result of npm run typegen
in worker-configuration.d.ts
:
// /remix/worker-configuration.d.ts
// From
interface Env {
DB: Fetcher;
USER_SERVICE: Fetcher;
}
// To
interface Env {
DB: Fetcher;
USER_SERVICE: Service<UserService>;
}
In this case, binding is set to "DB" and I made a schema of User and Post. This will make an empty .sql file in /migrations.
# npx wrangler d1 migrations create $DB_NAME $MESSAGE
npx wrangler d1 migrations create DB create_user_and_post_table
This will transform schema.prisma
into sql schema in the migration file you just created.
Before any migrations
# npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prisma --script --output migrations/$FILE_JUST_CREATED.sql
npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prisma --script --output migrations/0001_create_user_and_post_table.sql
After first migration
npx prisma migrate diff --from-local-d1 --to-schema-datamodel ./prisma/schema.prisma --script --output migrations/$FILE_JUST_CREATED.sql
--local
will be generated in .wrangler/state
.
npx wrangler d1 migrations apply $DB_NAME --local
Now with the Database set, you could generate your Priama Client!
npx prisma generate
export const loader: LoaderFunction = async ({ context, params }) => {
let env = context.cloudflare.env;
const adapter = new PrismaD1(env["YOUR_DB_NAME"]);
const prisma = new PrismaClient({ adapter });
let { results } = await prisma.user.findMany();
return json(results);
};
# /prisma-worker
npm run start
# /remix
npm run dev
Create D1 either by CLI or dashboard, name is the same as defined in /prisma-worker/wrangler.toml
.
% npx wrangler d1 create prisma-worker-test
⛅️ wrangler 3.75.0
-------------------
✅ Successfully created DB 'prisma-worker-test' in region ENAM
Created your new D1 database.
[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "prisma-worker-test"
database_id = "732f77c8-054c-4da0-a810-01544c6f1066"
Replace the new id in /prisma-worker/wrangler.toml
. Then run in prisma-worker
:
# npx wrangler d1 migrations apply $DB_NAME --remote
% npx wrangler d1 migrations apply DB --remote
⛅️ wrangler 3.75.0
-------------------
Migrations to be applied:
┌─────────────────────────────────────┐
│ name │
├─────────────────────────────────────┤
│ 0001_create_user_and_post_table.sql │
└─────────────────────────────────────┘
✔ About to apply 1 migration(s)
Your database may not be available to serve requests during the migration, continue? … yes
🌀 Executing on remote database DB (732f77c8-054c-4da0-a810-01544c6f1066):
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
🚣 Executed 4 commands in 0.5319ms
┌─────────────────────────────────────┬────────┐
│ name │ status │
├─────────────────────────────────────┼────────┤
│ 0001_create_user_and_post_table.sql │ ✅ │
└─────────────────────────────────────┴────────┘
You should create a D1 (step 7.) before deploy.
# /prisma-worker
npm run deploy
Use dashboard connect git to CI/CD.
Set binding with Prisma Worker in Dashboard > Workers & Pages > cloudflare-pages-remix-prisma-d1 (or your Page deployment) > Settings > Functinos > Service bindings

- Cloudflare Example: Access your D1 database in a Remix application
- Query D1 from Remix
- Query D1 using Prisma ORM
- Prisma Cloudflare D1
- Prisma Cloudflare D1 Deploy
Starts from official Remix Cloudflare template:
npx create-remix@latest --template remix-run/remix/templates/cloudflare
- Added wrangler.toml
npm run typegen
, you will see the change in worker-configuration.d.ts- Change the definition of
interface Env
- Removes the
interface ENV {}
in load-context.ts by template
npm create cloudflare@latest
with Hello World example/Hello World Worker/TypeScriptnpm install prisma --save-dev @prisma/adapter-d1
npm install @prisma/adapter-d1
npx prisma init
npm run cf-typegen
- Migration
Run the dev server:
npm run dev
To run Wrangler:
npm run build
npm run start
Generate types for your Cloudflare bindings in wrangler.toml
:
npm run typegen
You will need to rerun typegen whenever you make changes to wrangler.toml
.
First, build your app for production:
npm run build
Then, deploy your app to Cloudflare Pages:
npm run deploy
This template comes with Tailwind CSS already configured for a simple default starting experience. You can use whatever css framework you prefer. See the Vite docs on css for more information.