rivet-gg / modules

🧩 Official registry of Rivet Modules.
https://rivet.gg/modules
Apache License 2.0
110 stars 5 forks source link

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

Open NathanFlurry opened 3 months ago

NathanFlurry commented 3 months ago

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.

greptile-apps[bot] commented 3 months ago

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

linear[bot] commented 3 months ago

OGBE-162 Fork node-postgres to add option to force pg-cloudflare

vr-varad commented 2 months ago

@NathanFlurry I would like to work upon this issue