From ce53495b76cd3b00e12d758909e9f517b808c743 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 7 Mar 2024 14:59:20 +0500 Subject: [PATCH] core: do not change date modified on items when bulk adding --- packages/core/src/database/index.ts | 2 +- packages/core/src/database/sql-cached-collection.ts | 9 +++++---- packages/core/src/database/sql-collection.ts | 9 ++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.ts index e4e34fc28..ce9672814 100644 --- a/packages/core/src/database/index.ts +++ b/packages/core/src/database/index.ts @@ -123,7 +123,7 @@ export interface DatabaseCollection { exists(id: string): AsyncOrSyncResult; count(): AsyncOrSyncResult; get(id: string): AsyncOrSyncResult; - put(items: (T | undefined)[]): Promise; + put(items: (T | undefined)[]): Promise[]>; update(ids: string[], partial: Partial): Promise; ids(options: GroupOptions): AsyncOrSyncResult; records( diff --git a/packages/core/src/database/sql-cached-collection.ts b/packages/core/src/database/sql-cached-collection.ts index e95296419..ae87775c4 100644 --- a/packages/core/src/database/sql-cached-collection.ts +++ b/packages/core/src/database/sql-cached-collection.ts @@ -106,11 +106,12 @@ export class SQLCachedCollection< return item; } - async put(items: (T | undefined)[]): Promise { - await this.collection.put(items); - for (const item of items) { - if (item) this.cache.set(item.id, item); + async put(items: (T | undefined)[]) { + const entries = await this.collection.put(items); + for (const item of entries) { + this.cache.set(item.id, item as T); } + return entries; } async update(ids: string[], partial: Partial): Promise { diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts index 207a8778d..8b15ece25 100644 --- a/packages/core/src/database/sql-collection.ts +++ b/packages/core/src/database/sql-collection.ts @@ -153,11 +153,13 @@ export class SQLCollection< } async put(items: (SQLiteItem | undefined)[]) { - if (items.length <= 0) return; + if (items.length <= 0) return []; const entries = items.reduce((array, item) => { if (!item) return array; if (!item.remote) { - item.dateModified = Date.now(); + // NOTE: this is intentional + // When we are bulk adding items, we shouldn't touch the dateModified + // item.dateModified = Date.now(); item.synced = false; } delete item.remote; @@ -165,7 +167,7 @@ export class SQLCollection< return array; }, [] as SQLiteItem[]); - if (entries.length <= 0) return; + if (entries.length <= 0) return []; await this.startTransaction(async (tx) => { for (const chunk of toChunks(entries, 200)) { @@ -175,6 +177,7 @@ export class SQLCollection< .execute(); } }); + return entries; } async update(