2
2

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.

LambdaでGSI(とか)をQueryする【忘備録】

Last updated at Posted at 2019-08-29

なんか情報少なくね

という訳で忘備録として投稿

前提

  • Lambda(Node.js)

やり方

index.js
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient({
    region: 'ap-northeast-1'//リージョン
});
exports.handler = async (event) => {
let params = {
        TableName: TABLE_NAME,//テーブル名を指定
        IndexName: INDEX_NAME,//インデックス名を指定
        ExpressionAttributeNames:{'#hoge': 'hoge','#time': 'time'},
        ExpressionAttributeValues:{':val': 'foo',':time': '0'},
        KeyConditionExpression: '#hoge = :val and #time >= :time'//上の2文はプレースホルダー
    };
const request = await dynamo.query(params).promise();
return request.Count;
}

以上。IndexNameでインデックスを指定するのが肝

おまけ

まとめて書き込みたいときはbatchWriteが便利
ドキュメント等をみるとコールバックで処理するのが多いですが、await 〇〇.promise();で同期処理できてコールバック地獄にならない。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?