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

Fork node-postgres to add option to force pg-cloudflare #410

Open
NathanFlurry opened this issue Jun 23, 2024 — with Linear · 3 comments
Open

Fork node-postgres to add option to force pg-cloudflare #410

NathanFlurry opened this issue Jun 23, 2024 — with Linear · 3 comments
Labels

Comments

Copy link
Member

Motivation

Currently, we depend on node-postgres automagically detecting the Cloudflare runtime by:

  1. Defining the user agent in esbuild: https://github.com/cloudflare/workers-sdk/blob/e8997b879605fb2eabc3e241086feb7aa219ef03/packages/wrangler/src/deployment-bundle/bundle.ts#L306
  2. Reading the defined user agent: https://github.com/brianc/node-postgres/blob/50c06f9bc6ff2ca1e8d7b7268b9af54ce49d72c1/packages/pg/lib/stream.js#L60

Implementation

We shouldn't have to force our user agent to Cloudflare. We should allow passing in a string forcing it to use pg-cloudflare.

@NathanFlurry NathanFlurry added good first issue Good for newcomers maintenance labels Jun 23, 2024 — with Linear
Copy link

greptile-apps bot commented Jun 23, 2024

To implement the feature to force pg-cloudflare, follow these steps:

  1. Modify src/runtime/deps.ts:

    • Fork the node-postgres library to add an option to force pg-cloudflare.
    • Update the import statement to use the forked version.
    export { QueryClient, Transaction } from "https://your-forked-repo-url/mod.ts";
  2. Update src/runtime/postgres.ts:

    • Add a parameter to the getOrCreatePool method to accept a flag for forcing pg-cloudflare.
    • Modify the pool creation logic to use this flag.
    public getOrCreatePool(module: Module, forceCloudflare: boolean = false): Pool | undefined {
        if (!module.db) return undefined;
        if (this.isShutDown) throw new Error("Postgres is shutting down");
    
        if (this.pools.has(module.db.name)) {
            return this.pools.get(module.db.name)!;
        } else {
            const url = getDatabaseUrl(module.db.name).toString();
    
            // Create & insert pool
            const output = module.db.createPrisma(url, { forceCloudflare });
            const pool = {
                prisma: output.prisma,
                pgPool: output.pgPool,
            } as Pool;
            this.pools.set(module.db.name, pool);
            return pool;
        }
    }
  3. Update createPrisma method in the relevant module:

    • Ensure it accepts and handles the forceCloudflare option.
    function createPrisma(url: string, options: { forceCloudflare: boolean }) {
        // Logic to force pg-cloudflare based on the options
    }

References

/src/runtime/deps.ts
/src/runtime/postgres.ts

Ask Greptile

Copy link

linear bot commented Jun 23, 2024

@vr-varad
Copy link
Contributor

@NathanFlurry I would like to work upon this issue

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

No branches or pull requests

2 participants