Ich möchte eine eindeutige und lesbare ID für die Aufträge/Rechnungen auf Firebase mit den Cloud-Funktionen generiert. Ich denke, dass eine zufällige ID basierend auf der UID und dem Zeitstempel eine gute Lösung ist, da ein Benutzer nicht zwei Aufträge gleichzeitig senden kann.Auftrag Id Generator Algorithmus für Firebase
Was denkst du?
Ich habe diesen Kern bearbeitet =>https://gist.github.com/mikelehen/3596a30bd69384624c11
Und das ist das Ergebnis:
export default (() => {
const PUSH_CHARS = 'abcdefghijklmnopqrstuvwxyz';
let lastPushTime = 0;
let lastRandChars = [];
return (uid) => {
if (uid.length != 28) throw new Error('UID length should be 28.');
let now = new Date().getTime();
const duplicateTime = (now === lastPushTime);
lastPushTime = now;
// CONVERT TimeStamp to CHARS
const timeStampChars = new Array(8);
let i;
for (i = timeStampChars.length - 1; i >= 0; i--) {
timeStampChars[i] = PUSH_CHARS.charAt(now % 36);
now = Math.floor(now/36);
}
if (now !== 0) throw new Error('We should have converted the entire timestamp.');
let id = timeStampChars.join('');
// ADD 2 random CHARS
for (i = 0; i < 2; i++) {
id += PUSH_CHARS.charAt(Math.floor(Math.random() * 36)).toLowerCase();
}
Math.max
// ADD random chars of UID
if (!duplicateTime) {
for (i = 0; i < 6; i++) {
lastRandChars[i] = Math.floor(Math.random() * 28);
}
} else {
for (i = 5; i >= 0 && lastRandChars[i] === 27; i--) {
lastRandChars[i] = 0;
}
lastRandChars[i]++;
}
for (i = 0; i < 6; i++) {
id += uid.charAt(lastRandChars[i]).toLowerCase();
}
// The id must be 16
if (id.length != 16) throw new Error('Length should be 16.');
return id;
};
})();
Ja, aber Feuerbasis einen Schlüssel wie dieses => '-JRHTHaIs-jNPLXOQivY' erzeugen, aber ich möchte eine besser lesbare ID wie dieses => 'j4jo-f9v2-l6v8-srpv' – SaroVin
Sie NodeJS 'UUID verwenden können 'lib es erzeugt eine gute lesbare UUID –
Ich habe ein Repo erstellt => https://github.com/sarovin/order-id-generator – SaroVin