jeudi 26 février 2015

Shorten ObjectId in node.js and mongoose

my URLs look like this at the moment:



http://ift.tt/1EQjk5u.....


So, as you can see, it gets pretty long, pretty fast. I was thinking about shortening these ObjectIds. Idea is that I should add new field called "shortId" to every model in my database. So instead of having:



var CompanySchema = mongoose.Schema({
/* _id will be added automatically by mongoose */
String name;
String address;
String directorName;
});


we would have this:



var CompanySchema = mongoose.Schema({
/* _id will be added automatically by mongoose */
String shortId; /* WE SHOULD ADD THIS */
String name;
String address;
String directorName;
});


I found a way to do it like this:



// Encode
var b64 = new Buffer('47cc67093475061e3d95369d', 'hex')
.toString('base64')
.replace('+','-')
.replace('/','_')
;
// -> shortID is now: R8xnCTR1Bh49lTad


But I still think it could be shorter.


Also, I found this npm module: http://ift.tt/1ajrpXc but I don't see it's being used too much so I can't tell if it's reliable.


Anyone has any suggestions?


Aucun commentaire:

Enregistrer un commentaire