(
args: string[] = process.argv.slice(2),
runOptions: RunImportOptions = {},
)
| 201 | } |
| 202 | |
| 203 | export const runImportEvents = async ( |
| 204 | args: string[] = process.argv.slice(2), |
| 205 | runOptions: RunImportOptions = {}, |
| 206 | ): Promise<number> => { |
| 207 | const options = parseCliArgs(args) |
| 208 | |
| 209 | if (options.showHelp) { |
| 210 | printUsage() |
| 211 | return 0 |
| 212 | } |
| 213 | |
| 214 | const inputFile = await ensureValidInputFile(options.filePath) |
| 215 | |
| 216 | if (inputFile.compressionFormat && inputFile.format === 'json') { |
| 217 | throw new Error('Compressed JSON array import is not supported. Use .json (uncompressed) or .jsonl.gz/.jsonl.xz.') |
| 218 | } |
| 219 | |
| 220 | const dbClient = getMasterDbClient() |
| 221 | const eventRepository = new EventRepository(dbClient, dbClient) |
| 222 | const importer = new EventImportService(createEventBatchPersister(eventRepository)) |
| 223 | |
| 224 | let loggedErrors = 0 |
| 225 | let suppressedErrors = 0 |
| 226 | |
| 227 | const onLineError = ({ lineNumber, reason }: EventImportLineError) => { |
| 228 | if (loggedErrors < MAX_ERROR_LOGS) { |
| 229 | console.warn(`[line ${lineNumber}] ${reason}`) |
| 230 | loggedErrors += 1 |
| 231 | return |
| 232 | } |
| 233 | |
| 234 | suppressedErrors += 1 |
| 235 | } |
| 236 | |
| 237 | const onProgress = (stats: EventImportStats) => { |
| 238 | console.log(formatProgress(stats)) |
| 239 | } |
| 240 | |
| 241 | const startedAt = Date.now() |
| 242 | |
| 243 | try { |
| 244 | if (inputFile.compressionFormat) { |
| 245 | console.log(`Detected ${inputFile.compressionFormat} compression. Decompressing on-the-fly...`) |
| 246 | } |
| 247 | |
| 248 | const stats = |
| 249 | inputFile.format === 'json' |
| 250 | ? await importer.importFromJsonArray(inputFile.absolutePath, { |
| 251 | batchSize: options.batchSize, |
| 252 | onLineError, |
| 253 | onProgress, |
| 254 | }) |
| 255 | : await importer.importFromReadable(createImportStream(inputFile), { |
| 256 | batchSize: options.batchSize, |
| 257 | onLineError, |
| 258 | onProgress, |
| 259 | }) |
| 260 |
no test coverage detected