Skip to content
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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ language: node_js
node_js:
- "0.11"
- "0.10"
after_script:
- npm run coverage && cat ./coverage/lcov.info | ./node_modules/.bin/codeclimate
addons:
code_climate:
repo_token: 351483555263cf9bcd2416c58b0e0ae6ca1b32438aa51bbab2c833560fb67cc0
2 changes: 1 addition & 1 deletion lib/waterline/collection/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*/
module.exports = {

migrate: 'alter'
migrate: 'safe'

};
18 changes: 12 additions & 6 deletions lib/waterline/core/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,20 @@ Schema.prototype.objectAttribute = function(attrName, value) {
*/

Schema.prototype.cleanValues = function(values) {

var clone = {};

// Return if hasSchema === false
if(!this.hasSchema) return values;

for(var key in values) {
if(hasOwnProperty(this.schema, key)) clone[key] = values[key];
for (var key in values) {

// The value can pass through if either the collection does have a schema and the key is in the schema,
// or otherwise if the collection is schemaless and the key does not represent an associated collection.
if ((this.hasSchema && hasOwnProperty(this.schema, key)) ||
(!this.hasSchema && !(hasOwnProperty(this.context._attributes, key) && hasOwnProperty(this.context._attributes[key], 'collection')))) {

clone[key] = values[key];
}

}

return clone;
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"waterline-schema": "~0.1.17"
},
"devDependencies": {
"codeclimate-test-reporter": "0.0.4",
"istanbul": "^0.3.11",
"mocha": "~2.1.0",
"sails-memory": "balderdashy/sails-memory",
Expand Down
1 change: 1 addition & 0 deletions test/integration/Collection.ddl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('calling describe', function() {
bootstrapCollection({
adapter: Adapter,
properties: {
migrate: 'alter',
attributes: {
name: 'string',
age: 'integer'
Expand Down
99 changes: 99 additions & 0 deletions test/unit/core/core.schema/schema.cleanValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var Waterline = require('../../../../lib/waterline'),
assert = require('assert');

describe('Core Schema', function() {

describe('cleanValues method', function() {
var user;
var userschemaless;

before(function(done) {
var waterline = new Waterline();

var UserSchema = Waterline.Collection.extend({
identity: 'user',
connection: 'foo',
schema: true,
attributes: {
name: {
type: 'string',
defaultsTo: 'Foo Bar'
},
age: {
type: 'integer',
},
schemalessFriends: {
collection: 'userschemaless',
via: 'schemaFriends'
}
}
});

var UserSchemaless = Waterline.Collection.extend({
identity: 'userschemaless',
connection: 'foo',
schema: false,
attributes: {
name: {
type: 'string',
defaultsTo: 'Foo Bar'
},
age: {
type: 'integer',
},
schemaFriends: {
collection: 'user',
via: 'schemalessFriends'
}
}
});

waterline.loadCollection(UserSchema);
waterline.loadCollection(UserSchemaless);

var connections = {
'foo': {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: {} }, connections: connections }, function(err, colls) {
if(err) return done(err);
user = colls.collections.user;
userschemaless = colls.collections.userschemaless;
done();
});
});

it('when collection is schemaless, should only remove collection attributes.', function() {

var rawValues = {
name: 'don-moe',
non: 'should be here',
schemaFriends: []
}

var cleanValues = userschemaless._schema.cleanValues(rawValues);

assert.equal(cleanValues.name, 'don-moe');
assert.equal(cleanValues.non, 'should be here');
assert.equal(cleanValues.schemaFriends, undefined);
});

it('when collection has schema, should clean attributes not in the schema, including collection attributes.', function() {

var rawValues = {
name: 'don-moe',
non: 'should be here',
schemalessFriends: []
}

var cleanValues = user._schema.cleanValues(rawValues);

assert.equal(cleanValues.name, 'don-moe');
assert.equal(cleanValues.non, undefined);
assert.equal(cleanValues.schemalessFriends, undefined);
});
});

});