-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add listTables method - resolves #10
- Loading branch information
1 parent
19e686a
commit a599769
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This class forms the builder pattern for listing all the tables. | ||
* | ||
* @author Sam Verschueren <sam.verschueren@gmail.com> | ||
* @since 24 Jan. 2015 | ||
*/ | ||
|
||
// module dependencies | ||
var Promise = require('pinkie-promise'); | ||
var pify = require('pify'); | ||
|
||
/** | ||
* Creates a new list tables object that can then be used to built the entire | ||
* request. | ||
* | ||
* @param {DynamoDB} dynamodb The dynamodb object. | ||
*/ | ||
function ListTables(dynamodb) { | ||
this._dynamodb = dynamodb; | ||
} | ||
|
||
/** | ||
* This method will execute the list table request that was built up. | ||
* | ||
* @return {Promise} The promise object that resolves all the tables. | ||
*/ | ||
ListTables.prototype.exec = function () { | ||
var db = this._dynamodb._dynamodb; | ||
var prefix = this._dynamodb._prefix; | ||
|
||
if (!db) { | ||
return Promise.reject(new Error('Call .connect() before executing queries.')); | ||
} | ||
|
||
function execHelper(result, params) { | ||
result = result || []; | ||
params = params || {}; | ||
|
||
return pify(db.service.listTables.bind(db.service), Promise)(params) | ||
.then(function (data) { | ||
result = result.concat(data.TableNames); | ||
|
||
if (data.LastEvaluatedTableName) { | ||
params.ExclusiveStartTableName = data.LastEvaluatedTableName; | ||
return execHelper(result, params); | ||
} | ||
|
||
return prefix === undefined ? result : result.filter(function (table) { | ||
return table.indexOf(prefix) === 0; | ||
}); | ||
}); | ||
} | ||
|
||
return execHelper(); | ||
}; | ||
|
||
// Export the module | ||
module.exports = ListTables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import test from 'ava'; | ||
import sinon from 'sinon'; | ||
import db from '../../'; | ||
|
||
db.connect(); | ||
|
||
test.before(() => { | ||
const stub = sinon.stub(db._dynamodb.service, 'listTables'); | ||
stub.onFirstCall().yields(undefined, {LastEvaluatedTableName: 'test.baz', TableNames: ['test.baz']}); | ||
stub.yields(undefined, {TableNames: ['test.foo', 'test.bar', 'prod.foo']}); | ||
}); | ||
|
||
test.after(() => { | ||
db._dynamodb.service.listTables.restore(); | ||
}); | ||
|
||
test.serial('result', async t => { | ||
t.same(await db.listTables().exec(), ['test.baz', 'test.foo', 'test.bar', 'prod.foo']); | ||
}); | ||
|
||
test.serial('filter result on prefix', async t => { | ||
db._prefix = 'test'; | ||
|
||
t.same(await db.listTables().exec(), ['test.foo', 'test.bar']); | ||
|
||
db._prefix = undefined; | ||
}); | ||
|
||
test.serial('error if not connected', async t => { | ||
const original = db._dynamodb; | ||
db._dynamodb = undefined; | ||
|
||
await t.throws(db.listTables().exec(), 'Call .connect() before executing queries.'); | ||
|
||
db._dynamodb = original; | ||
}); |