forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-user-emails.ts
More file actions
56 lines (48 loc) · 1.47 KB
/
Copy pathexport-user-emails.ts
File metadata and controls
56 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { writeFileSync } from 'fs'
import { pluralize } from '@codebuff/common/util/string'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
async function exportUserEmails(): Promise<void> {
console.log('Exporting user emails...\n')
try {
const users = await db
.select({
email: schema.user.email,
name: schema.user.name,
created_at: schema.user.created_at,
})
.from(schema.user)
.orderBy(schema.user.created_at)
// Create CSV content
const headers = ['Email', 'Name', 'Created At']
const rows = users.map((user) => [
user.email,
user.name || '',
user.created_at?.toISOString() || '',
])
const csvContent = [
headers.join(','),
...rows.map((row) =>
row
.map((field) =>
// Escape fields that contain commas or quotes
field.includes(',') || field.includes('"')
? `"${field.replace(/"/g, '""')}"`
: field,
)
.join(','),
),
].join('\n')
// Write to file with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
const filename = `user-emails-${timestamp}.csv`
writeFileSync(filename, csvContent)
console.log(
`\nExported ${pluralize(users.length, 'user email')} to ${filename}`,
)
} catch (error) {
console.error('Error exporting user emails:', error)
}
}
// Run the script
exportUserEmails()