explain internal _id format (e.g. author_1_0000000000000019)
Closed this issue · 7 comments
I couldn’t tell from the README what the 1
means in author_1_0000000000000019? For example here: https://github.com/nolanlawson/relational-pouch#dbrelparsedociddocid
I understand the author_
prefix, and I understand the 0000000000000019
part is the author’s id
, just not the part in between :)
Can you explain and maybe amend the README?
@gr2m the number in the middle is the "type" of ID used.
If the id is undefined the value is 0, if the id is a number, the value is 1, if the id is a string the value is 2, and if the id is an object the value is 3. For example:
Given an object of type author with the following ids:
var authorId = 123;
var relPouchId = db.rel.makeDocID({
id: authorId,
type: 'author'
});
//relPouchId = 'author_1_123'
var authorId = 'onetwothree';
var relPouchId = db.rel.makeDocID({
id: authorId,
type: 'author'
});
//relPouchId = 'author_2_onetwothree'
var authorId = { foo: 'bar' };
var relPouchId = db.rel.makeDocID({
id: authorId,
type: 'author'
});
//relPouchId = 'author_3'
The reason for these numbers is sorting/collation so that ids sort as follows:
// undefined < numbers < strings < object
The only use case I have seen for the undefined and "object" id is when you are looking for a range of docs using startkey/endkey and you use a start key with an id of undefined and an end key with an object id to get the "max value", eg as used here: https://github.com/nolanlawson/relational-pouch/blob/7f3b34f70be189cabbdf0f525b64f6be464b0921/lib/index.js#L425
This is a great explanation, thanks John! Maybe we can add this to or link this from the README?
@gr2m yeah, I think it could be added here: https://github.com/nolanlawson/relational-pouch#dbrelmakedocidparsedid
@nolanlawson Did I properly explain the reasoning for the middle part of relational-pouch ids above?
@jkleinsc Yep! :)
Thanks!