diff --git a/packages/common/src/utils/export-notes.ts b/packages/common/src/utils/export-notes.ts index 4fdb0d31f..97bea607e 100644 --- a/packages/common/src/utils/export-notes.ts +++ b/packages/common/src/utils/export-notes.ts @@ -72,7 +72,9 @@ export async function* exportNotes( const pathTree = new PathTree(); const notePathMap: Map = new Map(); - for await (const note of notes.fields(["notes.id", "notes.title"])) { + for await (const note of notes + .fields(["notes.id", "notes.title"]) + .iterate()) { const filename = `${sanitizeFilename(note.title || "Untitled", { replacement: "-" })}.${FORMAT_TO_EXT[format]}`; diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts index de9ce51c5..de46f93a0 100644 --- a/packages/core/src/api/lookup.ts +++ b/packages/core/src/api/lookup.ts @@ -184,7 +184,7 @@ export default class Lookup { ) { const results: Map = new Map(); const columns = fields.map((f) => f.column); - for await (const item of selector.fields(columns)) { + for await (const item of selector.fields(columns).iterate()) { if (limit && results.size >= limit) break; for (const field of fields) { diff --git a/packages/core/src/collections/attachments.ts b/packages/core/src/collections/attachments.ts index ff731dfb4..0a966ccf5 100644 --- a/packages/core/src/collections/attachments.ts +++ b/packages/core/src/collections/attachments.ts @@ -422,7 +422,7 @@ export class Attachments implements ICollection { async cleanup() { const now = dayjs().unix(); const ids: string[] = []; - for await (const attachment of this.deleted) { + for await (const attachment of this.deleted.iterate()) { if (dayjs(attachment.dateDeleted).add(7, "days").unix() < now) continue; const isDeleted = await this.db.fs().deleteFile(attachment.hash); diff --git a/packages/core/src/database/backup.ts b/packages/core/src/database/backup.ts index 7acfd7114..78fedd1e5 100644 --- a/packages/core/src/database/backup.ts +++ b/packages/core/src/database/backup.ts @@ -361,7 +361,7 @@ export default class Backup { }; let current = 0; - for await (const attachment of this.db.attachments.all) { + for await (const attachment of this.db.attachments.all.iterate()) { current++; if ( !(await this.db diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts index 39d8f9ee4..5483eab7a 100644 --- a/packages/core/src/database/sql-collection.ts +++ b/packages/core/src/database/sql-collection.ts @@ -476,7 +476,7 @@ export class FilteredSelector { async *map( fn: (item: T) => TReturnType ): AsyncIterableIterator { - for await (const item of this) { + for await (const item of this.iterate()) { yield fn(item); } } @@ -567,37 +567,43 @@ export class FilteredSelector { ); } - async *[Symbol.asyncIterator]() { - let lastRow: any | null = null; - const fields = this._fields.slice(); - if (fields.length > 0) { - if (!fields.find((f) => f.includes(".dateCreated"))) - fields.push("dateCreated"); - if (!fields.find((f) => f.includes(".id"))) fields.push("id"); - } + iterate() { + // eslint-disable-next-line @typescript-eslint/no-this-alias + const thisArg = this; + return { + async *[Symbol.asyncIterator]() { + let lastRow: any | null = null; + const fields = thisArg._fields.slice(); + if (fields.length > 0) { + if (!fields.find((f) => f.includes(".dateCreated"))) + fields.push("dateCreated"); + if (!fields.find((f) => f.includes(".id"))) fields.push("id"); + } - while (true) { - const rows = await this.filter - .orderBy("dateCreated asc") - .orderBy("id asc") - .$if(lastRow !== null, (qb) => - qb.where( - (eb) => eb.refTuple("dateCreated", "id"), - ">", - (eb) => eb.tuple(lastRow.dateCreated, lastRow.id) - ) - ) - .limit(this.batchSize) - .$if(fields.length === 0, (eb) => eb.selectAll()) - .$if(fields.length > 0, (eb) => eb.select(fields)) - .execute(); - if (rows.length === 0) break; - for (const row of rows) { - yield row as T; + while (true) { + const rows = await thisArg.filter + .orderBy("dateCreated asc") + .orderBy("id asc") + .$if(lastRow !== null, (qb) => + qb.where( + (eb) => eb.refTuple("dateCreated", "id"), + ">", + (eb) => eb.tuple(lastRow.dateCreated, lastRow.id) + ) + ) + .limit(thisArg.batchSize) + .$if(fields.length === 0, (eb) => eb.selectAll()) + .$if(fields.length > 0, (eb) => eb.select(fields)) + .execute(); + if (rows.length === 0) break; + for (const row of rows) { + yield row as T; + } + + lastRow = rows[rows.length - 1]; + } } - - lastRow = rows[rows.length - 1]; - } + }; } private buildSortExpression(options: GroupOptions, hasDueDate?: boolean) {