Database |> Postgres |> 

Hyperdrive

LSD is Postgres compatible making it easy to connect to an application being developed on Cloudflare. If you’d like to use Hyperdrive to reduce the latency between your application and the web, here’s how you can do that.

Note: If you don’t have an application already set up on Cloudflare, here’s a tutorial you can follow.

Contents

  1. Configuring with wrangler
  2. Configuring with dash
  3. Workers on LSD

Configuring with wrangler

Adding LSD to your Cloudflare application is simple with the hyperdrive command.

npx wrangler hyperdrive create lsd-hyperdrive \
    --connection-string="<connection_string>"

For the <connection_string> you can either provide your credentials or connect to our public instance to try out LSD first.

npx wrangler hyperdrive create lsd-hyperdrive \
    --connection-string="postgresql://cloudflare:hyperdrive@lsd.so:5432?application_name=hyperdrive"

Note: The [application_name] is necessary to inform LSD to proceed with AuthenticationMD5Password

Configuring with dash

From dash click on the blue Create configuration button.

Cloudflare Hyperdrive dashboard

When entering the Connection String you can either use the credentials for your account or, if you’d like to prototype first on our public instance then feel free to use the following:

postgresql://cloudflare:hyperdrive@lsd.so:5432?application_name=hyperdrive

Note: The [application_name] query parameter is necessary to inform LSD to proceed with AuthenticationMD5Password

Cloudflare Hyperdrive configuration

Click on the blue Create button to create your Hyperdrive and read on to see an example of how to use LSD on Cloudflare.

Workers on LSD

Following Cloudflare’s recommendation, here’s how it looks to have your Workers on LSD using postgres.js.

First, if you haven’t already, install the Postgres.js driver.

$ npm install postgres

In your wrangler.toml, add in the following configuration using the ID of the Hyperdrive configuration you just created.

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<hyperdrive_id>"

Now you can use LSD with the following lines

// Importing the driver
import postgres from "postgres";

// Connecting to the Hyperdrive instance
const sql = postgres(env.HYPERDRIVE.connectionString);

// Querying the web
const result = await sql`<lsd_query>`;

Or, if you’d like complete source code for a Worker that obtains the front page of Hacker News (based on Cloudflare’s example).

import postgres from "postgres";

export interface Env {
  // If you set another name in wrangler.toml as the value for 'binding',
  // replace "HYPERDRIVE" with the variable name you defined.
  HYPERDRIVE: Hyperdrive;
}

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    // NOTE: if `prepare: false` is passed when connecting, performance will
    // be slower but still correctly supported.
    const sql = postgres(env.HYPERDRIVE.connectionString);

    try {
      const result = await sql`
	  FROM https://news.ycombinator.com
	  |> GROUP BY span.titleline
	  |> SELECT a AS post, a@href AS link
	  `;

      // Clean up the client, ensuring we don't kill the worker before that is
      // completed.
      ctx.waitUntil(sql.end());

      // Return result rows as JSON
      return Response.json({ result: result });
    } catch (e) {
      console.log(e);
      return Response.json({ error: e.message }, { status: 500 });
    }
  },
} satisfies ExportedHandler<Env>;