@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.
51 lines (50 loc) • 1.81 kB
JavaScript
/* * */
import { MongoCollectionClass } from '../../common/mongo-collection.js';
import { CreateAlertSchema, UpdateAlertSchema } from '@tmlmobilidade/types';
import { asyncSingletonProxy } from '@tmlmobilidade/utils';
/* * */
class AlertsClass extends MongoCollectionClass {
static _instance;
createSchema = CreateAlertSchema;
updateSchema = UpdateAlertSchema;
constructor() {
super();
}
static async getInstance() {
if (!AlertsClass._instance) {
const instance = new AlertsClass();
await instance.connect();
AlertsClass._instance = instance;
}
return AlertsClass._instance;
}
async findByExternalId(external_id) {
return this.mongoCollection.findOne({ external_id });
}
async findByMunicipalityId(municipality_id) {
return this.mongoCollection.find({ municipality_ids: { $in: [municipality_id] } }).toArray();
}
async findByTitle(title) {
return this.mongoCollection.findOne({ title });
}
getCollectionIndexes() {
return [
{ background: true, key: { agency_ids: 1 } },
{ background: true, key: { line_ids: 1 } },
{ background: true, key: { municipality_ids: 1 } },
{ background: true, key: { route_ids: 1 } },
{ background: true, key: { stop_ids: 1 } },
{ background: true, key: { title: 1 } },
{ background: true, key: { active_period_end_date: -1, active_period_start_date: -1 } },
{ background: true, key: { publish_end_date: -1, publish_start_date: -1 } },
];
}
getCollectionName() {
return 'alerts';
}
getEnvName() {
return 'DATABASE_URI';
}
}
/* * */
export const alerts = asyncSingletonProxy(AlertsClass);