Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Hyperdrive(Postgres と MySQL)

最終更新 Markdown で表示Agent セットアップ

既存のリージョンデータベースを、グローバル分散データベースとして使えます。

Free および Paid プランで利用できます

Hyperdrive は、既存データベースへのクエリを高速化するサービスです。Cloudflare Workers から、ユーザーの所在地に関係なく、世界中からデータへより速くアクセスできます。

Hyperdrive は、AWS、Google Cloud、Azure、Neon、PlanetScale 上のものを含め、あらゆる Postgres または MySQL データベースに対応しています。CockroachDB や Timescale など、Postgres 互換データベースにも対応しています。 新しいコードを書いたり、使い慣れたツールを置き換えたりする必要はありません。Hyperdrive は既存のコードと、普段使っているツールで動作します。

Cloudflare Workers アプリケーションでは、既存のデータベースドライバーとオブジェクトリレーショナルマッピング(ORM)ライブラリに、Hyperdrive の接続情報を使います。

PostgreSQL

import { Client } from "pg";

export default {
	async fetch(request, env, ctx): Promise<Response> {
		// Create a new client instance for each request. Hyperdrive maintains the
		// underlying database connection pool, so creating a new client is fast.
		const client = new Client({
			connectionString: env.HYPERDRIVE.connectionString,
		});

		try {
			// Connect to the database
			await client.connect();
			// Sample SQL query
			const result = await client.query("SELECT * FROM pg_tables");

			return Response.json(result.rows);
		} catch (e) {
			return Response.json({ error: e instanceof Error ? e.message : e }, { status: 500 });
		}
	},
} satisfies ExportedHandler<{ HYPERDRIVE: Hyperdrive }>;
	{
		"$schema": "node_modules/wrangler/config-schema.json",
		"name": "WORKER-NAME",
		"main": "src/index.ts",
		"compatibility_date": "2025-02-04",
		"compatibility_flags": [
			"nodejs_compat"
		],
		"observability": {
			"enabled": true
		},
		"hyperdrive": [
			{
				"binding": "HYPERDRIVE",
				"id": "<YOUR_HYPERDRIVE_ID>",
				"localConnectionString": "<ENTER_LOCAL_CONNECTION_STRING_FOR_LOCAL_DEVELOPMENT_HERE>"
			}
		]
	}

MySQL

import { createConnection } from 'mysql2/promise';

export default {
  async fetch(request, env, ctx): Promise<Response> {
    // Create a new connection on each request. Hyperdrive maintains the
    // underlying database connection pool, so creating a new client is fast.
    const connection = await createConnection({
		 host: env.HYPERDRIVE.host,
		 user: env.HYPERDRIVE.user,
		 password: env.HYPERDRIVE.password,
		 database: env.HYPERDRIVE.database,
		 port: env.HYPERDRIVE.port,

     // This is needed to use mysql2 with Workers
     // This configures mysql2 to use static parsing instead of eval() parsing (not available on Workers)
     disableEval: true
  });

  const [results, fields] = await connection.query('SHOW tables;');

  return new Response(JSON.stringify({ results, fields }), {
    headers: {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '\*',
    },
  });
}} satisfies ExportedHandler<{ HYPERDRIVE: Hyperdrive }>;
	{
		"$schema": "node_modules/wrangler/config-schema.json",
		"name": "WORKER-NAME",
		"main": "src/index.ts",
		"compatibility_date": "2025-02-04",
		"compatibility_flags": [
			"nodejs_compat"
		],
		"observability": {
			"enabled": true
		},
		"hyperdrive": [
			{
				"binding": "HYPERDRIVE",
				"id": "<YOUR_HYPERDRIVE_ID>",
				"localConnectionString": "<ENTER_LOCAL_CONNECTION_STRING_FOR_LOCAL_DEVELOPMENT_HERE>"
			}
		]
	}
はじめる

機能

データベースを接続する

Hyperdrive を既存のデータベースに接続し、クエリを実行する Worker をデプロイします。

PostgreSQL 対応

Hyperdrive では、任意の PostgreSQL または PostgreSQL 互換データベースに接続できます。

MySQL 対応

Hyperdrive では、任意の MySQL データベースに接続できます。

クエリキャッシュ

データベースに対して実行される、よく使われるクエリをデフォルトでキャッシュします。


関連製品

Workers

サーバーレスアプリケーションを構築し、世界中に即座にデプロイできます。パフォーマンス、信頼性、スケールに優れています。

Pages

動的なフロントエンドアプリケーションを短時間でデプロイします。


その他のリソース

料金

Hyperdrive の料金を確認します。

制限

Hyperdrive の制限を確認します。

Developer Discord

Discord の Workers コミュニティに参加し、質問したり、作っているものを紹介したり、他の開発者とプラットフォームについて話し合えます。

@CloudflareDev

@CloudflareDev を Twitter でフォローし、製品発表と Cloudflare Developer Platform の最新情報を確認できます。

役に立ちましたか?