Merge pull request #1661 from strongloop/allow-flags-in-options
Allow flags to be passed via options
This commit is contained in:
commit
8f095520f4
45
lib/dao.js
45
lib/dao.js
|
@ -236,22 +236,7 @@ DataAccessObject.getConnector = function() {
|
||||||
* @returns {Boolean} Returns `true` if allowExtendedOperators is enabled, else `false`.
|
* @returns {Boolean} Returns `true` if allowExtendedOperators is enabled, else `false`.
|
||||||
*/
|
*/
|
||||||
DataAccessObject._allowExtendedOperators = function(options) {
|
DataAccessObject._allowExtendedOperators = function(options) {
|
||||||
options = options || {};
|
return this._getSetting('allowExtendedOperators', options) === true;
|
||||||
|
|
||||||
var Model = this;
|
|
||||||
var dsSettings = this.getDataSource().settings;
|
|
||||||
var allowExtendedOperators = dsSettings.allowExtendedOperators;
|
|
||||||
// options settings enable allowExtendedOperators per request (for example if
|
|
||||||
// enable allowExtendedOperators only server side);
|
|
||||||
// model settings enable allowExtendedOperators only for specific model.
|
|
||||||
// dataSource settings enable allowExtendedOperators globally (all models);
|
|
||||||
// options -> model -> dataSource (connector)
|
|
||||||
if (options.hasOwnProperty('allowExtendedOperators')) {
|
|
||||||
allowExtendedOperators = options.allowExtendedOperators === true;
|
|
||||||
} else if (Model.settings && Model.settings.hasOwnProperty('allowExtendedOperators')) {
|
|
||||||
allowExtendedOperators = Model.settings.allowExtendedOperators === true;
|
|
||||||
}
|
|
||||||
return allowExtendedOperators;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Empty callback function
|
// Empty callback function
|
||||||
|
@ -803,7 +788,7 @@ DataAccessObject.upsertWithWhere = function(where, data, options, cb) {
|
||||||
|
|
||||||
function callConnector() {
|
function callConnector() {
|
||||||
try {
|
try {
|
||||||
ctx.where = Model._sanitizeQuery(ctx.where);
|
ctx.where = Model._sanitizeQuery(ctx.where, options);
|
||||||
ctx.where = Model._coerce(ctx.where, options);
|
ctx.where = Model._coerce(ctx.where, options);
|
||||||
update = Model._sanitizeData(update);
|
update = Model._sanitizeData(update);
|
||||||
update = Model._coerce(update, options);
|
update = Model._coerce(update, options);
|
||||||
|
@ -1454,14 +1439,20 @@ DataAccessObject.all = function() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get settings via hierarchical determination
|
* Get settings via hierarchical determination
|
||||||
|
* - method level options
|
||||||
|
* - model level settings
|
||||||
|
* - datasource level settings
|
||||||
*
|
*
|
||||||
* @param {String} key The setting key
|
* @param {String} key The setting key
|
||||||
*/
|
*/
|
||||||
DataAccessObject._getSetting = function(key) {
|
DataAccessObject._getSetting = function(key, options) {
|
||||||
|
// Check method level options
|
||||||
|
var val = options && options[key];
|
||||||
|
if (val !== undefined) return val;
|
||||||
// Check for settings in model
|
// Check for settings in model
|
||||||
var m = this.definition;
|
var m = this.definition;
|
||||||
if (m && m.settings) {
|
if (m && m.settings) {
|
||||||
var val = m.settings[key];
|
val = m.settings[key];
|
||||||
if (val !== undefined) {
|
if (val !== undefined) {
|
||||||
return m.settings[key];
|
return m.settings[key];
|
||||||
}
|
}
|
||||||
|
@ -1580,7 +1571,7 @@ DataAccessObject._normalize = function(filter, options) {
|
||||||
Object.keys(this.definition.properties), this.settings.strict);
|
Object.keys(this.definition.properties), this.settings.strict);
|
||||||
}
|
}
|
||||||
|
|
||||||
filter = this._sanitizeQuery(filter);
|
filter = this._sanitizeQuery(filter, options);
|
||||||
this._coerce(filter.where, options);
|
this._coerce(filter.where, options);
|
||||||
return filter;
|
return filter;
|
||||||
};
|
};
|
||||||
|
@ -1676,16 +1667,16 @@ DataAccessObject._sanitizeQuery = function(query, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
// Get settings to normalize `undefined` values
|
// Get settings to normalize `undefined` values
|
||||||
var normalizeUndefinedInQuery = this._getSetting('normalizeUndefinedInQuery');
|
var normalizeUndefinedInQuery = this._getSetting('normalizeUndefinedInQuery', options);
|
||||||
// Get setting to prohibit hidden/protected properties in query
|
// Get setting to prohibit hidden/protected properties in query
|
||||||
var prohibitHiddenPropertiesInQuery = this._getSetting('prohibitHiddenPropertiesInQuery');
|
var prohibitHiddenPropertiesInQuery = this._getSetting('prohibitHiddenPropertiesInQuery', options);
|
||||||
if (prohibitHiddenPropertiesInQuery == null) {
|
if (prohibitHiddenPropertiesInQuery == null) {
|
||||||
// By default, hidden properties are prohibited in query
|
// By default, hidden properties are prohibited in query
|
||||||
prohibitHiddenPropertiesInQuery = true;
|
prohibitHiddenPropertiesInQuery = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
|
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
|
||||||
var maxDepthOfQuery = (+this._getSetting('maxDepthOfQuery')) || 12;
|
var maxDepthOfQuery = (+this._getSetting('maxDepthOfQuery', options)) || 12;
|
||||||
|
|
||||||
var prohibitedKeys = [];
|
var prohibitedKeys = [];
|
||||||
// Check violation of keys
|
// Check violation of keys
|
||||||
|
@ -1706,7 +1697,7 @@ DataAccessObject._sanitizeQuery = function(query, options) {
|
||||||
DataAccessObject._sanitizeData = function(data, options) {
|
DataAccessObject._sanitizeData = function(data, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
|
// See https://github.com/strongloop/loopback-datasource-juggler/issues/1651
|
||||||
var maxDepthOfQuery = (+this._getSetting('maxDepthOfQuery')) || 12;
|
var maxDepthOfQuery = (+this._getSetting('maxDepthOfQuery', options)) || 12;
|
||||||
return sanitizeQueryOrData(data,
|
return sanitizeQueryOrData(data,
|
||||||
Object.assign({
|
Object.assign({
|
||||||
maxDepth: maxDepthOfQuery,
|
maxDepth: maxDepthOfQuery,
|
||||||
|
@ -2363,7 +2354,7 @@ DataAccessObject.destroyAll = function destroyAll(where, options, cb) {
|
||||||
try {
|
try {
|
||||||
// Support an optional where object
|
// Support an optional where object
|
||||||
// alter configuration of how sanitizeQuery handles undefined values
|
// alter configuration of how sanitizeQuery handles undefined values
|
||||||
where = Model._sanitizeQuery(where);
|
where = Model._sanitizeQuery(where, options);
|
||||||
where = Model._coerce(where, options);
|
where = Model._coerce(where, options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return process.nextTick(function() {
|
return process.nextTick(function() {
|
||||||
|
@ -2517,7 +2508,7 @@ DataAccessObject.count = function(where, options, cb) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// alter configuration of how sanitizeQuery handles undefined values
|
// alter configuration of how sanitizeQuery handles undefined values
|
||||||
where = Model._sanitizeQuery(where);
|
where = Model._sanitizeQuery(where, options);
|
||||||
where = this._coerce(where, options);
|
where = this._coerce(where, options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
|
@ -2810,7 +2801,7 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
|
||||||
function doUpdate(where, data) {
|
function doUpdate(where, data) {
|
||||||
try {
|
try {
|
||||||
// alter configuration of how sanitizeQuery handles undefined values
|
// alter configuration of how sanitizeQuery handles undefined values
|
||||||
where = Model._sanitizeQuery(where);
|
where = Model._sanitizeQuery(where, options);
|
||||||
where = Model._coerce(where, options);
|
where = Model._coerce(where, options);
|
||||||
data = Model._sanitizeData(data);
|
data = Model._sanitizeData(data);
|
||||||
data = Model._coerce(data, options);
|
data = Model._coerce(data, options);
|
||||||
|
|
|
@ -285,6 +285,19 @@ describe('ModelDefinition class', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should honor maxDepthOfQuery in options', function(done) {
|
||||||
|
var MyModel = memory.createModel('my-model', {}, {
|
||||||
|
maxDepthOfQuery: 5,
|
||||||
|
});
|
||||||
|
|
||||||
|
var filter = givenComplexFilter();
|
||||||
|
|
||||||
|
MyModel.find(filter, {maxDepthOfQuery: 20}, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function givenComplexFilter() {
|
function givenComplexFilter() {
|
||||||
var filter = {where: {and: [{and: [{and: [{and: [{and: [{and:
|
var filter = {where: {and: [{and: [{and: [{and: [{and: [{and:
|
||||||
[{and: [{and: [{and: [{x: 1}]}]}]}]}]}]}]}]}]}};
|
[{and: [{and: [{and: [{x: 1}]}]}]}]}]}]}]}]}]}};
|
||||||
|
@ -406,6 +419,17 @@ describe('ModelDefinition class', function() {
|
||||||
children[0].secret.should.equal('guess');
|
children[0].secret.should.equal('guess');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be allowed if prohibitHiddenPropertiesInQuery is `false` in options', function() {
|
||||||
|
return Child.find({
|
||||||
|
where: {secret: 'guess'},
|
||||||
|
}, {
|
||||||
|
prohibitHiddenPropertiesInQuery: false,
|
||||||
|
}).then(function(children) {
|
||||||
|
children.length.should.equal(1);
|
||||||
|
children[0].secret.should.equal('guess');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with hidden object', function() {
|
describe('with hidden object', function() {
|
||||||
|
|
Loading…
Reference in New Issue