XCEPTION
XCEPTION

Reputation: 1753

Apostrophe CMS: ID value for embedded docs

I have written a migrations script and running it using apostrophe-migrations module. I am trying to update some existing docs and need to add new embedded docs. I can see the embedded docs generated by apostrophe have ids like the one below.

"_id" : "w958610296299634883"

What are these IDs composed of? For now I am using

self.apos.utils.generateId()

to provide IDs for embedded docs, which generates MongoDB ObjectID. How can I generate the ID like above so I don't face any issues in future? Or I can go ahead with MongoDB ObjectID?

Upvotes: 0

Views: 142

Answers (1)

Tom Boutell
Tom Boutell

Reputation: 7572

First, on the server side, self.apos.utils.generateId() does not generate a MongoDB ObjectID. You shouldn't ever see those in an apostrophe doc. It generates an id using Eric Elliot's excellent cuid module.

Second though, those "embedded docs" you're referring to are Apostrophe widgets. And since those are "born" on the browser side, their IDs are usually generated on the browser side. The algorithm there is not sophisticated at all since it's not being used for independent documents in the database; it's just enough to keep widget ids from colliding within a single document:

'w' + Math.floor(Math.random() * 1000000000) + Math.floor(Math.random() * 1000000000);

However, you don't have to generate them this way and I wouldn't really recommend it on the server where you have something better available and you don't have to reinvent any wheels. I would use:

'w' + self.apos.utils.generateId();

This provides consistency with what the browser side is doing, as we do occasionally use this letter to distinguish true doc IDs from widget IDs. (A cuid starts with a c.)

Hope you find this helpful!

Upvotes: 2

Related Questions