-
Notifications
You must be signed in to change notification settings - Fork 203
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 Error #14
Comments
After hours of Googling, I finally found a workaround. The solution is still pretty elegant in my opinion, but this should actually be supported out of the box. The workaround I use, is using a provider. My image.repository.ts /**
* The image repository represents the
* interface between the appliaction image entity
* and the database.
*/
@EntityRepository(Image)
export class ImageRepository extends Repository<Image> {
/**
* Finds all images and applies the given pagination
* options
* @param pagination The pagination options
*/
async findAll(pagination: PaginationOptionsDto): Promise<[Image[], number]> {
// Get Images with the pagination options applied
// from the database.
return await this
.createQueryBuilder('image')
.offset(pagination.offset)
.limit(pagination.limit)
.getManyAndCount();
}
}
export const ImageRepositoryProvider = {
provide: 'ImageRepository',
useFactory: (connection: Connection) => connection.getCustomRepository(ImageRepository),
inject: [Connection]
}; In the module, simply inject the provider. E.g. import { ImageRepositoryProvider } from './image.repository';
@Module({
imports: [
TypeOrmModule.forFeature([Image])
],
controllers: [ImageController],
components: [
ImageRepositoryProvider
],
})
/**
* The Image module, which bundles all
* operational or processable image related
* modules, controllers and components
*/
export class ImageModule { } Now using @Inject('ImageRepository'), you can inject it into your services etc. |
oh yeah :) thx a lot @BrunnerLivio ! i'll try it soon to give you my feedback. |
@BrunnerLivio |
This feature will be available in the nearest release (here's a pull request #18) |
@kamilmysliwiec this indicates that we shouldn't be creating services from custom repositories, especially if you want to use transactions. Thoughts? |
@kamilmysliwiec What would be the correct use of this feature? Do you have an example? |
@kamilmysliwiec yeah some documentation would be appreciated |
@kamilmysliwiec +1 for documentation. |
With typeorm we can use custom repository to override default one to add extra methods.
For example:
After we juste have to inject it inside controller/model/service etc... like this:
But only InjectRepository decorator is available on nestjs, so if we try to inject repo into a service:
We got this error for any repo function:
How it is possible to use custom repo?
The text was updated successfully, but these errors were encountered: