diff --git a/src/core/classes/database/eager-database-table.ts b/src/core/classes/database/eager-database-table.ts index 3bfbf6e8d..5820dd34f 100644 --- a/src/core/classes/database/eager-database-table.ts +++ b/src/core/classes/database/eager-database-table.ts @@ -172,7 +172,7 @@ export class CoreEagerDatabaseTable< continue; } - Object.assign(record, updates); + this.updateMemoryRecord(record, updates, this.records); } } @@ -187,7 +187,7 @@ export class CoreEagerDatabaseTable< continue; } - Object.assign(record, updates); + this.updateMemoryRecord(record, updates, this.records); } } diff --git a/src/core/classes/database/inmemory-database-table.ts b/src/core/classes/database/inmemory-database-table.ts index 7f01ad912..b3af85408 100644 --- a/src/core/classes/database/inmemory-database-table.ts +++ b/src/core/classes/database/inmemory-database-table.ts @@ -96,4 +96,24 @@ export abstract class CoreInMemoryDatabaseTable< return rowId; } + /** + * Update a record in memory. + * + * @param record Record to update. + * @param updates New values. + * @param records Records object. + */ + protected updateMemoryRecord(record: DBRecord, updates: Partial, records: Record): void { + const previousPrimaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record)); + + Object.assign(record, updates); + + const newPrimaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record)); + + if (newPrimaryKey !== previousPrimaryKey) { + delete records[previousPrimaryKey]; + records[newPrimaryKey] = record; + } + } + } diff --git a/src/core/classes/database/lazy-database-table.ts b/src/core/classes/database/lazy-database-table.ts index a745e29f4..d06aafc1c 100644 --- a/src/core/classes/database/lazy-database-table.ts +++ b/src/core/classes/database/lazy-database-table.ts @@ -156,7 +156,7 @@ export class CoreLazyDatabaseTable< continue; } - Object.assign(record, updates); + this.updateMemoryRecord(record, updates, this.records); } } @@ -171,7 +171,7 @@ export class CoreLazyDatabaseTable< continue; } - Object.assign(record, updates); + this.updateMemoryRecord(record, updates, this.records); } }