@tmlmobilidade/import-gtfs 20251009.1357.48 → 20251010.751.17

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.d.ts CHANGED
@@ -1,5 +1,15 @@
1
- import { type GtfsSQLTables, type ImportGtfsToDatabaseConfig } from './src/types.js';
2
- import { type Plan } from '@tmlmobilidade/types';
1
+ import { type GtfsSQLTables } from './src/types.js';
2
+ import { type OperationalDate, type Plan } from '@tmlmobilidade/types';
3
+ /**
4
+ * Configuration options for importing GTFS data into a database.
5
+ */
6
+ export interface ImportGtfsToDatabaseConfig {
7
+ date_range?: {
8
+ end: OperationalDate;
9
+ start: OperationalDate;
10
+ };
11
+ discrete_dates?: OperationalDate[];
12
+ }
3
13
  /**
4
14
  * Imports GTFS data into the database for a given plan.
5
15
  * @param plan The plan containing GTFS feed information.
package/dist/index.js CHANGED
@@ -45,8 +45,8 @@ export async function importGtfsToDatabase(plan, config = {}) {
45
45
  }
46
46
  //
47
47
  // Process GTFS files in the correct order
48
- await processCalendarFile(context, config.start_date ?? plan.gtfs_feed_info.feed_start_date, config.end_date ?? plan.gtfs_feed_info.feed_end_date);
49
- await processCalendarDatesFile(context, config.start_date ?? plan.gtfs_feed_info.feed_start_date, config.end_date ?? plan.gtfs_feed_info.feed_end_date);
48
+ await processCalendarFile(context, config);
49
+ await processCalendarDatesFile(context, config);
50
50
  await processTripsFile(context);
51
51
  await processRoutesFile(context);
52
52
  await processShapesFile(context);
@@ -1,5 +1,5 @@
1
1
  import { type ImportGtfsContext } from '../types.js';
2
- import { OperationalDate } from '@tmlmobilidade/types';
2
+ import { type ImportGtfsToDatabaseConfig } from '../../index.js';
3
3
  /**
4
4
  * Processes the calendar.txt file from the GTFS dataset.
5
5
  * It extracts service_ids that are valid between the given start_date and end_date,
@@ -8,4 +8,4 @@ import { OperationalDate } from '@tmlmobilidade/types';
8
8
  * @param startDate The start date of the range to filter service_ids.
9
9
  * @param endDate The end date of the range to filter service_ids.
10
10
  */
11
- export declare function processCalendarFile(context: ImportGtfsContext, startDate: OperationalDate, endDate: OperationalDate): Promise<void>;
11
+ export declare function processCalendarFile(context: ImportGtfsContext, config: ImportGtfsToDatabaseConfig): Promise<void>;
@@ -12,7 +12,7 @@ import fs from 'node:fs';
12
12
  * @param startDate The start date of the range to filter service_ids.
13
13
  * @param endDate The end date of the range to filter service_ids.
14
14
  */
15
- export async function processCalendarFile(context, startDate, endDate) {
15
+ export async function processCalendarFile(context, config) {
16
16
  try {
17
17
  //
18
18
  const calendarParseTimer = new TIMETRACKER();
@@ -23,21 +23,39 @@ export async function processCalendarFile(context, startDate, endDate) {
23
23
  // Validate the current row against the proper type
24
24
  const validatedData = validateGtfsCalendar(data);
25
25
  //
26
- // Check if this service_id is between the given start_date and end_date.
27
- // Clip the service_id's start and end dates to the given start and end dates.
28
- let serviceIdStartDate = validatedData.start_date;
29
- let serviceIdEndDate = validatedData.end_date;
30
- if (serviceIdEndDate < startDate || serviceIdStartDate > endDate)
31
- return;
32
- if (serviceIdStartDate < startDate)
33
- serviceIdStartDate = startDate;
34
- if (serviceIdEndDate > endDate)
35
- serviceIdEndDate = endDate;
26
+ // Setup an array to keep track of the valid operational dates for this service_id
27
+ // for the given start_date and end_date or single dates from the config.
28
+ const allOperationalDatesInRange = [];
29
+ //
30
+ // If the config is of date-range type, check if this service_id
31
+ // is between the given start_date and end_date. Clip the service_id's
32
+ // start and end dates to the given start and end dates.
33
+ if (config.date_range?.start && config.date_range?.end) {
34
+ let serviceIdStartDate = validatedData.start_date;
35
+ let serviceIdEndDate = validatedData.end_date;
36
+ if (serviceIdEndDate < config.date_range.start || serviceIdStartDate > config.date_range.end)
37
+ return;
38
+ if (serviceIdStartDate < config.date_range.start)
39
+ serviceIdStartDate = config.date_range.start;
40
+ if (serviceIdEndDate > config.date_range.end)
41
+ serviceIdEndDate = config.date_range.end;
42
+ const operationalDates = getOperationalDatesFromRange(serviceIdStartDate, serviceIdEndDate);
43
+ allOperationalDatesInRange.push(...operationalDates);
44
+ }
45
+ //
46
+ // If the config is of discrete-dates type, get the operational dates
47
+ // for this service_id that are in the given discrete dates array.
48
+ if (config.discrete_dates?.length) {
49
+ config.discrete_dates.forEach((date) => {
50
+ if (date >= validatedData.start_date && date <= validatedData.end_date) {
51
+ allOperationalDatesInRange.push(date);
52
+ }
53
+ });
54
+ }
36
55
  //
37
56
  // If we're here, it means the service_id is valid between the given dates.
38
57
  // For the configured weekly schedule, create the individual operational dates
39
58
  // for each day of the week that is active.
40
- const allOperationalDatesInRange = getOperationalDatesFromRange(serviceIdStartDate, serviceIdEndDate);
41
59
  const validOperationalDates = new Set();
42
60
  for (const currentDate of allOperationalDatesInRange) {
43
61
  const dayOfWeek = Dates.fromOperationalDate(currentDate, 'Europe/Lisbon').toFormat('c');
@@ -1,5 +1,5 @@
1
1
  import { type ImportGtfsContext } from '../types.js';
2
- import { OperationalDate } from '@tmlmobilidade/types';
2
+ import { type ImportGtfsToDatabaseConfig } from '../../index.js';
3
3
  /**
4
4
  * Processes the calendar_dates.txt file from the GTFS dataset.
5
5
  * It extracts service_ids that are valid between the given start_date and end_date,
@@ -8,4 +8,4 @@ import { OperationalDate } from '@tmlmobilidade/types';
8
8
  * @param startDate The start date of the range to filter service_ids.
9
9
  * @param endDate The end date of the range to filter service_ids.
10
10
  */
11
- export declare function processCalendarDatesFile(context: ImportGtfsContext, startDate: OperationalDate, endDate: OperationalDate): Promise<void>;
11
+ export declare function processCalendarDatesFile(context: ImportGtfsContext, config: ImportGtfsToDatabaseConfig): Promise<void>;
@@ -12,7 +12,7 @@ import fs from 'node:fs';
12
12
  * @param startDate The start date of the range to filter service_ids.
13
13
  * @param endDate The end date of the range to filter service_ids.
14
14
  */
15
- export async function processCalendarDatesFile(context, startDate, endDate) {
15
+ export async function processCalendarDatesFile(context, config) {
16
16
  try {
17
17
  //
18
18
  const calendarDatesParseTimer = new TIMETRACKER();
@@ -24,8 +24,18 @@ export async function processCalendarDatesFile(context, startDate, endDate) {
24
24
  const validatedData = validateGtfsCalendarDate(data);
25
25
  //
26
26
  // Skip if this row's date is not between the given start and end dates
27
- if (validatedData.date < startDate || validatedData.date > endDate)
28
- return;
27
+ // if they are provided in the config.
28
+ if (config.date_range?.start && config.date_range?.end) {
29
+ if (validatedData.date < config.date_range.start || validatedData.date > config.date_range.end)
30
+ return;
31
+ }
32
+ //
33
+ // Skip if this row's date is not in the given discrete dates array
34
+ // if it is provided in the config.
35
+ if (config.discrete_dates?.length) {
36
+ if (!config.discrete_dates.includes(validatedData.date))
37
+ return;
38
+ }
29
39
  //
30
40
  // If we're here, it means the service_id is valid between the given dates.
31
41
  // Get the previously saved calendars and check if it exists for this service_id.
@@ -1,13 +1,6 @@
1
1
  import { SQLiteWriter } from '@tmlmobilidade/connectors';
2
2
  import { type GTFS_Route_Extended, type GTFS_Shape, type GTFS_Stop_Extended, type GTFS_StopTime, type GTFS_Trip_Extended, type Plan } from '@tmlmobilidade/types';
3
3
  import { type OperationalDate } from '@tmlmobilidade/types';
4
- /**
5
- * Configuration options for importing GTFS data into a database.
6
- */
7
- export interface ImportGtfsToDatabaseConfig {
8
- end_date?: OperationalDate;
9
- start_date?: OperationalDate;
10
- }
11
4
  /**
12
5
  * Holds references to all GTFS-related SQL tables and writers.
13
6
  * Each property corresponds to a specific GTFS entity and is associated
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/import-gtfs",
3
- "version": "20251009.1357.48",
3
+ "version": "20251010.751.17",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",