适用于 Node.js 的 Amazon QLDB 驱动程序 - Amazon Quantum Ledger Database (Amazon QLDB)

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

适用于 Node.js 的 Amazon QLDB 驱动程序

要处理分类账中的数据,您可以使用AWS提供的驱动程序从 Node.js 应用程序连接到 Amazon QLDB。以下主题介绍了如何开始使用 Node.js 上 QLDB 驱动程序。

驱动程序资源

有关 Node.js 驱动程序支持功能的更多信息,请参阅以下资源:

先决条件

开始使用适用于 Node.js 的 QLDB 驱动程序之前,您必须执行以下操作:

  1. 按照 访问 Amazon QLDB 中的 AWS 的设置说明进行操作。这包括以下这些:

    1. 注册AWS。

    2. 创建具有适当 QLDB 权限的用户。

    3. 授权以编程方式访问开发。

  2. Node.js 下载 网站安装 Node.js 14.x 或更高版本。(该驱动程序先前版本支持 Node.js 版本 10.x 或更高版本。)

  3. AWSNode.js 中的 JavaScript SDK配置开发环境:

    1. 设置您的 AWS 凭证。我们建议创建共享的凭证文件。

      有关说明,请参阅AWS SDK for JavaScript 开发指南中的从共享凭据文件加载 Node.js 中的凭据

    2. 设置您的默认 AWS 区域。要了解如何使用,请参阅 设置AWS 区域

      有关可用区域的完整列表,请参阅 AWS 一般参考 中的 Amazon QLDB 端点和限额

接下来,您可下载完整的教程示例应用程序,也可以只在 Node.js 项目中安装驱动程序并运行短代码示例。

  • 要在现有项目 Node.js 中安装 QLDB 驱动程序和适用于 JavaScript 的 AWSSDK,请继续执行。安装

  • 要设置项目并运行演示分类账上基本数据事务的简短代码示例,请参阅 快速入门教程

  • 要在完整的教程示例应用程序中运行更深入的数据和管理 API 操作示例,请参阅 Node.js 教程

安装

QLDB 支持以下驱动程序版本及其 Node.js 依赖项。

驱动程序版本 Node.js 版本 状态 最新发布日期
1.x 10.x 或更高版本 量产版 2020 年 6 月 5 日
2.x 10.x 或更高版本 量产版 2021 年 5 月 6 日
3.x 14.x 或更高版本 量产版 2023 年 11 月 10 日

使用 npm (Node.js 包管理器)安装 QLDB 驱动程序,请从项目根目录中输入以下命令。

3.x
npm install amazon-qldb-driver-nodejs
2.x
npm install amazon-qldb-driver-nodejs@2.2.0
1.x
npm install amazon-qldb-driver-nodejs@1.0.0

驱动程序对以下软件包具有对等依赖项。您还必须将这些软件包作为依赖项安装至项目中。

3.x

模块化聚合 QLDB 客户端 (管理 API)

npm install @aws-sdk/client-qldb

模块化聚合 QLDB 会话客户端 (数据 API)

npm install @aws-sdk/client-qldb-session

Amazon Ion 数据格式

npm install ion-js

BigInt纯 JavaScript 实施

npm install jsbi
2.x

AWS SDK for JavaScript

npm install aws-sdk

Amazon Ion 数据格式

npm install ion-js@4.0.0

BigInt纯 JavaScript 实施

npm install jsbi@3.1.1
1.x

AWS SDK for JavaScript

npm install aws-sdk

Amazon Ion 数据格式

npm install ion-js@4.0.0

BigInt纯 JavaScript 实施

npm install jsbi@3.1.1

使用驱动程序连接至分类账

然后,您可以导入驱动程序,并使用它来连接到分类账。以下 TypeScript 代码示例说明如何为指定的分类账名称和AWS 区域创建驱动程序实例。

3.x
import { Agent } from 'https'; import { QLDBSessionClientConfig } from "@aws-sdk/client-qldb-session"; import { QldbDriver, RetryConfig } from 'amazon-qldb-driver-nodejs'; import { NodeHttpHandlerOptions } from "@aws-sdk/node-http-handler"; const maxConcurrentTransactions: number = 10; const retryLimit: number = 4; //Reuse connections with keepAlive const lowLevelClientHttpOptions: NodeHttpHandlerOptions = { httpAgent: new Agent({ maxSockets: maxConcurrentTransactions }) }; const serviceConfigurationOptions: QLDBSessionClientConfig = { region: "us-east-1" }; //Use driver's default backoff function for this example (no second parameter provided to RetryConfig) const retryConfig: RetryConfig = new RetryConfig(retryLimit); const qldbDriver: QldbDriver = new QldbDriver("testLedger", serviceConfigurationOptions, lowLevelClientHttpOptions, maxConcurrentTransactions, retryConfig); qldbDriver.getTableNames().then(function(tableNames: string[]) { console.log(tableNames); });
2.x
import { Agent } from 'https'; import { QldbDriver, RetryConfig } from 'amazon-qldb-driver-nodejs'; const maxConcurrentTransactions: number = 10; const retryLimit: number = 4; //Reuse connections with keepAlive const agentForQldb: Agent = new Agent({ keepAlive: true, maxSockets: maxConcurrentTransactions }); const serviceConfigurationOptions = { region: "us-east-1", httpOptions: { agent: agentForQldb } }; //Use driver's default backoff function for this example (no second parameter provided to RetryConfig) const retryConfig: RetryConfig = new RetryConfig(retryLimit); const qldbDriver: QldbDriver = new QldbDriver("testLedger", serviceConfigurationOptions, maxConcurrentTransactions, retryConfig); qldbDriver.getTableNames().then(function(tableNames: string[]) { console.log(tableNames); });
1.x
import { Agent } from 'https'; import { QldbDriver } from 'amazon-qldb-driver-nodejs'; const poolLimit: number = 10; const retryLimit: number = 4; //Reuse connections with keepAlive const agentForQldb: Agent = new Agent({ keepAlive: true, maxSockets: poolLimit }); const serviceConfigurationOptions = { region: "us-east-1", httpOptions: { agent: agentForQldb } }; const qldbDriver: QldbDriver = new QldbDriver("testLedger", serviceConfigurationOptions, retryLimit, poolLimit); qldbDriver.getTableNames().then(function(tableNames: string[]) { console.log(tableNames); });

有关如何在分类账上运行基本数据交易的简短代码示例,请参阅 说明书参考

设置建议

重复使用具有保持连接功能的连接

默认的 Node.js HTTP/HTTPS 代理会为每个新请求创建一个新的 TCP 连接。为了避免重建连接的成本,AWS SDK for JavaScriptv3 默认会重复使用 TCP 连接。有关更多信息以及如何禁用 “重用” 连接,请参阅AWS SDK for JavaScript 开发人员指南中的在 Node.js 中通过 keep-alive 重用连接

我们建议使用默认设置来重用 Node.js 的 QLDB 驱动程序中的连接。在驱动程序初始化期间,将低级客户端 HTTP 选项 maxSockets 设置为maxConcurrentTransactions 与您设置的值相同。

例如,请参见以下 JavaScript 或 TypeScript 代码。

JavaScript
const qldb = require('amazon-qldb-driver-nodejs'); const https = require('https'); //Replace this value as appropriate for your application const maxConcurrentTransactions = 50; const agentForQldb = new https.Agent({ //Set this to the same value as `maxConcurrentTransactions`(previously called `poolLimit`) //Do not rely on the default value of `Infinity` "maxSockets": maxConcurrentTransactions }); const lowLevelClientHttpOptions = { httpAgent: agentForQldb } let driver = new qldb.QldbDriver("testLedger", undefined, lowLevelClientHttpOptions, maxConcurrentTransactions);
TypeScript
import { Agent } from 'https'; import { NodeHttpHandlerOptions } from "@aws-sdk/node-http-handler"; import { QldbDriver } from 'amazon-qldb-driver-nodejs'; //Replace this value as appropriate for your application const maxConcurrentTransactions: number = 50; const agentForQldb: Agent = new Agent({ //Set this to the same value as `maxConcurrentTransactions`(previously called `poolLimit`) //Do not rely on the default value of `Infinity` maxSockets: maxConcurrentTransactions }); const lowLevelClientHttpOptions: NodeHttpHandlerOptions = { httpAgent: agentForQldb }; let driver = new QldbDriver("testLedger", undefined, lowLevelClientHttpOptions, maxConcurrentTransactions);

默认的 Node.js HTTP/HTTPS 代理会为每个新请求创建一个新的 TCP 连接。为了节省建立新连接的成本,建议重复使用现有的连接。

要重用 Node.js 的 QLDB 驱动程序中的连接,请使用以下选项之一:

  • 在驱动程序初始化时,设置以下低级客户端 HTTP 选项:

    • keepAlivetrue

    • maxSockets — 与您设置的maxConcurrentTransactions值相同

    例如,请参见以下 JavaScript 或 TypeScript 代码。

    JavaScript
    const qldb = require('amazon-qldb-driver-nodejs'); const https = require('https'); //Replace this value as appropriate for your application const maxConcurrentTransactions = 50; const agentForQldb = new https.Agent({ "keepAlive": true, //Set this to the same value as `maxConcurrentTransactions`(previously called `poolLimit`) //Do not rely on the default value of `Infinity` "maxSockets": maxConcurrentTransactions }); const serviceConfiguration = { "httpOptions": { "agent": agentForQldb }}; let driver = new qldb.QldbDriver("testLedger", serviceConfiguration, maxConcurrentTransactions);
    TypeScript
    import { Agent } from 'https'; import { ClientConfiguration } from 'aws-sdk/clients/acm'; import { QldbDriver } from 'amazon-qldb-driver-nodejs'; //Replace this value as appropriate for your application const maxConcurrentTransactions: number = 50; const agentForQldb: Agent = new Agent({ keepAlive: true, //Set this to the same value as `maxConcurrentTransactions`(previously called `poolLimit`) //Do not rely on the default value of `Infinity` maxSockets: maxConcurrentTransactions }); const serviceConfiguration: ClientConfiguration = { httpOptions: { agent: agentForQldb }}; let driver = new QldbDriver("testLedger", serviceConfiguration, maxConcurrentTransactions);
  • 或者,您也可以设置 AWS_NODEJS_CONNECTION_REUSE_ENABLED 环境变量为 1。有关示例,请参阅 AWS SDK for JavaScript 开发人员指南 中的在 Node.js 中重复使用具有保持连接功能的连接

    注意

    如果您设置此环境变量,它将影响所有使用AWS SDK for JavaScript的AWS 服务。