Manually generating a Meteor style _id

Hello. I’m trying to figure out how to generate an _id in the same that Meteor does.

For context, I want to do this because I’m following some Domain-Driven Design tutorials, and I notice they generate a unique id for entities before that entity is persisted to the database. For more context,

  • I’m using Meteor 1.8.2
  • I’m using GraphQL mutations instead of Meteor methods

I did some searching, and I see these lines of code

I see that if your collection is named, they use the DDP API. I’d prefer the detail of the collection name to be hidden from the higher-level concept of an entity, so I’m curious if it’s okay to use Random.insecure.id to generate a mongo id. (Also, I’m not using Meteor methods, so from what I understand, I don’t think I have to worry about utilizing DDP.) However, the term insecure, the comments around it (“not cryptographically secure”), and the fact that it isn’t part of the public api are giving me some pause as to whether I should use that or not. (For what it’s worth, the DDP API also doesn’t seem public as it’s not listed in the Meteor docs).

In the Random docs, I do see Random.id which might be another option, but I’m curious why Meteor uses Random.insecure instead of Random.id.

So, to sum things up, I think I have three options:

  1. Random.insecure.id()
  2. DDP.randomStream('/collection/' + name).id()
  3. Random.id()

I’m leaning towards the latter mainly because it’s a public function. Does anyone think one of these is best or does it even really matter? Thank you for taking the time to read this : )

I always use Random.id() I’m not sure how much slower it is but I just call it once and it took only couple miliseconds (I guess).

Thanks for sharing your experience! :slightly_smiling_face:

If you just need a random id for doc reference purposes, then the insecure method suffice.

If you will be using it as a seed in a password, for example, then the secured one is required.

1 Like

Thank you! I wasn’t thinking about the wording in that manner. That makes sense now.