Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom repository example #44

Closed
MattIzSpooky opened this issue Aug 13, 2018 · 3 comments
Closed

Custom repository example #44

MattIzSpooky opened this issue Aug 13, 2018 · 3 comments

Comments

@MattIzSpooky
Copy link

MattIzSpooky commented Aug 13, 2018

I'm submitting a basic example


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

It is not clear how to properly inject a custom repository into their services, this is sadly not documented. People will look through old GitHub issues to find how others managed to solve their issues, this is far from ideal for various reasons.

Expected behavior

A properly documented example so that people can figure out how to do it themselves.

Solution

First create your custom repository. I threw in some useless functions to show that it works.

@EntityRepository(Author)
export class AuthorRepository extends Repository<Author> {
  test() {
    console.log('testasfasdfasdf');
  }
  customSave(entity) {
    return this.save(entity);
  }
}

Then go to your feature module

@Module({
  imports: [TypeOrmModule.forFeature([Author, AuthorRepository /* <--- put your custom repository here together with your entities */])],
  providers: [AuthorService, AuthorResolver],
})
export class TestModule {
}

Now we go to our service

@Injectable()
export class AuthorService {
  constructor(
    @InjectRepository(AuthorRepository)
    private readonly authorRepository: AuthorRepository, // <-- inject like this
  ) {
  }

  async create(author: AuthorInterface) {
    this.authorRepository.test();
    return await this.authorRepository.customSave(this.authorRepository.create(author));
  }
}

Voila! You're done, you can now use your custom repository as if it was a default repository but with your own extra goodies!

Some proof that it works:
I use GraphQL so here's the other code:

@Resolver('Author')
export class AuthorResolver {
  constructor(
    private readonly authorService: AuthorService
  ) {
  }

  @Mutation('createAuthor')
  async createAuthor(obj, args, context, info) {
    return await this.authorService.create(args);
  }
}

Execute the mutation

image

Result

image

Database

image

Console, to show that the console.log function is called.
image

What is the motivation / use case for changing the behavior?

There's a risk people will use outdated answers like this one

People are more likely to look for other people's answers rather than finding their own.

Environment


Nest version: ^5.1.0
Nestjs/graphql: ^3.0.0
Nestjs/typeorm: ^5.1.0

 
For Tooling issues:
- Node version: 10.0.0  
- Platform: Windows 

Others:

This is my first GitHub issue. I'd like to have feedback in case there's something wrong with my it.
@kamilmysliwiec
Copy link
Member

Thanks @MattIzSpooky, it helped a lot! Just created an issue in the documentation repository: nestjs/docs.nestjs.com#136

@arielweinberger
Copy link

arielweinberger commented May 20, 2019

Helped me a lot, thank you!

Just a small note, @kamilmysliwiec @MattIzSpooky - it's not necessary to include Author anymore when executing TypeORM.forFeature(), at least not within this example's scope - as the Author entity isn't used (except for with the repository, which seems to be enough).

@KenySushant

This comment has been minimized.

@nestjs nestjs locked as resolved and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants