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.

50 lines (49 loc) 1.47 kB
/* * */ import { MongoCollectionClass } from '../../mongo-collection.js'; import { HttpException, HttpStatus } from '@tmlmobilidade/lib'; import { RoleSchema, UpdateRoleSchema } from '@tmlmobilidade/types'; import { AsyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class RolesClass extends MongoCollectionClass { static _instance; createSchema = RoleSchema; updateSchema = UpdateRoleSchema; constructor() { super(); } static async getInstance() { if (!RolesClass._instance) { const instance = new RolesClass(); await instance.connect(); RolesClass._instance = instance; } return RolesClass._instance; } /** * Finds a role by its name * * @param name - The name of the role to find * @returns A promise that resolves to the matching role document or null if not found */ async findByName(name) { return this.mongoCollection.findOne({ name }); } /** * Disable Update Many */ async updateMany() { throw new HttpException(HttpStatus.METHOD_NOT_ALLOWED, 'Method not allowed for roles'); } getCollectionIndexes() { return [ { background: true, key: { name: 1 }, unique: true }, ]; } getCollectionName() { return 'roles'; } getEnvName() { return 'TML_INTERFACE_AUTH'; } } export const roles = AsyncSingletonProxy(RolesClass);