Skip to content

Commit

Permalink
refactor(queries): extract queries in correct file
Browse files Browse the repository at this point in the history
  • Loading branch information
trollepierre committed Aug 8, 2022
1 parent dc61669 commit 89dedb8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
42 changes: 41 additions & 1 deletion src/drive/web/modules/queries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CozyClient, { Q } from 'cozy-client'
import { TRASH_DIR_ID } from 'drive/constants/config'
import { DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes'
import { DOCTYPE_FILES, DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes'

// Needs to be less than 10 minutes, since "thumbnails" links
// are only valid for 10 minutes.
Expand Down Expand Up @@ -325,6 +325,46 @@ export const buildEncryptionByIdQuery = id => ({
}
})

/**
* Query to get all the files not trashed
* @returns {{definition: QueryDefinition}}
*/
export const buildSuggestionsQuery = () => ({
definition: Q(DOCTYPE_FILES)
.partialIndex({
_id: {
$ne: TRASH_DIR_ID
},
trashed: {
$or: [
{
$exists: false
},
{
$eq: false
}
]
},
path: {
$or: [
{
$exists: false
},
{
$regex: '^(?!/.cozy_trash)'
}
]
}
})
.select(['_id', 'trashed', 'dir_id', 'name', 'path'])
.where({
_id: {
$gt: null
}
})
.limitBy(1000)
})

export {
buildDriveQuery,
buildRecentQuery,
Expand Down
41 changes: 3 additions & 38 deletions src/drive/web/modules/services/components/SuggestionProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react'
import FuzzyPathSearch from '../FuzzyPathSearch'
import { withClient, Q } from 'cozy-client'
import { withClient } from 'cozy-client'

import { DOCTYPE_FILES } from 'drive/lib/doctypes'
import { TYPE_DIRECTORY, makeNormalizedFile } from './helpers'
import { getIconUrl } from './iconContext'
import { TRASH_DIR_ID } from 'drive/constants/config'
import { buildSuggestionsQuery } from '../../queries'

class SuggestionProvider extends React.Component {
componentDidMount() {
Expand Down Expand Up @@ -51,41 +50,7 @@ class SuggestionProvider extends React.Component {
async indexFiles() {
const { client } = this.props

const files = await client.queryAll(
Q(DOCTYPE_FILES)
.partialIndex({
_id: {
$ne: TRASH_DIR_ID
},
trashed: {
$or: [
{
$exists: false
},
{
$eq: false
}
]
},
path: {
$or: [
{
$exists: false
},
{
$regex: '^(?!/.cozy_trash)'
}
]
}
})
.select(['_id', 'trashed', 'dir_id', 'name', 'path'])
.where({
_id: {
$gt: null
}
})
.limitBy(1000)
)
const files = await client.queryAll(buildSuggestionsQuery().definition)

const folders = files.filter(file => file.type === TYPE_DIRECTORY)

Expand Down

0 comments on commit 89dedb8

Please sign in to comment.