Skip to content

Commit

Permalink
Rename meta key and model setting to allowMutatingArgs for clarity. (…
Browse files Browse the repository at this point in the history
…Also, one other minor tweak as an optimization.)
  • Loading branch information
mikermcneil committed Sep 30, 2017
1 parent 4ea52f8 commit 65eff72
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions lib/waterline/utils/query/forge-stage-two-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

}//>-
}//


// ┌─┐┬─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌┬┐┌─┐ ┌┐┌┌─┐┌┐┌ ┌─┐┌┐ ┬┌─┐┌─┐┌┬┐ ┬┌┬┐ ┌┬┐┌─┐┬ ┌─┐┬─┐┌─┐┌┐┌┌─┐┌─┐
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 65eff72

Please sign in to comment.