Skip to content

Commit

Permalink
Merge pull request #127 from vangorra/vangorra
Browse files Browse the repository at this point in the history
Default function enhancements
  • Loading branch information
brandongoode committed Apr 20, 2017
2 parents 408adb1 + 8b1526a commit 3786510
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 15 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ node_js:
env: DEBUG=dynamoose*
before_script:
- npm install -g grunt-cli
- wget http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz
- tar xfz dynamodb_local_latest.tar.gz
- java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory &

12 changes: 11 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,20 @@ Defines the attribute as a local or global secondary index. Index can either be

Applies a default to the attribute's value when saving, if the values is null or undefined.

If default is a function, the function is called, and the response is assigned to the attribute's value.
If default is a function, the function is called with the current model instance, and the response is assigned to the attribute's value.

If it is a value, the value is simply assigned.

```js
function(model) {
return model.name +'_'+ model.category;
}
```

**forceDefault: boolean**

(default: false) Will force the default value to always be applied to the attribute event if it already set. This is good for populating data that will be used as sort or secondary indexes.

**validate**: function, regular expression, or value

Validation required before for saving.
Expand Down
21 changes: 18 additions & 3 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use strict';

var DynamoDbLocal = require('dynamodb-local');
var DYNAMO_DB_PORT = 8000;

module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
Expand Down Expand Up @@ -56,12 +60,23 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mocha-test');

grunt.registerTask('dynamo:start', function() {
var done = this.async();
DynamoDbLocal
.launch(DYNAMO_DB_PORT)
.then(function() { done(); })
.catch(function(e) { done(e); });
});

grunt.registerTask('dynamo:stop', function() {
DynamoDbLocal.stop(DYNAMO_DB_PORT);
});

// Register the default tasks
grunt.registerTask('default', ['jshint', 'mochaTest']);
grunt.registerTask('default', ['jshint', 'dynamo:start', 'mochaTest']);

grunt.registerTask('test', ['jshint', 'mochaTest:test']);
grunt.registerTask('test', ['jshint', 'dynamo:start', 'mochaTest:test']);

grunt.registerTask('coverage', ['jshint', 'mochaTest:testCoverage', 'mochaTest:coverage', 'mochaTest:travis-cov']);
grunt.registerTask('coverage', ['jshint', 'dynamo:start', 'mochaTest:testCoverage', 'mochaTest:coverage', 'mochaTest:travis-cov']);

};
4 changes: 2 additions & 2 deletions lib/Attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ Attribute.prototype.applyIndexes = function(indexes) {
Attribute.prototype.setDefault = function(model) {
if (model === undefined || model === null){ return;}
var val = model[this.name];
if((val === null || val === undefined || val === '') && this.default) {
model[this.name] = this.default();
if((val === null || val === undefined || val === '' || this.options.forceDefault) && this.default) {
model[this.name] = this.default(model);
debug('Defaulted %s to %s', this.name, model[this.name]);
}
};
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
"author": "Brandon Goode <brandon@automategreen.com>",
"license": "MIT",
"devDependencies": {
"mocha": "*",
"should": "*",
"blanket": "1.1.5",
"dynamodb-local": "0.0.18",
"grunt": "*",
"grunt-contrib-jshint": "*",
"grunt-mocha-test": "*",
"blanket": "1.1.5",
"mocha": "*",
"should": "*",
"travis-cov": "*"
},
"dependencies": {
Expand Down
65 changes: 65 additions & 0 deletions test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,69 @@ describe('Model', function (){
});

});

describe('Model.default', function() {
it('Default is set properly', function() {
var CatModel = dynamoose.model('CatDefault',
{
id: {
type: Number,
validate: function (v) { return v > 0; }
},
name: String,
owner: String,
shouldRemainUnchanged: {
type: String,
default: function(model) {
return 'shouldRemainUnchanged_'+ model.name +'_'+ model.owner;
}
},
shouldBeChanged: {
type: String,
default: function(model) {
return 'shouldBeChanged_'+ model.name +'_'+ model.owner;
}
},
shouldAlwaysBeChanged: {
type: String,
default: function(model) {
return 'shouldAlwaysBeChanged_'+ model.name +'_'+ model.owner;
},
forceDefault: true
},
unsetShouldBeChanged: {
type: String,
default: function(model) {
return 'unsetShouldBeChanged_'+ model.name +'_'+ model.owner;
}
},
unsetShouldAlwaysBeChanged: {
type: String,
default: function(model) {
return 'unsetShouldAlwaysBeChanged_'+ model.name +'_'+ model.owner;
}
}
}
);

var cat = new CatModel({
id: 1111,
name: 'NAME_VALUE',
owner: 'OWNER_VALUE',
shouldRemainUnchanged: 'AAA',
shouldBeChanged: undefined,
shouldAlwaysBeChanged: 'BBB'
});

return cat
.save()
.then(function() {
should(cat.shouldRemainUnchanged).eql('AAA');
should(cat.shouldBeChanged).eql('shouldBeChanged_NAME_VALUE_OWNER_VALUE');
should(cat.shouldAlwaysBeChanged).eql('shouldAlwaysBeChanged_NAME_VALUE_OWNER_VALUE');
should(cat.unsetShouldBeChanged).eql('unsetShouldBeChanged_NAME_VALUE_OWNER_VALUE');
should(cat.unsetShouldAlwaysBeChanged).eql('unsetShouldAlwaysBeChanged_NAME_VALUE_OWNER_VALUE');
});
});
});
});
4 changes: 2 additions & 2 deletions test/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ describe('Table tests', function () {
done(err);
}
else {
console.log("---------------------REVISED TABLE");
console.log(JSON.stringify(data, null, 2));
// console.log("---------------------REVISED TABLE");
// console.log(JSON.stringify(data, null, 2));
var found = false;
for (var i in data.Table.GlobalSecondaryIndexes) {
var gsi = data.Table.GlobalSecondaryIndexes[i];
Expand Down

0 comments on commit 3786510

Please sign in to comment.