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(knex): Ensure that columns are selected unambigiously #3144

Merged
merged 1 commit into from
Apr 5, 2023
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
3 changes: 2 additions & 1 deletion packages/knex/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ export class KnexAdapter<

// $select uses a specific find syntax, so it has to come first.
if (filters.$select) {
const select = filters.$select.map((column) => (column.includes('.') ? column : `${name}.${column}`))
// always select the id field, but make sure we only select it once
builder.select(...new Set([...filters.$select, `${name}.${id}`]))
builder.select(...new Set([...select, `${name}.${id}`]))
} else {
builder.select(`${name}.*`)
}
Expand Down
12 changes: 11 additions & 1 deletion packages/knex/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ describe('Feathers Knex Service', () => {
describe('associations', () => {
const todoService = app.service('todos')

it('create, query and get with associations', async () => {
it('create, query and get with associations, can unambigiously $select', async () => {
const dave = await peopleService.create({
name: 'Dave',
age: 133
Expand All @@ -696,6 +696,16 @@ describe('Feathers Knex Service', () => {
})
const got = await todoService.get(todo.id)

assert.deepStrictEqual(
await todoService.get(todo.id, {
query: { $select: ['id', 'text'] }
}),
{
id: todo.id,
text: todo.text,
personName: 'Dave'
}
)
assert.strictEqual(got.personName, dave.name)
assert.deepStrictEqual(got, todo)
assert.deepStrictEqual(found, todo)
Expand Down