@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) • 1.95 kB
JavaScript
/* * */
import { MongoCollectionClass } from '../../mongo-collection.js';
import { UpdateZoneSchema, ZoneSchema } from '@tmlmobilidade/types';
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
/* * */
class ZonesClass extends MongoCollectionClass {
static _instance;
createSchema = ZoneSchema;
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 'TML_INTERFACE_ZONES';
}
}
/* * */
export const zones = AsyncSingletonProxy(ZonesClass);