Coder Social home page Coder Social logo

Comments (4)

ramiel avatar ramiel commented on May 28, 2024

In this case I think the best option is to disable automatic increment and manually increment the counter when needed. Look at the documentation: https://github.com/ramiel/mongoose-sequence#not-automatic-sequences

A simple way is to disable the library hook and write your own post/pre save hook. In the hook you can determine the type of user and if the type is student you can call user.setNext. An example code

UserSchema.plugin(AutoIncrement, {id:'student_counter', inc_field: 'user_id', disable_hooks: true});

// then, somewhere in your code, probably in a mongoose hook
if(user.type === 'student') {
  user.setNext('student_counter', function(err, user){
     // ...
  });
}

from mongoose-sequence.

andrealeo83 avatar andrealeo83 commented on May 28, 2024

I see from code setNext exec mongoose save method. I don't want to save again the document to avoid further saving on the db and improve performance. I think it's preferable to conditionally activate the sequence and save only one time the document to MongoDB in my code.

from mongoose-sequence.

ramiel avatar ramiel commented on May 28, 2024

Ok, I see your use case. So you'd want something like this:

UserSchema.plugin(AutoIncrement, {
  id:'student_counter', 
  inc_field: 'user_id', 
  condition: (user) => user.type === 'student'
});

I'm not sure this is generally useful but you can provide a PR if you want.

from mongoose-sequence.

andrealeo83 avatar andrealeo83 commented on May 28, 2024

this works

from mongoose-sequence.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.