• 沒有找到結果。

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

function main() {

// Use default settings

const driver = new qldb.QldbDriver("quick-start");

} main();

TypeScript

import { QldbDriver } from "amazon-qldb-driver-nodejs";

function main(): void { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

}

if (require.main === module) { main();

}

Note

You can set the AWS_REGION environment variable to specify the Region. For more information, see Setting the AWS Region in the AWS SDK for JavaScript Developer Guide.

Step 3: Create a table and an index

The following code examples show how to run CREATE TABLE and CREATE INDEX statements.

1. Add the following function that creates a table named People.

JavaScript

async function createTable(txn) {

await txn.execute("CREATE TABLE People");

}

TypeScript

async function createTable(txn: TransactionExecutor): Promise<void> { await txn.execute("CREATE TABLE People");

}

2. Add the following function that creates an index for the firstName field on the People table.

Indexes (p. 654) are required to optimize query performance and help to limit optimistic concurrency control (OCC) (p. 476) conflict exceptions.

JavaScript

async function createIndex(txn) {

await txn.execute("CREATE INDEX ON People (firstName)");

}

Quick start tutorial TypeScript

async function createIndex(txn: TransactionExecutor): Promise<void> { await txn.execute("CREATE INDEX ON People (firstName)");

}

3. In the main function, you first call createTable, and then call createIndex.

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

async function main() { // Use default settings

const driver = new qldb.QldbDriver("quick-start");

await driver.executeLambda(async (txn) => { console.log("Create table People");

await createTable(txn);

console.log("Create index on firstName");

await createIndex(txn);

});

driver.close();

} main();

TypeScript

import { QldbDriver, TransactionExecutor } from "amazon-qldb-driver-nodejs";

async function main(): Promise<void> { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

await driver.executeLambda(async (txn: TransactionExecutor) => { console.log("Create table People");

await createTable(txn);

console.log("Create index on firstName");

await createIndex(txn);

});

driver.close();

}

if (require.main === module) { main();

}

4. Run the code to create the table and index.

JavaScript

$ node app.js

TypeScript

$ tsc app.ts; node app.js

Quick start tutorial

Step 4: Insert a document

The following code example shows how to run an INSERT statement. QLDB supports the

PartiQL (p. 646) query language (SQL compatible) and the Amazon Ion (p. 697) data format (superset of JSON).

1. Add the following function that inserts a document into the People table.

JavaScript

async function insertDocument(txn) { const person = {

firstName: "John", lastName: "Doe", age: 42

};

await txn.execute("INSERT INTO People ?", person);

}

TypeScript

async function insertDocument(txn: TransactionExecutor): Promise<void> { const person: Record<string, any> = {

firstName: "John", lastName: "Doe", age: 42

};

await txn.execute("INSERT INTO People ?", person);

}

This example uses a question mark (?) as a variable placeholder to pass the document information to the statement. The execute method supports values in both Amazon Ion types and Node.js native types.

TipTo insert multiple documents by using a single INSERT (p. 664) statement, you can pass a parameter of type list (p. 431) to the statement as follows.

// people is a list

txn.execute("INSERT INTO People ?", people);

You don't enclose the variable placeholder (?) in double angle brackets ( <<...>> ) when passing a list. In manual PartiQL statements, double angle brackets denote an unordered collection known as a bag.

2. In the main function, remove the createTable and createIndex calls, and add a call to insertDocument.

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

async function main() { // Use default settings

const driver = new qldb.QldbDriver("quick-start");

await driver.executeLambda(async (txn) => { console.log("Insert document");

await insertDocument(txn);

Quick start tutorial });

driver.close();

} main();

TypeScript

import { QldbDriver, TransactionExecutor } from "amazon-qldb-driver-nodejs";

async function main(): Promise<void> { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

await driver.executeLambda(async (txn: TransactionExecutor) => { console.log("Insert document");

await insertDocument(txn);

});

driver.close();

}

if (require.main === module) { main();

}

Step 5: Query the document

The following code example shows how to run a SELECT statement.

1. Add the following function that queries a document from the People table.

JavaScript

async function fetchDocuments(txn) {

return await txn.execute("SELECT firstName, age, lastName FROM People WHERE firstName = ?", "John");

}

TypeScript

async function fetchDocuments(txn: TransactionExecutor): Promise<dom.Value[]> { return (await txn.execute("SELECT firstName, age, lastName FROM People WHERE firstName = ?", "John")).getResultList();

}

2. In the main function, add the following call to fetchDocuments after the call to insertDocument.

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

async function main() { // Use default settings

const driver = new qldb.QldbDriver("quick-start");

var resultList = await driver.executeLambda(async (txn) => {

Quick start tutorial console.log("Insert document");

await insertDocument(txn);

console.log("Fetch document");

var result = await fetchDocuments(txn);

return result.getResultList();

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

} main();

TypeScript

import { QldbDriver, TransactionExecutor } from "amazon-qldb-driver-nodejs";

import { dom } from "ion-js";

async function main(): Promise<void> { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

const resultList: dom.Value[] = await driver.executeLambda(async (txn:

TransactionExecutor) => {

console.log("Insert document");

await insertDocument(txn);

console.log("Fetch document");

return await fetchDocuments(txn);

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

}

if (require.main === module) { main();

}

Step 6: Update the document

The following code example shows how to run an UPDATE statement.

1. Add the following function that updates a document in the People table by changing lastName to

"Stiles".

JavaScript

async function updateDocuments(txn) {

await txn.execute("UPDATE People SET lastName = ? WHERE firstName = ?", "Stiles", "John");

}

TypeScript

async function updateDocuments(txn: TransactionExecutor): Promise<void> { await txn.execute("UPDATE People SET lastName = ? WHERE firstName = ?", "Stiles", "John");

Quick start tutorial }

2. In the main function, add the following call to updateDocuments after the call to fetchDocuments. Then, call fetchDocuments again to see the updated results.

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

async function main() { // Use default settings

const driver = new qldb.QldbDriver("quick-start");

var resultList = await driver.executeLambda(async (txn) => { console.log("Insert document");

await insertDocument(txn);

console.log("Fetch document");

await fetchDocuments(txn);

console.log("Update document");

await updateDocuments(txn);

console.log("Fetch document after update");

var result = await fetchDocuments(txn);

return result.getResultList();

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

} main();

TypeScript

import { QldbDriver, TransactionExecutor } from "amazon-qldb-driver-nodejs";

import { dom } from "ion-js";

async function main(): Promise<void> { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

const resultList: dom.Value[] = await driver.executeLambda(async (txn:

TransactionExecutor) => {

console.log("Insert document");

await insertDocument(txn);

console.log("Fetch document");

await fetchDocuments(txn);

console.log("Update document");

await updateDocuments(txn);

console.log("Fetch document after update");

return await fetchDocuments(txn);

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

}

if (require.main === module) { main();

}

3. Run the code to insert, query, and update a document.

Quick start tutorial JavaScript

$ node app.js TypeScript

$ tsc app.ts; node app.js

Running the complete application

The following code examples are the complete versions of app.js and app.ts. Instead of doing the previous steps individually, you can also run this code end to end. This application demonstrates some basic CRUD operations on the ledger named quick-start.

NoteBefore you run this code, make sure that you don't already have an active table named People in the quick-start ledger.

JavaScript

const qldb = require('amazon-qldb-driver-nodejs');

async function createTable(txn) {

await txn.execute("CREATE TABLE People");

}

async function createIndex(txn) {

await txn.execute("CREATE INDEX ON People (firstName)");

}

async function insertDocument(txn) { const person = {

firstName: "John", lastName: "Doe", age: 42

};

await txn.execute("INSERT INTO People ?", person);

}

async function fetchDocuments(txn) {

return await txn.execute("SELECT firstName, age, lastName FROM People WHERE firstName = ?", "John");

}

async function updateDocuments(txn) {

await txn.execute("UPDATE People SET lastName = ? WHERE firstName = ?", "Stiles", "John");

}

async function main() { // Use default settings

const driver = new qldb.QldbDriver("quick-start");

var resultList = await driver.executeLambda(async (txn) => { console.log("Create table People");

await createTable(txn);

console.log("Create index on firstName");

await createIndex(txn);

console.log("Insert document");

await insertDocument(txn);

Quick start tutorial console.log("Fetch document");

await fetchDocuments(txn);

console.log("Update document");

await updateDocuments(txn);

console.log("Fetch document after update");

var result = await fetchDocuments(txn);

return result.getResultList();

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

} main();

TypeScript

import { QldbDriver, TransactionExecutor } from "amazon-qldb-driver-nodejs";

import { dom } from "ion-js";

async function createTable(txn: TransactionExecutor): Promise<void> { await txn.execute("CREATE TABLE People");

}

async function createIndex(txn: TransactionExecutor): Promise<void> { await txn.execute("CREATE INDEX ON People (firstName)");

}

async function insertDocument(txn: TransactionExecutor): Promise<void> { const person: Record<string, any> = {

firstName: "John", lastName: "Doe", age: 42

};

await txn.execute("INSERT INTO People ?", person);

}

async function fetchDocuments(txn: TransactionExecutor): Promise<dom.Value[]> { return (await txn.execute("SELECT firstName, age, lastName FROM People WHERE firstName = ?", "John")).getResultList();

}

async function updateDocuments(txn: TransactionExecutor): Promise<void> {

await txn.execute("UPDATE People SET lastName = ? WHERE firstName = ?", "Stiles", "John");

};

async function main(): Promise<void> { // Use default settings

const driver: QldbDriver = new QldbDriver("quick-start");

const resultList: dom.Value[] = await driver.executeLambda(async (txn:

TransactionExecutor) => {

console.log("Create table People");

await createTable(txn);

console.log("Create index on firstName");

await createIndex(txn);

console.log("Fetch document after update");

Cookbook reference return await fetchDocuments(txn);

});

// Pretty print the result list

console.log("The result List is ", JSON.stringify(resultList, null, 2));

driver.close();

}

if (require.main === module) { main();

}

To run the complete application, enter the following command.

JavaScript

$ node app.js

TypeScript

$ tsc app.ts; node app.js

Amazon QLDB driver for Node.js – Cookbook