본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Return objects (반환 객체)

원문 보기 위키 갱신

이 문서는 D1 Worker Binding API 쿼리 메서드가 반환하는 D1Result와 D1ExecResult 객체를 설명해요.

출처: 문서

본문

일부 D1 Worker Binding API는 타입이 지정된 객체를 반환해요.

D1 Worker Binding API Return object
D1PreparedStatement::run, D1Database::batch D1Result
D1Database::exec D1ExecResult

D1Result

메서드 D1PreparedStatement::run과 D1Database::batch는 각 쿼리 문장에 대해 타입이 지정된 D1Result 객체를 반환해요. 이 객체는 다음을 담고 있어요:

  • 성공 상태(success status)
  • 밀리초 단위의 작업 내부 지속 시간을 담은 meta 객체
  • (해당된다면) 배열로 나타낸 결과(results)
{
  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
}

예시 (Example)

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
}

예시 (Example)

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
}

큰 수 저장하기 (Storing large numbers)

열의 모든 숫자 값은 숫자에 대한 JavaScript의 52비트 정밀도 영향을 받아요. 매우 큰 수를 int64로 저장하고 같은 값을 다시 검색하면, 반환된 값이 원래 숫자보다 정밀도가 낮을 수 있어요.