romeerez/orchid-orm

reuse repo in repo

Opened this issue · 5 comments

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
      })
    },
  },
});

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.

@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.

@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.

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

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.