Comparing version 0.9.1 to 0.9.2
@@ -5,2 +5,3 @@ var typ = require('typ') | ||
var Builder = require('./Builder') | ||
var IndexNotExistError = require('./errors').IndexNotExistError | ||
@@ -17,2 +18,15 @@ /** | ||
/** | ||
* If this scan runs on a local index or global index, set a | ||
* function that can generate an index name based on query | ||
* conditions. | ||
* | ||
* @param {function(string, string): string} fn The generator function | ||
*/ | ||
ScanBuilder.prototype.setIndexNameGenerator = function (fn) { | ||
this._indexNameGenerator = fn | ||
return this | ||
} | ||
ScanBuilder.prototype.setStartKey = function (key) { | ||
@@ -54,3 +68,3 @@ this._startKey = key | ||
ScanBuilder.prototype.execute = function () { | ||
var queryData = new DynamoRequest(this.getOptions()) | ||
var query = new DynamoRequest(this.getOptions()) | ||
.setTable(this._tablePrefix, this._table) | ||
@@ -63,4 +77,14 @@ .returnConsumedCapacity() | ||
.setParallelScan(this._segment, this._totalSegments) | ||
.build() | ||
if (this._indexNameGenerator) { | ||
var rangeKeyName = this._rangeKey ? this._rangeKey.name : '' | ||
var indexName = this._indexNameGenerator(this._hashKey.name, rangeKeyName) | ||
if (!indexName) { | ||
throw new IndexNotExistError(this._hashKey.name, this._rangeKey.name) | ||
} | ||
query.setIndexName(indexName) | ||
} | ||
var queryData = query.build() | ||
return this.request("scan", queryData) | ||
@@ -67,0 +91,0 @@ .then(this.prepareOutput.bind(this)) |
{ | ||
"name": "dynamite", | ||
"description": "promise-based DynamoDB client", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"homepage": "https://github.com/Medium/dynamite", | ||
@@ -12,3 +12,4 @@ "license": "Apache-2.0", | ||
"Artem Titoulenko <artem.titoulenko@gmail.com> (https://github.com/ArtemTitoulenko)", | ||
"Xiao Ma <xiao@medium.com> (https://github.com/x-ma)" | ||
"Xiao Ma <xiao@medium.com> (https://github.com/x-ma)", | ||
"Jamie Talbot <jamie@medium.com> (https://github.com/majelbstoat)" | ||
], | ||
@@ -15,0 +16,0 @@ "keywords": [ |
@@ -23,3 +23,3 @@ // Copyright 2013 The Obvious Corporation. | ||
utils.ensureLocalDynamo() | ||
utils.createTable(this.db, tableName, "userId", "column") | ||
utils.createTable(this.db, tableName, "userId", "column", [{hashKey: "post", hashKeyType: "N"}, {hashKey: "post", hashKeyType: "N", rangeKey: "email"}, {hashKey: "description"}]) | ||
.thenBound(utils.initTable, null, {"db": this.db, "tableName": tableName, "data": rawData}) | ||
@@ -46,6 +46,6 @@ .fail(onError) | ||
.then(function (data) { | ||
test.equal(data.result.length, expect.length, expect.length + " records should be returned") | ||
test.equal(data.result.length, expect.length, expect.length + " records should be returned, got " + data.result.length) | ||
data.result.sort(function(a, b) {return (a.userId < b.userId) ? -1 : ((a.userId > b.userId) ? 1 : 0)}) | ||
for (var i = 0; i < data.result.length; i++) { | ||
test.deepEqual(data.result[i], rawData[expect[i]], "Some records are wrong") | ||
test.deepEqual(data.result[i], rawData[expect[i]]) | ||
} | ||
@@ -67,2 +67,25 @@ }) | ||
builder.add(function testScanOnGlobalSecondaryIndex(test) { | ||
var scan = this.client.newScanBuilder(tableName) | ||
.setIndexNameGenerator(utils.indexNameGenerator) | ||
.setHashKey('post') | ||
.setRangeKey('email') | ||
return scanAndCheck(scan, [0, 2, 5], test) | ||
}) | ||
builder.add(function testScanOnGlobalSecondaryIndexWithoutRangeKey(test) { | ||
var scan = this.client.newScanBuilder(tableName) | ||
.setIndexNameGenerator(utils.indexNameGenerator) | ||
.setHashKey('description') | ||
return scanAndCheck(scan, [5], test) | ||
}) | ||
builder.add(function testParallelScanOnGlobalSecondaryIndex(test) { | ||
var scan = this.client.newScanBuilder(tableName) | ||
.setIndexNameGenerator(utils.indexNameGenerator) | ||
.setHashKey('post') | ||
.setParallelScan(1, 2) | ||
return scanAndCheck(scan, [0, 2, 5, 6], test) | ||
}) | ||
// test filtering with post == 2 | ||
@@ -69,0 +92,0 @@ builder.add(function testFilterByEqual(test) { |
@@ -78,2 +78,14 @@ /** | ||
/* | ||
* A helper to generate an index name for testing indices. | ||
* | ||
* @param hashKey {string} | ||
* @param rangeKey {string} | ||
*/ | ||
utils.indexNameGenerator = function (hashKey, rangeKey) { | ||
var name = 'index-' + hashKey | ||
if (rangeKey) name = name + '-' + rangeKey | ||
return name | ||
} | ||
/* | ||
* A helper function that creates the testing table. | ||
@@ -84,3 +96,3 @@ * | ||
*/ | ||
utils.createTable = function (db, tableName, hashKey, rangeKey) { | ||
utils.createTable = function (db, tableName, hashKey, rangeKey, gsiDefinitions) { | ||
var defer = Q.defer() | ||
@@ -103,7 +115,7 @@ var opts = {} | ||
} else if (apiVersion === AWSName.API_VERSION_2012) { | ||
var attributeDefinitions = {} | ||
attributeDefinitions[hashKey] = "S" | ||
opts = { | ||
TableName: tableName, | ||
AttributeDefinitions: [ | ||
{AttributeName: hashKey, AttributeType: "S"} | ||
], | ||
AttributeDefinitions: [], | ||
KeySchema: [ | ||
@@ -116,6 +128,3 @@ {AttributeName: hashKey, KeyType: "HASH"} | ||
if (rangeKey) { | ||
opts.AttributeDefinitions.push({ | ||
AttributeName: rangeKey, | ||
AttributeType: "S" | ||
}) | ||
attributeDefinitions[rangeKey] = "S" | ||
opts.KeySchema.push({ | ||
@@ -127,2 +136,31 @@ AttributeName: rangeKey, | ||
if (gsiDefinitions) { | ||
opts.GlobalSecondaryIndexes = gsiDefinitions.map(function (index) { | ||
var keySchema = [ | ||
{AttributeName: index.hashKey, KeyType: "HASH"} | ||
] | ||
var hashKeyType = index.hashKeyType || "S" | ||
attributeDefinitions[index.hashKey] = hashKeyType | ||
if (index.rangeKey) { | ||
var rangeKeyType = index.rangeKeyType || "S" | ||
keySchema.push({AttributeName: index.rangeKey, KeyType: "RANGE"}) | ||
attributeDefinitions[index.rangeKey] = rangeKeyType | ||
} | ||
return { | ||
IndexName: utils.indexNameGenerator(index.hashKey, index.rangeKey), | ||
KeySchema: keySchema, | ||
Projection: { | ||
ProjectionType: "ALL" | ||
}, | ||
ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1} | ||
} | ||
}) | ||
} | ||
for (var field in attributeDefinitions) { | ||
opts.AttributeDefinitions.push({AttributeName: field, AttributeType: attributeDefinitions[field]}) | ||
} | ||
db.createTable(opts, defer.makeNodeResolver()) | ||
@@ -129,0 +167,0 @@ } else { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
254842
7021