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.
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ import { parse } from "csv-parse";
12
12
  import pluralize2 from "pluralize";
13
13
  import stripBomStream from "strip-bom-stream";
14
14
  import { temporaryDirectory } from "tempy";
15
+ import Timer from "timer-machine";
15
16
  import untildify3 from "untildify";
16
17
  import mapSeries2 from "promise-map-series";
17
18
 
@@ -3926,41 +3927,50 @@ var createGtfsIndexes = (db) => {
3926
3927
  var formatGtfsLine = (line, model, totalLineCount) => {
3927
3928
  const lineNumber = totalLineCount + 1;
3928
3929
  const formattedLine = {};
3930
+ const filenameBase = model.filenameBase;
3931
+ const filenameExtension = model.filenameExtension;
3929
3932
  for (const columnSchema of model.schema) {
3930
- const lineValue = line[columnSchema.name];
3931
- if (columnSchema.type === "date") {
3932
- if (lineValue !== "" && lineValue !== void 0) {
3933
- const dateValue = lineValue.replace(/-/g, "");
3934
- if (dateValue.length !== 8) {
3933
+ const { name, type, required, min, max } = columnSchema;
3934
+ let value = line[name];
3935
+ if (value === "" || value === void 0 || value === null) {
3936
+ formattedLine[name] = null;
3937
+ if (required) {
3938
+ throw new Error(
3939
+ `Missing required value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}.`
3940
+ );
3941
+ }
3942
+ continue;
3943
+ }
3944
+ switch (type) {
3945
+ case "date":
3946
+ value = value.replace(/-/g, "");
3947
+ if (value.length !== 8) {
3935
3948
  throw new Error(
3936
- `Invalid date in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}.`
3949
+ `Invalid date in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}.`
3937
3950
  );
3938
3951
  }
3939
- formattedLine[columnSchema.name] = Number.parseInt(dateValue, 10);
3940
- }
3941
- } else if (columnSchema.type === "integer") {
3942
- formattedLine[columnSchema.name] = Number.parseInt(lineValue, 10);
3943
- } else if (columnSchema.type === "real") {
3944
- formattedLine[columnSchema.name] = Number.parseFloat(lineValue);
3945
- } else {
3946
- formattedLine[columnSchema.name] = lineValue;
3952
+ value = parseInt(value, 10);
3953
+ break;
3954
+ case "integer":
3955
+ value = parseInt(value, 10);
3956
+ break;
3957
+ case "real":
3958
+ value = parseFloat(value);
3959
+ break;
3947
3960
  }
3948
- if (formattedLine[columnSchema.name] === "" || formattedLine[columnSchema.name] === void 0 || formattedLine[columnSchema.name] === null || Number.isNaN(formattedLine[columnSchema.name])) {
3949
- formattedLine[columnSchema.name] = null;
3961
+ if (Number.isNaN(value)) {
3962
+ formattedLine[name] = null;
3963
+ continue;
3950
3964
  }
3951
- if (columnSchema.required === true && formattedLine[columnSchema.name] === null) {
3965
+ formattedLine[name] = value;
3966
+ if (min !== void 0 && value < min) {
3952
3967
  throw new Error(
3953
- `Missing required value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}.`
3968
+ `Invalid value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}: below minimum value of ${min}.`
3954
3969
  );
3955
3970
  }
3956
- if (columnSchema.min !== void 0 && formattedLine[columnSchema.name] < columnSchema.min) {
3971
+ if (max !== void 0 && value > max) {
3957
3972
  throw new Error(
3958
- `Invalid value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}: below minimum value of ${columnSchema.min}.`
3959
- );
3960
- }
3961
- if (columnSchema.max !== void 0 && formattedLine[columnSchema.name] > columnSchema.max) {
3962
- throw new Error(
3963
- `Invalid value in ${model.filenameBase}.${model.filenameExtension} for ${columnSchema.name} on line ${lineNumber}: above maximum value of ${columnSchema.max}.`
3973
+ `Invalid value in ${filenameBase}.${filenameExtension} for ${name} on line ${lineNumber}: above maximum value of ${max}.`
3964
3974
  );
3965
3975
  }
3966
3976
  }
@@ -3974,6 +3984,7 @@ var formatGtfsLine = (line, model, totalLineCount) => {
3974
3984
  }
3975
3985
  return formattedLine;
3976
3986
  };
3987
+ var BATCH_SIZE = 1e5;
3977
3988
  var importGtfsFiles = (db, task) => mapSeries2(
3978
3989
  Object.values(models_exports),
3979
3990
  (model) => new Promise((resolve, reject) => {
@@ -4047,19 +4058,37 @@ var importGtfsFiles = (db, task) => mapSeries2(
4047
4058
  while (record = parser.read()) {
4048
4059
  totalLineCount += 1;
4049
4060
  lines.push(formatGtfsLine(record, model, totalLineCount));
4061
+ if (lines.length >= BATCH_SIZE) {
4062
+ try {
4063
+ insertLines(lines);
4064
+ lines = [];
4065
+ } catch (error) {
4066
+ reject(error);
4067
+ }
4068
+ task.log(
4069
+ `Importing - ${filename} - ${totalLineCount} lines imported\r`,
4070
+ true
4071
+ );
4072
+ }
4050
4073
  }
4051
4074
  });
4052
4075
  parser.on("end", () => {
4053
4076
  try {
4054
- insertLines(lines);
4077
+ if (lines.length > 0) {
4078
+ try {
4079
+ insertLines(lines);
4080
+ } catch (error) {
4081
+ reject(error);
4082
+ }
4083
+ }
4055
4084
  task.log(
4056
4085
  `Importing - ${filename} - ${totalLineCount} lines imported\r`,
4057
4086
  true
4058
4087
  );
4088
+ resolve();
4059
4089
  } catch (error) {
4060
4090
  reject(error);
4061
4091
  }
4062
- resolve();
4063
4092
  });
4064
4093
  parser.on("error", reject);
4065
4094
  createReadStream(filepath).pipe(stripBomStream()).pipe(parser);
@@ -4089,6 +4118,8 @@ var importGtfsFiles = (db, task) => mapSeries2(
4089
4118
  })
4090
4119
  );
4091
4120
  async function importGtfs(initialConfig) {
4121
+ const timer = new Timer();
4122
+ timer.start();
4092
4123
  const config = setDefaultConfig(initialConfig);
4093
4124
  validateConfigForImport(config);
4094
4125
  const log2 = log(config);
@@ -4138,8 +4169,10 @@ async function importGtfs(initialConfig) {
4138
4169
  });
4139
4170
  log2(`Creating DB indexes`);
4140
4171
  createGtfsIndexes(db);
4172
+ const seconds = Math.round(timer.time() / 1e3);
4173
+ timer.stop();
4141
4174
  log2(
4142
- `Completed GTFS import for ${pluralize2("agency", agencyCount, true)}
4175
+ `Completed GTFS import for ${pluralize2("agency", agencyCount, true)} in ${seconds} seconds
4143
4176
  `
4144
4177
  );
4145
4178
  } catch (error) {