diff --git a/lib/waterline/utils/query/forge-stage-two-query.js b/lib/waterline/utils/query/forge-stage-two-query.js index 7de1ef6fe..ab14685ed 100644 --- a/lib/waterline/utils/query/forge-stage-two-query.js +++ b/lib/waterline/utils/query/forge-stage-two-query.js @@ -246,7 +246,8 @@ module.exports = function forgeStageTwoQuery(query, orm) { }//>-• - // Now check a few different model settings, and set `meta` keys accordingly. + // Now check a few different model settings that correspond with `meta` keys, + // and set the relevant `meta` keys accordingly. // // > Remember, we rely on waterline-schema to have already validated // > these model settings when the ORM was first initialized. @@ -337,22 +338,22 @@ module.exports = function forgeStageTwoQuery(query, orm) { }//>- - // ┌┬┐┌─┐┌┐┌┌┬┐ ┌─┐┬ ┌─┐┌┐┌┌─┐ ┌─┐┬─┐┌─┐┬ ┬┌┬┐┌─┐┌┐┌┌┬┐┌─┐┌─┐ - // │││ ││││ │ │ │ │ ││││├┤ ├─┤├┬┘│ ┬│ ││││├┤ │││ │ └─┐ ┌┘ - // ─┴┘└─┘┘└┘ ┴ └─┘┴─┘└─┘┘└┘└─┘ ┴ ┴┴└─└─┘└─┘┴ ┴└─┘┘└┘ ┴ └─┘ o - if (!_.isUndefined(WLModel.dontCloneArguments)) { - if (!_.isBoolean(WLModel.dontCloneArguments)) { - throw new Error('Consistency violation: If specified, expecting `dontCloneArguments` model setting to be `true` or `false`. But instead, got: '+util.inspect(WLModel.dontCloneArguments, {depth:5})+''); + // ┌─┐┬ ┬ ┌─┐┬ ┬ ┌┬┐┬ ┬┌┬┐┌─┐┌┬┐┬┌┐┌┌─┐ ┌─┐┬─┐┌─┐┌─┐┌─┐ + // ├─┤│ │ │ ││││ ││││ │ │ ├─┤ │ │││││ ┬ ├─┤├┬┘│ ┬└─┐ ┌┘ + // ┴ ┴┴─┘┴─┘└─┘└┴┘ ┴ ┴└─┘ ┴ ┴ ┴ ┴ ┴┘└┘└─┘ ┴ ┴┴└─└─┘└─┘ o + if (!_.isUndefined(WLModel.allowMutatingArgs)) { + if (!_.isBoolean(WLModel.allowMutatingArgs)) { + throw new Error('Consistency violation: If specified, expecting `allowMutatingArgs` model setting to be `true` or `false`. But instead, got: '+util.inspect(WLModel.allowMutatingArgs, {depth:5})+''); } - // Only bother setting the `dontCloneArguments` meta key if the corresponding + // Only bother setting the `allowMutatingArgs` meta key if the corresponding // model setting is `true` (because otherwise it's `false`, which is the default anyway) - if (WLModel.dontCloneArguments) { + if (WLModel.allowMutatingArgs) { query.meta = query.meta || {}; - query.meta.dontCloneArguments = WLModel.dontCloneArguments; + query.meta.allowMutatingArgs = WLModel.allowMutatingArgs; } - }//>- + }//fi // ┌─┐┬─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌┬┐┌─┐ ┌┐┌┌─┐┌┐┌ ┌─┐┌┐ ┬┌─┐┌─┐┌┬┐ ┬┌┬┐ ┌┬┐┌─┐┬ ┌─┐┬─┐┌─┐┌┐┌┌─┐┌─┐ @@ -387,34 +388,46 @@ module.exports = function forgeStageTwoQuery(query, orm) { })(); - // Next, check specific meta keys, to make sure they're valid. + // ┬ ┬┌─┐┬ ┬┌┬┐┌─┐┌┬┐┌─┐ ┌─┐┌─┐┌┬┐┌┬┐┌─┐┌┐┌ ┌┬┐┌─┐┌┬┐┌─┐ ┬┌─┌─┐┬ ┬┌─┐ + // └┐┌┘├─┤│ │ ││├─┤ │ ├┤ │ │ ││││││││ ││││ │││├┤ │ ├─┤ ├┴┐├┤ └┬┘└─┐ + // └┘ ┴ ┴┴─┘┴─┴┘┴ ┴ ┴ └─┘ └─┘└─┘┴ ┴┴ ┴└─┘┘└┘ ┴ ┴└─┘ ┴ ┴ ┴ ┴ ┴└─┘ ┴ └─┘ + // Next, check specific `meta` keys, to make sure they're valid. // (Not all `meta` keys can be checked, obviously, because there could be **anything** // in there, such as meta keys proprietary to particular adapters. But certain core - // `meta` keys can be properly verified.) + // `meta` keys can be properly verified. Currently, we only validate _some_ of the + // ones that are more commonly used.) + if (query.meta !== undefined) { - // ┌─┐┌─┐┌┬┐┌─┐┬ ┬ - // ├┤ ├┤ │ │ ├─┤ - // └ └─┘ ┴ └─┘┴ ┴ - if (query.meta.fetch !== undefined) { - if (!_.isBoolean(query.meta.fetch)) { - throw buildUsageError( - 'E_INVALID_META', - 'If provided, `fetch` should be a boolean.', - query.using - ); - } + // ┌─┐┌─┐┌┬┐┌─┐┬ ┬ + // ├┤ ├┤ │ │ ├─┤ + // └ └─┘ ┴ └─┘┴ ┴ + if (query.meta.fetch !== undefined) { + + if (!_.isBoolean(query.meta.fetch)) { + throw buildUsageError( + 'E_INVALID_META', + 'If provided, `fetch` should be a boolean.', + query.using + ); + } + + // If this is a findOrCreate query, make sure that the `fetch` meta key hasn't + // been explicitly set (because that wouldn't make any sense). + if (query.method === 'findOrCreate') { + throw buildUsageError( + 'E_INVALID_META', + 'The `fetch` meta key should not be provided when calling .findOrCreate(). '+ + 'This method always behaves as if `fetch` was set to `true`, and, if successful, '+ + 'guarantees a result.', + query.using + ); + } + + }//fi + + + // … - // If this is a findOrCreate query, make sure that the `fetch` meta key hasn't - // been explicitly set (because that wouldn't make any sense). - if (query.method === 'findOrCreate') { - throw buildUsageError( - 'E_INVALID_META', - 'The `fetch` meta key should not be provided when calling .findOrCreate(). '+ - 'This method always behaves as if `fetch` was set to `true`, and, if successful, '+ - 'guarantees a result.', - query.using - ); - } }//fi @@ -567,7 +580,7 @@ module.exports = function forgeStageTwoQuery(query, orm) { // └─ ┴└ ┴ ┴ ┴┴└─┘ ┴└─┘ ┴ ┴ ╚ ╩╝╚╝═╩╝ ╚═╝╝╚╝╚═╝ └─┘└└─┘└─┘┴└─ ┴ ─┘ // If this is a `findOne` query, then if `where` clause is not defined, or if it is `{}`, // then fail with a usage error for clarity. - if (query.method === 'findOne' && (_.isEqual(query.criteria.where, {}))) { + if (query.method === 'findOne' && Object.keys(query.criteria.where).length === 0) { throw buildUsageError('E_INVALID_CRITERIA', 'Cannot `findOne()` without specifying a more specific `where` clause. (If you want to work around this, use `.find().limit(1)`.)', query.using);