Skip to content

Instantly share code, notes, and snippets.

@royib
Created January 19, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royib/8df93b56aa5d85e2ebe29ee7968714c5 to your computer and use it in GitHub Desktop.
Save royib/8df93b56aa5d85e2ebe29ee7968714c5 to your computer and use it in GitHub Desktop.
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IDataServices } from '../../../core';
import { MongoGenericRepository } from './mongo-generic-repository';
import {
Author,
AuthorDocument,
Book,
BookDocument,
Genre,
GenreDocument,
} from './model';
@Injectable()
export class MongoDataServices
implements IDataServices, OnApplicationBootstrap
{
authors: MongoGenericRepository<Author>;
books: MongoGenericRepository<Book>;
genres: MongoGenericRepository<Genre>;
constructor(
@InjectModel(Author.name)
private AuthorRepository: Model<AuthorDocument>,
@InjectModel(Book.name)
private BookRepository: Model<BookDocument>,
@InjectModel(Genre.name)
private GenreRepository: Model<GenreDocument>,
) {}
onApplicationBootstrap() {
this.authors = new MongoGenericRepository<Author>(this.AuthorRepository);
this.books = new MongoGenericRepository<Book>(this.BookRepository, [
'author',
'genre',
]);
this.genres = new MongoGenericRepository<Genre>(this.GenreRepository);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment