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.

74 lines (73 loc) 2.89 kB
/* * */ import { MongoCollectionClass } from '../../common/mongo-collection.js'; import { CreateRoleSchema, PermissionCatalog, UpdateRoleSchema } from '@tmlmobilidade/types'; import { asyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class RolesClass extends MongoCollectionClass { static _instance; createSchema = CreateRoleSchema; 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 user document by its ID. * @param id The ID of the user document to find * @param includePasswordHash Whether to include the password hash in the result * @returns A promise that resolves to the matching user document or null if not found */ async findById(id, options) { const foundRole = await this.mongoCollection.findOne({ _id: id }, options); if (!foundRole) return null; return { ...foundRole, permissions: PermissionCatalog.sanitize(foundRole.permissions) }; } /** * Finds multiple documents matching the filter criteria * with optional pagination and sorting. * @param filter (Optional) filter criteria to match documents. * @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 documents. */ async findMany(filter, options) { const foundRoles = await this.mongoCollection.find(filter ?? {}, options).toArray(); return foundRoles.map(item => ({ ...item, permissions: PermissionCatalog.sanitize(item.permissions) })); } /** * Finds a single document matching the filter criteria. * @param filter Filter criteria to match the document. * @returns A promise that resolves to the matching document or null if not found. */ async findOne(filter) { const foundRole = await this.mongoCollection.findOne(filter); if (!foundRole) return null; return { ...foundRole, permissions: PermissionCatalog.sanitize(foundRole.permissions) }; } getCollectionIndexes() { return [ { background: true, key: { name: 1 }, unique: true }, ]; } getCollectionName() { return 'roles'; } getEnvName() { return 'DATABASE_URI'; } } /** * @deprecated This class is deprecated and will be removed in the future. * Use `@tmlmobilidade/go-interfaces-go-db` instead. */ export const roles = asyncSingletonProxy(RolesClass);