Return objects
Some D1 Worker Binding APIs return a typed object.
D1 Worker Binding API | Return object |
---|---|
D1PreparedStatement::run , D1Database::batch | D1Result |
D1Database::exec | D1ExecResult |
The methods D1PreparedStatement::run
and D1Database::batch
return a typed D1Result
object for each query statement. This object contains:
- The success status
- A meta object with the internal duration of the operation in milliseconds
- The results (if applicable) as an array
{ success: boolean, // true if the operation was successful, false otherwise meta: { served_by: string // the version of Cloudflare's backend Worker that returned the result duration: number, // the duration of the SQL query execution only, in milliseconds changes: number, // the number of changes made to the database last_row_id: number, // the last inserted row ID, only applies when the table is defined without the `WITHOUT ROWID` option changed_db: boolean, // true if something on the database was changed size_after: number, // the size of the database after the query is successfully applied rows_read: number, // the number of rows read (scanned) by this query rows_written: number // the number of rows written by this query } results: array | null, // [] if empty, or null if it does not apply}
const someVariable = `Bs Beverages`;const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bind(someVariable);const returnValue = await stmt.run();return Response.json(returnValue)
{ "success": true, "meta": { "served_by": "miniflare.db", "duration": 1, "changes": 0, "last_row_id": 0, "changed_db": false, "size_after": 8192, "rows_read": 4, "rows_written": 0 }, "results": [ { "CustomerId": 11, "CompanyName": "Bs Beverages", "ContactName": "Victoria Ashworth" }, { "CustomerId": 13, "CompanyName": "Bs Beverages", "ContactName": "Random Name" } ]}
The method D1Database::exec
returns a typed D1ExecResult
object for each query statement. This object contains:
- The number of executed queries
- The duration of the operation in milliseconds
{ "count": number, // the number of executed queries "duration": number // the duration of the operation, in milliseconds}
const returnValue = await env.DB.exec(`SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"`);return Response.json(returnValue);
{ "count": 1, "duration": 1}