本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
步驟 6:檢視文件的修訂歷史記錄
重要
支援終止通知:現有客戶將可以使用 Amazon QLDB,直到 07/31/2025 的支援結束為止。如需詳細資訊,請參閱將 Amazon QLDB Ledger 遷移至 Amazon Aurora PostgreSQL
在上一個步驟中修改車輛的註冊資料後,您可以查詢其所有已註冊擁有者的歷史記錄和任何其他更新的欄位。在此步驟中,您會查詢 vehicle-registration
分類帳中VehicleRegistration
資料表中文件的修訂歷史記錄。
檢視修訂歷史記錄
-
使用下列程式 (
QueryHistory.ts
) 查詢具有 VINVehicleRegistration
的文件修訂歷史記錄1N4AL11D75C109151
。/* * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { QldbDriver, Result, TransactionExecutor } from "amazon-qldb-driver-nodejs"; import { dom } from "ion-js"; import { getQldbDriver } from "./ConnectToLedger"; import { VEHICLE_REGISTRATION } from "./model/SampleData"; import { VEHICLE_REGISTRATION_TABLE_NAME } from "./qldb/Constants"; import { prettyPrintResultList } from "./ScanTable"; import { error, log } from "./qldb/LogUtil"; import { getDocumentId } from "./qldb/Util"; /** * Find previous primary owners for the given VIN in a single transaction. * @param txn The {@linkcode TransactionExecutor} for lambda execute. * @param vin The VIN to find previous primary owners for. * @returns Promise which fulfills with void. */ async function previousPrimaryOwners(txn: TransactionExecutor, vin: string): Promise<void> { const documentId: string = await getDocumentId(txn, VEHICLE_REGISTRATION_TABLE_NAME, "VIN", vin); const todaysDate: Date = new Date(); // set todaysDate back one minute to ensure end time is in the past // by the time the request reaches our backend todaysDate.setMinutes(todaysDate.getMinutes() - 1); const threeMonthsAgo: Date = new Date(todaysDate); threeMonthsAgo.setMonth(todaysDate.getMonth() - 3); const query: string = `SELECT data.Owners.PrimaryOwner, metadata.version FROM history ` + `(${VEHICLE_REGISTRATION_TABLE_NAME}, \`${threeMonthsAgo.toISOString()}\`, \`${todaysDate.toISOString()}\`) ` + `AS h WHERE h.metadata.id = ?`; await txn.execute(query, documentId).then((result: Result) => { log(`Querying the 'VehicleRegistration' table's history using VIN: ${vin}.`); const resultList: dom.Value[] = result.getResultList(); prettyPrintResultList(resultList); }); } /** * Query a table's history for a particular set of documents. * @returns Promise which fulfills with void. */ const main = async function(): Promise<void> { try { const qldbDriver: QldbDriver = getQldbDriver(); const vin: string = VEHICLE_REGISTRATION[0].VIN; await qldbDriver.executeLambda(async (txn: TransactionExecutor) => { await previousPrimaryOwners(txn, vin); }); } catch (e) { error(`Unable to query history to find previous owners: ${e}`); } } if (require.main === module) { main(); }
注意
-
您可以在下列語法歷史記錄函數中查詢內建 ,以檢視文件的修訂歷史記錄。
SELECT * FROM history(
table_name
[, `start-time
` [, `end-time
` ] ] ) AS h [ WHERE h.metadata.id = 'id
' ] -
開始時間和結束時間皆為選用。它們是 Amazon Ion 常值,可以用反引號表示 (
`...`
)。如需詳細資訊,請參閱在 Amazon QLDB 中使用 PartiQL 查詢離子。 -
最佳實務是,使用日期範圍 (開始時間和結束時間) 和文件 ID () 來限定歷史記錄查詢
metadata.id
。QLDB 會在交易中處理SELECT
查詢,這些查詢受到交易逾時限制的限制。QLDB 歷史記錄會依文件 ID 編製索引,此時您無法建立其他歷史記錄索引。包含開始時間和結束時間的歷史記錄查詢,可受益於日期範圍資格。
-
-
若要執行轉載程式,請輸入下列命令。
node dist/QueryHistory.js
若要以密碼編譯方式驗證vehicle-registration
分類帳中的文件修訂,請繼續 步驟 7:驗證分類帳中的文件。