Skip to content

Commit

Permalink
fix(memory/mongodb): $select as only property & force 'id' in '$selec…
Browse files Browse the repository at this point in the history
…t' (#3081)
  • Loading branch information
fratzinger committed Mar 8, 2023
1 parent 2456bf8 commit fbe3cf5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/adapter-tests/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default (test: AdapterMethodsTest, app: any, _errors: any, serviceName: s
query: { $select: ['name'] }
})

assert.strictEqual(data[idProp].toString(), doug[idProp].toString(), `${idProp} id property matches`)
assert.strictEqual(data.name, 'Doug', 'data.name matches')
assert.ok(!data.age, 'data.age is falsy')
})
Expand Down Expand Up @@ -244,6 +245,7 @@ export default (test: AdapterMethodsTest, app: any, _errors: any, serviceName: s
query: { $select: ['name'] }
})

assert.strictEqual(data[idProp].toString(), doug[idProp].toString(), `${idProp} id property matches`)
assert.strictEqual(data.name, 'Dougler', 'data.name matches')
assert.ok(!data.age, 'data.age is falsy')
})
Expand Down Expand Up @@ -333,6 +335,7 @@ export default (test: AdapterMethodsTest, app: any, _errors: any, serviceName: s
query: { $select: ['name'] }
})

assert.strictEqual(data[idProp].toString(), doug[idProp].toString(), `${idProp} id property matches`)
assert.strictEqual(data.name, 'PatchDoug', 'data.name matches')
assert.ok(!data.age, 'data.age is falsy')
})
Expand Down Expand Up @@ -617,6 +620,7 @@ export default (test: AdapterMethodsTest, app: any, _errors: any, serviceName: s
query: { $select: ['name'] }
})

assert.ok(idProp in data, 'data has id')
assert.strictEqual(data.name, 'William', 'data.name matches')
assert.ok(!data.age, 'data.age is falsy')

Expand Down
1 change: 1 addition & 0 deletions packages/adapter-tests/src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export default (test: AdapterSyntaxTest, app: any, _errors: any, serviceName: st
})

assert.strictEqual(data.length, 1)
assert.ok(idProp in data[0], 'data has id')
assert.strictEqual(data[0].name, 'Alice')
assert.strictEqual(data[0].age, undefined)
})
Expand Down
6 changes: 4 additions & 2 deletions packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface MemoryServiceOptions<T = any> extends AdapterServiceOptions {
sorter?: (sort: any) => any
}

const _select = (data: any, params: any, ...args: any[]) => {
const _select = (data: any, params: any, ...args: string[]) => {
const base = select(params, ...args)

return base(JSON.parse(JSON.stringify(data)))
Expand Down Expand Up @@ -103,14 +103,16 @@ export class MemoryAdapter<
continue
}

matched.push(_select(value, params))
matched.push(_select(value, params, this.id))

if (hasLimit && filters.$limit === matched.length) {
break
}
}

values = matched
} else {
values = values.map((value) => _select(value, params, this.id))
}

const result: Paginated<Result> = {
Expand Down
27 changes: 14 additions & 13 deletions packages/memory/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,33 +190,34 @@ describe('Feathers Memory Service', () => {
await people.remove(person.id)
})

it('does not $select the id', async () => {
it('update with null throws error', async () => {
try {
await app.service('people').update(null, {})
throw new Error('Should never get here')
} catch (error: any) {
assert.strictEqual(error.message, "You can not replace multiple instances. Did you mean 'patch'?")
}
})

it('use $select as only query property', async () => {
const people = app.service('people')
const person = await people.create({
name: 'Tester'
name: 'Tester',
age: 42
})

const results = await people.find({
paginate: false,
query: {
name: 'Tester',
$select: ['name']
}
})

assert.deepStrictEqual(results[0], { name: 'Tester' }, 'deepEquals the same')
assert.deepStrictEqual(results[0], { id: person.id, name: 'Tester' })

await people.remove(person.id)
})

it('update with null throws error', async () => {
try {
await app.service('people').update(null, {})
throw new Error('Should never get here')
} catch (error: any) {
assert.strictEqual(error.message, "You can not replace multiple instances. Did you mean 'patch'?")
}
})

testSuite(app, errors, 'people')
testSuite(app, errors, 'people-customid', 'customid')
})
10 changes: 10 additions & 0 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ export class MongoDbAdapter<

getSelect(select: string[] | { [key: string]: number }) {
if (Array.isArray(select)) {
if (!select.includes(this.id)) {
select = [this.id, ...select]
}
return select.reduce<{ [key: string]: number }>(
(value, name) => ({
...value,
Expand All @@ -168,6 +171,13 @@ export class MongoDbAdapter<
)
}

if (!select[this.id]) {
return {
...select,
[this.id]: 1
}
}

return select
}

Expand Down
9 changes: 5 additions & 4 deletions packages/schema/src/hooks/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,21 @@ export const resolveResult = <H extends HookContext>(...resolvers: Resolver<any,
throw new Error('The resolveResult hook must be used as an around hook')
}

const { $resolve, $select: select, ...query } = context.params?.query || {}
const $select = Array.isArray(select) ? select.filter((name) => !virtualProperties.has(name)) : select
const { $resolve, $select, ...query } = context.params?.query || {}
const hasVirtualSelects = Array.isArray($select) && $select.some((name) => virtualProperties.has(name))

const resolve = {
originalContext: context,
...context.params.resolve,
properties: $resolve || select
properties: $resolve || $select
}

context.params = {
...context.params,
resolve,
query: {
...query,
...($select ? { $select } : {})
...(!hasVirtualSelects ? { $select } : {})
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('@feathersjs/schema/hooks', () => {
$select: ['user', 'text']
}
})
assert.strictEqual(Object.keys(messages[0]).length, 2)
assert.deepStrictEqual(Object.keys(messages[0]), ['text', 'user'])
})

it('resolves find results with paginated result object', async () => {
Expand Down

0 comments on commit fbe3cf5

Please sign in to comment.