Skip to content

Commit

Permalink
fix(knex): Ensure that columns are selected unambigiously and avoid d…
Browse files Browse the repository at this point in the history
…uplicate id selection (#3144)
  • Loading branch information
daffl committed Apr 5, 2023
1 parent 7ed6cdf commit 3eb7428
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit 3eb7428

Please sign in to comment.