UNPKG

@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.

63 lines (62 loc) 2.03 kB
/* * */ import { MongoCollectionClass } from '../../mongo-collection.js'; import { StopSchema, UpdateStopSchema } from '@tmlmobilidade/types'; import { AsyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class StopsClass extends MongoCollectionClass { static _instance; createSchema = StopSchema; updateSchema = UpdateStopSchema; constructor() { super(); } static async getInstance() { if (!StopsClass._instance) { const instance = new StopsClass(); await instance.connect(); StopsClass._instance = instance; } return StopsClass._instance; } /** * Finds stop documents by municipality ID with optional pagination and sorting. * * @param id - The municipality ID to search for * @param perPage - Optional number of documents per page for pagination * @param page - Optional page number for pagination * @param sort - Optional sort specification * @returns A promise that resolves to an array of matching stop documents */ async findByMunicipalityId(id, perPage, page, sort) { const query = this.mongoCollection.find({ municipality_id: id }); if (perPage) query.limit(perPage); if (page && perPage) query.skip(perPage * (page - 1)); if (sort) query.sort(sort); return query.toArray(); } /** * Finds multiple stop documents by their IDs. * * @param ids - Array of stop IDs to search for * @returns A promise that resolves to an array of matching stop documents */ async findManyByIds(ids) { return this.mongoCollection.find({ _id: { $in: ids } }).toArray(); } getCollectionIndexes() { return [ { background: true, key: { name: 1 } }, ]; } getCollectionName() { return 'stops'; } getEnvName() { return 'TML_INTERFACE_STOPS'; } } /* * */ export const stops = AsyncSingletonProxy(StopsClass);