步骤 6:查看文档修订历史记录 - 亚马逊 Quantum Ledger 数据库(亚马逊QLDB)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

步骤 6:查看文档修订历史记录

重要

终止支持通知:现有客户将能够使用亚马逊,QLDB直到 2025 年 7 月 31 日终止支持。有关更多详细信息,请参阅将亚马逊QLDB账本迁移到亚马逊 Aurora Postgr SQL e。

上一步中修改车辆注册数据后,您可查询其所有注册车主的历史记录以及任何其他更新的字段。在此步骤中,您将在 vehicle-registration 分类账的 VehicleRegistration 表格中查询文档的修订历史记录。

若要查看修订历史记录
  1. 使用以下程序 (QueryHistory.ts) 查询VehicleRegistration文档的修订历史记录VIN1N4AL11D75C109151

    /* * 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 的字面值,可以用反引号(`...`)表示。有关更多信息,请参阅 在亚马逊中使用 partiQL 查询 Ion QLDB

    • 最佳做法是,使用日期范围(开始时间结束时间)和文档 ID(metadata.id)来限定历史记录查询。QLDB处理事务中的SELECT查询,这些事务受事务超时限制的约束。

      QLDB历史记录按文档 ID 编制索引,您目前无法创建其他历史索引。包含开始时间和结束时间的历史记录查询将从日期范围限定中获得便利。

  2. 要运行编译后的程序,请输入以下命令。

    node dist/QueryHistory.js

要以加密方式验证 vehicle-registration 分类账中的文档修订版,请继续 第 7 步:验证分类账中的文档