Skip to content

Commit

Permalink
add listTables method - resolves #10
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jan 24, 2016
1 parent 19e686a commit a599769
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/DynamoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var AWS = require('aws-sdk');
var pick = require('object-pick');
var Table = require('./Table');
var ListTables = require('./methods/ListTables');

module.exports = {
/**
Expand Down Expand Up @@ -49,6 +50,14 @@ module.exports = {
// Create a table and initialize
return this.table(schema.TableName).create(schema);
},
/**
* Method that will instantiate a list tables object.
*
* @return {ListTables} A ListTables object.
*/
listTables: function () {
return new ListTables(this);
},
/**
* Initializes the database settings.
*
Expand Down
60 changes: 60 additions & 0 deletions lib/methods/ListTables.js
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;
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ Employee.findOneAndRemove({Organisation: 'Amazon', Email: 'john.doe@amazon.com'}
});
```
### List all the tables
You can retrieve a list of all the tables.
```js
db.listTables().exec().then(tables => {
console.log(tables);
//=> ['foo', 'bar', 'baz']
});
```
If you passed in a `prefix` property in the connection object, only the tables with that prefix will
be returned.
### Create a table
A table can be created by either calling `create()` on a table instance or by calling `createTable` on the database instance.
Expand Down
36 changes: 36 additions & 0 deletions test/methods/listTables.js
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;
});

0 comments on commit a599769

Please sign in to comment.