PatrickJS / redis-dataloader

Batching and Caching layer using Redis as the Caching layer
MIT License
89 stars 20 forks source link

[QUESTION] Redis dataloader can accept parameters #9

Open AllanPinheiroDeLima opened 5 years ago

AllanPinheiroDeLima commented 5 years ago

Hi, I'm using the dataloader, but I cant send parameters besides the first parameter, cache ID Am I doing something wrong ?

DubFriend commented 5 years ago

Hmm, not sure. If you send me a code sample I could get a better diagnoses of the issue?

AllanPinheiroDeLima commented 5 years ago

I'm using like a factory function, so I can reuse it without redeclaring all redisloader again, but, when I tried to use a function that has parameters into the redisloader, it does not accepted my parameters if it wasn't a number ( wich is the cache container )

For example: I have a function that updates an user and the I need to clear the hash containing the cache of that specific user ( I'm caching content based on a company level ), and it goes like this:

const redisLoader = require('customRedisLoader')

async function updateUser(arg) {
  return User.update({ where: {  ...args  } }) 
}

const updateUserQuery = redisLoader(updateUser)

But the function does not receive my args, only numbers. My factory function is below


const redisClient = require('redis').createClient();
const RedisDataLoader = require('redis-dataloader')({ redis: redisClient });

var dataloader = (fn) => new RedisDataLoader(
    'avanco',
    new Dataloader(fn, { cache: false }),
    {
        cache: true
    }
)

export default dataloader```

Am I doing something wrong ?

[EDIT]

Thanks for your reply
DubFriend commented 5 years ago

One thing that looks like it might be off is the signature of the function you are passing into the Dataloader. In new Dataloader(fn, { cache: false }). fn should be a function that takes an array of arguments (so they can be batched into a single query)

So

async function updateUser(arg) {
  return User.update({ where: {  ...args  } }) 
}

might be changed to

async function updateUser(arrayOfArgs) {
  return await Promise.all(arrayOfArgs.map(args => User.update({ where: { ...args } })))
}

It may be useful to get deeper familiarity with the Facebook Dataloader documentation as well. https://github.com/facebook/dataloader