From b364178fc0d574644f6810cb02bef273de7304be Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 7 Nov 2023 08:14:45 +0500 Subject: [PATCH] core: add support for fetching trash as grouped --- packages/core/src/collections/trash.ts | 60 +++++++++++++++++--------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.ts index 994bb282f..c8e8a46dd 100644 --- a/packages/core/src/collections/trash.ts +++ b/packages/core/src/collections/trash.ts @@ -20,8 +20,9 @@ along with this program. If not, see . import dayjs from "dayjs"; import Database from "../api"; import { deleteItems } from "../utils/array"; -import { FilteredSelector } from "../database/sql-collection"; -import { TrashItem } from "../types"; +import { GroupOptions, TrashItem } from "../types"; +import { VirtualizedGrouping } from "../utils/virtualized-grouping"; +import { groupArray } from "../utils/grouping"; export default class Trash { collections = ["notes", "notebooks"] as const; @@ -110,18 +111,19 @@ export default class Trash { } } - async delete(...items: { id: string; type: "note" | "notebook" }[]) { - if (items.length <= 0) return; + async delete(...ids: string[]) { + if (ids.length <= 0) return; const noteIds = []; const notebookIds = []; - for (const item of items) { - if (item.type === "note") { - noteIds.push(item.id); - this.cache.notes.splice(this.cache.notes.indexOf(item.id), 1); - } else if (item.type === "notebook") { - notebookIds.push(item.id); - this.cache.notebooks.splice(this.cache.notebooks.indexOf(item.id), 1); + for (const id of ids) { + const isNote = this.cache.notes.includes(id); + if (isNote) { + noteIds.push(id); + this.cache.notes.splice(this.cache.notes.indexOf(id), 1); + } else if (!isNote) { + notebookIds.push(id); + this.cache.notebooks.splice(this.cache.notebooks.indexOf(id), 1); } } @@ -143,18 +145,19 @@ export default class Trash { } } - async restore(...items: { id: string; type: "note" | "notebook" }[]) { - if (items.length <= 0) return; + async restore(...ids: string[]) { + if (ids.length <= 0) return; const noteIds = []; const notebookIds = []; - for (const item of items) { - if (item.type === "note") { - noteIds.push(item.id); - this.cache.notes.splice(this.cache.notes.indexOf(item.id), 1); - } else if (item.type === "notebook") { - notebookIds.push(item.id); - this.cache.notebooks.splice(this.cache.notebooks.indexOf(item.id), 1); + for (const id of ids) { + const isNote = this.cache.notes.includes(id); + if (isNote) { + noteIds.push(id); + this.cache.notes.splice(this.cache.notes.indexOf(id), 1); + } else if (!isNote) { + notebookIds.push(id); + this.cache.notebooks.splice(this.cache.notebooks.indexOf(id), 1); } } @@ -208,6 +211,23 @@ export default class Trash { return [...trashedNotes, ...trashedNotebooks] as TrashItem[]; } + async grouped(options: GroupOptions) { + const items = await this.all(); + const ids = groupArray(items, options); + const records: Record = {}; + for (const item of items) records[item.id] = item; + + return new VirtualizedGrouping( + ids, + this.db.options?.batchSize || 500, + async (ids: string[]) => { + const items: Record = {}; + for (const id of ids) items[id] = records[id]; + return items; + } + ); + } + /** * * @param {string} id