Merge pull request #400 from chrene/protected
Add support for protected properties.
This commit is contained in:
commit
4793546353
|
@ -72,11 +72,11 @@ List.prototype.push = function (obj) {
|
||||||
return item;
|
return item;
|
||||||
};
|
};
|
||||||
|
|
||||||
List.prototype.toObject = function (onlySchema, removeHidden) {
|
List.prototype.toObject = function (onlySchema, removeHidden, removeProtected) {
|
||||||
var items = [];
|
var items = [];
|
||||||
this.forEach(function (item) {
|
this.forEach(function (item) {
|
||||||
if (item && typeof item === 'object' && item.toObject) {
|
if (item && typeof item === 'object' && item.toObject) {
|
||||||
items.push(item.toObject(onlySchema, removeHidden));
|
items.push(item.toObject(onlySchema, removeHidden, removeProtected));
|
||||||
} else {
|
} else {
|
||||||
items.push(item);
|
items.push(item);
|
||||||
}
|
}
|
||||||
|
|
47
lib/model.js
47
lib/model.js
|
@ -303,7 +303,7 @@ ModelBaseClass.toString = function () {
|
||||||
*
|
*
|
||||||
* @param {Boolean} onlySchema Restrict properties to dataSource only. Default is false. If true, the function returns only properties defined in the schema; Otherwise it returns all enumerable properties.
|
* @param {Boolean} onlySchema Restrict properties to dataSource only. Default is false. If true, the function returns only properties defined in the schema; Otherwise it returns all enumerable properties.
|
||||||
*/
|
*/
|
||||||
ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden, removeProtected) {
|
||||||
if (onlySchema === undefined) {
|
if (onlySchema === undefined) {
|
||||||
onlySchema = true;
|
onlySchema = true;
|
||||||
}
|
}
|
||||||
|
@ -322,6 +322,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
var props = Model.definition.properties;
|
var props = Model.definition.properties;
|
||||||
var keys = Object.keys(props);
|
var keys = Object.keys(props);
|
||||||
var propertyName, val;
|
var propertyName, val;
|
||||||
|
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (var i = 0; i < keys.length; i++) {
|
||||||
propertyName = keys[i];
|
propertyName = keys[i];
|
||||||
val = self[propertyName];
|
val = self[propertyName];
|
||||||
|
@ -335,11 +336,15 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (val instanceof List) {
|
if (val instanceof List) {
|
||||||
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
||||||
} else {
|
} else {
|
||||||
if (val !== undefined && val !== null && val.toObject) {
|
if (val !== undefined && val !== null && val.toObject) {
|
||||||
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
||||||
} else {
|
} else {
|
||||||
data[propertyName] = val;
|
data[propertyName] = val;
|
||||||
}
|
}
|
||||||
|
@ -363,13 +368,16 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
val = self[propertyName];
|
val = self[propertyName];
|
||||||
if (val !== undefined && data[propertyName] === undefined) {
|
if (val !== undefined && data[propertyName] === undefined) {
|
||||||
if (typeof val === 'function') {
|
if (typeof val === 'function') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (val !== null && val.toObject) {
|
if (val !== null && val.toObject) {
|
||||||
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
||||||
} else {
|
} else {
|
||||||
data[propertyName] = val;
|
data[propertyName] = val;
|
||||||
}
|
}
|
||||||
|
@ -387,16 +395,18 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (removeProtected && Model.isProtectedProperty(propertyName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
var ownVal = self[propertyName];
|
var ownVal = self[propertyName];
|
||||||
// The ownVal can be a relation function
|
// The ownVal can be a relation function
|
||||||
val = (ownVal !== undefined && (typeof ownVal !== 'function'))
|
val = (ownVal !== undefined && (typeof ownVal !== 'function')) ? ownVal : self.__data[propertyName];
|
||||||
? ownVal : self.__data[propertyName];
|
|
||||||
if (typeof val === 'function') {
|
if (typeof val === 'function') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (val !== undefined && val !== null && val.toObject) {
|
if (val !== undefined && val !== null && val.toObject) {
|
||||||
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
data[propertyName] = val.toObject(!schemaLess, removeHidden, true);
|
||||||
} else {
|
} else {
|
||||||
data[propertyName] = val;
|
data[propertyName] = val;
|
||||||
}
|
}
|
||||||
|
@ -407,6 +417,25 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ModelBaseClass.isProtectedProperty = function (propertyName) {
|
||||||
|
var Model = this;
|
||||||
|
var settings = Model.definition && Model.definition.settings;
|
||||||
|
var protectedProperties = settings && (settings.protectedProperties || settings.protected);
|
||||||
|
if (Array.isArray(protectedProperties)) {
|
||||||
|
// Cache the protected properties as an object for quick lookup
|
||||||
|
settings.protectedProperties = {};
|
||||||
|
for (var i = 0; i < protectedProperties.length; i++) {
|
||||||
|
settings.protectedProperties[protectedProperties[i]] = true;
|
||||||
|
}
|
||||||
|
protectedProperties = settings.protectedProperties;
|
||||||
|
}
|
||||||
|
if (protectedProperties) {
|
||||||
|
return protectedProperties[propertyName];
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ModelBaseClass.isHiddenProperty = function (propertyName) {
|
ModelBaseClass.isHiddenProperty = function (propertyName) {
|
||||||
var Model = this;
|
var Model = this;
|
||||||
var settings = Model.definition && Model.definition.settings;
|
var settings = Model.definition && Model.definition.settings;
|
||||||
|
@ -424,10 +453,10 @@ ModelBaseClass.isHiddenProperty = function (propertyName) {
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
ModelBaseClass.prototype.toJSON = function () {
|
ModelBaseClass.prototype.toJSON = function () {
|
||||||
return this.toObject(false, true);
|
return this.toObject(false, true, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
ModelBaseClass.prototype.fromObject = function (obj) {
|
ModelBaseClass.prototype.fromObject = function (obj) {
|
||||||
|
|
|
@ -36,9 +36,9 @@ describe('ModelDefinition class', function () {
|
||||||
assert.equal(json.properties.approved.type, "Boolean");
|
assert.equal(json.properties.approved.type, "Boolean");
|
||||||
assert.equal(json.properties.joinedAt.type, "Date");
|
assert.equal(json.properties.joinedAt.type, "Date");
|
||||||
assert.equal(json.properties.age.type, "Number");
|
assert.equal(json.properties.age.type, "Number");
|
||||||
|
|
||||||
assert.deepEqual(User.toJSON(), json);
|
assert.deepEqual(User.toJSON(), json);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -55,7 +55,7 @@ describe('ModelDefinition class', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
User.build();
|
User.build();
|
||||||
|
|
||||||
var json = User.toJSON();
|
var json = User.toJSON();
|
||||||
|
|
||||||
User.defineProperty("id", {type: "number", id: true});
|
User.defineProperty("id", {type: "number", id: true});
|
||||||
|
@ -64,12 +64,12 @@ describe('ModelDefinition class', function () {
|
||||||
assert.equal(User.properties.approved.type, Boolean);
|
assert.equal(User.properties.approved.type, Boolean);
|
||||||
assert.equal(User.properties.joinedAt.type, Date);
|
assert.equal(User.properties.joinedAt.type, Date);
|
||||||
assert.equal(User.properties.age.type, Number);
|
assert.equal(User.properties.age.type, Number);
|
||||||
|
|
||||||
assert.equal(User.properties.id.type, Number);
|
assert.equal(User.properties.id.type, Number);
|
||||||
|
|
||||||
json = User.toJSON();
|
json = User.toJSON();
|
||||||
assert.deepEqual(json.properties.id, {type: 'Number', id: true});
|
assert.deepEqual(json.properties.id, {type: 'Number', id: true});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -292,6 +292,45 @@ describe('ModelDefinition class', function () {
|
||||||
assert(grandChild.prototype instanceof child);
|
assert(grandChild.prototype instanceof child);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should serialize protected properties into JSON', function() {
|
||||||
|
var memory = new DataSource({connector: Memory});
|
||||||
|
var modelBuilder = memory.modelBuilder;
|
||||||
|
var ProtectedModel = memory.createModel('protected', {}, {
|
||||||
|
protected: ['protectedProperty']
|
||||||
|
});
|
||||||
|
var pm = new ProtectedModel({
|
||||||
|
id: 1, foo: 'bar', protectedProperty: 'protected'
|
||||||
|
});
|
||||||
|
var serialized = pm.toJSON();
|
||||||
|
assert.deepEqual(serialized, {
|
||||||
|
id: 1, foo: 'bar', protectedProperty: 'protected'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not serialize protected properties of nested models into JSON', function(done){
|
||||||
|
var memory = new DataSource({connector: Memory});
|
||||||
|
var modelBuilder = memory.modelBuilder;
|
||||||
|
var Parent = memory.createModel('parent');
|
||||||
|
var Child = memory.createModel('child', {}, {protected: ['protectedProperty']});
|
||||||
|
Parent.hasMany(Child);
|
||||||
|
Parent.create({
|
||||||
|
name: 'parent'
|
||||||
|
}, function(err, parent) {
|
||||||
|
parent.children.create({
|
||||||
|
name: 'child',
|
||||||
|
protectedProperty: 'protectedValue'
|
||||||
|
}, function(err, child) {
|
||||||
|
Parent.find({include: 'children'}, function(err, parents) {
|
||||||
|
var serialized = parents[0].toJSON();
|
||||||
|
var child = serialized.children[0];
|
||||||
|
assert.equal(child.name, 'child');
|
||||||
|
assert.notEqual(child.protectedProperty, 'protectedValue');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should not serialize hidden properties into JSON', function () {
|
it('should not serialize hidden properties into JSON', function () {
|
||||||
var memory = new DataSource({connector: Memory});
|
var memory = new DataSource({connector: Memory});
|
||||||
var modelBuilder = memory.modelBuilder;
|
var modelBuilder = memory.modelBuilder;
|
||||||
|
|
Loading…
Reference in New Issue