@tmlmobilidade/interfaces
Version:
This package provides SDK-style connectors for interacting with databases (e.g., stops, plans, rides, alerts) and external providers (e.g., authentication, storage). It simplifies data access and integration across projects.
106 lines (105 loc) • 3.84 kB
JavaScript
/* * */
import { MongoCollectionClass } from '../../mongo-collection.js';
import { RideSchema, UpdateRideSchema } from '@tmlmobilidade/types';
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
/* * */
class RidesClass extends MongoCollectionClass {
static _instance;
createSchema = RideSchema;
updateSchema = UpdateRideSchema;
constructor() {
super();
}
static async getInstance() {
if (!RidesClass._instance) {
const instance = new RidesClass();
await instance.connect();
RidesClass._instance = instance;
}
return RidesClass._instance;
}
/**
* Finds ride documents by Agency ID.
*
* @param agencyId - The Agency ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByAgencyId(agencyId) {
return this.mongoCollection.find({ agency_id: agencyId }).toArray();
}
/**
* Finds ride documents by Line ID.
*
* @param lineId - The Line ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByLineId(lineId) {
return this.mongoCollection.find({ line_id: lineId }).toArray();
}
/**
* Finds ride documents by Pattern ID.
*
* @param patternId - The Pattern ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByPatternId(patternId) {
return this.mongoCollection.find({ pattern_id: patternId }).toArray();
}
/**
* Finds ride documents by Plan ID.
*
* @param planId - The Plan ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByPlanId(planId) {
return this.mongoCollection.find({ plan_id: planId }).toArray();
}
/**
* Finds ride documents by Route ID.
*
* @param routeId - The Route ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByRouteId(routeId) {
return this.mongoCollection.find({ route_id: routeId }).toArray();
}
/**
* Finds ride documents by Trip ID.
*
* @param tripId - The Trip ID to search for
* @returns A promise that resolves to an array of matching ride documents
*/
async findByTripId(tripId) {
return this.mongoCollection.find({ trip_id: tripId }).toArray();
}
getCollectionIndexes() {
/**
* IMPORTANT:
* Automatic sorting (ESLint) of keys in the JS objects should be disabled.
* The order of keys in a compound index is very important and should be
* carefully considered based on the cardinality of each key.
*/
return [
{ background: true, key: { hashed_trip_id: 1 } },
{ background: true, key: { hashed_shape_id: 1 } },
{ background: true, key: { operational_date: 1 } },
{ background: true, key: { operational_date: 1, system_status: 1 } },
{ background: true, key: { start_time_scheduled: 1 } },
{ background: true, key: { system_status: 1 } },
// eslint-disable-next-line perfectionist/sort-objects
{ background: true, key: { system_status: 1, start_time_scheduled: 1 } },
// eslint-disable-next-line perfectionist/sort-objects
{ background: true, key: { trip_id: 1, start_time_scheduled: 1 } },
{ background: true, key: { trip_id: 1 } },
{ background: true, key: { plan_id: 1 } },
];
}
getCollectionName() {
return 'rides';
}
getEnvName() {
return 'TML_INTERFACE_RIDES';
}
}
/* * */
export const rides = AsyncSingletonProxy(RidesClass);