From e5e1d97af5308d340f91cb2d2ac50a288f02ff96 Mon Sep 17 00:00:00 2001 From: Crash-- Date: Wed, 17 Aug 2022 15:08:58 +0200 Subject: [PATCH 1/3] fix: Use find method where args are for find The idea is to check if there is some options related to _find. If yes, then we should use this route instead of {normal|all}_docs --- packages/cozy-client/src/StackLink.js | 34 +++++++++++++++++----- packages/cozy-client/src/StackLink.spec.js | 16 ++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/cozy-client/src/StackLink.js b/packages/cozy-client/src/StackLink.js index b38229711f..df71dbff33 100644 --- a/packages/cozy-client/src/StackLink.js +++ b/packages/cozy-client/src/StackLink.js @@ -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 +} /** * Returns full documents after a bulk update * @@ -66,7 +80,11 @@ export default class StackLink extends CozyLink { } return this.executeQuery(operation) } - + /** + * + * @param {QueryDefinition} query - Query to execute + * @returns {Promise} + */ executeQuery(query) { const { doctype, selector, id, ids, referenced, ...options } = query if (!doctype) { @@ -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) { diff --git a/packages/cozy-client/src/StackLink.spec.js b/packages/cozy-client/src/StackLink.spec.js index b7f74f4aa9..7bad5f60e3 100644 --- a/packages/cozy-client/src/StackLink.spec.js +++ b/packages/cozy-client/src/StackLink.spec.js @@ -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') + }) }) describe('reset', () => { From 76a4e50bf1a936d16c85193d67e2517bc4b7dd16 Mon Sep 17 00:00:00 2001 From: Crash-- Date: Wed, 17 Aug 2022 15:25:42 +0200 Subject: [PATCH 2/3] docs: Update doc --- docs/api/cozy-client/classes/StackLink.md | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/api/cozy-client/classes/StackLink.md b/docs/api/cozy-client/classes/StackLink.md index d8eb52d62f..d4ac8229fc 100644 --- a/docs/api/cozy-client/classes/StackLink.md +++ b/docs/api/cozy-client/classes/StackLink.md @@ -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 @@ -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 @@ -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) *** @@ -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* @@ -120,7 +120,7 @@ Transfers queries and mutations to a remote stack *Returns* -`any` +`Promise`<`any`> *Overrides* @@ -128,7 +128,7 @@ Transfers queries and mutations to a remote stack *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) *** @@ -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) From 4cdda31aa62a39e569defa737aef0bc33aa4901c Mon Sep 17 00:00:00 2001 From: Crash-- Date: Wed, 17 Aug 2022 15:37:28 +0200 Subject: [PATCH 3/3] docs: Type --- packages/cozy-client/types/StackLink.d.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cozy-client/types/StackLink.d.ts b/packages/cozy-client/types/StackLink.d.ts index 4040827f60..a52bcb8195 100644 --- a/packages/cozy-client/types/StackLink.d.ts +++ b/packages/cozy-client/types/StackLink.d.ts @@ -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} + */ + executeQuery(query: QueryDefinition): Promise; executeMutation(mutation: any, result: any, forward: any): Promise; } import { CouchDBBulkResult } from "./types"; import { CozyClientDocument } from "./types"; import CozyLink from "./CozyLink"; +import { QueryDefinition } from "./queries/dsl"; +import { ClientResponse } from "./types";