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.

60 lines (59 loc) 1.94 kB
/* * */ import { MongoCollectionClass } from '../../common/mongo-collection.js'; import { CreateZoneSchema, UpdateZoneSchema } from '@tmlmobilidade/types'; import { asyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class ZonesClass extends MongoCollectionClass { static _instance; createSchema = CreateZoneSchema; updateSchema = UpdateZoneSchema; constructor() { super(); } static async getInstance() { if (!ZonesClass._instance) { const instance = new ZonesClass(); await instance.connect(); ZonesClass._instance = instance; } return ZonesClass._instance; } /** * Finds a zone document by its code. * @param code The code of the zone to find * @returns A promise that resolves to the matching zone document or null if not found */ async findByCode(code) { return this.mongoCollection.findOne({ code }); } /** * Finds a zone document by its name. * @param name The name of the zone to find * @returns A promise that resolves to the matching zone document or null if not found */ async findByName(name) { return this.mongoCollection.findOne({ name }); } /** * Updates a zone document by its code. * @param code The code of the zone to update. * @param updateFields The fields to update in the zone document. * @returns A promise that resolves to the result of the update operation. */ async updateByCode(code, updateFields) { return this.mongoCollection.updateOne({ code }, { $set: updateFields }); } getCollectionIndexes() { return [ { background: true, key: { code: 1 }, unique: true }, ]; } getCollectionName() { return 'zones'; } getEnvName() { return 'DATABASE_URI'; } } /* * */ export const zones = asyncSingletonProxy(ZonesClass);