Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix `too many SQL variables` error on trash cleanup

Abdullah Attafbb3a56c0ca7
diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.ts
index 81f283602..584cea888 100644
--- a/packages/core/src/collections/trash.ts
+++ b/packages/core/src/collections/trash.ts
@@ -19,11 +19,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import dayjs from "dayjs";
import Database from "../api";
-import { deleteItems } from "../utils/array";
+import { deleteItems, toChunks } from "../utils/array";
import { GroupOptions, TrashItem } from "../types";
import { VirtualizedGrouping } from "../utils/virtualized-grouping";
import { getSortSelectors, groupArray } from "../utils/grouping";
import { sql } from "kysely";
+import { MAX_SQL_PARAMETERS } from "../database/sql-collection";
export default class Trash {
collections = ["notes", "notebooks"] as const;
@@ -143,17 +144,21 @@ export default class Trash {
private async _delete(noteIds: string[], notebookIds: string[]) {
if (noteIds.length > 0) {
- await this.db.content.removeByNoteId(...noteIds);
- await this.db.noteHistory.clearSessions(...noteIds);
- await this.db.notes.remove(...noteIds);
- deleteItems(this.cache.notes, ...noteIds);
+ for (const chunk of toChunks(noteIds, MAX_SQL_PARAMETERS)) {
+ await this.db.content.removeByNoteId(...chunk);
+ await this.db.noteHistory.clearSessions(...chunk);
+ await this.db.notes.remove(...chunk);
+ deleteItems(this.cache.notes, ...chunk);
+ }
}
if (notebookIds.length > 0) {
const ids = [...notebookIds, ...(await this.subNotebooks(notebookIds))];
- await this.db.notebooks.remove(...ids);
- await this.db.relations.unlinkOfType("notebook", ids);
- deleteItems(this.cache.notebooks, ...ids);
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
+ await this.db.notebooks.remove(...chunk);
+ await this.db.relations.unlinkOfType("notebook", chunk);
+ deleteItems(this.cache.notebooks, ...chunk);
+ }
}
}
diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts
index 5a741f87d..cd4c63537 100644
--- a/packages/core/src/database/sql-collection.ts
+++ b/packages/core/src/database/sql-collection.ts
@@ -63,7 +63,7 @@ const formats = {
default: "%Y-%W",
none: null
} satisfies Record<GroupOptions["groupBy"], string | null>;
-const MAX_PARAMETERS = 200;
+export const MAX_SQL_PARAMETERS = 200;
export class SQLCollection<
TCollectionType extends keyof DatabaseSchema,
@@ -114,7 +114,7 @@ export class SQLCollection<
async softDelete(ids: string[]) {
await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_PARAMETERS)) {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
await tx
.replaceInto<keyof DatabaseSchema>(this.type)
.values(
@@ -139,7 +139,7 @@ export class SQLCollection<
if (ids.length <= 0) return;
await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_PARAMETERS)) {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
await tx
.deleteFrom<keyof DatabaseSchema>(this.type)
.where("id", "in", chunk)
@@ -207,7 +207,7 @@ export class SQLCollection<
if (entries.length <= 0) return [];
await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(entries, MAX_PARAMETERS)) {
+ for (const chunk of toChunks(entries, MAX_SQL_PARAMETERS)) {
await tx
.replaceInto<keyof DatabaseSchema>(this.type)
.values(chunk)
@@ -225,7 +225,7 @@ export class SQLCollection<
if (!this.sanitizer.sanitize(this.type, partial)) return;
await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_PARAMETERS)) {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
await tx
.updateTable<keyof DatabaseSchema>(this.type)
.where("id", "in", chunk)