#Global Database connection on Cloudflare Pages?

27 messages · Page 1 of 1 (latest)

balmy surge
#

Hi, I'm using QwikCity deployed to Cloudflare Pages, connecting to PlanetScale using the Drizzle ORM.

I've wrote this code to handle connecting to the database:

// utils/db.ts

import { drizzle } from "drizzle-orm/planetscale-serverless";
import { connect } from "@planetscale/database";
import type { Connection } from "@planetscale/database";
import * as schema from "~/../drizzle/schema";
import type { EnvGetter } from "@builder.io/qwik-city/middleware/request-handler";

/* eslint-disable no-var */
declare global {
  var __connection: Connection | undefined;
  var __drizzle: ReturnType<typeof drizzle<typeof schema>> | undefined;
}
/* eslint-enable no-var */

export const getDb = (envOrUrl: EnvGetter | string | undefined) => {
  if (!globalThis.__connection)
    globalThis.__connection = connect({
      url: envOrUrl
        ? typeof envOrUrl === "string"
          ? envOrUrl
          : envOrUrl.get("DB_URL")
        : undefined,
      fetch: (url: string, init?: RequestInit) => {
        if (init) delete init["cache"];
        return fetch(url, init);
      },
    });

  if (!globalThis.__drizzle)
    globalThis.__drizzle = drizzle(globalThis.__connection, { schema });

  return globalThis.__drizzle;
};

This means, that in every server-side function I can call getDb(env) to get the database instance.

I just have two questions:

  1. Will the connection be reinitialized every time getDb is called in the same worker?
  2. Is it possible to refactor this code, such so the env doesn't have to get passed, and I can just export a variable? I can't just use process.env, as that's not available on Cloudflare.
balmy surge
#

does anyone know anything about this?

balmy surge
copper thicket
balmy surge
#

that would mean publishing my db url wouldnt it?

#

wonder if I can use this?

#

to not have to pass the env var each time

#

even though its serverless

copper thicket
#

serverless is still running on a server. You need to make sure that your DB secrets are only in your server code and then you're good. You should add a grep in your deploy script to make sure that there's no DB connection string in the client JS

balmy surge
urban ruin
#

u just need to provide the env in the cloudflare pages side

balmy surge
#

huh

urban ruin
#

as in set the env variable in cloudflare

#

pages dashboard

#

and access the env variable the qwik way

#

althought as i can see ur not using the db inside qwik code?

balmy surge
#

im literally doing that right now

urban ruin
#
export const useSendFeedback = routeAction$(async (data, requestEvent) => {
  const FeedbackData = createInsertSchema(schema.feedbacks).parse(data);
  const DataBaseENV = requestEvent.env.get("DATABASE_URL");
  const sql = neon(DataBaseENV as string);
  const db = drizzle(sql, { schema });
  await db.insert(schema.feedbacks).values(FeedbackData);
  return {
    success: true,
  };
});
#

here's how i did it

balmy surge
#

i am doing that

#

i just wrote a util to init db