Closed ryangoree closed 3 years ago
Forgot to mention, this will also use $or
to merge multiple pending requests with different fields.
// 2 calls for completely different fields -> merged with $or
const pendingUsers1 = this.findByFields({ userName: 'ryan' })
const pendingUsers2 = this.findByFields({ tags: 'gaming' })
await Promise.all([pendingUsers1, pendingUsers2]);
// resulting query:
// - collection.find({
// $or: [
// { userName: { $in: ['ryan'] } },
// { tags: { $in: ['gaming'] } }
// ]
// })
// 2 calls for partially different fields -> merged with $or
const pendingUsers1 = this.findByFields({ userName: 'ryan' })
const pendingUsers2 = this.findByFields({ userName: 'ryan', tags: 'gaming' })
await Promise.all([pendingUsers1, pendingUsers2]);
// resulting query:
// - collection.find({
// $or: [
// { userName: { $in: ['ryan'] } },
// {
// userName: { $in: ['ryan'] },
// tags: { $in: ['gaming'] } }
// }
// ]
// })
// 2 calls for the same fields -> merged without $or
const pendingUsers1 = this.findByFields({ userName: 'ryan' })
const pendingUsers2 = this.findByFields({ userName: 'loren' })
await Promise.all([pendingUsers1, pendingUsers2]);
// resulting query:
// - collection.find({ userName: { $in: ['ryan', 'loren'] } })
// Mixed
const pendingUsers1 = this.findByFields({ userName: 'ryan', tags: 'games' })
const pendingUsers2 = this.findByFields({ userName: 'loren', tags: 'gaming' })
const pendingUsers3 = this.findByFields({ tags: ['games', 'gaming'] })
await Promise.all([pendingUsers1, pendingUsers2, pendingUsers3]);
// resulting query:
// - collection.find({
// $or: [
// {
// userName: { $in: ['ryan', 'loren'] },
// tags: { $in: ['games', 'gaming'] } }
// },
// { tags: { $in: ['games', 'gaming'] } }
// ]
// })
Sorry for the delay! And thanks again for contributing. Published in 0.4.0
Fixes #48
Added a findByFields method and refactored the DataLoader batch fn and tests to make it work.
This new method makes it possible to batch and cache calls for arbitrary fields on the collection.
example: