Skip to content

Commit

Permalink
fix(minato): ensure full SELECT for JoinSelection only (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest authored May 12, 2024
1 parent 2c43c43 commit 2673719
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,19 @@ export abstract class Driver<T = any, C extends Context = Context> {
}

if (table instanceof Selection) {
if (!table.args[0].fields) return table.model
if (!table.args[0].fields && (typeof table.table === 'string' || table.table instanceof Selection)) {
return table.model
}
const model = new Model('temp')
model.fields = mapValues(table.args[0].fields, (expr, key) => ({
type: Type.fromTerm(expr),
}))
if (table.args[0].fields) {
model.fields = mapValues(table.args[0].fields, (expr) => ({
type: Type.fromTerm(expr),
}))
} else {
model.fields = mapValues(table.model.fields, (field) => ({
type: Type.fromField(field),
}))
}
return model
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ export class Builder {
suffix = ` WHERE ${filter}` + suffix
}

if (inline && !args[0].fields && !suffix) {
if (inline && !args[0].fields && !suffix && (typeof table === 'string' || table instanceof Selection)) {
return (addref && isBracketed(prefix)) ? `${prefix} ${ref}` : prefix
}

Expand Down
13 changes: 13 additions & 0 deletions packages/tests/src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ namespace SelectionTests {
.join(['foo', 'bar'])
.execute(row => $.count(row.bar.id))
).to.eventually.equal(6)

await expect(database
.join(['foo', 'bar'])
.where(row => $.gt(row.bar.id, 3))
.execute(row => $.count(row.bar.id))
).to.eventually.equal(3)

await expect(database
.join(['foo', 'bar'])
.where(row => $.gt(row.bar.id, 3))
.orderBy(row => row.bar.id)
.execute(row => $.count(row.bar.id))
).to.eventually.equal(3)
})
}

Expand Down

0 comments on commit 2673719

Please sign in to comment.