Skip to content

Commit

Permalink
fix: Support executing queries from store
Browse files Browse the repository at this point in the history
The `getCollectionFromSlice` helper expects an object
with doctypes as key.
However, the method `this.store.getState()` returns a
larger object such as
`{ cozy: { documents: { [doctype]: ... }, queries: {} }`.

In this case, the `client.getCollectionFromState` method
is more appropriate.
  • Loading branch information
Merkur39 committed Aug 7, 2024
1 parent 1421f15 commit 75d3228
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/api/cozy-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ Generated URL

*Defined in*

packages/cozy-client/src/store/stateHelpers.js:15
[packages/cozy-client/src/store/stateHelpers.js:15](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/stateHelpers.js#L15)

***

Expand Down
7 changes: 4 additions & 3 deletions packages/cozy-client/src/store/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import sift from 'sift'

import flag from 'cozy-flags'

import { getCollectionFromSlice, getDocumentFromSlice } from './documents'
import { getDocumentFromSlice } from './documents'
import { isReceivingMutationResult } from './mutations'
import { properId } from './helpers'
import { isAGetByIdQuery, QueryDefinition } from '../queries/dsl'
import logger from '../logger'
import { getCollectionFromState } from './stateHelpers'

const INIT_QUERY = 'INIT_QUERY'
const LOAD_QUERY = 'LOAD_QUERY'
Expand Down Expand Up @@ -310,12 +311,12 @@ const getSelectorFilterFn = queryDefinition => {
/**
* Execute the given query against the document state.
*
* @param {import('../types').DocumentsStateSlice} state - The documents state
* @param {import('../types').CozyStore} state - The cozy state
* @param {QueryDefinition} queryDefinition - The query definition to execute
* @returns {import("../types").QueryStateData} - The returned documents from the query
*/
export const executeQueryFromState = (state, queryDefinition) => {
const documents = getCollectionFromSlice(state, queryDefinition.doctype)
const documents = getCollectionFromState(state, queryDefinition.doctype)
const isSingleObjectResponse = !!queryDefinition.id
if (!documents) {
return { data: isSingleObjectResponse ? null : [] }
Expand Down
25 changes: 17 additions & 8 deletions packages/cozy-client/src/store/queries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,15 @@ describe('execute query from state', () => {
}
}
}
const state = {
cozy: {
documents: {
...docState
},
queries: {}
}
}

it('should get the correct filtered results from state thanks to selector', () => {
const query1 = {
doctype: 'io.cozy.files',
Expand All @@ -705,7 +714,7 @@ describe('execute query from state', () => {
}
}
}
const res1 = executeQueryFromState(docState, query1)
const res1 = executeQueryFromState(state, query1)
expect(res1.data.length).toEqual(2)
expect(res1.data[0]).toEqual(docState['io.cozy.files']['456'])
expect(res1.data[1]).toEqual(docState['io.cozy.files']['789'])
Expand All @@ -716,7 +725,7 @@ describe('execute query from state', () => {
name: 'well'
}
}
const res2 = executeQueryFromState(docState, query2)
const res2 = executeQueryFromState(state, query2)
expect(res2.data.length).toEqual(1)
expect(res2.data[0]).toEqual(docState['io.cozy.files']['123'])

Expand All @@ -729,7 +738,7 @@ describe('execute query from state', () => {
name: 'hello'
}
}
const res3 = executeQueryFromState(docState, query3)
const res3 = executeQueryFromState(state, query3)
expect(res3.data.length).toEqual(1)
expect(res3.data[0]).toEqual(docState['io.cozy.files']['456'])

Expand All @@ -742,7 +751,7 @@ describe('execute query from state', () => {
}
}
}
const res4 = executeQueryFromState(docState, query4)
const res4 = executeQueryFromState(state, query4)
expect(res4.data.length).toEqual(0)
})

Expand All @@ -751,14 +760,14 @@ describe('execute query from state', () => {
doctype: 'io.cozy.files',
id: '123'
}
const res1 = executeQueryFromState(docState, query1)
const res1 = executeQueryFromState(state, query1)
expect(res1.data).toEqual(docState['io.cozy.files']['123'])

const query2 = {
doctype: 'io.cozy.files',
ids: ['123', '789']
}
const res2 = executeQueryFromState(docState, query2)
const res2 = executeQueryFromState(state, query2)
expect(res2.data.length).toEqual(2)
expect(res2.data[0]).toEqual(docState['io.cozy.files']['123'])
expect(res2.data[1]).toEqual(docState['io.cozy.files']['789'])
Expand All @@ -767,15 +776,15 @@ describe('execute query from state', () => {
doctype: 'io.cozy.files',
id: '-1'
}
const res3 = executeQueryFromState(docState, query3)
const res3 = executeQueryFromState(state, query3)
expect(res3.data).toEqual(null)
})

it('should get all the docs from state for the doctype when no filter', () => {
const query1 = {
doctype: 'io.cozy.files'
}
const res1 = executeQueryFromState(docState, query1)
const res1 = executeQueryFromState(state, query1)
expect(res1.data.length).toEqual(3)
})

Expand Down
11 changes: 11 additions & 0 deletions packages/cozy-client/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ import { QueryDefinition } from './queries/dsl'
* @property {Boolean} [encrypted] - encrypted value of the field (legacy)
*/

/**
* @typedef {object} CozyState
* @property {DocumentsStateSlice} documents
* @property {QueriesStateSlice} queries
*/

/**
* @typedef {object} CozyStore
* @property {CozyState} cozy
*/

/**
* @typedef {{
* [key: string]: ManifestField;
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-client/types/store/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function sortAndLimitDocsIds(queryState: import("../types").QueryState, d
}): Array<string>;
export function convert$gtNullSelectors(selector: any): object;
export function mergeSelectorAndPartialIndex(queryDefinition: object): object;
export function executeQueryFromState(state: import('../types').DocumentsStateSlice, queryDefinition: QueryDefinition): import("../types").QueryStateData;
export function executeQueryFromState(state: import('../types').CozyStore, queryDefinition: QueryDefinition): import("../types").QueryStateData;
export function makeSorterFromDefinition(definition: QueryDefinition): (arg0: Array<import("../types").CozyClientDocument>) => Array<import("../types").CozyClientDocument>;
export function updateData(query: import("../types").QueryState, newData: Array<import("../types").CozyClientDocument>, documents: import("../types").DocumentsStateSlice): import("../types").QueryState;
export default queries;
Expand Down
7 changes: 7 additions & 0 deletions packages/cozy-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,13 @@ export type ManifestField = {
*/
encrypted?: boolean;
};
export type CozyState = {
documents: DocumentsStateSlice;
queries: QueriesStateSlice;
};
export type CozyStore = {
cozy: CozyState;
};
export type ManifestFields = {
[key: string]: ManifestField;
};
Expand Down

0 comments on commit 75d3228

Please sign in to comment.