Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sequence not counting on cosmosDB #117

Open
sghoe opened this issue Jan 31, 2022 · 3 comments
Open

sequence not counting on cosmosDB #117

sghoe opened this issue Jan 31, 2022 · 3 comments

Comments

@sghoe
Copy link

sghoe commented Jan 31, 2022

Hello, thank you for this nice middleware.

We switched from mongoDB to cosmosDB but it stopped counting. The counters were created, but the seq. stays and remains at 1.

Now we are on:

"mongoose": "^6.1.6",
"mongoose-sequence": "^5.3.1",

Is sequence compatible with cosmosDB?

@ramiel
Copy link
Owner

ramiel commented Jan 31, 2022

To be honest, I don't know. I never tried it on CosmosDB. There's nothing special in this middleware, so as long as basic operations on mongo are supported on cosmos, it should work. I can't tell you more, sorry

@sghoe
Copy link
Author

sghoe commented Feb 1, 2022

hello ramiel, thank you for your quick response.

While debugging it seems that every time the lastErrorObject.updatedExisting is false at the counter and it puts in the startSeq everytime

// lastErrorObject.updatedExisting is true if new entry was upserted if (_.has(counter, 'lastErrorObject') && !counter.lastErrorObject.updatedExisting) { return callback(null, startSeq); }

we are using "mongoose": "5.13.14",

do you have any additional ideas?

@perfusorius
Copy link

perfusorius commented Jun 15, 2022

Hello @sghoe , Hello @ramiel

I also have this issue. My app was using MongoDB in MongoCloud and everything was working as expected. But a switch to Azure CosmosDB was neccessary and suddenly mongoose-sequence stopped working.

Through local debugging and testing I found the cause(s) and how to fix them.

Cosmos/Mongo will prevent inserting and updating of Counters documents when the uniqueness check in the defined index is in place. So changing (line 200)

    CounterSchema.index({ id: 1, reference_value: 1 }, { unique: true });

to

    CounterSchema.index({ id: 1, reference_value: 1 });

is the first step. Since you already use upserts with filtering for id and reference_value uniqueness of all documents should still be given.

As @sghoe mentions in the above comment in the callback in Sequence.prototype._createCounter always startSeq is returned to the callback. When the upsert was successful then counter object in that callback has a property lastErrorObject, and a property value that contains the created document. That means it did work and in that case, according to how I understood your logic, the callback that is then called should get null as its second parameter instead of startSeq.
So my second proposal would be to change (lines 389 ff.)

        if (_.has(counter, 'lastErrorObject') && !counter.lastErrorObject.updatedExisting) {
          return callback(null, startSeq);
        }

to

        if ('value' in counter && 'seq' in counter.value) {
          return callback(null, null);
        }
        else if (_.has(counter, 'lastErrorObject') && !counter.lastErrorObject.updatedExisting) {
          return callback(null, startSeq);
        }

With these two changes I could get my app to work with CosmosDB. Counter-documents where created again as expected and seqvalues were increased correctly for each new document in the watched collections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants