Skip to content

Commit

Permalink
chore(db-postgres): correct joins order
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed May 26, 2024
1 parent 2a8f144 commit 0c2fd92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
13 changes: 6 additions & 7 deletions packages/db-postgres/src/find/findMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ export const findMany = async function find({

if (pagination !== false && (orderedIDs ? orderedIDs?.length <= limit : true)) {
const selectCountMethods: ChainedMethods = []
joinAliases.forEach(({ condition, table }) => {
selectCountMethods.push({
args: [table, condition],
method: 'leftJoin',
})
})

Object.entries(joins).forEach(([joinTable, condition]) => {
if (joinTable) {
Expand All @@ -132,13 +138,6 @@ export const findMany = async function find({
}
})

joinAliases.forEach(({ condition, table }) => {
selectCountMethods.push({
args: [table, condition],
method: 'leftJoin',
})
})

const countResult = await chainMethods({
methods: selectCountMethods,
query: db
Expand Down
15 changes: 7 additions & 8 deletions packages/db-postgres/src/queries/selectDistinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export const selectDistinct = ({
chainedMethods.push({ args: [where], method: 'where' })
}

joinAliases.forEach(({ condition, table }) => {
chainedMethods.push({
args: [table, condition],
method: 'leftJoin',
})
})

Object.entries(joins).forEach(([joinTable, condition]) => {
if (joinTable) {
chainedMethods.push({
Expand All @@ -44,14 +51,6 @@ export const selectDistinct = ({
})
}
})

joinAliases.forEach(({ condition, table }) => {
chainedMethods.push({
args: [table, condition],
method: 'leftJoin',
})
})

return chainMethods({
methods: chainedMethods,
query: db.selectDistinct(selectFields).from(adapter.tables[tableName]),
Expand Down

0 comments on commit 0c2fd92

Please sign in to comment.