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

use same counter (shared counter) for multiple schemas #36

Closed
jpolvora opened this issue Aug 30, 2018 · 3 comments
Closed

use same counter (shared counter) for multiple schemas #36

jpolvora opened this issue Aug 30, 2018 · 3 comments

Comments

@jpolvora
Copy link

jpolvora commented Aug 30, 2018

Suppose I have 2 schemas:

let sch1 = new Schema({
    _id: Number
})

let  sch2 = new Schema({
    _id: Number
})

I need to use same counter (same sequence) for both schemas

So, I expected to write the following code:

sch1.Plugin(AutoIncrement, {id: 'mySharedCounter'})
sch2.Plugin(AutoIncrement, {id: 'mySharedCounter'})

But the program crashes with the following error

Counter already defined for field "mySharedCounter"
at Sequence.getInstance (D:\PROJECTS\golden\node_modules\mongoose-sequence\lib\sequence.js:69:15)
at Schema.plugin (D:\PROJECTS\golden\node_modules\mongoose\lib\schema.js:1198:3)

Not sure but I believe that the behavior should be: "If the counter already exists, use that counter". Or maybe create a new option (perhaps useSharedCounter: true //default false
So the library not throw error if there's already a counter with same name if useSharedCounter=true

Then I write the following code:

sch1.Plugin(AutoIncrement, {id: 'mySharedCounter', useSharedCounter: true})
sch2.Plugin(AutoIncrement, {id: 'mySharedCounter', useSharedCounter: true})
@jpolvora jpolvora changed the title use same counter (sequence) from multiple schemas use same counter (shared counter) for multiple schemas Aug 30, 2018
@jpolvora
Copy link
Author

I have forked this repository and I'm doing changes on my own. If anyone is interested, I can create and send a pull request for approval.

@jpolvora
Copy link
Author

#37

@ramiel
Copy link
Owner

ramiel commented Oct 13, 2020

This is available in version 5.3.0

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

2 participants