/** * Generate UUIDs v7 (timestamp-based) for better sorting and database performance */ export function generateUUIDv7(): string { // Get current timestamp in milliseconds const timestamp = Date.now(); // Convert timestamp to hex (48 bits) const timestampHex = timestamp.toString(16).padStart(12, '0'); // Generate random bits for the rest const randomBytes = crypto.getRandomValues(new Uint8Array(10)); const randomHex = Array.from(randomBytes) .map(byte => byte.toString(16).padStart(2, '0')) .join(''); // Format as UUID v7: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx const uuid = [ timestampHex.substring(0, 8), timestampHex.substring(8, 12), '7' + randomHex.substring(0, 3), // Version 7 ((parseInt(randomHex.substring(3, 4), 16) & 0x3) | 0x8).toString(16) + randomHex.substring(4, 7), // Variant bits randomHex.substring(7, 19) ].join('-'); return uuid; } export function generateCustomerId(): string { return generateUUIDv7(); } export function generateBackupId(): string { return generateUUIDv7(); }