@@ -5,15 +5,26 @@ sidebar:
55 order : 1
66---
77
8- import { Type , MetaInfo , Details } from " ~/components" ;
8+ import { Type , MetaInfo , Details , Tabs , TabItem } from " ~/components" ;
99
1010To interact with your D1 database from your Worker, you need to access it through the environment bindings provided to the Worker (` env ` ).
1111
12+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
1213``` js
1314async fetch (request , env ) {
1415 // D1 database is 'env.DB', where "DB" is the binding name from the Wrangler configuration file.
1516}
1617```
18+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
19+ ``` py
20+ from workers import WorkerEntrypoint
21+
22+ class Default (WorkerEntrypoint ):
23+ async def fetch (self , request ):
24+ # D1 database is 'self.env.DB', where "DB" is the binding name from the Wrangler configuration file.
25+ pass
26+ ```
27+ </TabItem > </Tabs >
1728
1829A D1 binding has the type ` D1Database ` , and supports a number of methods, as listed below.
1930
@@ -23,10 +34,17 @@ A D1 binding has the type `D1Database`, and supports a number of methods, as lis
2334
2435Prepares a query statement to be later executed.
2536
37+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
2638``` js
2739const someVariable = ` Bs Beverages` ;
2840const stmt = env .DB .prepare (" SELECT * FROM Customers WHERE CompanyName = ?" ).bind (someVariable);
2941```
42+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
43+ ``` py
44+ some_variable = " Bs Beverages"
45+ stmt = self .env.DB .prepare(" SELECT * FROM Customers WHERE CompanyName = ?" ).bind(some_variable)
46+ ```
47+ </TabItem > </Tabs >
3048
3149#### Parameters
3250
@@ -44,18 +62,30 @@ You can use the `bind` method to dynamically bind a value into the query statem
4462
4563- Example of a static statement without using ` bind ` :
4664
65+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
4766 ``` js
4867 const stmt = db
4968 .prepare (" SELECT * FROM Customers WHERE CompanyName = Alfreds Futterkiste AND CustomerId = 1" )
5069 ```
70+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
71+ ``` py
72+ stmt = db.prepare(" SELECT * FROM Customers WHERE CompanyName = Alfreds Futterkiste AND CustomerId = 1" )
73+ ```
74+ </TabItem > </Tabs >
5175
5276- Example of an ordered statement using ` bind ` :
5377
78+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
5479 ``` js
5580 const stmt = db
5681 .prepare (" SELECT * FROM Customers WHERE CompanyName = ? AND CustomerId = ?" )
5782 .bind (" Alfreds Futterkiste" , 1 );
5883 ```
84+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
85+ ``` py
86+ stmt = db.prepare(" SELECT * FROM Customers WHERE CompanyName = ? AND CustomerId = ?" ).bind(" Alfreds Futterkiste" , 1 )
87+ ```
88+ </TabItem > </Tabs >
5989
6090Refer to the [ ` bind ` method documentation] ( /d1/worker-api/prepared-statements/#bind ) for more information.
6191
@@ -67,6 +97,7 @@ Batched statements are [SQL transactions](https://www.sqlite.org/lang_transactio
6797
6898To send batch statements, provide ` D1Database::batch ` a list of prepared statements and get the results in the same order.
6999
100+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
70101``` js
71102const companyName1 = ` Bs Beverages` ;
72103const companyName2 = ` Around the Horn` ;
@@ -76,6 +107,19 @@ const batchResult = await env.DB.batch([
76107 stmt .bind (companyName2)
77108]);
78109```
110+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
111+ ``` py
112+ from pyodide.ffi import to_js
113+
114+ company_name1 = " Bs Beverages"
115+ company_name2 = " Around the Horn"
116+ stmt = self .env.DB .prepare(" SELECT * FROM Customers WHERE CompanyName = ?" )
117+ batch_result = await self .env.DB .batch(to_js([
118+ stmt.bind(company_name1),
119+ stmt.bind(company_name2)
120+ ]))
121+ ```
122+ </TabItem > </Tabs >
79123
80124#### Parameters
81125
@@ -90,6 +134,7 @@ const batchResult = await env.DB.batch([
90134
91135<Details header = " Example of return values" open = { false } >
92136
137+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
93138``` js
94139const companyName1 = ` Bs Beverages` ;
95140const companyName2 = ` Around the Horn` ;
@@ -99,7 +144,22 @@ const stmt = await env.DB.batch([
99144]);
100145return Response .json (stmt)
101146```
102- ``` js output
147+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
148+ ``` py
149+ from pyodide.ffi import to_js
150+ from workers import Response
151+
152+ company_name1 = " Bs Beverages"
153+ company_name2 = " Around the Horn"
154+ stmt = await self .env.DB .batch(to_js([
155+ self .env.DB .prepare(" SELECT * FROM Customers WHERE CompanyName = ?" ).bind(company_name1),
156+ self .env.DB .prepare(" SELECT * FROM Customers WHERE CompanyName = ?" ).bind(company_name2)
157+ ]))
158+ return Response.json(stmt)
159+ ```
160+ </TabItem > </Tabs >
161+
162+ ``` json output
103163[
104164 {
105165 "success" : true ,
@@ -148,10 +208,17 @@ return Response.json(stmt)
148208 }
149209]
150210```
211+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
151212``` js
152213console .log (stmt[1 ].results );
153214```
154- ``` js output
215+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
216+ ``` py
217+ print (stmt[1 ].results)
218+ ```
219+ </TabItem > </Tabs >
220+
221+ ``` json output
155222[
156223 {
157224 "CustomerId" : 4 ,
@@ -166,24 +233,46 @@ console.log(stmt[1].results);
166233
167234- You can construct batches reusing the same prepared statement:
168235
236+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
169237 ``` js
170- const companyName1 = ` Bs Beverages` ;
171- const companyName2 = ` Around the Horn` ;
172- const stmt = env .DB .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` );
173- const batchResult = await env .DB .batch ([
174- stmt .bind (companyName1),
175- stmt .bind (companyName2)
176- ]);
177- return Response .json (batchResult);
238+ const companyName1 = ` Bs Beverages` ;
239+ const companyName2 = ` Around the Horn` ;
240+ const stmt = env .DB .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` );
241+ const batchResult = await env .DB .batch ([
242+ stmt .bind (companyName1),
243+ stmt .bind (companyName2)
244+ ]);
245+ return Response .json (batchResult);
246+ ```
247+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
248+ ``` py
249+ from pyodide.ffi import to_js
250+ from workers import Response
251+
252+ company_name1 = " Bs Beverages"
253+ company_name2 = " Around the Horn"
254+ stmt = self .env.DB .prepare(" SELECT * FROM Customers WHERE CompanyName = ?" )
255+ batch_result = await self .env.DB .batch(to_js([
256+ stmt.bind(company_name1),
257+ stmt.bind(company_name2)
258+ ]))
259+ return Response.json(batch_result)
178260 ```
261+ </TabItem > </Tabs >
179262
180263### ` exec() `
181264
182265Executes one or more queries directly without prepared statements or parameter bindings.
183266
267+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
184268``` js
185269const returnValue = await env .DB .exec (` SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"` );
186270```
271+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
272+ ``` py
273+ return_value = await self .env.DB .exec(' SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"' )
274+ ```
275+ </TabItem > </Tabs >
187276
188277#### Parameters
189278
@@ -198,11 +287,22 @@ const returnValue = await env.DB.exec(`SELECT * FROM Customers WHERE CompanyName
198287 - Refer to [ ` D1ExecResult ` ] ( /d1/worker-api/return-object/#d1execresult ) for more information.
199288
200289<Details header = " Example of return values" open = { false } >
290+
291+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
201292``` js
202293const returnValue = await env .DB .exec (` SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"` );
203294return Response .json (returnValue);
204295```
205- ``` js output
296+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
297+ ``` py
298+ from workers import Response
299+
300+ return_value = await self .env.DB .exec(' SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"' )
301+ return Response.json(return_value)
302+ ```
303+ </TabItem > </Tabs >
304+
305+ ``` json output
206306{
207307 "count" : 1 ,
208308 "duration" : 1
@@ -225,6 +325,7 @@ This API only works on databases created during D1's alpha period. Check which v
225325
226326Dumps the entire D1 database to an SQLite compatible file inside an ArrayBuffer.
227327
328+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
228329``` js
229330const dump = await db .dump ();
230331return new Response (dump, {
@@ -234,6 +335,14 @@ return new Response(dump, {
234335 },
235336});
236337```
338+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
339+ ``` py
340+ from workers import Response
341+
342+ dump = await db.dump()
343+ return Response(dump, status = 200 , headers = {" Content-Type" : " application/octet-stream" })
344+ ```
345+ </TabItem > </Tabs >
237346
238347#### Parameters
239348
@@ -247,9 +356,15 @@ return new Response(dump, {
247356
248357Starts a D1 session which maintains sequential consistency among queries executed on the returned ` D1DatabaseSession ` object.
249358
250- ``` ts
359+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
360+ ``` js
251361const session = env .DB .withSession (" <parameter>" );
252362```
363+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
364+ ``` py
365+ session = self .env.DB .withSession(" <parameter>" )
366+ ```
367+ </TabItem > </Tabs >
253368
254369#### Parameters
255370
@@ -283,13 +398,21 @@ const session = env.DB.withSession("<parameter>");
283398
284399Retrieves the latest ` bookmark ` from the D1 Session.
285400
286- ``` ts
401+ <Tabs syncKey = " workersExamples" > <TabItem label = " JavaScript" icon = " seti:javascript" >
402+ ``` js
287403const session = env .DB .withSession (" first-primary" );
288404const result = await session
289405 .prepare (` SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'` )
290406 .run ()
291407return { bookmark } = session .getBookmark ();
292408```
409+ </TabItem > <TabItem label = " Python" icon = " seti:python" >
410+ ``` py
411+ session = self .env.DB .withSession(" first-primary" )
412+ result = await session.prepare(" SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'" ).run()
413+ bookmark = session.getBookmark()
414+ ```
415+ </TabItem > </Tabs >
293416
294417#### Parameters
295418
0 commit comments