You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rule proposal: Enforce the use of either .exec() or .then() on DB queries
Objective:
Avoid mistakes where you use Bluebird methods like tap/nodeify on queries
To figure out that e.g. User is a database model, and thus that User.find() is a query call, we'll consider anything to be a database model if it is the result of a call to db.model('...') or <anything>.db.model('...'), in order to take into account calls like context.db.model('user') or job.db.model('user').
Bad
constUser=context.db.model('user');// User is a db modelUser.find({});// Result is not used, and not executedfunctionfoo(){returnUser.find({});// Result is returned but not executed. Forcing the caller to execute the Promise, which is dangereous}// Use of a non-query method before a `then` / `exec` callconstuserP=User.find({}).tap(fn);constuserP=User.find({}).nodeify(fn);// Detect temporary assignment in variable (much harder to enforce)constuserP=User.find({});constsortedUserP.sort(order);sortedUserP.tap(fn);
Good
// Stat is not considered as a DB model, so it does not get reportedconstStat={};Stat.find({}).nodeify(fn);constStat=model('stat');Stat.find({}).nodeify(fn);constUser=context.db.model('user');// User is a db modelUser.find({});// Result is not used, and not executedfunctionfoo(){returnUser.find({}).exec();}functionfoo(){returnUser.find({}).then(fn);}constuserP=User.find({}).exec().tap(fn);constuserP=User.find({}).then(fn1).nodeify(fn2);// Use of known query modifiers before executionconstuserP=User.find({}).sort(order).exec().tap(fn);// Allow temporary assignment in variable (much harder to enforce)constuserP=User.find({});constsortedUserP.sort(order);sortedUserP.exec().tap(fn);
Rule proposal: Enforce the use of either
.exec()
or.then()
on DB queriesObjective:
tap
/nodeify
on queriesTo figure out that e.g.
User
is a database model, and thus thatUser.find()
is a query call, we'll consider anything to be a database model if it is the result of a call todb.model('...')
or<anything>.db.model('...')
, in order to take into account calls likecontext.db.model('user')
orjob.db.model('user')
.Bad
Good
cc @lcalvy @godu
The text was updated successfully, but these errors were encountered: