返回对象

跳转到内容

文档索引
在此获取完整文档索引:/d1/llms.txt
在进一步探索之前,可使用该文件发现所有可用页面。

返回对象

最后更新:2026年4月21日|复制为 Markdown|查看 Markdown 原文|Agent setup

某些 D1 Worker Binding API 会返回一个类型化对象。

D1 Worker Binding API 返回对象
D1PreparedStatement::run、D1Database::batch D1Result
D1Database::exec D1ExecResult

D1Result

方法 D1PreparedStatement::run 和 D1Database::batch 会为每条查询语句返回一个类型化的 D1Result 对象。该对象包含:

  • 成功状态
  • 一个 meta 对象,包含该操作的内部耗时(以毫秒计)
  • 结果(如适用)组成的数组
{
  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
    served_by_region: string // the region of the database instance that executed the query
    served_by_primary: boolean // true if (and only if) the database instance that executed the query was the primary
    timings: {
      sql_duration_ms: number // the duration of the SQL query execution by the database instance (not including any network time)
    }
    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
    total_attempts: number //the number of total attempts to successfully execute the query, including retries
  }
  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)
from workers import Response

some_variable = "Bs Beverages"
stmt = self.env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bind(some_variable)
return_value = await stmt.run()
return Response.json(return_value)
{
  "success": true,
  "meta": {
    "served_by": "miniflare.db",
    "served_by_region": "WEUR",
    "served_by_primary": true,
    "timings": {
      "sql_duration_ms": 0.2552
    },
    "duration": 0.2552,
    "changes": 0,
    "last_row_id": 0,
    "changed_db": false,
    "size_after": 16384,
    "rows_read": 4,
    "rows_written": 0
  },
  "results": [
    {
      "CustomerId": 11,
      "CompanyName": "Bs Beverages",
      "ContactName": "Victoria Ashworth"
    },
    {
      "CustomerId": 13,
      "CompanyName": "Bs Beverages",
      "ContactName": "Random Name"
    }
  ]
}

D1ExecResult

方法 D1Database::exec 会为每条查询语句返回一个类型化的 D1ExecResult 对象。该对象包含:

  • 已执行查询的数量
  • 该操作的耗时(以毫秒计)
{
	"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);
from workers import Response

return_value = await self.env.DB.exec('SELECT * FROM Customers WHERE CompanyName = "Bs Beverages"')
return Response.json(return_value)
{
  "count": 1,
  "duration": 1
}

存储大数字

列中的任何数值都会受到 JavaScript 数字 52 位精度的限制。如果你存储一个非常大的数字(以 int64 形式),然后再检索同一个值,返回的值可能不如你原始数字精确。