DynamoDB JavaScript SDK v2 v3: gestione degli elementi – 4

Spread the love

Nell’ultimo articolo avevamo visto quante e quali tipologie di richieste possiamo fare, ma ora andremo a vedere le operazioni base che possiamo fare sui singoli dati.

DynamoDB JavaScript SDK v2 and v3 manage items
DynamoDB JavaScript SDK v2 and v3 manage items

Ripetiamo anche le modalità di aggiunta dati viste nell’ultimo capitolo.

Aggiungi un oggetto

SDK v2

Per prima cosa vedremo lo script asincrono, come nelle parti precedenti.
Per eseguire questo script vai nella cartella dynamodb-examples\jsv2 e avvia con node item_add.js

/*
 * DynamoDB Script Examples
 * Add item with DynamoDB
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

console.log("Start script!");

var params = {
    TableName: "TestTableMischianti",
    Item: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"},
        "ValueStr": {S: "Mischianti main url"},
        "ValueNum": {N: "1"},
    }
};


ddb.putItem(params, function (err, data) {
    if (err) {
        console.error("Unable add item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("PutItem succeeded:", JSON.stringify(params, null, 2), data);
    }
});

console.log("End script!");

Il risultato nella console è

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv2>node item_add.js
Start script!
End script!
PutItem succeeded: {
  "TableName": "TestTableMischianti",
  "Item": {
    "ItemId": {
      "S": "mischianti"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    },
    "ValueStr": {
      "S": "Mischianti main url"
    },
    "ValueNum": {
      "N": "1"
    }
  }
} {}

Verificare la sintassi dei parametri

var params = {
    TableName: "TestTableMischianti",
    Item: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"},
        "ValueStr": {S: "Mischianti main url"},
        "ValueNum": {N: "1"},
    }
};

dobbiamo specificare la chiave definita (controlla l’articolo di creazione tabella), quindi puoi aggiungere i valori che ti servono, il valore SN è il tipo di dati S --> StringN --> Number.

Ora la versione asincrona ( node item_add_async_await.js).

/*
 * DynamoDB Script Examples
 * Add item with DynamoDB async await
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Item: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"},
            "ValueStr": {S: "Mischianti main url"},
            "ValueNum": {N: "1"},
        }
    };

    try {
        const data = await ddb.putItem(params).promise();
        console.log("PutItem succeeded:", JSON.stringify(params, null, 2), data);
    }catch (err) {
        console.error("Unable add item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");})();

Come già descritto la differenza era solo l’ordine di esecuzione.

SDK v3

Nella versione v3 ci sono alcune modifiche e Amazon consiglia di utilizzare async await (sincrono) con modularizzazione degli import. Ma ricominceremo dalla sintassi dell’ultimo articolo.

Ora la versione asincrona (modalità sconsigliata) dello script di aggiunta.

Per eseguire questo script vai nella cartella dynamodb-examples\jsv3 e avvia node item_add.js

/*
 * DynamoDB Script Examples v3
 * Add item with DynamoDB
 * DB of selected region in configDynamoDB
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb");
const configDynamoDB = {
    version: 'latest',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // credentials: {
    //     // accessKeyId default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     accessKeyId: "9oiaf7",
    //     // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     secretAccessKey: "yz5i9"
    //
    // }
};
const dbClient = new DynamoDBClient(configDynamoDB);

const addElement = (params) => {
    const command = new PutItemCommand(params);
    return dbClient.send(command);
}

console.log("Start script!");

var params = {
    TableName: "TestTableMischianti",
    Item: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"},
        "ValueStr": {S: "Mischianti main url"},
        "ValueNum": {N: "1"},
    }

};

const command = new PutItemCommand(params);
const data = dbClient.send(command)
    .then(
        data => console.log("PutItem succeeded:", JSON.stringify(params, null, 2), data)
    ).catch(
        err => console.error("Unable add item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2))
    ).finally(
        () => console.log("END Of execution!")
    );

console.log("End script!");

L’ output della console diventa più dettagliato e abbiamo informazioni aggiuntive.

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv3>node item_add.js
Start script!
End script!
PutItem succeeded: {
  "TableName": "TestTableMischianti",
  "Item": {
    "ItemId": {
      "S": "mischianti"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    },
    "ValueStr": {
      "S": "Mischianti main url"
    },
    "ValueNum": {
      "N": "1"
    }
  }
} { '$metadata':
   { httpStatusCode: 200,
     httpHeaders:
      { server: 'Server',
        date: 'Fri, 15 Jan 2021 21:38:39 GMT',
        'content-type': 'application/x-amz-json-1.0',
        'content-length': '2',
        connection: 'keep-alive',
        'x-amzn-requestid': '035UKDLIR2FGUQ66ISB3UG8K17VV4KQNSO5AEMVJF66Q9ASUAAJG',
        'x-amz-crc32': '2745614147' },
     requestId: '035UKDLIR2FGUQ66ISB3UG8K17VV4KQNSO5AEMVJF66Q9ASUAAJG',
     attempts: 1,
     totalRetryDelay: 0 },
  Attributes: undefined,
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined }
END Of execution!

In dettaglio abbiamo il codice di stato http per identificare lo stato della response ( httpStatusCode: 200), e per lo sviluppatore ora è esplicito che lavoriamo con chiamate REST.

Ora lo script sincrono (node item_add_async_await.js)

/*
 * DynamoDB Script Examples v3
 * Add item with DynamoDB async await
 * DB of selected region in configDynamoDB
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb");
const configDynamoDB = {
    version: 'latest',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // credentials: {
    //     // accessKeyId default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     accessKeyId: "9oiaf7",
    //     // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     secretAccessKey: "yz5i9"
    //
    // }
};

const dbClient = new DynamoDBClient(configDynamoDB);

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Item: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"},
            "ValueStr": {S: "Mischianti main url"},
            "ValueNum": {N: "1"},
        }
    };

    try {
        const command = new PutItemCommand(params);
        const data = await dbClient.send(command);
        console.log("PutItem succeeded:", JSON.stringify(params, null, 2), data);
    } catch (err) {
        console.error("Unable add item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");
})();

Controlla i dati

  1. Puoi trovare tutti i dati nella sezione DynamoDB della console AWS, vai a questo link (controlla la regione).
https://eu-west-1.console.aws.amazon.com/dynamodb/home?region=eu-west-1#
  1. Click on Table/Tabelle;
  2. Select TestTableMischianti table;
  3. Click on element tab.
DynamoDB Elementi in tabella nell'AWS console
DynamoDB Elementi in tabella nell’AWS console

Cancellare gli oggetti

La logica è la stessa per tutti i comandi, quindi ora andiamo più veloci.

SDK v2

Qui lo script sincrono (node item_delete.js).

/*
 * DynamoDB Script Examples
 * Delete item with DynamoDB
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

console.log("Start script!");

var params = {
    TableName: "TestTableMischianti",
    Key: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"}
    }
};


ddb.deleteItem(params, function (err, data) {
    if (err) {
        console.error("Unable delete item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("DeleteItem succeeded:", JSON.stringify(params, null, 2), data);
    }
});

console.log("End script!")

Puoi vedere che nei parametri dobbiamo specificare tutta la chiave dichiarata, e non puoi omettere una chiave.

var params = {
    TableName: "TestTableMischianti",
    Key: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"}
    }
};

e qui l’output della console.

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv2>node item_delete.js
Start script!
End script!
DeleteItem succeeded: {
  "TableName": "TestTableMischianti",
  "Key": {
    "ItemId": {
      "S": "mischianti"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    }
  }
} {}

Nessun risultato è esposto.

E ora lo script asincrono (node item_delete_async_await.js).

/*
 * DynamoDB Script Examples
 * Delete item with DynamoDB
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Key: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"}
        }
    };

    try {
        const data = await ddb.deleteItem(params).promise();
        console.log("DeleteItem succeeded:", JSON.stringify(params, null, 2), data);
    }catch (err) {
        console.error("Unable delete item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");
})();

L’output in console cambia solo per l’ordine di log.

SDK v3

Per eseguire questo script vai nella cartella dynamodb-examples\jsv3 e avvia node item_delete_async_await.js

/*
 * DynamoDB Script Examples
 * Delete item with DynamoDB
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Key: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"}
        }
    };

    try {
        const data = await ddb.deleteItem(params).promise();
        console.log("DeleteItem succeeded:", JSON.stringify(params, null, 2), data);
    }catch (err) {
        console.error("Unable delete item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");
})();

with this console output

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv3>node item_delete_async_await.js
Start script!
DeleteItem succeeded: {
  "TableName": "TestTableMischianti",
  "Key": {
    "ItemId": {
      "S": "mischianti"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    }
  }
} { '$metadata':
   { httpStatusCode: 200,
     httpHeaders:
      { server: 'Server',
        date: 'Wed, 27 Jan 2021 08:01:21 GMT',
        'content-type': 'application/x-amz-json-1.0',
        'content-length': '2',
        connection: 'keep-alive',
        'x-amzn-requestid': 'GGMGF3FS2QKQHPR35SGABV9VEJVV4KQNSO5AEMVJF66Q9ASUAAJG',
        'x-amz-crc32': '2745614147' },
     requestId: 'GGMGF3FS2QKQHPR35SGABV9VEJVV4KQNSO5AEMVJF66Q9ASUAAJG',
     attempts: 1,
     totalRetryDelay: 0 },
  Attributes: undefined,
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined }
End script!

Ottieni elementi

È ora di recuperare l’elemento che avevamo inserito, puoi ottenere quell’informazione filtrando per tutta la chiave dichiarata.

SDK v2

Per eseguire questo script vai nella cartella dynamodb-examples\jsv2 e avvia node item_get.js

/*
 * DynamoDB Script Examples
 * Get item with DynamoDB
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

console.log("Start script!");

var params = {
    TableName: "TestTableMischianti",
    Key: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"},
    }
};


ddb.getItem(params, function (err, data) {
    if (err) {
        console.error("Unable ti get item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Get Item succeeded:", JSON.stringify(data, null, 2));
    }
});

console.log("End script!");

È abbastanza semplice, devi solo specificare la tabella e la chiave nei parametri

var params = {
    TableName: "TestTableMischianti",
    Key: {
        "ItemId": {S: "mischianti"},
        "ItemName": {S: "www.mischianti.org"},
    }
};

allo stesso modo puoi usare uno script sincrono item_get_async_await.js:

/*
 * DynamoDB Script Examples
 * Get item with DynamoDB async await
 * DB of selected region in AWS.config.update
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

var AWS = require("aws-sdk");

AWS.config.update({
    apiVersion: '2012-08-10',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // // accessKeyId default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // accessKeyId: "9oiaf7",
    // // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    // // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    // secretAccessKey: "yz5i9"
});

var ddb = new AWS.DynamoDB();

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Key: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"},
        }
    };

    try {
        const data = await ddb.getItem(params).promise();
        console.log("Get Item succeeded:", JSON.stringify(data, null, 2));
    }catch (err) {
        console.error("Unable ti get item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");
})();

L’output della console è

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv2>node item_get_async_await.js
Start script!
Get Item succeeded: {
  "Item": {
    "ValueStr": {
      "S": "Mischianti main url"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    },
    "ItemId": {
      "S": "mischianti"
    },
    "ValueNum": {
      "N": "1"
    }
  }
}
End script!

Puoi trovare i dati sotto la chiave Item.

SDK v3

Per eseguire questo script vai nella cartella dynamodb-examples\jsv3 e avvia node item_get.js

/*
 * DynamoDB Script Examples v3
 * Get item with DynamoDB async await
 * DB of selected region in configDynamoDB
 *
 * AUTHOR:  Renzo Mischianti
 *
 * https://mischianti.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
 *
 * You may copy, alter and reuse this code in any way you like, but please leave
 * reference to www.mischianti.org in your comments if you redistribute this code.
 *
 */

const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb");
const configDynamoDB = {
    version: 'latest',
    region: "eu-west-1",
    // endpoint: "http://localhost:8000",
    // credentials: {
    //     // accessKeyId default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     accessKeyId: "9oiaf7",
    //     // secretAccessKey default can be used while using the downloadable version of DynamoDB.
    //     // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    //     secretAccessKey: "yz5i9"
    //
    // }
};

const dbClient = new DynamoDBClient(configDynamoDB);

(async function () {
    console.log("Start script!");

    var params = {
        TableName: "TestTableMischianti",
        Key: {
            "ItemId": {S: "mischianti"},
            "ItemName": {S: "www.mischianti.org"}
        }
    };

    try {
        const command = new GetItemCommand(params);
        const data = await dbClient.send(command);
        console.log("GetItem succeeded:", JSON.stringify(params, null, 2), JSON.stringify(data, null, 2));
    }catch (err) {
        console.error("Unable get item", JSON.stringify(params, null, 2), ". Error JSON:", JSON.stringify(err, null, 2));
    }

    console.log("End script!");
})();

L’output in console è

D:\Projects\AlexaProjects\dynamodb-management\dynamodb-examples\jsv3>node item_get_async_await.js
Start script!
GetItem succeeded: {
  "TableName": "TestTableMischianti",
  "Key": {
    "ItemId": {
      "S": "mischianti"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    }
  }
} {
  "$metadata": {
    "httpStatusCode": 200,
    "httpHeaders": {
      "server": "Server",
      "date": "Wed, 27 Jan 2021 08:33:59 GMT",
      "content-type": "application/x-amz-json-1.0",
      "content-length": "136",
      "connection": "keep-alive",
      "x-amzn-requestid": "26ORUJ7O63QIV916K5S3D0OLLBVV4KQNSO5AEMVJF66Q9ASUAAJG",
      "x-amz-crc32": "1542980662"
    },
    "requestId": "26ORUJ7O63QIV916K5S3D0OLLBVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "attempts": 1,
    "totalRetryDelay": 0
  },
  "Item": {
    "ValueStr": {
      "S": "Mischianti main url"
    },
    "ItemName": {
      "S": "www.mischianti.org"
    },
    "ItemId": {
      "S": "mischianti"
    },
    "ValueNum": {
      "N": "1"
    }
  }
}
End script!

I dati sono ancora sotto la chiave Item.

Grazie

  1. DynamoDB JavaScript SDK v2 v3: prerequisiti ed introduzione
  2. DynamoDB JavaScript SDK v2 v3: gestione delle tabelle
  3. DynamoDB JavaScript SDK v2 v3: aggiungere elementi con DB o DocumentClient
  4. DynamoDB JavaScript SDK v2 v3: gestione degli elementi
  5. DynamoDB JavaScript SDK v2 v3: scansionare i dati delle tabelle e paginazione
  6. DynamoDB JavaScript SDK v2 v3: query

Spread the love

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *