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

reuse repo in repo #255

Open
bingtsingw opened this issue Apr 13, 2024 · 5 comments
Open

reuse repo in repo #255

bingtsingw opened this issue Apr 13, 2024 · 5 comments

Comments

@bingtsingw
Copy link
Sponsor Contributor

In issue #244, this works:

export const myRepo = createRepo(db.relationTable, {
  queryMethods: {
    whereSomething: (xxx: SomeType) => {
      return q.where(xxx);
    },
  },
});

const data = db.xxx.select({
  relationCount: q => myRepo(q.relation).whereSomething(xxx).count() // it works
})

But when reuse myRepo in another repo, it does not work

export const myRepo = createRepo(db.relationTable, {
  queryMethods: {
    whereSomething: (xxx: SomeType) => {
      return q.where(xxx);
    },
  },
});

const myRepo2 = createRepo(db.relationTable2, {
  queryMethods: {
    selectRelation: (q) => {
      return q.select({
         relationCount: q => myRepo(q.relation).whereSomething(xxx).count() // not work
      })
    },
  },
});
@romeerez
Copy link
Owner

romeerez commented Apr 13, 2024

I tried to reproduce it and I didn't have a problem.

Perhaps, the relation here myRepo(q.relation) doesn't match the target table of myRepo.

It should be fine as long as db.xxx in your code above is the same as db.relationTable2.

@romeerez
Copy link
Owner

@bingtsingw please give more details on this issue (ideally, a repro). I just pushed update related to joins, maybe it's fixed, then confirm please if this is still relevant, or let's close.

@bingtsingw
Copy link
Sponsor Contributor Author

@romeerez Sorry for the late response, I'm a little busy these days, I'll make a reproduce repo may be next week.
By the way, the latest version 1.24.3 seems to break some test of my project, so I'll keep 1.24.2 for a few days.

@bingtsingw
Copy link
Sponsor Contributor Author

And I found this fixed issue (#242) breaks in the 1.24.2, not sure if it worked in 1.24.3

@romeerez
Copy link
Owner

No problem, take your time.

I ran the test for 242 and it passed, there are type errors in the test suite, but it doesn't seem to be a bug in ORM.

  await dr.activityMember
    .whereByUserVipTo('>', new Date())
    .whereByUserVipTo('<', addDays(new Date(), 5))
    .select('id', 'user.id');

Here user.id is not recognized, because it's possible that the user relation isn't joined. Removing the ternary in activityMember.whereByUserVipTo fixes it:

return query.leftJoin('user');
// instead of:
return isJoinUser ? query : query.leftJoin('user');

And later there is a type error here:

query = query.whereByUserVipTo(...

The type doesn't match, but it doesn't have to match, it doesn't seem to be a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants