Yes in the context! Something like this middleware is recommended ```ts // Middleware to inject the

Yes in the context! Something like this middleware is recommended
// Middleware to inject the database client into the context
export const database = async (c: Context, next: Next) => {
  if (c.env.ENV === "test") {
    const database = new MockDatabaseClient();
    c.set("database", database);
    await next();
  } else {
    const sql = postgres(c.env.HYPERDRIVE.connectionString);
    const database = new KVDatabaseClient(sql);
    c.set("database", database); // set the database client to the context
    await next();
    // clean up the client ensuring we don't kill the worker before that is completed
    c.executionCtx.waitUntil(sql.end());
  }
};

Note that I'm wrapping the sql connection in another class for mocking purposes
Was this page helpful?