retrieve all from indexed db in one call

This commit is contained in:
Julien Calixte
2025-01-12 10:58:03 +01:00
parent 2661ae0a67
commit 4512530250
2 changed files with 43 additions and 20 deletions

View File

@@ -121,6 +121,7 @@ class Data {
keys: keys.map((key) => this.generateId(prefix, key))
})
if (includeDocs) {
return response.rows
.map((row) => {
if ('error' in row) {
@@ -129,7 +130,18 @@ class Data {
return row.doc
})
.filter((doc) => !!doc) as T[]
.filter(Boolean) as T[]
} else {
return response.rows
.map((row) => {
if ('error' in row) {
return null
}
return { _id: row.id }
})
.filter(Boolean) as T[]
}
}
const response = await this.locale.allDocs({

View File

@@ -1,7 +1,10 @@
import { useAsyncState } from '@vueuse/core'
import { asyncComputed, useAsyncState } from '@vueuse/core'
import { computed, ref } from 'vue'
import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum'
import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache'
import { Note } from '@/modules/note/models/Note'
import { queryFileContent } from '@/modules/repo/services/repo'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
@@ -11,6 +14,20 @@ export const useOfflineNotes = () => {
const noteCompleted = ref(0)
const cachedNotesFromSha = asyncComputed(
async () =>
data.getAll<DataType.Note, Note>({
prefix: DataType.Note,
keys: store.files.map((file) => file.sha).filter(Boolean) as string[],
includeDocs: false
}),
[]
)
const cachedNotesSet = computed(
() => new Set(cachedNotesFromSha.value.map((note) => note._id))
)
const cacheAllNotes = async () => {
const isInitialized = store.user && store.repo && totalOfNotes.value > 0
@@ -23,20 +40,14 @@ export const useOfflineNotes = () => {
for (const file of store.files) {
noteCompleted.value++
if (!file.sha) {
if (
!file.sha ||
cachedNotesSet.value.has(data.generateId(DataType.Note, file.sha))
) {
continue
}
const { getCachedNote, saveCacheNote } = prepareNoteCache(
file.sha,
file.path
)
const { from } = await getCachedNote()
if (from === 'sha') {
continue
}
const { saveCacheNote } = prepareNoteCache(file.sha, file.path)
const contentFile = await queryFileContent(
store.user,