gtfs-sqljs
Load GTFS data into sql.js SQLite database for querying in browser and Node.js
Features
🌐 Universal
Works in both browser and Node.js environments
📦 ZIP Loading
Load GTFS data directly from ZIP files (URL or local path)
🔍 Powerful Queries
Query stops, routes, trips, calendars with efficient SQLite indexes
💾 Export/Import
Export databases to ArrayBuffer for persistence
📘 TypeScript
Full TypeScript support with comprehensive GTFS types
✅ Tested
77+ tests covering all features with real GTFS data
Installation
npm install gtfs-sqljs sql.js
Quick Start
import { GtfsSqlJs } from 'gtfs-sqljs';
// Load from ZIP file (skip shapes.txt to reduce memory)
const gtfs = await GtfsSqlJs.fromZip('https://example.com/gtfs.zip', {
skipFiles: ['shapes.txt']
});
// Get all routes (using flexible API)
const routes = gtfs.getRoutes();
// Get stops for a trip
const stops = gtfs.getStops({ tripId: 'trip_id' });
// Get active trips for a date
const trips = gtfs.getTrips({ date: '20240115' });
// Clean up
gtfs.close();
API Methods
⭐ Flexible Filter-Based Methods (Recommended)
Pass optional filter objects to combine multiple criteria:
getStops(filters?)- Filter by: stopId, stopCode, name, tripId, limitgetRoutes(filters?)- Filter by: routeId, agencyId, limitgetTrips(filters?)- Filter by: tripId, routeId, date, directionId, limitgetStopTimes(filters?)- Filter by: tripId, stopId, routeId, date, directionId, limit
// Example: Get trips for a route on a specific date going one direction
const trips = gtfs.getTrips({
routeId: 'ROUTE_1',
date: '20240115',
directionId: 0
});
Direct Lookup Methods
getStopById(stopId)- Get stop by IDgetRouteById(routeId)- Get route by IDgetTripById(tripId)- Get trip by IDgetAgencyById(agencyId)- Get agency by ID
Calendar Methods
getActiveServiceIds(date)- Get active services for a date (YYYYMMDD)getCalendarByServiceId(serviceId)- Get calendar by service IDgetCalendarDates(serviceId)- Get calendar date exceptionsgetCalendarDatesForDate(date)- Get exceptions for a specific date
Special Methods
getStopTimesByTrip(tripId)- Get stop times for a trip (ordered by stop_sequence)
Database Methods
export()- Export database to ArrayBuffergetDatabase()- Get direct access to sql.js databaseclose()- Close database connection
License
MIT License