Skip to content

Commit 38b8b6e

Browse files
Repurpose newObjectId as more general id generating function. Set username to random string if missing
1 parent 244febf commit 38b8b6e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/RestWrite.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function RestWrite(config, auth, className, query, data, originalData) {
5656
this.data.updatedAt = this.updatedAt;
5757
if (!this.query) {
5858
this.data.createdAt = this.updatedAt;
59-
this.data.objectId = newObjectId();
59+
this.data.objectId = newStringId(10);
6060
}
6161
}
6262
}
@@ -319,8 +319,7 @@ RestWrite.prototype.transformUser = function() {
319319
// Check for username uniqueness
320320
if (!this.data.username) {
321321
if (!this.query) {
322-
// TODO: what's correct behavior here
323-
this.data.username = '';
322+
this.data.username = newStringId(25);
324323
}
325324
return;
326325
}
@@ -714,13 +713,13 @@ RestWrite.prototype.objectId = function() {
714713
return this.data.objectId || this.query.objectId;
715714
};
716715

717-
// Returns a unique string that's usable as an object id.
718-
function newObjectId() {
716+
// Returns a unique string that's usable as an object or other id.
717+
function newStringId(size) {
719718
var chars = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
720719
'abcdefghijklmnopqrstuvwxyz' +
721720
'0123456789');
722721
var objectId = '';
723-
var bytes = crypto.randomBytes(10);
722+
var bytes = crypto.randomBytes(size);
724723
for (var i = 0; i < bytes.length; ++i) {
725724
// Note: there is a slight modulo bias, because chars length
726725
// of 62 doesn't divide the number of all bytes (256) evenly.

0 commit comments

Comments
 (0)