Comparing version 0.3.1 to 0.3.2
@@ -56,4 +56,10 @@ 'use strict'; | ||
Model.getItems = batch(table, serializer).getItems; | ||
Model.batchGetItems = batch(table, serializer).getItems; | ||
// table ddl methods | ||
Model.createTable = _.bind(table.createTable, table); | ||
Model.updateTable = _.bind(table.updateTable, table); | ||
Model.describeTable = _.bind(table.describeTable, table); | ||
table.itemFactory = Model; | ||
@@ -60,0 +66,0 @@ |
@@ -30,3 +30,3 @@ 'use strict'; | ||
internals.baseSetup = function (schema, attrName, type, options) { | ||
internals.baseSetup = function (schema, attrName, type, attributeType, options) { | ||
internals.parseOptions(schema, attrName, options); | ||
@@ -36,2 +36,3 @@ | ||
type: type, | ||
dynamoType : attributeType, | ||
options: options || {} | ||
@@ -88,23 +89,29 @@ }; | ||
Schema.prototype.String = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.String(), options); | ||
var attributeType = 'S'; | ||
return internals.baseSetup(this, attrName, Schema.types.String(), attributeType, options); | ||
}; | ||
Schema.prototype.Number = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.Number(), options); | ||
var attributeType = 'N'; | ||
return internals.baseSetup(this, attrName, Schema.types.Number(), attributeType, options); | ||
}; | ||
Schema.prototype.Boolean = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.Boolean(), options); | ||
var attributeType = 'N'; | ||
return internals.baseSetup(this, attrName, Schema.types.Boolean(), attributeType, options); | ||
}; | ||
Schema.prototype.Date = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.Date(), options); | ||
var attributeType = 'S'; | ||
return internals.baseSetup(this, attrName, Schema.types.Date(), attributeType, options); | ||
}; | ||
Schema.prototype.StringSet = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.StringSet(), options); | ||
var attributeType = 'SS'; | ||
return internals.baseSetup(this, attrName, Schema.types.StringSet(), attributeType, options); | ||
}; | ||
Schema.prototype.NumberSet = function (attrName, options) { | ||
return internals.baseSetup(this, attrName, Schema.types.NumberSet(), options); | ||
var attributeType = 'NS'; | ||
return internals.baseSetup(this, attrName, Schema.types.NumberSet(), attributeType, options); | ||
}; | ||
@@ -115,3 +122,4 @@ | ||
return internals.baseSetup(this, attrName, Schema.types.UUID(), opts); | ||
var attributeType = 'S'; | ||
return internals.baseSetup(this, attrName, Schema.types.UUID(), attributeType, opts); | ||
}; | ||
@@ -122,3 +130,4 @@ | ||
return internals.baseSetup(this, attrName, Schema.types.TimeUUID(), opts); | ||
var attributeType = 'S'; | ||
return internals.baseSetup(this, attrName, Schema.types.TimeUUID(), attributeType, opts); | ||
}; | ||
@@ -125,0 +134,0 @@ |
@@ -205,2 +205,4 @@ 'use strict'; | ||
serializer.serializeItem = function (schema, item, options) { | ||
options = options || {}; | ||
if(!item) { | ||
@@ -207,0 +209,0 @@ return null; |
@@ -245,1 +245,100 @@ 'use strict'; | ||
}; | ||
internals.attributeDefinition = function (schema, key) { | ||
return { | ||
AttributeName : key, | ||
AttributeType : schema.attrs[key].dynamoType | ||
}; | ||
}; | ||
internals.keySchema = function (hashKey, rangeKey) { | ||
var result = [{ | ||
AttributeName : hashKey, | ||
KeyType : 'HASH' | ||
}]; | ||
if(rangeKey) { | ||
result.push({ | ||
AttributeName : rangeKey, | ||
KeyType : 'RANGE' | ||
}); | ||
} | ||
return result; | ||
}; | ||
internals.secondaryIndex = function (schema, indexKey) { | ||
var indexName = indexKey + 'Index'; | ||
return { | ||
IndexName : indexName, | ||
KeySchema : internals.keySchema(schema.hashKey, indexKey), | ||
Projection : { | ||
ProjectionType : 'ALL' | ||
} | ||
}; | ||
}; | ||
Table.prototype.createTable = function (options, callback) { | ||
var self = this; | ||
if (typeof options === 'function' && !callback) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var attributeDefinitions = []; | ||
attributeDefinitions.push(internals.attributeDefinition(self.schema, self.schema.hashKey)); | ||
if(self.schema.rangeKey) { | ||
attributeDefinitions.push(internals.attributeDefinition(self.schema, self.schema.rangeKey)); | ||
} | ||
var localSecondaryIndexes = []; | ||
_.forEach(self.schema.secondaryIndexes, function (key) { | ||
attributeDefinitions.push(internals.attributeDefinition(self.schema, key)); | ||
localSecondaryIndexes.push(internals.secondaryIndex(self.schema, key)); | ||
}); | ||
var keySchema = internals.keySchema(self.schema.hashKey, self.schema.rangeKey); | ||
var params = { | ||
AttributeDefinitions : attributeDefinitions, | ||
TableName : self.config.name, | ||
KeySchema : keySchema, | ||
ProvisionedThroughput : { | ||
ReadCapacityUnits : options.readCapacity || 1, | ||
WriteCapacityUnits : options.writeCapacity || 1 | ||
} | ||
}; | ||
if(localSecondaryIndexes.length >= 1) { | ||
params.LocalSecondaryIndexes = localSecondaryIndexes; | ||
} | ||
self.dynamodb.createTable(params, callback); | ||
}; | ||
Table.prototype.describeTable = function (callback) { | ||
var params = { | ||
TableName : this.config.name | ||
}; | ||
this.dynamodb.describeTable(params, callback); | ||
}; | ||
Table.prototype.updateTable = function (throughput, callback) { | ||
callback = callback || function () {}; | ||
var params = { | ||
TableName : this.config.name, | ||
ProvisionedThroughput : { | ||
ReadCapacityUnits : throughput.readCapacity, | ||
WriteCapacityUnits : throughput.writeCapacity | ||
} | ||
}; | ||
this.dynamodb.updateTable(params, callback); | ||
}; |
{ | ||
"name": "vogels", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"author": "Ryan Fitzgerald <ryan@codebrewstudios.com>", | ||
@@ -5,0 +5,0 @@ "description": "DynamoDB data mapper", |
@@ -9,4 +9,8 @@ 'use strict'; | ||
Scan = require('../lib//scan'), | ||
Item = require('../lib/item'); | ||
Item = require('../lib/item'), | ||
chai = require('chai'), | ||
expect = chai.expect; | ||
chai.should(); | ||
describe('table', function () { | ||
@@ -557,2 +561,149 @@ var schema, | ||
describe('#createTable', function () { | ||
it('should create table with hash key', function (done) { | ||
schema.String('email', {hashKey: true}); | ||
schema.String('name'); | ||
table = new Table('accounts', schema, serializer, dynamodb); | ||
var request = { | ||
TableName: 'accounts', | ||
AttributeDefinitions : [ | ||
{ AttributeName: 'email', AttributeType: 'S' } | ||
], | ||
KeySchema: [ | ||
{ AttributeName: 'email', KeyType: 'HASH' } | ||
], | ||
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } | ||
}; | ||
dynamodb.createTable.yields(null, {}); | ||
table.createTable({readCapacity : 5, writeCapacity: 5}, function (err) { | ||
expect(err).to.be.null; | ||
dynamodb.createTable.calledWith(request).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
it('should create table with range key', function (done) { | ||
schema.String('name', {hashKey: true}); | ||
schema.String('email', {rangeKey: true}); | ||
table = new Table('accounts', schema, serializer, dynamodb); | ||
var request = { | ||
TableName: 'accounts', | ||
AttributeDefinitions : [ | ||
{ AttributeName: 'name', AttributeType: 'S' }, | ||
{ AttributeName: 'email', AttributeType: 'S' } | ||
], | ||
KeySchema: [ | ||
{ AttributeName: 'name', KeyType: 'HASH' }, | ||
{ AttributeName: 'email', KeyType: 'RANGE' } | ||
], | ||
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } | ||
}; | ||
dynamodb.createTable.yields(null, {}); | ||
table.createTable({readCapacity : 5, writeCapacity: 5}, function (err) { | ||
expect(err).to.be.null; | ||
dynamodb.createTable.calledWith(request).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
it('should create table with secondary index', function (done) { | ||
schema.String('name', {hashKey: true}); | ||
schema.String('email', {rangeKey: true}); | ||
schema.Number('age', {secondaryIndex: true}); | ||
table = new Table('accounts', schema, serializer, dynamodb); | ||
var request = { | ||
TableName: 'accounts', | ||
AttributeDefinitions : [ | ||
{ AttributeName: 'name', AttributeType: 'S' }, | ||
{ AttributeName: 'email', AttributeType: 'S' }, | ||
{ AttributeName: 'age', AttributeType: 'N' } | ||
], | ||
KeySchema: [ | ||
{ AttributeName: 'name', KeyType: 'HASH' }, | ||
{ AttributeName: 'email', KeyType: 'RANGE' } | ||
], | ||
LocalSecondaryIndexes : [ | ||
{ | ||
IndexName : 'ageIndex', | ||
KeySchema: [ | ||
{ AttributeName: 'name', KeyType: 'HASH' }, | ||
{ AttributeName: 'age', KeyType: 'RANGE' } | ||
], | ||
Projection : { | ||
ProjectionType : 'ALL' | ||
} | ||
} | ||
], | ||
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } | ||
}; | ||
dynamodb.createTable.yields(null, {}); | ||
table.createTable({readCapacity : 5, writeCapacity: 5}, function (err) { | ||
expect(err).to.be.null; | ||
dynamodb.createTable.calledWith(request).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#describeTable', function () { | ||
it('should make describe table request', function (done) { | ||
schema.String('email', {hashKey: true}); | ||
schema.String('name'); | ||
table = new Table('accounts', schema, serializer, dynamodb); | ||
var request = { | ||
TableName: 'accounts' | ||
}; | ||
dynamodb.describeTable.yields(null, {}); | ||
table.describeTable(function (err) { | ||
expect(err).to.be.null; | ||
dynamodb.describeTable.calledWith(request).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#updateTable', function () { | ||
it('should make update table request', function (done) { | ||
schema.String('email', {hashKey: true}); | ||
schema.String('name'); | ||
table = new Table('accounts', schema, serializer, dynamodb); | ||
var request = { | ||
TableName: 'accounts', | ||
ProvisionedThroughput: { ReadCapacityUnits: 4, WriteCapacityUnits: 2 } | ||
}; | ||
dynamodb.updateTable.yields(null, {}); | ||
table.updateTable({readCapacity: 4, writeCapacity: 2}, function (err) { | ||
expect(err).to.be.null; | ||
dynamodb.updateTable.calledWith(request).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -8,8 +8,11 @@ 'use strict'; | ||
var dynamodb = { | ||
scan : sinon.stub(), | ||
putItem : sinon.stub(), | ||
deleteItem : sinon.stub(), | ||
query : sinon.stub(), | ||
getItem : sinon.stub(), | ||
updateItem : sinon.stub() | ||
scan : sinon.stub(), | ||
putItem : sinon.stub(), | ||
deleteItem : sinon.stub(), | ||
query : sinon.stub(), | ||
getItem : sinon.stub(), | ||
updateItem : sinon.stub(), | ||
createTable : sinon.stub(), | ||
describeTable : sinon.stub(), | ||
updateTable : sinon.stub() | ||
}; | ||
@@ -16,0 +19,0 @@ |
138230
38
3258
9