1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【備忘録】dynamoDBをいじる Node.js

Last updated at Posted at 2020-05-08

DynamoDBのデータを書き込んだり読み込んだりするときにみるqiita

dynamoDBをlambdaからじゃなくて外部から読み込みたい時もありますよね。
そんな時に使えるやつです

記事を探したけどれが動くかわからなかったので現時点で動くやつを書いておこう
特に詳しくないので動く重視のコードです。

よく使いそうなやつだけサンプルコード載せておきます

  • getItem 一個だけ取得
  • batchGetItem 複数取得
  • putItem 一個だけ追加
  • batchWriteItem 複数追加
  • listTable AWSアカウントに紐づいているテーブル一覧

ドキュメント

ドキュメント見ればだいたいなんでもできる

getItem 一個だけ取得

index.js
const AWS = require("aws-sdk");
//インスタンス生成
const dynamodb = new AWS.DynamoDB({
  region: "ap-northeast-1",
  apiVersion: "2012-08-10",
  accessKeyId: "xxxxxxxxxxxxxxxx",
  secretAccessKey: "xxxxxxxxx",
});

async function test() {
  var params = {
    Key: {
     "<プライマリパーティションキー>": {
       S: "<キーの値>"
      }
    }, 
    TableName: "<TableName>"
   };
  const Data = await (() =>
    new Promise((resolve) => {
      dynamodb.getItem(params, function (err, data) {
        if (err) {
          console.error("Error occured", err);
        }
        console.log(data);
        resolve(data);
      });
    }))();

  if (Data !== undefined) {
   console.log(Data)
  }
}

test();

batchGetItem 複数取得

取得するキーの値が同じだとエラーを吐いた。

index.js
const AWS = require("aws-sdk");
//インスタンス生成
const dynamodb = new AWS.DynamoDB({
  region: "ap-northeast-1",
  apiVersion: "2012-08-10",
  accessKeyId: "xxxxxxx",
  secretAccessKey: "xxxxxxxx",
});

async function test() {
 
  var params = {
    RequestItems: {
      "<TableName>": {
        Keys: [
          {"<プライマリパーティションキー>": { S: "<値>" } },
          {id: { S: "<値>" }}
        ],
      },
    },
  };

  const Data = await (() =>
    new Promise((resolve) => {
      dynamodb.batchGetItem(params, function (err, data) {
        if (err) {
          console.error("Error occured", err);
        }
        console.log(data);
        resolve(data);
      });
    }))();

  if (Data !== undefined) {
    console.log(Data)
  }
}

test();

putItem 一個だけ追加

iindex.js
const AWS = require("aws-sdk");
//インスタンス生成
const dynamodb = new AWS.DynamoDB({
  region: "ap-northeast-1",
  apiVersion: "2012-08-10",
  accessKeyId: "xxxxxxxxxxxxxxxxx",
  secretAccessKey: "xxxxxx",
});

async function test() {
 
  var params = {
    Item: {
     "<項目>": {
       S: ""
      }, 
     "<項目>": {
       S: ""
      }, 
     "<項目>": {
       S: ""
      }
    }, 
    TableName: "<TableName>"
   };
   
  const Data = await (() =>
    new Promise((resolve) => {
      dynamodb.putItem(params, function (err, data) {
        if (err) {
          console.error("Error occured", err);
        }
        console.log(data);
        resolve(data);
      });
    }))();

  if (Data !== undefined) {
    console.log(Data)
  }
}

test();

batchWriteItem 複数追加

index.js

const AWS = require("aws-sdk");
//インスタンス生成
const dynamodb = new AWS.DynamoDB({
  region: "ap-northeast-1",
  apiVersion: "2012-08-10",
  accessKeyId: "xxxxxxxK",
  secretAccessKey: "xxx",
});

async function test() {
 
  var params = {
    RequestItems: {
     "<TableName>": [
         {
        PutRequest: {
         Item: {
          "<キー>": {
            S: "<値>"
           }, 
          "<項目>": {
            S: "<値>"
           }, 
          "<項目>": {
            S: "<値>"
           }
         }
        }
       }, 
         {
        PutRequest: {
          Item: {
            "<キー>": {
              S: "<値>"
             }, 
            "<項目>": {
              S: "<値>"
             }, 
            "<項目>": {
              S: "<値>"
             }
           }
        }
       }, 
         {
        PutRequest: {
          Item: {
            "<キー>": {
              S: "<値>"
             }, 
            "<項目>": {
              S: "<値>"
             }, 
            "<項目>": {
              S: "<値>"
             }
           }
        }
       }
      ]
    }
   };

  const Data = await (() =>
    new Promise((resolve) => {
      dynamodb.batchWriteItem(params, function (err, data) {
        if (err) {
          console.error("Error occured", err);
        }
        console.log(data);
        resolve(data);
      });
    }))();

  if (Data !== undefined) {
    console.log(Data)
  }
}

test();


listTable AWSアカウントに紐づいているテーブル一覧

index.js

const AWS = require("aws-sdk");
//インスタンス生成
const dynamodb = new AWS.DynamoDB({
  region: "ap-northeast-1",
  apiVersion: "2012-08-10",
  accessKeyId: "xxxxxxxx",
  secretAccessKey: "xxx",
});

async function test() {
 
  var params = {};

  const Data = await (() =>
    new Promise((resolve) => {
      dynamodb.listTables(params, function (err, data) {
        if (err) {
          console.error("Error occured", err);
        }
        console.log(data);
        resolve(data);
      });
    }))();

  if (Data !== undefined) {
    console.log(Data)
  }
}

test();

参考にした記事

1
3
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?