@toiroakr/lines-db 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -375,6 +375,7 @@ var LinesDB = class LinesDB {
375
375
  async initialize(options) {
376
376
  const allErrors = [];
377
377
  const allWarnings = [];
378
+ const allRowCounts = /* @__PURE__ */ new Map();
378
379
  const tableName = options?.tableName;
379
380
  const detailedValidate = options?.detailedValidate ?? false;
380
381
  const transform = options?.transform;
@@ -386,14 +387,27 @@ var LinesDB = class LinesDB {
386
387
  const attemptedTables = /* @__PURE__ */ new Set();
387
388
  for (const tableNameToLoad of tablesToLoad) if (!attemptedTables.has(tableNameToLoad)) {
388
389
  const tableTransform = tableNameToLoad === tableName ? transform : void 0;
389
- const { errors, warnings } = await this.loadTableWithDependencies(tableNameToLoad, loadedTables, loadingTables, attemptedTables, detailedValidate, tableTransform);
390
+ const { errors, warnings, rowCounts: tableRowCounts } = await this.loadTableWithDependencies(tableNameToLoad, loadedTables, loadingTables, attemptedTables, detailedValidate, tableTransform);
390
391
  allErrors.push(...errors);
391
392
  allWarnings.push(...warnings);
393
+ for (const [k, v] of tableRowCounts) allRowCounts.set(k, v);
392
394
  }
395
+ const tableResults = tablesToLoad.map((name) => {
396
+ const tableErrors = allErrors.filter((e) => e.tableName === name);
397
+ const tableWarnings = allWarnings.filter((w) => w.includes(`'${name}'`));
398
+ return {
399
+ tableName: name,
400
+ valid: tableErrors.length === 0,
401
+ rowCount: allRowCounts.get(name) ?? 0,
402
+ errors: tableErrors,
403
+ warnings: tableWarnings
404
+ };
405
+ });
393
406
  return {
394
407
  valid: allErrors.length === 0,
395
408
  errors: allErrors,
396
- warnings: allWarnings
409
+ warnings: allWarnings,
410
+ tableResults
397
411
  };
398
412
  }
399
413
  /**
@@ -402,9 +416,11 @@ var LinesDB = class LinesDB {
402
416
  async loadTableWithDependencies(tableName, loadedTables, loadingTables, attemptedTables, detailedValidate, transform) {
403
417
  const errors = [];
404
418
  const warnings = [];
419
+ const rowCounts = /* @__PURE__ */ new Map();
405
420
  if (attemptedTables.has(tableName)) return {
406
421
  errors,
407
- warnings
422
+ warnings,
423
+ rowCounts
408
424
  };
409
425
  attemptedTables.add(tableName);
410
426
  if (loadingTables.has(tableName)) throw new Error(`Circular dependency detected for table '${tableName}'`);
@@ -428,10 +444,21 @@ var LinesDB = class LinesDB {
428
444
  const depResult = await this.loadTableWithDependencies(referencedTable, loadedTables, loadingTables, attemptedTables, detailedValidate, void 0);
429
445
  errors.push(...depResult.errors);
430
446
  warnings.push(...depResult.warnings);
447
+ for (const [k, v] of depResult.rowCounts) rowCounts.set(k, v);
431
448
  } else throw new Error(`Foreign key reference to non-existent table '${referencedTable}' in table '${tableName}'`);
432
449
  }
433
- const { loaded, errors: loadErrors } = await this.loadTable(tableName, tableConfig, detailedValidate, transform);
450
+ const failedDependencies = /* @__PURE__ */ new Set();
451
+ if (foreignKeys && foreignKeys.length > 0) {
452
+ for (const fk of foreignKeys) {
453
+ const referencedTable = fk.references.table;
454
+ if (referencedTable === tableName) continue;
455
+ if (attemptedTables.has(referencedTable) && !loadedTables.has(referencedTable)) failedDependencies.add(referencedTable);
456
+ }
457
+ if (failedDependencies.size > 0) for (const dep of failedDependencies) warnings.push(`Skipping foreign key validation for table '${tableName}': referenced table '${dep}' has validation errors`);
458
+ }
459
+ const { loaded, rowCount, errors: loadErrors } = await this.loadTable(tableName, tableConfig, detailedValidate, transform, failedDependencies);
434
460
  errors.push(...loadErrors);
461
+ rowCounts.set(tableName, rowCount);
435
462
  if (loaded) loadedTables.add(tableName);
436
463
  else {
437
464
  warnings.push(`Table '${tableName}' was not loaded (no data or skipped)`);
@@ -442,14 +469,15 @@ var LinesDB = class LinesDB {
442
469
  }
443
470
  return {
444
471
  errors,
445
- warnings
472
+ warnings,
473
+ rowCounts
446
474
  };
447
475
  }
448
476
  /**
449
477
  * Load a single table from JSONL file
450
478
  * @returns Object with loaded status and validation errors
451
479
  */
452
- async loadTable(tableName, config, detailedValidate, transform) {
480
+ async loadTable(tableName, config, detailedValidate, transform, failedDependencies) {
453
481
  let data = await JsonlReader.read(config.jsonlPath);
454
482
  if (transform) data = data.map((row) => transform(row));
455
483
  let validationSchema = config.validationSchema;
@@ -504,6 +532,7 @@ var LinesDB = class LinesDB {
504
532
  }));
505
533
  if (validationErrors.length > 0) return {
506
534
  loaded: false,
535
+ rowCount: data.length,
507
536
  errors: validationErrorDetails
508
537
  };
509
538
  let schema;
@@ -518,6 +547,7 @@ var LinesDB = class LinesDB {
518
547
  } else if (config.autoInferSchema !== false) {
519
548
  if (validatedData.length === 0) return {
520
549
  loaded: false,
550
+ rowCount: 0,
521
551
  errors: []
522
552
  };
523
553
  schema = inferredSchema;
@@ -533,7 +563,7 @@ var LinesDB = class LinesDB {
533
563
  const idColumn = schema.columns.find((c) => c.name === "id");
534
564
  if (idColumn) idColumn.primaryKey = true;
535
565
  }
536
- if (foreignKeys) schema.foreignKeys = foreignKeys;
566
+ if (foreignKeys) schema.foreignKeys = failedDependencies && failedDependencies.size > 0 ? foreignKeys.filter((fk) => !failedDependencies.has(fk.references.table)) : foreignKeys;
537
567
  if (indexes) {
538
568
  schema.indexes = indexes;
539
569
  for (const index of indexes) if (index.unique && index.columns.length === 1) {
@@ -547,11 +577,13 @@ var LinesDB = class LinesDB {
547
577
  const insertErrors = this.insertDataWithDetailedValidation(tableName, schema, validatedData, config.jsonlPath);
548
578
  if (insertErrors.length > 0) return {
549
579
  loaded: false,
580
+ rowCount: data.length,
550
581
  errors: insertErrors
551
582
  };
552
583
  } else this.insertData(tableName, schema, validatedData);
553
584
  return {
554
585
  loaded: true,
586
+ rowCount: data.length,
555
587
  errors: []
556
588
  };
557
589
  }