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

[BUG]:Failed to retrieve the Cloudflare request context #2083

Closed
AdamGoodApp opened this issue Mar 29, 2024 · 1 comment
Closed

[BUG]:Failed to retrieve the Cloudflare request context #2083

AdamGoodApp opened this issue Mar 29, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@AdamGoodApp
Copy link

What version of drizzle-orm are you using?

^0.30.4

What version of drizzle-kit are you using?

^0.20.14

Describe the Bug

I'm using next.js 14 with cloudflare D1. I have everything running locally running drizzle queries fine no problem. However when building the project to push to Cloudflare pages:build, I get

Failed to retrieve the Cloudflare request context

If I use cloudflare api instead of drizzle it works. Cloudflare has same tables as local.

/api/projects/routes.ts

export async function POST(request: NextRequest) {
  const clerkUser = getAuth(request);
  const result = await getPostSchema().safeParseAsync(await request.json());

  if(!clerkUser){
    return NextResponse.json("Unauthorized", { status: 401 });
  }

  // validate the form data
  if (!result.success) {
    return NextResponse.json({ message: result.error.issues[0].message, error: result.error.issues }, {status: 400});
  }

  const { title } = result.data

  try {
    const projectId = await db.insert(projects).values({ title: title }).returning({ id: projects.id }); 
    return NextResponse.json(projectId[0], { status: 200 });
  } catch (error) {
    console.error('Error inserting project', error);
    return NextResponse.json("Error inserting project.", { status: 500 });
  }
}

drizzle.ts

import * as users from './schema/users';
import * as projects from './schema/projects';
import * as userOnProjects from './schema/userOnProjects';
import { drizzle } from 'drizzle-orm/d1';
import { getRequestContext } from '@cloudflare/next-on-pages'

const db = drizzle(getRequestContext().env.DB, { schema: { ...users, ...projects, ...userOnProjects } });

export default db;

wrangler.toml

[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "split-sonic-prod"
database_id = "d57bbc32-b607-4c39-81ff-e82d6b43cb0c"
preview_database_id = "DB"

[env.preview]
name = "preview"
[[env.preview.d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "split-sonic-preview"
database_id = "e91bfc73-92c6-4551-b2c2-576a0eb4d50b"

Expected behavior

Cloudflare D1 to work with Drizzle on Cloudflare workers.

Environment & setup

Cloudflare pages:build

@AdamGoodApp AdamGoodApp added the bug Something isn't working label Mar 29, 2024
@AdamGoodApp
Copy link
Author

Managed to solve it with this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant