()
| 5 | import * as schema from '@codebuff/internal/db/schema' |
| 6 | |
| 7 | async function exportUserEmails(): Promise<void> { |
| 8 | console.log('Exporting user emails...\n') |
| 9 | |
| 10 | try { |
| 11 | const users = await db |
| 12 | .select({ |
| 13 | email: schema.user.email, |
| 14 | name: schema.user.name, |
| 15 | created_at: schema.user.created_at, |
| 16 | }) |
| 17 | .from(schema.user) |
| 18 | .orderBy(schema.user.created_at) |
| 19 | |
| 20 | // Create CSV content |
| 21 | const headers = ['Email', 'Name', 'Created At'] |
| 22 | const rows = users.map((user) => [ |
| 23 | user.email, |
| 24 | user.name || '', |
| 25 | user.created_at?.toISOString() || '', |
| 26 | ]) |
| 27 | |
| 28 | const csvContent = [ |
| 29 | headers.join(','), |
| 30 | ...rows.map((row) => |
| 31 | row |
| 32 | .map((field) => |
| 33 | // Escape fields that contain commas or quotes |
| 34 | field.includes(',') || field.includes('"') |
| 35 | ? `"${field.replace(/"/g, '""')}"` |
| 36 | : field, |
| 37 | ) |
| 38 | .join(','), |
| 39 | ), |
| 40 | ].join('\n') |
| 41 | |
| 42 | // Write to file with timestamp |
| 43 | const timestamp = new Date().toISOString().replace(/[:.]/g, '-') |
| 44 | const filename = `user-emails-${timestamp}.csv` |
| 45 | writeFileSync(filename, csvContent) |
| 46 | |
| 47 | console.log( |
| 48 | `\nExported ${pluralize(users.length, 'user email')} to ${filename}`, |
| 49 | ) |
| 50 | } catch (error) { |
| 51 | console.error('Error exporting user emails:', error) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Run the script |
| 56 | exportUserEmails() |
no test coverage detected