File tree Expand file tree Collapse file tree 3 files changed +18
-8
lines changed Expand file tree Collapse file tree 3 files changed +18
-8
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " d1-manager " : patch
3+ ---
4+
5+ Change D1 shim fallback logic
Original file line number Diff line number Diff line change 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
104104class 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
Original file line number Diff line number Diff line change 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 ( / ^ ( _ _ D 1 _ B E T A _ _ ) ? D B _ ? / , "" ) || "default" ] = db ;
1520 }
1621 return results ;
You can’t perform that action at this time.
0 commit comments