export * from './_base-action' export * from './query-entity-action' export * from './query-collection-action' import { Model, FilterQuery, PopulateOptions, ToObjectOptions } from 'mongoose' import { QueryCollection } from './query-collection-action' import { QueryEntity } from './query-entity-action' /** * Настройки репозитория по умолчанию. */ export type TRepositoryOptions = { select?: string[] populate?: PopulateOptions | Array sort?: any toObject?: false | ToObjectOptions pagination?: { limit?: number maxLimit?: number } } export class Repository { /** * Конфигурация сервиса. */ private _options: TRepositoryOptions /** * Модель монгуса. */ public model: Model constructor(model: Model, options?: TRepositoryOptions) { this.model = model this._options = options || {} } /** * Копирует и возвращает клон объекта настроек для использования * при инициализации других классов внутри этого. */ getOptions(): TRepositoryOptions { return Object.assign({}, this._options) } /** * Выборка из коллекции. */ find(filter: FilterQuery = {}): QueryCollection { return new QueryCollection(filter, this.model, this.getOptions()) } /** * Одна сущность. */ findOne(filter: FilterQuery): QueryEntity { const query = this.model.findOne(filter) return new QueryEntity(query, this.getOptions()) } /** * Одна сущность по Ид. */ findById(id: string): QueryEntity { const query = this.model.findById(id) return new QueryEntity(query, this.getOptions()) } /** * Проверка ObjectId */ isValidObjectId(id: string): boolean { if (typeof id !== 'string') return false return id.match(/^[a-f\d]{24}$/i) ? true : false } }