export async function getAvailableUsernameSuggestions(base: string, count = 3, maxTries = 10) {What it does:
- Declares an
asyncfunction (can wait for database operations) - Takes 3 parameters:
base: the starting username (like "john")count: how many suggestions to return (default: 3)maxTries: maximum attempts to find usernames (default: 10)
const suggestions: string[] = [];What it does:
- Creates an empty array to store the available usernames we find
- TypeScript notation
: string[]means it only holds strings
for (let i = 1; suggestions.length < count && i < maxTries; i++) {What it does:
- Starts a loop with counter
i = 1 - Continues while BOTH conditions are true:
- We haven't found enough suggestions yet (
suggestions.length < count) - We haven't exceeded our attempt limit (
i < maxTries)
- We haven't found enough suggestions yet (
- Increments
iafter each attempt
const candidate = `${base}${i}`;What it does:
- Creates a potential username by combining the base with the current number
- Example: if
base = "john"andi = 1, thencandidate = "john1"
const exists = await db.user.findUnique({ where: { username: candidate } });What it does:
- Searches the database to see if this username already exists
awaitpauses the function until the database responds- Returns the user record if found, or
nullif not found
if (!exists) suggestions.push(candidate);What it does:
- If the username doesn't exist in the database (
!existsmeans "not exists") - Adds the available username to our suggestions array
return suggestions;What it does:
- Returns the array of available usernames we found
If you call getAvailableUsernameSuggestions("sarah", 3):
- Try "sarah1" → Check database → If available, add to suggestions
- Try "sarah2" → Check database → If available, add to suggestions
- Try "sarah3" → Check database → If available, add to suggestions
- Continue until we have 3 suggestions OR we've tried 10 times
Possible result: ["sarah1", "sarah3", "sarah7"] (if "sarah2" was taken)
- Smart stopping: Stops when it finds enough suggestions, doesn't waste time
- Prevents infinite loops: Won't try forever if most usernames are taken
- Efficient: Only checks database for usernames that might work
- Flexible: You can customize how many suggestions and how many attempts