Skip to content

Commit

Permalink
Merge pull request dynamoose#217 from jutaz/bugfix/provide-model
Browse files Browse the repository at this point in the history
Bugfix - Provide model when converting keys .toDynamo.
  • Loading branch information
brandongoode authored Oct 25, 2017
2 parents 95e6ad5 + cd6eefe commit 50a98b2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ Model.get = function(NewModel, key, options, next) {
Key: {}
};

getReq.Key[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName]);
getReq.Key[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName], undefined, key);

if(schema.rangeKey) {
var rangeKeyName = schema.rangeKey.name;
getReq.Key[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName]);
getReq.Key[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName], undefined, key);
}

if(options.attributes) {
Expand Down Expand Up @@ -527,11 +527,11 @@ Model.update = function(NewModel, key, update, options, next) {
};
processCondition(updateReq, options, NewModel.$__.schema);

updateReq.Key[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName]);
updateReq.Key[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName], undefined, key);

if(schema.rangeKey) {
var rangeKeyName = schema.rangeKey.name;
updateReq.Key[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName]);
updateReq.Key[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName]), undefined, key;
}

// determine the set of operations to be executed
Expand Down Expand Up @@ -851,11 +851,11 @@ Model.prototype.delete = function(options, next) {
};

try {
getDelete.Key[hashKeyName] = schema.hashKey.toDynamo(this[hashKeyName]);
getDelete.Key[hashKeyName] = schema.hashKey.toDynamo(this[hashKeyName], undefined, this);

if(schema.rangeKey) {
var rangeKeyName = schema.rangeKey.name;
getDelete.Key[rangeKeyName] = schema.rangeKey.toDynamo(this[rangeKeyName]);
getDelete.Key[rangeKeyName] = schema.rangeKey.toDynamo(this[rangeKeyName], undefined, this);
}
} catch (err) {
deferred.reject(err);
Expand Down Expand Up @@ -1014,11 +1014,11 @@ Model.batchGet = function(NewModel, keys, options, next) {

getReq.Keys = keys.map(function (key) {
var ret = {};
ret[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName]);
ret[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName], undefined, key);

if(schema.rangeKey) {
var rangeKeyName = schema.rangeKey.name;
ret[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName]);
ret[rangeKeyName] = schema.rangeKey.toDynamo(key[rangeKeyName], undefined, key);
}
return ret;
});
Expand Down Expand Up @@ -1218,10 +1218,10 @@ Model.batchDelete = function(NewModel, keys, options, next) {

var batchRequests = toBatchChunks(newModel$.name, keys, MAX_BATCH_WRITE_SIZE, function(key) {
var key_element = {};
key_element[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName]);
key_element[hashKeyName] = schema.hashKey.toDynamo(key[hashKeyName]), undefined, key;

if(schema.rangeKey) {
key_element[schema.rangeKey.name] = schema.rangeKey.toDynamo(key[schema.rangeKey.name]);
key_element[schema.rangeKey.name] = schema.rangeKey.toDynamo(key[schema.rangeKey.name], undefined, key);
}

return {
Expand Down
43 changes: 42 additions & 1 deletion test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dynamoose.local();

var should = require('should');

var Cat, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7, Cat8, CatWithOwner, Owner, ExpiringCat;
var Cat, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7, Cat8, CatWithOwner, Owner, ExpiringCat, CatWithGeneratedID;

var ONE_YEAR = 365*24*60*60; // 1 years in seconds
var NINE_YEARS = 9*ONE_YEAR; // 9 years in seconds
Expand Down Expand Up @@ -199,6 +199,28 @@ describe('Model', function (){
expires: NINE_YEARS
}
);

CatWithGeneratedID = dynamoose.model('CatWithGeneratedID',
{
id: {
type: String,
default: function (model) {
return model.owner.name + '_' + model.name;
},
validate: function (value, model) {
return value === model.owner.name + '_' + model.name;
}
},
name: {
type: String,
},
owner: {
name: String,
address: String
}
}
);

done();
});

Expand Down Expand Up @@ -634,6 +656,25 @@ describe('Model', function (){
});
});

it('Should support deletions with validators', function (done) {
var cat = new CatWithGeneratedID({
owner: {
name: 'Joe',
address: 'Somewhere'
},
name: 'Garfield',
id: 'Joe_Garfield'
});
cat.delete(function (err) {
should.not.exist(err);
CatWithGeneratedID.get(cat, function (err, delCat) {
should.not.exist(err);
should.not.exist(delCat);
done();
});
});
});

it('Static Delete with range key', function (done) {
Cat2.delete({ ownerId: 666, name: 'Garfield' }, function (err) {
should.not.exist(err);
Expand Down

0 comments on commit 50a98b2

Please sign in to comment.