Skip to content

Commit

Permalink
fix(model): allows string for array attribute in contains condition
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasEK committed Apr 26, 2019
1 parent cedea45 commit f68c13a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Model.js
Expand Up @@ -47,7 +47,8 @@ async function processCondition (req, options, model) {
const val = options.conditionValues[k];
const attr = model.$__.schema.attributes[k];
if (attr) {
req.ExpressionAttributeValues[`:${k}`] = await attr.toDynamo(val, undefined, model, {'updateTimestamps': false});
const noSet = options.condition.startsWith('contains');
req.ExpressionAttributeValues[`:${k}`] = await attr.toDynamo(val, noSet, model, {'updateTimestamps': false});
} else {
throw new errors.ModelError(`Invalid condition value: ${k}. The name must either be in the schema or a full DynamoDB object must be specified.`);
}
Expand Down
33 changes: 33 additions & 0 deletions test/Model.js
Expand Up @@ -2425,6 +2425,39 @@ describe('Model', function () {
done();
});
});

it('Allows simple string for array attribute in contains condition', (done) => {
const kitten = new Cats.Cat(
{
'id': 1,
'name': 'Fluffy',
'legs': ['front right', 'front left', 'back right', 'back left']
}
);

kitten.save(() => {
const updateOptions = {
'condition': 'contains(legs, :legs)',
'conditionValues': {'legs': 'front right'}
};

Cats.Cat.update({'id': 1}, {'name': 'Puffy'}, updateOptions, (err, data) => {
should.not.exist(err);
should.exist(data);
data.id.should.eql(1);
data.name.should.equal('Puffy');
Cats.Cat.get(1, (errA, puffy) => {
should.not.exist(errA);
should.exist(puffy);
puffy.id.should.eql(1);
puffy.name.should.eql('Puffy');
done();
});
});
});

});

});

describe('Model.populate', () => {
Expand Down

0 comments on commit f68c13a

Please sign in to comment.