romeerez/orchid-orm

[ask] How to reuse repo code in subquery

Closed this issue · 3 comments

const data = db.xxx.select({
  relationCount: q => q.relation.where(xxx).count() // the q in subquery can't re-use repo methods
})

How about myRepo(q.relation).whereSomething(xxx)?

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() // the q in subquery can't re-use repo methods
})

Thank you, romeerez, it works well, I didn't think it would work.
This technique should be written into docs.

It's present here in repo docs in examples.