From 2ef9d5788fc382ef717991729d1da3c3903598ed Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 8 Mar 2024 16:16:37 +0100 Subject: [PATCH] fix(d1): support `__env__` for accessing binding used by nitro and unenv --- docs/2.connectors/cloudflare.md | 5 +++++ src/connectors/cloudflare-d1.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/2.connectors/cloudflare.md b/docs/2.connectors/cloudflare.md index 3373dba..bbcb7c3 100644 --- a/docs/2.connectors/cloudflare.md +++ b/docs/2.connectors/cloudflare.md @@ -26,6 +26,11 @@ const db = createDatabase( ); ``` +> [!NOTE] +> In order for the driver to work, `globalThis.__env__.DB` value should be set. +> +> If you are using [Nitro](https://nitro.unjs.io/) you don't need to do any extra steps. + ## Options ### `bindingName` diff --git a/src/connectors/cloudflare-d1.ts b/src/connectors/cloudflare-d1.ts index faffc49..55a3dc1 100644 --- a/src/connectors/cloudflare-d1.ts +++ b/src/connectors/cloudflare-d1.ts @@ -4,9 +4,10 @@ export interface ConnectorOptions { bindingName?: string; } -export default function sqliteConnector(options: ConnectorOptions) { +export default function cloudflareD1Connector(options: ConnectorOptions) { const getDB = () => { - const binding = globalThis.__cf_env__?.[options.bindingName]; + // TODO: Remove legacy __cf_env__ support in next major version + const binding = globalThis.__env__?.[options.bindingName] || globalThis.__cf_env__?.[options.bindingName]; if (!binding) { throw new Error(`[db0] [d1] binding \`${options.bindingName}\` not found`); }