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: Use find method where args are for find #1225

Merged
merged 3 commits into from
Aug 17, 2022
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
28 changes: 14 additions & 14 deletions docs/api/cozy-client/classes/StackLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Transfers queries and mutations to a remote stack

*Defined in*

[packages/cozy-client/src/StackLink.js:45](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L45)
[packages/cozy-client/src/StackLink.js:59](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L59)

## Properties

Expand All @@ -40,7 +40,7 @@ Transfers queries and mutations to a remote stack

*Defined in*

[packages/cozy-client/src/StackLink.js:52](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L52)
[packages/cozy-client/src/StackLink.js:66](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L66)

## Methods

Expand All @@ -62,27 +62,27 @@ Transfers queries and mutations to a remote stack

*Defined in*

[packages/cozy-client/src/StackLink.js:91](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L91)
[packages/cozy-client/src/StackLink.js:111](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L111)

***

### executeQuery

▸ **executeQuery**(`query`): `any`
▸ **executeQuery**(`query`): `Promise`<`any`>

*Parameters*

| Name | Type |
| :------ | :------ |
| `query` | `any` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `query` | [`QueryDefinition`](QueryDefinition.md) | Query to execute |

*Returns*

`any`
`Promise`<`any`>

*Defined in*

[packages/cozy-client/src/StackLink.js:70](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L70)
[packages/cozy-client/src/StackLink.js:88](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L88)

***

Expand All @@ -102,13 +102,13 @@ Transfers queries and mutations to a remote stack

*Defined in*

[packages/cozy-client/src/StackLink.js:55](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L55)
[packages/cozy-client/src/StackLink.js:69](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L69)

***

### request

▸ **request**(`operation`, `result`, `forward`): `any`
▸ **request**(`operation`, `result`, `forward`): `Promise`<`any`>

*Parameters*

Expand All @@ -120,15 +120,15 @@ Transfers queries and mutations to a remote stack

*Returns*

`any`
`Promise`<`any`>

*Overrides*

[CozyLink](CozyLink.md).[request](CozyLink.md#request)

*Defined in*

[packages/cozy-client/src/StackLink.js:63](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L63)
[packages/cozy-client/src/StackLink.js:77](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L77)

***

Expand All @@ -142,4 +142,4 @@ Transfers queries and mutations to a remote stack

*Defined in*

[packages/cozy-client/src/StackLink.js:59](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L59)
[packages/cozy-client/src/StackLink.js:73](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/StackLink.js#L73)
34 changes: 27 additions & 7 deletions packages/cozy-client/src/StackLink.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { MutationTypes } from './queries/dsl'
import { MutationTypes, QueryDefinition } from './queries/dsl'
import CozyLink from './CozyLink'
import { CozyClientDocument, CouchDBBulkResult } from './types'
import { CozyClientDocument, CouchDBBulkResult, ClientResponse } from './types'
import { DOCTYPE_FILES } from './const'
import { BulkEditError } from './errors'
import zipWith from 'lodash/zipWith'

/**
*
* To know if cozy-client should use Document.find()
* or Document.all()
* Similar to what is done in CozyPouchLink executeQuery()
*
* @param {QueryDefinition} queryDefinition - QueryDefinition to check
* @returns {boolean} If has find options
*
*/
const hasFindOptions = queryDefinition => {
const { selector, partialFilter, sort, fields } = queryDefinition
if (selector || partialFilter || sort || fields) return true
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you prefer return selector || partialFilter || sort || fields ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have any pref. At first, this method was more complicated than just returning the fields. Since I wrote it that way, I will commit that way. Maybe next time you'll change it :p

}
/**
* Returns full documents after a bulk update
*
Expand Down Expand Up @@ -66,7 +80,11 @@ export default class StackLink extends CozyLink {
}
return this.executeQuery(operation)
}

/**
*
* @param {QueryDefinition} query - Query to execute
* @returns {Promise<ClientResponse>}
*/
executeQuery(query) {
const { doctype, selector, id, ids, referenced, ...options } = query
if (!doctype) {
Expand All @@ -83,9 +101,11 @@ export default class StackLink extends CozyLink {
if (referenced) {
return collection.findReferencedBy(referenced, options)
}
return !selector && !options.sort
? collection.all(options)
: collection.find(selector, options)
if (hasFindOptions(query)) {
return collection.find(selector, options)
} else {
return collection.all(options)
}
}

async executeMutation(mutation, result, forward) {
Expand Down
16 changes: 16 additions & 0 deletions packages/cozy-client/src/StackLink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ describe('StackLink', () => {
expect(stackClient.collection().all).toHaveBeenCalled()
expect(stackClient.collection).toHaveBeenCalledWith('io.cozy.todos')
})

it('should use find if a partialFilter is given', async () => {
const query = Q('io.cozy.todos').partialIndex({ trashed: false })
stackClient.collection().find.mockReset()
await link.request(query)
expect(stackClient.collection().find).toHaveBeenCalled()
expect(stackClient.collection).toHaveBeenCalledWith('io.cozy.todos')
})

it('should use find if fields are given', async () => {
const query = Q('io.cozy.todos').select(['trashed'])
stackClient.collection().find.mockReset()
await link.request(query)
expect(stackClient.collection().find).toHaveBeenCalled()
expect(stackClient.collection).toHaveBeenCalledWith('io.cozy.todos')
})
Crash-- marked this conversation as resolved.
Show resolved Hide resolved
})

describe('reset', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/cozy-client/types/StackLink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export default class StackLink extends CozyLink {
stackClient: any;
registerClient(client: any): void;
reset(): void;
executeQuery(query: any): any;
/**
*
* @param {QueryDefinition} query - Query to execute
* @returns {Promise<ClientResponse>}
*/
executeQuery(query: QueryDefinition): Promise<ClientResponse>;
executeMutation(mutation: any, result: any, forward: any): Promise<any>;
}
import { CouchDBBulkResult } from "./types";
import { CozyClientDocument } from "./types";
import CozyLink from "./CozyLink";
import { QueryDefinition } from "./queries/dsl";
import { ClientResponse } from "./types";