2do2go / rate-limit-mongo

A MongoDB store for express-rate-limit middleware
32 stars 10 forks source link

Library attempts to create indexes #15

Closed gregorskii closed 4 years ago

gregorskii commented 4 years ago

Can this be optionally turned off?

The implementor could create the indexes manually if they were detailed in the readme.

This is currently failing for me with Atlas even when the writer has admin permissions, and the index exists. Looking into this more on my end.

Thanks

okv commented 4 years ago

Hi, I got your point. I'll try to check current behavior and possible solution (I have a couple of ideas) soon.

gregorskii commented 4 years ago

Awesome thank you!

okv commented 4 years ago

I'm trying to reproduce the error but no luck currently - createIndex considered to be idempotent operation and works as expected:

const {MongoClient} = require('mongodb');

const main = async () => {
    const url = 'mongodb://localhost:27017/test';
    const client = new MongoClient(url);
    await client.connect();
    const db = client.db();
    await db.collection('testIndexes').createIndex(
        {expirationDate: 1},
        {expireAfterSeconds: 0}
    );
    await db.collection('testIndexes').createIndex(
        {expirationDate: 1},
        {expireAfterSeconds: 0}
    );
};

main()
    .then(() => {
        console.log('All done');
    })
    .catch((err) => {
        console.error('Error occurred: ', err.stack || err);
    });

I get "All done" output (nodejs 10.x, mongodb@3.5.7).

@gregorskii could you help me with that? What error do you have? Do you have any thoughts about why did it happen?

gregorskii commented 4 years ago

It says the user does not have the necessary permissions to add the indexes. I have added a custom role to mongo atlas with the ability to create indexes but it’s not working.

My point though is that I don’t think the code necessarily needs the ability to create the index it just needs to be able to write. I can add the index manually.

Shouldn’t it be optional that it creates them? Or that you can optionally not create them.

okv commented 4 years ago

I wanted to fully understand the situation to provide reasonable solution. Ok, I agree, since craeteIndex may fail due to permission issues (e.g. with atlas) it looks reasonable to me to add option to create an index (will be enabled by default). I'll implement it.

gregorskii commented 4 years ago

Yep, enabled by default, but removed if defined would be great. Thanks so much!

okv commented 4 years ago

Implemented and published to npm as rate-limit-mongo@2.2.0 @gregorskii could you check it against your expectations?)

gregorskii commented 4 years ago

Looks great! Thanks!

jaankoppe commented 3 years ago

Hey!

Having the same issue, when setting the option createTtlIndex to false, then I will get the following error: MongoError: user is not allowed to do action [find] on [<dbname>.expressRateRecords]

before it was: MongoError: user is not allowed to do action [createIndex] on [<dbname>.expressRateRecords]

Using Mongo Atlas and have readWrite rights to the user which I am connection to database. readWrite rights has built-in createCollection and createIndex rights.

Any thoughts how to solve this?

Thanks!

okv commented 3 years ago

Hey @jaankoppe, I don't have a chance to check it out particularly with Mongo Atlas right now but when createTtlIndex is false there should not be an index creation call. Are sure that you're using rate limit mongo >= 2.2.0 and createTtlIndex: false?