#EntityRepository is deprecated, moving to CustomRepository, what to do?

1 messages · Page 1 of 1 (latest)

narrow crescent
#

I installed @nestjs/typeorm@next
I was trying to follow this: https://gist.github.com/anchan828/9e569f076e7bc18daf21c652f7c3d012
but the 2 files he created:

  • typeorm-ex.decorator.ts
  • database.options.ts
    are on top of my head and don't understand them, can someone explain?
Gist

This is an improvement to allow @nestjs/[email protected] to handle CustomRepository. I won't explain it specifically, but it will help in some way. https://github.com/nestjs/typeorm/pull/1233 ...

obtuse rune
#

Question: Why not just create your own class called FeatureRepository that uses @InjectRepository(Feature) and then builds on top of that? Then you just inject the FeatureRepository as any other class. You can also expose the underlying repository and do something like this.repository.repo.find()

narrow crescent
obtuse rune
#

Then break it down. What are you trying to do?

narrow crescent
#

the tutorial created a repository, and gave it decorator EntityRepository, it's deprecated and i found this CustomRepository and tried to implement it

obtuse rune
#

Right. So what will this custom repository allow you to do?

#

Rather than blindly follow the tutorial, think about what it's achieving in the end

narrow crescent
#

so i have to import CustomRepository from somewhere, do I have to create it?

narrow crescent
#

does ex in the name refer to "example" ?

obtuse rune
#

Probably. I'm honestly not familiar with the approach in the gist

narrow crescent
obtuse rune
#

You still haven't answered why you want a custom repository

narrow crescent
obtuse rune
#

So what's wrong with the approach I suggested? Making a class called a FeatureRepository that exposes the repo so you can still use the built in methods and provides its own custom methods on top of it?

#
@Injectable()
export class FooRepository {
  constructor(@InjectRepository(Foo) readonly repo: Repository<Foo>) {}

  findByName(name: string): Foo {
    return this.repo.find({ name });
  }
}

And you can call this.fooRepo.repo.find() or this.fooRepo.findByName(someName) just fine

#

I mean, really a custom repository from TypeORM, in my opinion, is just like a service in Nest. It's an abstraction away from the rest of the logic so that it's all co-located. That's absolutely what you can do with a service though

narrow crescent
#

i'm re reading and trying to follow along

#

so from your explanation, i can dump the annotation?

#

and make my custom queries inside the repository

obtuse rune
#

Yeah, it's just another class at that point

narrow crescent
#

can i also keep those custom queries in the original repository also?

obtuse rune
#

What do you mean by "original repository"?

narrow crescent
#

I have TaskRepository, extends repository, so i have access to default methods in it, at same time I want to have my custom methods

#

I think that's what i wanted to achieve

#

but at same time give the annotation of @CustomEntity to indicate its custom repo

obtuse rune
#

return this.repo.findOne({ where: { title } }) like the example above shows

#

Because now TaskRepository would be its own class definition and has a dependency on Repository<Task>

narrow crescent
obtuse rune
#

<#1034657005169823794 message>
It comes from the @InjectRepository(Task) readonly repo: Repository<Task> you would use in the TaskRespository's constructor

narrow crescent
#

so in order to create custom queries, i have to create for example TaskCustomQueries class and inject the Task repo in it?

obtuse rune
#

That's what I would do

narrow crescent
#

I can't put the custom queries directly in repo?

obtuse rune
#

Not with Typeorm v0.3

narrow crescent
obtuse rune
#

I would. I feel it's simpler and easier to track.

narrow crescent
#

thank you, bye

narrow crescent
obtuse rune
#

No, this approach is no longer viable with the 0.3.x changes from Typeorm

narrow crescent
#

@obtuse rune i hope you bear with me one last time, this is my final result.

#

i think it's not detecting the repository, it can't read the property findOne, I hope you help cause it's a blocker for me @obtuse rune

#

im trying to retrive a task by ID

obtuse rune
#

Let me ask you this: If you called the "Repository" class TaskRepo, would you still do TypeormModule.forFeature([TaskRepo])? What if it was TaskQueryImplementation?

TaskRepository is now just a regular ole' class that TypeORM doesn't need to know about in any regard. So just add TaskRepository to the providers array and remove the @InjectRepository(TaskRepository) from the TaskService

obtuse rune
#

Yeah, because you're missing imports: [TypeormModule.forFeature([Task])]. You still use @InjectRepository(Task) inside of the TaskRepository class, so you still need that provider created

narrow crescent
# obtuse rune Yeah, because you're missing `imports: [TypeormModule.forFeature([Task])]`. You ...

I think I finally started understanding the logic, if i use injectRepository i'll always have to provide it in the module imports, and since my taskRepository is just a class, i can add it to the providers array as if it was a service, I hope i understand it correctly, if you tell me what to look for in the documentation to understand this more.

But it works! finally, thank you for your patience

obtuse rune
#

@dire plank

#

This post has my suggested approach on custom repositories

dire plank
#

@obtuse rune Thank you will check it out

obtuse rune
#

@coarse parrot my opinion and take on how to do custom repositories