Merge pull request #17816 from overleaf/bg-eslint-rule-for-find-with-await

add eslint rule for find with await

GitOrigin-RevId: 7e78104e610073ff3c151d7314753c8301a8c787
This commit is contained in:
Brian Gough 2024-05-16 14:34:58 +01:00 committed by Copybot
parent 9f86f90a08
commit 598eaf08fd
2 changed files with 6 additions and 1 deletions

View file

@ -133,6 +133,11 @@
{ {
"selector": "CallExpression[callee.property.name='map'] Identifier[name='ObjectId']:first-child, CallExpression[callee.property.name='map'] MemberExpression[property.name='ObjectId']:first-child", "selector": "CallExpression[callee.property.name='map'] Identifier[name='ObjectId']:first-child, CallExpression[callee.property.name='map'] MemberExpression[property.name='ObjectId']:first-child",
"message": "Don't map ObjectId directly. Use `id => new ObjectId(id)` instead" "message": "Don't map ObjectId directly. Use `id => new ObjectId(id)` instead"
},
// Catch incorrect usage of `await db.collection.find()`
{
"selector": "AwaitExpression > CallExpression > MemberExpression[property.name='find'][object.object.name='db']",
"message": "Mongo find returns a cursor not a promise, use `for await (const result of cursor)` or `.toArray()` instead."
} }
] ]
} }

View file

@ -2,7 +2,7 @@ const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
async function updateStringDates() { async function updateStringDates() {
await waitForDb() await waitForDb()
const users = await db.users.find({ const users = db.users.find({
splitTests: { $exists: true }, splitTests: { $exists: true },
}) })