Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default function enhancements #127

Merged
merged 5 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -35,12 +35,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 @@ -1089,4 +1089,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