I have two repository A, B.
In constructor repository A: @repository(B) public b: B
In constructor repository B: @repository(A) public a: A.
I have error , when remove @repository(A) public a: A in constructor repository B. it work ok.
Can anyone explain it !
Why do you want to inject an instance of A into A constructor? Please show the complete code and use case. It seems that you have circular dependencies here.
@raymondfeng
this is my post.repository.ts:
export class PostRepository extends SoftDeleteEntityRepository
typeof Post.prototype.id> {
constructor(
@inject('datasources.campp_db') dataSource: CamppDbDataSource,
@repository(TagsRepository)
protected tagsRepository: TagsRepository,
@repository(PostTagsRepository)
protected postTagsRepository: PostTagsRepository,
@repository(UserRepository)
protected userRepository: UserRepository,
@repository(UserLikePostRepository)
protected userLikePostRepository: UserLikePostRepository,
@repository(PostBookmarkRepository)
public postBookmarkRepository: PostBookmarkRepository,
@repository(CommentRepository)
public commentRepository: CommentRepository,
) {
super(Post, dataSource);
}
>
this comment.repository.ts:
export class CommentRepository extends SoftDeleteEntityRepository
typeof Comment.prototype.id> {
constructor(
@inject('datasources.campp_db') dataSource: CamppDbDataSource,
@repository(UserRepository)
protected userRepository: UserRepository,
@repository(PostRepository)
protected postRepository: PostRepository,
) {
super(Comment, dataSource);
}
I want to use postRepository in comment.repository.ts
and use commentRepository in post.repository.ts
possible ?
when remove @repository(PostRepository)
protected postRepository: PostRepository,
in comment.repository.ts.
This will be ok
i have create small project : https://github.com/thangns204/lb4Test.git with the same error
@thangns204 , I receive circular dependency error when used @inject . and When used the @repository decorator I receive the error you describe.
Probably is because the IOC Container creates the post repository first and thus when it finds a reference it still does not exist. Why don't you place your business logic in the controller ?. In real scenarios like a payment updating an invoice and an invoice updating something else you usually don't have this circular references at the repository level.
@thangns204 Please note that TypeScript is transpiled to JavaScript - decorations become function calls against the class or its prototype right after the class is declared. In your case, there are circular dependencies that lead to decorator functions to be called before the class prototype is set up. I think two things should happen:
@repository('repositories.PostRepository') to avoid reference to PostRepository too early@marioestradarosa
Repositories are classes provide to to persist and query the data (such as CRUD).
In my project, i put all function relate to db in repository class.
And my controller put only operations defined by application鈥檚 API.
Ex: api: /comment/addComment in CommentController: i will call this.commentRepository.addComment
And in CommentRepository, i will create function addComment() , may be i need to call postRepository in addComment() .
u say: "Why don't you place your business logic in the controller ?."
Its mean , all function logic stored in controller?
So what should we put in repository ?
Thanks
@thangns204 you are 100% correct in the repository purpose. For instance imagine a bank operation for a deposit:
None are circular references
depositRepository -> AccountRepository
depositRepository -> AuditRepository
Or a payment :
paymentRepository -> InvoiceRepository -> CustomerRepository
or
paymentRepository -> InvoiceRepository
paymentRepository -> CustomerRepository
My suggestion was in the case you want circular references probably the orchestration could be at the controller level. Long time ago, we used to call this 'updateExists' in old Wang VS machines, but never had a real case for circular reference, that's why I was curious about your use case.
@marioestradarosa
In my case:
PostRepository: i need commentRepository for calculate all comment in a post
CommentRepository: i need postRepository for check comment on a post exists
i know can use hasMany, belongto Relation for fix, but i think, many other cases need circular reference.
Tks for your sol: " the orchestration could be at the controller level"
@thangns204 ,
i need postRepository for check comment on a post exists .
what would a comment unique ?
i know can use hasMany, belongto Relation for fix
馃憤
Also as a last resource, did you know you can make a custom query right? , using something like
const mySQL = `
SELECT * FROM mytable WHERE xxxx = ${somevar}
`
const result = await this.datasource.execute(mySQL); // it returns an array [] or with rows
Tks for your sol: " the orchestration could be at the controller level"
Please close the issue if that could solve your problem !
@marioestradarosa , ok tks guy