Skip to content

Commit cd94990

Browse files
committed
fix: d1 shim fallback logic
1 parent fe0598d commit cd94990

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

.changeset/tricky-garlics-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"d1-manager": patch
3+
---
4+
5+
Change D1 shim fallback logic

src/lib/server/d1.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
// adapted from https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/templates/d1-beta-facade.js
3-
export class D1Database {
3+
export class D1Shim {
44
binding: Fetcher;
55

66
constructor(binding: Fetcher) {
@@ -102,11 +102,11 @@ export class D1Database {
102102
}
103103

104104
class D1PreparedStatement {
105-
database: D1Database;
105+
database: D1Shim;
106106
statement: any;
107107
params: any;
108108

109-
constructor(database: D1Database, statement: any, values?: any) {
109+
constructor(database: D1Shim, statement: any, values?: any) {
110110
this.database = database;
111111
this.statement = statement;
112112
this.params = values || [];
@@ -160,7 +160,7 @@ class D1PreparedStatement {
160160
async all() {
161161
return firstIfArray(await this.database._send("/query", this.statement, this.params));
162162
}
163-
async raw() {
163+
async raw<T>(): Promise<T[]> {
164164
const s = firstIfArray(await this.database._send("/query", this.statement, this.params));
165165
const raw = [];
166166
for (const r in s.results) {
@@ -169,7 +169,7 @@ class D1PreparedStatement {
169169
});
170170
raw.push(entry);
171171
}
172-
return raw;
172+
return raw as any;
173173
}
174174
}
175175

src/lib/server/db/dbms.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { D1Database } from "../d1";
1+
import type { D1Database } from "@cloudflare/workers-types";
2+
import { D1Shim } from "../d1";
23

3-
export function DBMS(env: Record<string, Fetcher | string>): Record<string, D1Database> {
4+
export function DBMS(
5+
env: Record<string, Fetcher | D1Database | string>,
6+
): Record<string, D1Database> {
47
const keys = Object.keys(env).filter(
58
(k) => k.startsWith("DB") || k.startsWith("__D1_BETA__DB"),
69
);
10+
console.log("Database Bindings:", keys.join(", "));
11+
712
const results: Record<string, D1Database> = {};
813
for (const k of keys) {
914
const e = env[k];
1015
if (typeof e === "string") {
1116
continue;
1217
}
13-
const db = "fetch" in e ? new D1Database(e) : e;
18+
const db = "prepare" in e ? e : new D1Shim(e);
1419
results[k.replace(/^(__D1_BETA__)?DB_?/, "") || "default"] = db;
1520
}
1621
return results;

0 commit comments

Comments
 (0)