Skip to content

Commit 72af609

Browse files
authored
Added TsDoc to interfaces (#193)
1 parent 3c1ec7f commit 72af609

2 files changed

Lines changed: 162 additions & 14 deletions

File tree

src/lib/import-gtfs.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ import {
2222
validateConfigForImport,
2323
} from './utils.ts';
2424

25-
import { Config, ConfigAgency, Model } from '../types/global_interfaces.ts';
25+
import {
26+
Config,
27+
ConfigAgency,
28+
Model,
29+
TableNames,
30+
} from '../types/global_interfaces.ts';
2631

2732
interface GtfsImportTask {
28-
exclude?: string[];
33+
exclude?: TableNames[];
2934
url?: string;
3035
headers?: Record<string, string>;
3136
realtimeAlerts?: {
@@ -198,11 +203,11 @@ const createGtfsTables = (db: Database.Database): void => {
198203
if (column.type === 'time') {
199204
sqlColumnCreateStatements.push(
200205
`${getTimestampColumnName(column.name)} INTEGER GENERATED ALWAYS AS (
201-
CASE
202-
WHEN ${column.name} IS NULL OR ${column.name} = '' THEN NULL
206+
CASE
207+
WHEN ${column.name} IS NULL OR ${column.name} = '' THEN NULL
203208
ELSE CAST(
204-
substr(${column.name}, 1, instr(${column.name}, ':') - 1) * 3600 +
205-
substr(${column.name}, instr(${column.name}, ':') + 1, 2) * 60 +
209+
substr(${column.name}, 1, instr(${column.name}, ':') - 1) * 3600 +
210+
substr(${column.name}, instr(${column.name}, ':') + 1, 2) * 60 +
206211
substr(${column.name}, -2) AS INTEGER
207212
)
208213
END
@@ -536,6 +541,11 @@ const importGtfsFiles = (
536541
}),
537542
);
538543

544+
/**
545+
* Function to import GTFS files into the database
546+
*
547+
* @param initialConfig
548+
*/
539549
export async function importGtfs(initialConfig: Config): Promise<void> {
540550
// Start timer
541551
const startTime = process.hrtime.bigint();
@@ -559,15 +569,13 @@ export async function importGtfs(initialConfig: Config): Promise<void> {
559569

560570
const task = {
561571
exclude: agency.exclude,
562-
url: agency.url,
563572
headers: agency.headers,
564573
realtimeAlerts: agency.realtimeAlerts,
565574
realtimeTripUpdates: agency.realtimeTripUpdates,
566575
realtimeVehiclePositions: agency.realtimeVehiclePositions,
567576
downloadDir: tempPath,
568577
downloadTimeout: config.downloadTimeout,
569578
gtfsRealtimeExpirationSeconds: config.gtfsRealtimeExpirationSeconds,
570-
path: agency.path,
571579
csvOptions: config.csvOptions || {},
572580
ignoreDuplicates: config.ignoreDuplicates,
573581
ignoreErrors: config.ignoreErrors,
@@ -579,8 +587,14 @@ export async function importGtfs(initialConfig: Config): Promise<void> {
579587
logError: logError(config),
580588
};
581589

582-
if (task.url) {
590+
if ('url' in agency) {
591+
Object.assign(task, { url: agency.url });
592+
583593
await downloadGtfsFiles(task);
594+
} else {
595+
Object.assign(task, {
596+
path: agency.path,
597+
});
584598
}
585599

586600
await extractGtfsFiles(task);

src/types/global_interfaces.ts

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,171 @@ import type { Database } from 'better-sqlite3';
33

44
export type UnixTimestamp = number;
55

6-
export interface ConfigAgency {
7-
exclude?: string[];
8-
url?: string;
9-
path?: string;
6+
export type TableNames =
7+
| 'agency'
8+
| 'stops'
9+
| 'routes'
10+
| 'trips'
11+
| 'stop_times'
12+
| 'calendar'
13+
| 'calendar_dates'
14+
| 'fare_attributes'
15+
| 'fare_rules'
16+
| 'timeframes'
17+
| 'rider_categories'
18+
| 'fare_media'
19+
| 'fare_products'
20+
| 'fare_leg_rules'
21+
| 'fare_leg_join_rules'
22+
| 'fare_transfer_rules'
23+
| 'areas'
24+
| 'stop_areas'
25+
| 'networks'
26+
| 'route_networks'
27+
| 'shapes'
28+
| 'frequencies'
29+
| 'transfers'
30+
| 'pathways'
31+
| 'levels'
32+
| 'location_groups'
33+
| 'location_group_stops'
34+
| 'locations'
35+
| 'booking_rules'
36+
| 'translations'
37+
| 'feed_info'
38+
| 'attributions';
39+
40+
interface BaseConfigAgency {
41+
/**
42+
* An array of GTFS file names (without .txt) to exclude when importing
43+
*/
44+
exclude?: TableNames[];
45+
/**
46+
* An object of HTTP headers in key:value format to use when fetching GTFS from the url specified
47+
*/
1048
headers?: Record<string, string>;
49+
/**
50+
* Settings for fetching GTFS-Realtime alerts
51+
*/
1152
realtimeAlerts?: {
53+
/**
54+
* URL for fetching GTFS-Realtime alerts
55+
*/
1256
url: string;
57+
/**
58+
* Headers to use when fetching GTFS-Realtime alerts
59+
*/
1360
headers?: Record<string, string>;
1461
};
62+
/**
63+
* Settings for fetching GTFS-Realtime trip updates
64+
*/
1565
realtimeTripUpdates?: {
66+
/**
67+
* URL for fetching GTFS-Realtime trip updates
68+
*/
1669
url: string;
70+
/**
71+
* Headers to use when fetching GTFS-Realtime trip updates
72+
*/
1773
headers?: Record<string, string>;
1874
};
75+
/**
76+
* Settings for fetching GTFS-Realtime vehicle positions
77+
*/
1978
realtimeVehiclePositions?: {
79+
/**
80+
* URL for fetching GTFS-Realtime vehicle positions
81+
*/
2082
url: string;
83+
/**
84+
* Headers to use when fetching GTFS-Realtime vehicle positions
85+
*/
2186
headers?: Record<string, string>;
2287
};
88+
/**
89+
* A prefix to be added to every ID field maintain uniqueness when importing multiple GTFS from multiple agencies
90+
*/
2391
prefix?: string;
2492
}
2593

94+
export type ConfigAgency = BaseConfigAgency &
95+
(
96+
| {
97+
/**
98+
* The URL to a zipped GTFS file. Required if path not present
99+
*/
100+
url: string;
101+
}
102+
| {
103+
/**
104+
* A path to a zipped GTFS file or a directory of unzipped .txt files. Required if url is not present
105+
*/
106+
path: string;
107+
}
108+
);
109+
26110
export interface Config {
111+
/**
112+
* An existing database instance to use instead of relying on node-gtfs to connect.
113+
*/
27114
db?: Database;
115+
/**
116+
* A path to an SQLite database. Defaults to using an in-memory database.
117+
*/
28118
sqlitePath?: string;
119+
/**
120+
* Amount of time in seconds to allow GTFS-Realtime data to be stored in database before allowing to be deleted.
121+
*
122+
* Note: is an integer
123+
*
124+
* @defaultValue 0
125+
*/
29126
gtfsRealtimeExpirationSeconds?: number;
127+
/**
128+
* The number of milliseconds to wait before throwing an error when downloading GTFS.
129+
*
130+
* Note: is an integer
131+
*/
30132
downloadTimeout?: number;
133+
/**
134+
* Options passed to `csv-parse` for parsing GTFS CSV files.
135+
*/
31136
csvOptions?: Options;
137+
/**
138+
* A path to a directory to put exported GTFS files.
139+
*
140+
* @defaultValue `gtfs-export/<agency_name>`
141+
*/
32142
exportPath?: string;
143+
/**
144+
* Whether or not to ignore unique constraints on ids when importing GTFS, such as `trip_id`, `calendar_id`.
145+
*
146+
* @defaultValue false
147+
*/
33148
ignoreDuplicates?: boolean;
149+
/**
150+
* Whether or not to ignore errors during the import process. If true, failed files will be skipped while the rest are processed.
151+
*
152+
* @defaultValue false
153+
*/
34154
ignoreErrors?: boolean;
155+
/**
156+
* An array of GTFS files to be imported, and which files to exclude.
157+
*/
35158
agencies: ConfigAgency[];
159+
/**
160+
* Whether or not to print output to the console.
161+
*
162+
* @defaulValue true
163+
*/
36164
verbose?: boolean;
165+
/**
166+
* An optional custom logger instead of the build in console.log
167+
*
168+
* @param message
169+
* @returns
170+
*/
37171
logFunction?: (message: string) => void;
38172
}
39173

@@ -52,7 +186,7 @@ export interface ModelColumn {
52186
}
53187

54188
export interface Model {
55-
filenameBase: string;
189+
filenameBase: TableNames;
56190
filenameExtension?: string;
57191
extension?: string;
58192
nonstandard?: boolean;

0 commit comments

Comments
 (0)