Skip to content

Instantly share code, notes, and snippets.

@imerla1
Created January 18, 2023 11:48
Show Gist options
  • Save imerla1/7c2005103da990f3eb614d849185fb4b to your computer and use it in GitHub Desktop.
Save imerla1/7c2005103da990f3eb614d849185fb4b to your computer and use it in GitHub Desktop.
NestJS mongoose Repository Template
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
SportTemplate,
SportTemplateDocument,
} from './schemas/sporttemplate.schema';
import { NftEditionTemplate } from '../edition/schemas/nft-edition.schema';
/**
* Repository for managing sport templates in the database.
*/
@Injectable()
export class SportTemplateRepository {
/**
* Creates a new instance of the `SportTemplateRepository` class.
*
* @param {Model<SportTemplateDocument>} sportTemplateModel - The Mongoose model for the `SportTemplate` entity.
*/
constructor(
@InjectModel(SportTemplate.name)
private sportTemplateModel: Model<SportTemplateDocument>,
) {}
/**
* Retrieves all sport templates from the database
* @returns {Promise<SportTemplate[]>} - A promise that resolves with an array of SportTemplate objects
*/
async findAll(): Promise<SportTemplate[]> {
return this.sportTemplateModel.find().exec();
}
/**
* Retrieves a single sport template by its sportId
* @param {number} id - The sportId of the sport template to retrieve
* @returns {Promise<SportTemplate>} - A promise that resolves with a single SportTemplate object
*/
async findOne(id: number): Promise<SportTemplate> {
return this.sportTemplateModel.findOne({ sportId: id }).exec();
}
/**
* Creates a new sport template in the database
* @param {SportTemplate} sportTemplate - The sport template to create
* @returns {Promise<SportTemplate>} - A promise that resolves with the newly created SportTemplate object
*/
async create(sportTemplate: SportTemplate): Promise<SportTemplate> {
const newSportTemplate = new this.sportTemplateModel(sportTemplate);
return newSportTemplate.save();
}
/**
* Deletes a single sport template by its sportId
* @param {number} id - The sportId of the sport template to delete
* @returns {Promise<SportTemplate>} - A promise that resolves with the deleted SportTemplate object
*/
async deleteOne(id: number): Promise<SportTemplate> {
return this.sportTemplateModel.findOneAndDelete({ sportId: id });
}
/**
* Retrieves a list of sport templates from the database based on the provided query, sort, and limit options
* @param {T} [query={}] - The query to filter the sport templates by
* @param {K} [sort={}] - The sort options to order the sport templates by
* @param {number} [limit] - The maximum number of sport templates to retrieve
* @returns {Promise<SportTemplate[]>} - A promise that resolves with an array of SportTemplate objects
*/
async find<T, K>(
query?: T,
sort?: K,
limit?: number,
): Promise<SportTemplate[]> {
return this.sportTemplateModel
.find(query ?? {})
.sort(sort ?? {})
.limit(limit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment