2

I am using the mongoose-auto-increment plugin and I am using TypeScript Node.js and I have installed the needed typed definitions but when I try to get the next count of the auto increment, I get this

Property 'nextCount' does not exist on type 'Model<Document>'

My schema is the one from the example:

let bookSchema = new Schema({
    author: { type: Number, ref: 'Author' },
    title: String,
    genre: String,
    publishDate: Date
});

bookSchema.plugin(autoIncrement.plugin, 'Book');
var Book = connection.model('Book', bookSchema);

//// error here
Book.nextCount(function(err, count) {
});

My tsconfig.json is like so

{
  "compilerOptions": {
    "types" : ["node", "socket.io"],
    "module": "commonjs",
    "experimentalDecorators": true,
    "target": "es2015",
    "lib": ["es2015", "es2017", "dom"]
  },
  "include": [
    "**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
mega-crazy
  • 838
  • 2
  • 17
  • 36
  • are you using mongoose-auto-increment plugin ? – shivshankar Jan 09 '18 at 06:00
  • yes and i have installed this as well. https://www.npmjs.com/package/@types/mongoose-auto-increment – mega-crazy Jan 09 '18 at 06:01
  • plugin last updated on 4 year before. can you explain why do you need this plugin ? we can do same thing manually with more customization – shivshankar Jan 09 '18 at 06:05
  • well i want an auto increment id, im storing chat messages here and i am pushing to the database once there is like around 50 messages to do so. In case the user wants to edit or delete i need a reference id and i know there is the _id in mongo by default but since im not pushing for each message, i need an auto increment part. – mega-crazy Jan 09 '18 at 06:09
  • and not to mention im using mongoose, so. – mega-crazy Jan 09 '18 at 06:13
  • mongo always maintain _id (primary, indexed, unique, hex value) and __v (for revison, number value), either you supplied or not. plugin only facilitate to read _id as numeric form by an interal counter and getter setter methods. but still _id is Hex internally. – shivshankar Jan 09 '18 at 06:24
  • Yes, i am aware of that, but is the id predictable? – mega-crazy Jan 09 '18 at 06:28
  • yes .save and .findOneAndUpdate or other mongoose method return updated/saved document and simply ready _id from returned value also _id is always incremented hex you can use it to sorting etc. – shivshankar Jan 09 '18 at 06:30
  • My problem still exists, i am not pushing for every message, i am pushing in batches, so i need to know the id's before inserting and also i need to know that they would be the same after inserting. – mega-crazy Jan 09 '18 at 06:37
  • mongo/mongoose provide insert Many/bulk that also return inserted objects. – shivshankar Jan 09 '18 at 07:09

1 Answers1

0

Just use ObjectId and set _id for every chat message before saving batch:

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

messages.forEach(message => {
  message._id = message._id || ObjectId()
})

// call you method which save batches
saveBatch(messages)
Sergii Stotskyi
  • 5,134
  • 1
  • 22
  • 21