Firestore の autoId を UUID にする
Firestore は Base64 のサブセットで PostgreSQL はHEX 表記をサポートしているので、その相互変換をしてあげればよい Base64 は A–Z, a–z, 0–9, +, / を使用し、autoId はこのうち +, / 以外を使う
uuid の仕様違反では…?
13桁目はバージョン番号
「f」とか来たらだめ?
code:ts
const convertBase64ToHex = (base64: string) => {
const raw = atob(
base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), '=')
);
return Array.from(raw)
.map((char) => char.charCodeAt(0).toString(16).padStart(2, '0'))
.join('');
};
const convertFirebaseAutoIdToUuid = (firebaseAutoId: string) => {
const hex = convertBase64ToHex(firebaseAutoId);
return [
hex.slice(0, 8),
hex.slice(8, 12),
4${hex.slice(12, 15)}, // Set version to 4
8${hex.slice(15, 18)}, // Set variant to 8
hex.slice(18, 30),
].join('-');
};