-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-emails-from-file.ts
More file actions
138 lines (114 loc) · 3.83 KB
/
Copy pathadd-emails-from-file.ts
File metadata and controls
138 lines (114 loc) · 3.83 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import mongoose from 'mongoose'
import fs from 'fs'
import path from 'path'
import { config } from './src/config/environment'
import { EmailList } from './src/models'
import logger from './src/utils/logger'
const connectDB = async (): Promise<void> => {
try {
const conn = await mongoose.connect(config.MONGODB_URI!, {
dbName: config.MONGODB_DB_NAME,
})
logger.info(`MongoDB Connected: ${conn.connection.host}`)
} catch (error) {
logger.error('Error connecting to MongoDB:', error)
process.exit(1)
}
}
const disconnectDB = async (): Promise<void> => {
try {
await mongoose.connection.close()
logger.info('MongoDB connection closed')
} catch (error) {
logger.error('Error closing MongoDB connection:', error)
}
}
const readEmailsFromFile = (filePath: string): string[] => {
try {
const content = fs.readFileSync(filePath, 'utf8')
// Split by newlines and filter out empty lines
const emails = content
.split('\n')
.map(email => email.trim())
.filter(email => email && email.includes('@'))
logger.info(`Read ${emails.length} valid emails from file`)
return emails
} catch (error) {
logger.error(`Error reading file ${filePath}:`, error)
throw error
}
}
const addEmailsWithIndex = async (emails: string[], clearExisting: boolean = false): Promise<void> => {
try {
logger.info('Starting to add emails with indexes...')
if (clearExisting) {
await EmailList.deleteMany({})
logger.info('Cleared existing email list')
}
const batchSize = 25
const totalEmails = emails.length
for (let i = 0; i < totalEmails; i += batchSize) {
const batch = emails.slice(i, i + batchSize)
const index = Math.floor(i / batchSize)
logger.info(`Processing batch ${index + 1}: emails ${i + 1}-${Math.min(i + batchSize, totalEmails)} with index ${index}`)
const emailDocuments = batch.map(email => ({
email: email.toLowerCase().trim(),
index: index
}))
// Insert batch
const result = await EmailList.insertMany(emailDocuments, { ordered: false })
logger.info(`Successfully added ${result.length} emails with index ${index}`)
}
// Verify the results
const totalAdded = await EmailList.countDocuments()
logger.info(`Total emails in database: ${totalAdded}`)
// Show distribution by index
const distribution = await EmailList.aggregate([
{ $group: { _id: '$index', count: { $sum: 1 } } },
{ $sort: { _id: 1 } }
])
logger.info('Index distribution:')
distribution.forEach(item => {
logger.info(`Index ${item._id}: ${item.count} emails`)
})
} catch (error) {
logger.error('Error adding emails:', error)
throw error
}
}
const main = async (): Promise<void> => {
try {
// Check command line arguments
const args = process.argv.slice(2)
const filePath = args[0]
const clearExisting = args.includes('--clear')
if (!filePath) {
logger.error('Usage: ts-node add-emails-from-file.ts <email-file-path> [--clear]')
logger.error('Example: ts-node add-emails-from-file.ts emails.txt --clear')
process.exit(1)
}
if (!fs.existsSync(filePath)) {
logger.error(`File not found: ${filePath}`)
process.exit(1)
}
await connectDB()
const emails = readEmailsFromFile(filePath)
if (emails.length === 0) {
logger.warn('No valid emails found in file')
return
}
await addEmailsWithIndex(emails, clearExisting)
logger.info('Script completed successfully!')
} catch (error) {
logger.error('Script failed:', error)
process.exit(1)
} finally {
await disconnectDB()
process.exit(0)
}
}
// Run the script
if (require.main === module) {
main()
}
export { addEmailsWithIndex, readEmailsFromFile }