From 9cc118f5f7d5449338c25a0bb448550aac7c1d42 Mon Sep 17 00:00:00 2001 From: dfahlander Date: Tue, 2 Apr 2024 18:22:21 +0200 Subject: [PATCH] Resolve #1946 --- src/live-query/observability-middleware.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/live-query/observability-middleware.ts b/src/live-query/observability-middleware.ts index 571687034..838bff38c 100644 --- a/src/live-query/observability-middleware.ts +++ b/src/live-query/observability-middleware.ts @@ -45,8 +45,11 @@ export const observabilityMiddleware: Middleware = { table: (tableName) => { const table = core.table(tableName); const { schema } = table; - const { primaryKey } = schema; + const { primaryKey, indexes } = schema; const { extractKey, outbound } = primaryKey; + const indexesWithAutoIncPK = primaryKey.autoIncrement && indexes.filter( + (index) => index.compound && (index.keyPath as string[]).includes(primaryKey.keyPath as string) + ); const tableClone: DBCoreTable = { ...table, mutate: (req) => { @@ -117,6 +120,22 @@ export const observabilityMiddleware: Middleware = { // Less than 50 requests (keys truthy) (otherwise we've added full range anyway) // autoincrement means we might not have got all keys until now pkRangeSet.addKeys(res.results); + if (indexesWithAutoIncPK) { + // Dexie Issue 1946: + // If an auto-incremented primary key is part of a compound index, + // we need to compute the resulting value of that index after inserting + // the rows. + indexesWithAutoIncPK.forEach(idx => { + // Extract values of this compound index where primary key is not yet set: + const idxVals = req.values.map(v => idx.extractKey(v)); + // Find the position of the primary key in the index: + const pkPos = (idx.keyPath as string[]).findIndex(prop => prop === primaryKey.keyPath); + // Update idxVals with the resulting primary keys to complete the index value: + res.results!.forEach(pk => idxVals[pkPos] = pk); + // Add the updated index to the rangeset: + getRangeSet(idx.name).addKeys(idxVals); + }); + } } trans.mutatedParts = extendObservabilitySet ( trans.mutatedParts || {},