james-elicx / cf-bindings-proxy

Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages
MIT License
94 stars 4 forks source link

Migrations and proxy uses different d1 dbs #43

Closed gerhardcit closed 1 year ago

gerhardcit commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

@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: https://github.com/james-elicx/cf-bindings-proxy/blob/963e08352f0077e8c52f85647b6ddf92c0d45da0/src/index.ts#L13-L15

james-elicx commented 1 year ago

Please could you try using the prerelease in https://github.com/james-elicx/cf-bindings-proxy/pull/44#issuecomment-1788701836 and see if that fixes this problem?

gerhardcit commented 1 year ago

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

james-elicx commented 1 year ago

Awesome, thank you :D