ITあれこれ弄り道

IT系のあれこれを適当に弄ってみよう的な

DynamoDBでちょっとハマった話

Bluemixのドキュメント更新の遅さに辟易して浮気してます。

んで、AWSでNode.js使ってあれこれしようとしてる訳ですが・・・

 

ローカルでDynamoDBを動かしてて、まずテーブル作成だなとこんなコード書いてみた。

const AWS = require('aws-sdk');

 

AWS.config.update({

    region: 'ap-eastwest-1',

    endpoint: 'http://localhost:8000'

});

const db = new AWS.DynamoDB();

 

const params = {

    TableName: 'Users',

    KeySchema: [

        { AttributeName: 'email', KeyType: 'HASH' },

        { AttributeName: 'name', KeyType: 'RANGE' },

    ],

    AttributeDefinitions: [

        { AttributeName: 'email', AttributeType: 'S' },

        { AttributeName: 'name', AttributeType: 'S' },

        { AttributeName: 'password', AttributeType: 'S' }

    ],

    ProvisionedThroughput: {

        ReadCapacityUnits: 10,

        WriteCapacityUnits: 10

    },

};

 

db.createTable(params, function(err, data) {

    if (err) {

        console.error('Unable to create table. Error JSON: ' + JSON.stringify(err, null, 2));

    } else {

        console.log('Created table. Table description JSON: ' + JSON.stringify(data, null, 2));

    }

});

 

で、結果がコレ

The number of attributes in key schema must match the number of attributesdefined in attribute definitions.

 

つまり、キースキーマと属性は一対一じゃないとダメよって事で。

んで、「は?全部の属性にソートキーつけろって事かよ」って考えたSQL脳。

 

よくよくドキュメント読んでみると、

AttributeDefinitions – キースキーマ属性のデータ型。

直訳して「属性の定義」だから、カラムを全部定義するんだと勘違いしてたが、要はプライマリキーとソートキー以外はテーブル作成時に定義する必要ないよ、と。

 

コードからpasswordの行を削除すると、無事通りました。ヤレヤレ