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

fix(minato): ensure full SELECT for JoinSelection only #95

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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