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

Set initial value once #10

Open
andregoldstein opened this issue Nov 17, 2017 · 5 comments
Open

Set initial value once #10

andregoldstein opened this issue Nov 17, 2017 · 5 comments

Comments

@andregoldstein
Copy link

Hi there,

Was trying to figure out if it is possible to set an initial starting value for the counter (say 100)?

My initial idea was to add it to test to see if the counter was zero inside a Meteor.startup call and then set to 100 if so but don't think there is a getter for a counter?

Thanks

@sampaiodiego
Copy link

I think it is a good idea having a initial value, but I don't know how it could be set though..

let's say you two collections, contracts and people, how would you define an initial value of 100 for contracts collection but would keep the initial value of 1 for people's collection?

@Maxhodges
Copy link

it's very simple! just add 100 to the value in code.

@sampaiodiego
Copy link

in this case the package has nothing to do about it, it is done by your application..

@Maxhodges
Copy link

Maxhodges commented Nov 17, 2017

@sampaiodiego

let's say you two collections, contracts and people, how would you define an initial value of 100 for contracts collection but would keep the initial value of 1 for people's collection?

You could add a switch statement so it uses a different subroutine depending on your collection.

App = {};

function randInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

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;
  }
}

_.extend(App, {
  generateRemoteIdString: function(remoteId, idType) {
    switch (idType) {
      case "helpScout":
        return "HS" + remoteId;
        break;
      default:
        return null;
        break;
    }
  },

  generateIdString: function(idType, parcelType) {
    // Initial character
    var generatedId;
    switch (idType) {
      case "lineItem":
        generatedId = sequentialString("M", idType);
        break;
      case "purchaseId":
        generatedId = sequentialString("P", idType);
        break;
      case "userId":
        generatedId = sequentialString("C", idType);
        break;
      case "orderId":
        generatedId = sequentialString("WX", idType);
        break;
      case "shippingOrderId":
        generatedId = sequentialString("SX", idType);
        break;
      case "consolidationRequest":
        generatedId = sequentialString("CR", idType);
        break;
      case "parcelId":
        generatedId = sequentialString("PX", idType);
        break;
      case "adhocId":
        generatedId = sequentialString("AH", idType);
        break;
      case "packageForwardingId":
        generatedId = randInt(100000, 999999);
        break;
      default:
        break;
    }

    return generatedId;
  },

@andregoldstein
Copy link
Author

andregoldstein commented Nov 17, 2017

Thanks for the quick replies guys. I was actually thinking of going with your first idea @Maxhodges
Probably the simplest way to do it, can't beat simple!

Could either keep the reference as 1, 2, 3 and then add the extra hundred on the client level or store it directly in DB with the 100 added on.

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