A TypeScript library for loading GTFS (General Transit Feed Specification) data into a sql.js SQLite database for querying in both browser and Node.js environments.
Live Demo — A fully static demo website with GTFS and GTFS-RT data running in a Web Worker, with no backend.
Théophile Helleboid / SysDevRun
This project is greatly inspired by node-gtfs, also MIT licensed. The main difference is that gtfs-sqljs aims to run on both browser and Node.js environments.
examples/cache/npm install gtfs-sqljs
You also need to install sql.js as a peer dependency:
npm install sql.js
import { GtfsSqlJs } from 'gtfs-sqljs';
// Load GTFS data from a ZIP file
const gtfs = await GtfsSqlJs.fromZip('https://example.com/gtfs.zip');
// Query routes
const routes = gtfs.getRoutes();
// Query stops with filters
const stops = gtfs.getStops({ name: 'Central Station' });
// Get trips for a route on a specific date
const trips = gtfs.getTrips({
routeId: 'ROUTE_1',
date: '20240115',
directionId: 0
});
// Get stop times for a trip
const stopTimes = gtfs.getStopTimes({ tripId: trips[0].trip_id });
// Clean up
gtfs.close();
For detailed usage examples, see the Usage Guide.
Full API documentation: API Reference
GtfsSqlJs.fromZip(zipPath, options?) - Create instance from GTFS ZIP fileGtfsSqlJs.fromDatabase(database, options?) - Create instance from existing databaseAll methods support flexible filtering with both single values and arrays:
getAgencies(filters?) - Get agencies (filters: agencyId, limit)getStops(filters?) - Get stops (filters: stopId, stopCode, name, tripId, limit)getRoutes(filters?) - Get routes (filters: routeId, agencyId, limit)getTrips(filters?) - Get trips (filters: tripId, routeId, serviceIds, directionId, agencyId, includeRealtime, limit, date)getStopTimes(filters?) - Get stop times (filters: tripId, stopId, routeId, serviceIds, directionId, agencyId, includeRealtime, limit, date)getShapes(filters?) - Get shape points (filters: shapeId, routeId, tripId, limit)getShapesToGeojson(filters?, precision?) - Get shapes as GeoJSON FeatureCollection (same filters, precision default: 6)buildOrderedStopList(tripIds) - Build an ordered list of stops from multiple trips (handles express/local variations)getActiveServiceIds(date) - Get active service IDs for a date (YYYYMMDD format)getCalendars(filters?) - Get calendars (filters: serviceId, limit)getCalendarDates(serviceId) - Get calendar date exceptions for a servicegetCalendarDatesForDate(date) - Get calendar exceptions for a specific datefetchRealtimeData(urls?) - Fetch and load RT data from protobuf feedsclearRealtimeData() - Clear all realtime data from databasesetRealtimeFeedUrls(urls) - Configure RT feed URLsgetRealtimeFeedUrls() - Get configured RT feed URLssetStalenessThreshold(seconds) - Set staleness threshold (default: 120 seconds)getStalenessThreshold() - Get current staleness thresholdgetLastRealtimeFetchTimestamp() - Get Unix timestamp (seconds) of last successful RT fetch, or null if never fetchedgetAlerts(filters?) - Get alerts (filters: alertId, routeId, stopId, tripId, activeOnly, cause, effect, limit)getVehiclePositions(filters?) - Get vehicle positions (filters: tripId, routeId, vehicleId, limit)getTripUpdates(filters?) - Get trip updates (filters: tripId, routeId, limit)getStopTimeUpdates(filters?) - Get stop time updates (filters: tripId, stopId, stopSequence, limit)export() - Export database to ArrayBuffer (includes RT data)getDatabase() - Get direct access to sql.js database for advanced queriesclose() - Close database connectiondebugExportAllAlerts() - Export all alerts without staleness filteringdebugExportAllVehiclePositions() - Export all vehicle positions without staleness filteringdebugExportAllTripUpdates() - Export all trip updates without staleness filteringdebugExportAllStopTimeUpdates() - Export all stop time updates without staleness filteringThis library is written in TypeScript and provides full type definitions for all GTFS entities, filter options, GTFS-RT types, and progress tracking:
import type {
// Static GTFS types
Stop, Route, Trip, StopTime, Shape,
TripFilters, StopTimeFilters, ShapeFilters,
// GeoJSON types
GeoJsonFeatureCollection,
// GTFS-RT types
Alert, VehiclePosition, TripWithRealtime, StopTimeWithRealtime,
AlertFilters, VehiclePositionFilters,
// GTFS-RT enums
AlertCause, AlertEffect, ScheduleRelationship,
// Progress tracking types
ProgressInfo, ProgressCallback
} from 'gtfs-sqljs';
This library implements:
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any problems or have suggestions, please open an issue.