Merge pull request #338 from fabien/fix/fields-vs-defaults
Don't apply defaults when fields are specified
This commit is contained in:
commit
11963ea12a
32
lib/model.js
32
lib/model.js
|
@ -53,7 +53,7 @@ function ModelBaseClass(data, options) {
|
||||||
ModelBaseClass.prototype._initProperties = function (data, options) {
|
ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var ctor = this.constructor;
|
var ctor = this.constructor;
|
||||||
|
|
||||||
if(data instanceof ctor) {
|
if(data instanceof ctor) {
|
||||||
// Convert the data to be plain object to avoid pollutions
|
// Convert the data to be plain object to avoid pollutions
|
||||||
data = data.toObject(false);
|
data = data.toObject(false);
|
||||||
|
@ -73,10 +73,6 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
strict = ctor.definition.settings.strict;
|
strict = ctor.definition.settings.strict;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.persisted !== undefined) {
|
|
||||||
this.__persisted = options.persisted === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctor.hideInternalProperties) {
|
if (ctor.hideInternalProperties) {
|
||||||
// Object.defineProperty() is expensive. We only try to make the internal
|
// Object.defineProperty() is expensive. We only try to make the internal
|
||||||
// properties hidden (non-enumerable) if the model class has the
|
// properties hidden (non-enumerable) if the model class has the
|
||||||
|
@ -110,6 +106,13 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
value: strict
|
value: strict
|
||||||
|
},
|
||||||
|
|
||||||
|
__persisted: {
|
||||||
|
writable: true,
|
||||||
|
enumerable: false,
|
||||||
|
configurable: true,
|
||||||
|
value: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -117,6 +120,11 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
this.__data = {};
|
this.__data = {};
|
||||||
this.__dataSource = options.dataSource;
|
this.__dataSource = options.dataSource;
|
||||||
this.__strict = strict;
|
this.__strict = strict;
|
||||||
|
this.__persisted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.persisted !== undefined) {
|
||||||
|
this.__persisted = options.persisted === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.__cachedRelations) {
|
if (data.__cachedRelations) {
|
||||||
|
@ -124,6 +132,13 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys = Object.keys(data);
|
var keys = Object.keys(data);
|
||||||
|
|
||||||
|
if (Array.isArray(options.fields)) {
|
||||||
|
keys = keys.filter(function(k) {
|
||||||
|
return (options.fields.indexOf(k) != -1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var size = keys.length;
|
var size = keys.length;
|
||||||
var p, propVal;
|
var p, propVal;
|
||||||
for (var k = 0; k < size; k++) {
|
for (var k = 0; k < size; k++) {
|
||||||
|
@ -169,6 +184,13 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
keys = Object.keys(properties);
|
keys = Object.keys(properties);
|
||||||
|
|
||||||
|
if (Array.isArray(options.fields)) {
|
||||||
|
keys = keys.filter(function(k) {
|
||||||
|
return (options.fields.indexOf(k) != -1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
size = keys.length;
|
size = keys.length;
|
||||||
|
|
||||||
for (k = 0; k < size; k++) {
|
for (k = 0; k < size; k++) {
|
||||||
|
|
|
@ -35,4 +35,18 @@ describe('defaults', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ignore defaults with limited fields', function (done) {
|
||||||
|
Server.create({ host: 'localhost', port: 8080 }, function(err, s) {
|
||||||
|
should.not.exist(err);
|
||||||
|
s.port.should.equal(8080);
|
||||||
|
Server.find({ fields: ['host'] }, function (err, servers) {
|
||||||
|
servers[0].host.should.equal('localhost');
|
||||||
|
servers[0].should.have.property('host');
|
||||||
|
servers[0].should.not.have.property('port');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue