pouchdb-community/relational-pouch

Why adding id type to id string?

Opened this issue · 2 comments

In the serialize function, id type added to id parameter. Why this is necessary?
I think it's for the find function.

function serialize(type, id) {
  // simple collation that goes like this:
  // undefined < numbers < strings < object
  var res = type.replace('_', '') + '_';
  if (typeof id === 'number') {
    // zpad
    id = id.toString();
    while (id.length < MAX_INT_LENGTH) {
      id = '0' + id;
    }
    res += TYPE_NUM + '_' + id;
  } else if (typeof id === 'undefined') {
    // need lowest possible value
    res += TYPE_UNDEF;
  } else if (typeof id === 'object') {
    // need highest possible value
    res += TYPE_OBJ;
  } else { // string
    res += TYPE_STRING + '_' + id;
  }
  return res;
}

@izadmehr The readme explains it here:
https://github.com/nolanlawson/relational-pouch#dbrelmakedocidparsedid
It provides a natural sort order for keys.

On reason it is useful is that you can easily determine min and max keys for a particular document type, which can be used as startkey/endkey on Batch Fetch and Query
Here's some example code determining min and max keys for an object type of "person":

var minId = db.rel.makeDocID({ "type": "person");
var maxId = = db.rel.makeDocID({ "type": "person", "id": {}); 

This would produce a minId of person_0 and a maxId of person_3 which according to sort order would make min id less than a id of number or string (eg less than person_1_0000000000000123 or person_2_onetwothree) and the max id would be greater than these ids.

Does that make sense?

Thanks for your answer @jkleinsc. I'm working on syncing pouchDB with couchBase, so data added to couchBase & used in PouchDB. I think this id generator is not necessary. Just adding prefix with underline, do the work.