VelaVela
Cloudflare

Hyperdrive

Use Cloudflare Hyperdrive for accelerated database connections in your Vela application.

The HyperdriveModule provides a managed HyperdriveService that exposes accelerated connection details for your database through Cloudflare Hyperdrive.

Setup

Import the module and configure the binding name:

import { Module } from '@velajs/vela';
import { HyperdriveModule } from '@velajs/cloudflare';

@Module({
  imports: [HyperdriveModule.forRoot({ binding: 'MY_HYPERDRIVE' })],
})
class AppModule {}

Add the Hyperdrive binding to wrangler.toml:

[[hyperdrive]]
binding = "MY_HYPERDRIVE"
id = "xxx"

Using HyperdriveService

Inject HyperdriveService to access the accelerated connection parameters:

import { Controller, Get } from '@velajs/vela';
import { HyperdriveService } from '@velajs/cloudflare';
import { Client } from 'pg';

@Controller('db')
class DbController {
  constructor(private readonly hyperdrive: HyperdriveService) {}

  @Get('status')
  async status() {
    const client = new Client({
      connectionString: this.hyperdrive.connectionString,
    });
    await client.connect();
    const result = await client.query('SELECT NOW()');
    await client.end();
    return { time: result.rows[0].now };
  }
}

Properties

PropertyDescription
connectionStringFull connection string for the accelerated database connection.
hostHyperdrive proxy hostname.
portHyperdrive proxy port.
userDatabase username.
passwordDatabase password.
databaseDatabase name.
Edit on GitHub

Last updated on

On this page