1
0

More than 3 years have passed since last update.

Node.jsからDynamoDBのテーブルの作成・削除と行追加・読み取り

Posted at

表題の通りNode.jsからDynamoDBのテーブルの作成・削除と行追加・読み取りをメモします。
動作確認した後Qiita掲載用に少々手を入れているのでもしかしたら動かないかもしれません。

テーブルの作成

プライマリキーに「accessKey」を指定する場合

const AWS = require("aws-sdk");
AWS.config.update({
    region: "ap-northeast-1"
});
// DynamoDB Local使うならendpointの指定が必要
const dynamo_opt = {
    apiVersion: '2012-08-10',
    endpoint: `http://localhost:8000`
};
const dynamodb = new AWS.DynamoDB(dynamo_opt);

const dynamoCreateParams = {
    TableName : "テーブル名",
    KeySchema: [
        { AttributeName: "accessKey", KeyType: "HASH"},
    ],
    AttributeDefinitions: [
        { AttributeName: "accessKey", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(dynamoCreateParams, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data));
    }
});

参考: テーブルの作成 - Amazon DynamoDB

行追加

プライマリキーのaccessKeyに加えuserIdも含めた行を3行追加する。

cost tableName = "テーブル名";
var dynamoDataParams = [
    {
        TableName: tableName,
        Item: {
            "accessKey": "hoge",
            "userId": 1
        }
    },
    {
        TableName: tableName,
        Item: {
            "accessKey": "hage",
            "userId": 2
        }
    },
    {
        TableName: tableName,
        Item: {
            "accessKey": "fuga",
            "userId": 3
        }
    }
];

var ddbClient = new AWS.DynamoDB.DocumentClient(dynamo_opt)
var promises = [];
for(let i = 0; i < dynamoDataParams.length; i++) {
    (function(i) {
        promises.push(ddbClient.put(dynamoDataParams[i]).promise());
    })(i);
}

Promise.all(promises).then(function() {
    console.log('done');
})

行の読み取り

const dynamo_opt = {
    apiVersion: '2012-08-10',
    endpoint: `http://localhost:8000`
};
const dynamo_params = {
    TableName: "テーブル名",
    KeyConditionExpression: "accessKey = :a",
    ExpressionAttributeValues: {
        ":a": "検索文字列"
    }
};
const ddb = new AWS.DynamoDB.DocumentClient(dynamo_opt);

ddb.query(dynamo_params, function(err, data) {
    if (err) {
        console.error('Unable to query:' + JSON.stringify(err));
    } else {
        console.log(JSON.stringify(data));
    }
});

テーブルの削除

const AWS = require("aws-sdk");
AWS.config.update({
    region: "ap-northeast-1"
});
// DynamoDB Local使うならendpointの指定が必要
const dynamo_opt = {
    apiVersion: '2012-08-10',
    endpoint: `http://localhost:8000`
};
const dynamodb = new AWS.DynamoDB(dynamo_opt);

dynamodb.deleteTable({TableName: tableName}, function(err, data) {
    if (err) {
        console.log("Unable to Delete table. Error JSON:", JSON.stringify(err));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data));
    }
});

参考: テーブルの削除 - Amazon DynamoDB

1
0
0

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
0