Skip to content

Commit

Permalink
fix(d1): support __env__ for accessing binding
Browse files Browse the repository at this point in the history
used by nitro and unenv
  • Loading branch information
pi0 committed Mar 8, 2024
1 parent 2ab001b commit 2ef9d57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/2.connectors/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 3 additions & 2 deletions src/connectors/cloudflare-d1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down

0 comments on commit 2ef9d57

Please sign in to comment.