chai-dynamodb
文字列の中で DynamoDB 予約済ワード が使用されることをチェックする Matcher を追加する Chai プラグイン。AWS Lambda コードのテストに非常に便利です!
使用方法
また、テストを参照してください
ブラウザ側
Chai の後に chai-dynamodb を組み込みます。
<script src="chai.js"></script>
<script src="chai-dynamodb.js"></script>
サーバー側
Chai で chai-dynamodb を使用させます。
var chai = require('chai');
chai.use(require('chai-dynamodb'));
アサーション
任意の文字列で DynamoDB 予約語をチェックします。この語は #
か :
でプレフィックスが付いていません (慣例として、それぞれ式属性名と値にプレフィックスを付けるために使用されます)。このアサーションは、DynamoDB へのコールが失敗するような予約語が文字列にない場合、true になります。
noReservedWords
const safe = 'id, phone, email';
const notSafe = 'id, name, phone, email';
const usingAttributeNamePrefix = 'id, #name, phone, email';
const usingAttributeValuePrefix = 'username = :name';
// using should-style assertions
safe.should.have.noReservedWords();
notSafe.should.have.noReservedWords(); // fails
usingAttributeNamePrefix.should.have.noReservedWords();
usingAttributeValuePrefix.should.have.noReservedWords();
// using expect-style assertions
expect(safe).to.have.noReservedWords();
expect(notSafe).to.have.noReservedWords(); // fails
expect(usingAttributeNamePrefix).to.have.noReservedWords();
expect(usingAttributeValuePrefix).to.have.noReservedWords();
// using tdd assertions
assert.noReservedWords(safe);
assert.noReservedWords(notSafe); // fails
assert.noReservedWords(usingAttributeNamePrefix);
assert.noReservedWords(usingAttributeValuePrefix);
noReservedWordsExcept
特定のキーワードが意図的に使用される場合は役立ちます。引数として 1 つの文字列または文字列の配列を取ります。大文字と小文字を区別しません。
const usingAttributeNamePrefix = 'SET #name = :fullname';
const usingAttributeValuePrefix = 'SET username = :name';
const missingPrefix = 'SET username = name';
// using should-style assertions
usingAttributeNamePrefix.should.have.noReservedWordsExcept('set');
usingAttributeValuePrefix.should.have.noReservedWordsExcept('SET');
missingPrefix.should.have.noReservedWordsExcept(['SET', 'NAME']);
missingPrefix.should.have.noReservedWordsExcept('SET'); // fails
// using expect-style assertions
expect(usingAttributeNamePrefix).to.have.noReservedWordsExcept('set');
expect(usingAttributeValuePrefix).to.have.noReservedWordsExcept('SET');
expect(missingPrefix).to.have.noReservedWordsExcept(['SET', 'NAME']);
expect(missingPrefix).to.have.noReservedWordsExcept('SET'); // fails
// using tdd assertions
assert.noReservedWordsExcept(usingAttributeNamePrefix, 'set');
assert.noReservedWordsExcept(usingAttributeValuePrefix, 'SET');
assert.noReservedWordsExcept(missingPrefix, ['SET', 'NAME']);
assert.noReservedWordsExcept(missingPrefix, 'SET'); // fails
制限事項
DynamoDB は AWS で積極的に開発されている製品のため、現在の573個の予約済みリストにさらに追加される可能性があります。このライブラリは、私たち全員を悩ませる小さな間違いに対する早期の防御策として役立ちますが、それがミッションクリティカルな場合はおそらく https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html を自分でチェックする必要があります。