I’m trying to have a collection with a JobNumber value that increments everytime a job is added to the collection.
I realize that the way to go is by autoValue in the schema and by using konecty:mongo-counter package. But I have been trying with several failed attempts.
Is there a simple example out there that uses this package?
Thank you very much for your help.
Edit:
So I did this, which initially works, but I’m not sure how effective it is in the long run.
autoValue: function(){
if ( this.isInsert ){
var LastjobNUm = Recipes.find().count();
var jobNum = LastjobNUm++;
return jobNum;
} else if ( this.isSet ){
this.unset();
}
}
I stayed away from doing this in autoValue for performance and de-coupling reasons.
IMO I’ll put validation logic in the schema but auto-increment is a DB thing so I like that separate - that way I can use the schema whenever I want for data validation purposes without worrying about peripheral data access occurring.
In my SQL Server days I used auto-increment all over the place like you’re doing here but in Meteor I don’t.
That said I do all CRUD in methods - so when I am inserting a record that requires an auto-incremented value (I use the mongo-counter package too) I just do that as a pre-step to the insert passing in the result of mongo-counter to the document in the insert operation.
I completely agree with you. The app that I am developing will be for a limited user capacity (5-10), would coupling still happen?
Also, could you point me to an example that follows Mongo Counter?
function sequentialString(prepend, idType) {
if (process.env.NODE_ENV !== "production") {
//Make ids more random during development to prevent saasu duplicate invoice# problems
return prepend + "-DEV-" + Random.id(6).toUpperCase();
} else {
// Sequence number, atomic increment
// Init number is 1001
// NOTE: `incrementCounter` is defined by the `konecty:mongo-counter`
var digitString = incrementCounter(AtomicCounters, idType, 1) + 1000;
return prepend + digitString;
}
}
Counters = new Mongo.Collection('counters');
Counters.generateRemoteIdString = function(remoteId, idType) {
switch(idType) {
case 'helpScout':
return "HS" + remoteId;
break;
default:
return null;
break;
}
}
Counters.generateIdString = function(idType, parcelType) {
// Initial character
var idString;
switch(idType) {
case 'lineItem':
idString = 'M';
break;
case 'purchaseId':
idString = 'P';
break;
case 'userId':
idString = 'C';
break;
case 'orderId':
idString = 'S';
break;
case 'shippingOrderId':
idString = 'SX';
break;
case 'consolidationRequest':
idString = "CR";
break;
case 'parcelId':
idString = (function() { // TODO XXX
if ( parcelType === Parcel.type.BUYING_SERVICE ) return 'PP';
else if ( parcelType === Parcel.type.FORWARDING_SERVICE ) return 'PF';
else return 'PX';
})();
break;
default:
// some default
idString = 'X';
break;
}
// Sequence number, atomic increment
// Init number is 1001
var digitString = incrementCounter(Counters, idType) + 1000;
idString += digitString;
return idString;
};