Skip to content

Commit

Permalink
Support for service_id in getRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Jun 17, 2024
1 parent e62881e commit f147e13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Support for `service_id` in `getRoutes`

## [4.11.3] - 2024-06-13

### Fixed
Expand Down
30 changes: 23 additions & 7 deletions lib/gtfs/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,30 @@ function buildStoptimeSubquery(query) {
}

function buildTripSubquery(query) {
return `SELECT DISTINCT route_id FROM trips WHERE trip_id IN (${buildStoptimeSubquery(
query,
)})`;
let whereClause = '';
const tripQuery = omit(query, ['stop_id']);
const stoptimeQuery = pick(query, ['stop_id']);

const whereClauses = Object.entries(tripQuery).map(([key, value]) =>
formatWhereClause(key, value),
);

if (Object.values(stoptimeQuery).length > 0) {
whereClauses.push(`trip_id IN (${buildStoptimeSubquery(stoptimeQuery)})`);
}

if (whereClauses.length > 0) {
whereClause = `WHERE ${whereClauses.join(' AND ')}`;
}

return `SELECT DISTINCT route_id FROM trips ${whereClause}`;
}

/*
* Returns an array of routes that match the query parameters. A `stop_id`
* query parameter may be passed to find all routes that contain that stop.
* A `service_id` query parameter may be passed to limit routes to specific
* calendars.
*/
export function getRoutes(query = {}, fields = [], orderBy = [], options = {}) {
const db = options.db ?? openDb();
Expand All @@ -33,15 +49,15 @@ export function getRoutes(query = {}, fields = [], orderBy = [], options = {}) {
let whereClause = '';
const orderByClause = formatOrderByClause(orderBy);

const routeQuery = omit(query, ['stop_id']);
const stoptimeQuery = pick(query, ['stop_id']);
const routeQuery = omit(query, ['stop_id', 'service_id']);
const tripQuery = pick(query, ['stop_id', 'service_id']);

const whereClauses = Object.entries(routeQuery).map(([key, value]) =>
formatWhereClause(key, value),
);

if (Object.values(stoptimeQuery).length > 0) {
whereClauses.push(`route_id IN (${buildTripSubquery(stoptimeQuery)})`);
if (Object.values(tripQuery).length > 0) {
whereClauses.push(`route_id IN (${buildTripSubquery(tripQuery)})`);
}

if (whereClauses.length > 0) {
Expand Down

0 comments on commit f147e13

Please sign in to comment.