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

Migrations and proxy uses different d1 dbs #43

Closed
gerhardcit opened this issue Oct 31, 2023 · 7 comments
Closed

Migrations and proxy uses different d1 dbs #43

gerhardcit opened this issue Oct 31, 2023 · 7 comments

Comments

@gerhardcit
Copy link

npx wrangler d1 migrations apply DB --local creates a local db and apply migrations.

npx cf-bindings-proxy --d1=DB seems to use another db.

how do I get to persist and reference the same db locally?

@james-elicx
Copy link
Owner

I think d1 is a bit iffy about which db to reference locally. In my own project when I was trying to do this, I used the following:

https://github.com/james-elicx/cloudy/blob/main/package.json#L21-L24
https://github.com/james-elicx/cloudy/blob/main/wrangler.dev.toml

@gerhardcit
Copy link
Author

Thanks, quick initial testing is promising. I'm using Drizzle and Sveltekit, which has been the problematic till now to develop locally. (I've been trying various things.. this is the first one to actually work locally)
(Still need to deploy this version to cloudflare)

import { memberApps } from '$lib/schemas/memberApps';
import { binding } from 'cf-bindings-proxy';

export const GET: RequestHandler = (async ({ platform }) => {
....
    const DBBinding = await binding<D1Database>('DB', { fallback: platform?.env! })
    const db = drizzle(DBBinding);

    // use drizzle command formats. 
    const list = await db.select().from(memberApps).all();
    return json(list);
}

developing with two windows:
pnpm proxy
pnpm dev

package.json

"scripts": {
		"dev": "vite dev",
                "proxy": "pnpm exec cf-bindings-proxy --d1=DB --local",
		"proxy:d1:execute": "pnpm exec wrangler d1 execute DB --local --config=wrangler.dev.toml --command",
		"proxy:migrations:list": "pnpm exec wrangler d1 migrations list DB --local --config=wrangler.dev.toml",
		"proxy:migrations:apply": "pnpm exec wrangler d1 migrations apply DB --local --config=wrangler.dev.toml"

@gerhardcit
Copy link
Author

@james-elicx , here is something you might understand. Errors when running on cloudlfare.
If I use this:
const DBBinding = await binding<D1Database>('DB', { fallback: platform?.env! })
I get an error caught in the logs:

{
      "message": [
        "ReferenceError: process is not defined"
      ],
      "level": "error",
      "timestamp": 1698827791203
    }

Somehow your fallback is looking for process that might not exists?
So I went with this:
const DBBinding = await platform?.env?.DB || binding<D1Database>('DB')
and that works both in dev as well as in deployed cloudflare.

below is full function.

export const GET: RequestHandler = async ({ platform }) => {
    try {
        // const DBBinding = await binding<D1Database>('DB', { fallback: platform?.env! })
        const DBBinding = await platform?.env?.DB || binding<D1Database>('DB')
        const db = drizzle(DBBinding);
        const list = await db.select().from(memberApps).all();
        return json(list);
    }
    catch (err) {
        return json({
            error: err.message || err
        }, {
            status: 500,
            statusText: "Internal Server Error"
        });
    }
};

@james-elicx
Copy link
Owner

@james-elicx , here is something you might understand. Errors when running on cloudlfare. If I use this: const DBBinding = await binding<D1Database>('DB', { fallback: platform?.env! }) I get an error caught in the logs:

{
      "message": [
        "ReferenceError: process is not defined"
      ],
      "level": "error",
      "timestamp": 1698827791203
    }

Somehow your fallback is looking for process that might not exists? So I went with this: const DBBinding = await platform?.env?.DB || binding<D1Database>('DB') and that works both in dev as well as in deployed cloudflare.

Hmm, I think this function might need a check for process being undefined, as I would assume this is what's causing that exception:

export const isProxyEnabled = () =>
process?.env?.ENABLE_BINDINGS_PROXY ||
(!process?.env?.DISABLE_BINDINGS_PROXY && process?.env?.NODE_ENV === 'development');

@james-elicx
Copy link
Owner

Please could you try using the prerelease in #44 (comment) and see if that fixes this problem?

@gerhardcit
Copy link
Author

Perfect. Tested, confirmed, it works locally and running on Cloudflare.

@james-elicx
Copy link
Owner

Awesome, thank you :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants