gtfs 4.15.0 → 4.15.2

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.
@@ -143,6 +143,7 @@ import { parse } from "csv-parse";
143
143
  import pluralize2 from "pluralize";
144
144
  import stripBomStream from "strip-bom-stream";
145
145
  import { temporaryDirectory } from "tempy";
146
+ import Timer from "timer-machine";
146
147
  import untildify3 from "untildify";
147
148
  import mapSeries2 from "promise-map-series";
148
149
 
@@ -3709,41 +3710,50 @@ var createGtfsIndexes = (db) => {
3709
3710
  var formatGtfsLine = (line, model, totalLineCount) => {
3710
3711
  const lineNumber = totalLineCount + 1;
3711
3712
  const formattedLine = {};
3713
+ const filenameBase = model.filenameBase;
3714
+ const filenameExtension = model.filenameExtension;
3712
3715
  for (const columnSchema of model.schema) {
3713
- const lineValue = line[columnSchema.name];
3714
- if (columnSchema.type === "date") {
3715
- if (lineValue !== "" && lineValue !== void 0) {
3716
- const dateValue = lineValue.replace(/-/g, "");
3717
- if (dateValue.length !== 8) {
3716
+ const { name, type, required, min, max } = columnSchema;
3717
+ let value = line[name];
3718
+ if (value === "" || value === void 0 || value === null) {
3719
+ formattedLine[name] = null;
3720
+ if (required) {
3721
+ throw new Error(
3722
+ `Missing required value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}.`
3723
+ );
3724
+ }
3725
+ continue;
3726
+ }
3727
+ switch (type) {
3728
+ case "date":
3729
+ value = value.replace(/-/g, "");
3730
+ if (value.length !== 8) {
3718
3731
  throw new Error(
3719
- `Invalid date in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}.`
3732
+ `Invalid date in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}.`
3720
3733
  );
3721
3734
  }
3722
- formattedLine[columnSchema.name] = Number.parseInt(dateValue, 10);
3723
- }
3724
- } else if (columnSchema.type === "integer") {
3725
- formattedLine[columnSchema.name] = Number.parseInt(lineValue, 10);
3726
- } else if (columnSchema.type === "real") {
3727
- formattedLine[columnSchema.name] = Number.parseFloat(lineValue);
3728
- } else {
3729
- formattedLine[columnSchema.name] = lineValue;
3730
- }
3731
- if (formattedLine[columnSchema.name] === "" || formattedLine[columnSchema.name] === void 0 || formattedLine[columnSchema.name] === null || Number.isNaN(formattedLine[columnSchema.name])) {
3732
- formattedLine[columnSchema.name] = null;
3733
- }
3734
- if (columnSchema.required === true && formattedLine[columnSchema.name] === null) {
3735
+ value = parseInt(value, 10);
3736
+ break;
3737
+ case "integer":
3738
+ value = parseInt(value, 10);
3739
+ break;
3740
+ case "real":
3741
+ value = parseFloat(value);
3742
+ break;
3743
+ }
3744
+ if (Number.isNaN(value)) {
3745
+ formattedLine[name] = null;
3746
+ continue;
3747
+ }
3748
+ formattedLine[name] = value;
3749
+ if (min !== void 0 && value < min) {
3735
3750
  throw new Error(
3736
- `Missing required value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}.`
3751
+ `Invalid value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}: below minimum value of ${min}.`
3737
3752
  );
3738
3753
  }
3739
- if (columnSchema.min !== void 0 && formattedLine[columnSchema.name] < columnSchema.min) {
3754
+ if (max !== void 0 && value > max) {
3740
3755
  throw new Error(
3741
- `Invalid value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}: below minimum value of ${columnSchema.min}.`
3742
- );
3743
- }
3744
- if (columnSchema.max !== void 0 && formattedLine[columnSchema.name] > columnSchema.max) {
3745
- throw new Error(
3746
- `Invalid value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}: above maximum value of ${columnSchema.max}.`
3756
+ `Invalid value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}: above maximum value of ${max}.`
3747
3757
  );
3748
3758
  }
3749
3759
  }
@@ -3757,6 +3767,7 @@ var formatGtfsLine = (line, model, totalLineCount) => {
3757
3767
  }
3758
3768
  return formattedLine;
3759
3769
  };
3770
+ var BATCH_SIZE = 1e5;
3760
3771
  var importGtfsFiles = (db, task) => mapSeries2(
3761
3772
  Object.values(models_exports),
3762
3773
  (model) => new Promise((resolve, reject) => {
@@ -3830,19 +3841,37 @@ var importGtfsFiles = (db, task) => mapSeries2(
3830
3841
  while (record = parser.read()) {
3831
3842
  totalLineCount += 1;
3832
3843
  lines.push(formatGtfsLine(record, model, totalLineCount));
3844
+ if (lines.length >= BATCH_SIZE) {
3845
+ try {
3846
+ insertLines(lines);
3847
+ lines = [];
3848
+ } catch (error) {
3849
+ reject(error);
3850
+ }
3851
+ task.log(
3852
+ `Importing - ${filename} - ${totalLineCount} lines imported\r`,
3853
+ true
3854
+ );
3855
+ }
3833
3856
  }
3834
3857
  });
3835
3858
  parser.on("end", () => {
3836
3859
  try {
3837
- insertLines(lines);
3860
+ if (lines.length > 0) {
3861
+ try {
3862
+ insertLines(lines);
3863
+ } catch (error) {
3864
+ reject(error);
3865
+ }
3866
+ }
3838
3867
  task.log(
3839
3868
  `Importing - ${filename} - ${totalLineCount} lines imported\r`,
3840
3869
  true
3841
3870
  );
3871
+ resolve();
3842
3872
  } catch (error) {
3843
3873
  reject(error);
3844
3874
  }
3845
- resolve();
3846
3875
  });
3847
3876
  parser.on("error", reject);
3848
3877
  createReadStream(filepath).pipe(stripBomStream()).pipe(parser);
@@ -3872,6 +3901,8 @@ var importGtfsFiles = (db, task) => mapSeries2(
3872
3901
  })
3873
3902
  );
3874
3903
  async function importGtfs(initialConfig) {
3904
+ const timer = new Timer();
3905
+ timer.start();
3875
3906
  const config = setDefaultConfig(initialConfig);
3876
3907
  validateConfigForImport(config);
3877
3908
  const log2 = log(config);
@@ -3921,8 +3952,10 @@ async function importGtfs(initialConfig) {
3921
3952
  });
3922
3953
  log2(`Creating DB indexes`);
3923
3954
  createGtfsIndexes(db);
3955
+ const seconds = Math.round(timer.time() / 1e3);
3956
+ timer.stop();
3924
3957
  log2(
3925
- `Completed GTFS import for ${pluralize2("agency", agencyCount, true)}
3958
+ `Completed GTFS import for ${pluralize2("agency", agencyCount, true)} in ${seconds} seconds
3926
3959
  `
3927
3960
  );
3928
3961
  } catch (error) {