Skip to content

Commit 1d6ce0e

Browse files
runspiredigorT
authored andcommitted
[BUGFIX] fix single-table poymorphic-type-switch
1 parent 96016f9 commit 1d6ce0e

File tree

1 file changed

+16
-3
lines changed
  • packages/store/addon/-private/identifiers

1 file changed

+16
-3
lines changed

packages/store/addon/-private/identifiers/cache.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export class IdentifierCache {
317317
let newId = coerceId(data.id);
318318

319319
const keyOptions = getTypeIndex(this._cache.types, identifier.type);
320-
let existingIdentifier = detectMerge(keyOptions, identifier, newId);
320+
let existingIdentifier = detectMerge(keyOptions, identifier, data, newId, this._cache.lids);
321321

322322
if (existingIdentifier) {
323323
identifier = this._mergeRecordIdentifiers(keyOptions, identifier, existingIdentifier, data, newId as string);
@@ -356,6 +356,9 @@ export class IdentifierCache {
356356

357357
// ensure a secondary cache entry for this id for the identifier we do keep
358358
keyOptions.id[newId] = kept;
359+
// ensure a secondary cache entry for this id for the abandoned identifier's type we do keep
360+
let baseKeyOptions = getTypeIndex(this._cache.types, existingIdentifier.type);
361+
baseKeyOptions.id[newId] = kept;
359362

360363
// make sure that the `lid` on the data we are processing matches the lid we kept
361364
data.lid = kept.lid;
@@ -509,13 +512,23 @@ function performRecordIdentifierUpdate(
509512
function detectMerge(
510513
keyOptions: KeyOptions,
511514
identifier: StableRecordIdentifier,
512-
newId: string | null
515+
data: ResourceIdentifierObject | ExistingResourceObject,
516+
newId: string | null,
517+
lids: IdentifierMap
513518
): StableRecordIdentifier | false {
514-
const { id } = identifier;
519+
const { id, type, lid } = identifier;
515520
if (id !== null && id !== newId && newId !== null) {
516521
const existingIdentifier = keyOptions.id[newId];
517522

518523
return existingIdentifier !== undefined ? existingIdentifier : false;
524+
} else {
525+
let newType = normalizeModelName(data.type);
526+
527+
if (id !== null && id === newId && newType === type && data.lid && data.lid !== lid) {
528+
const existingIdentifier = lids[data.lid];
529+
530+
return existingIdentifier !== undefined ? existingIdentifier : false;
531+
}
519532
}
520533

521534
return false;

0 commit comments

Comments
 (0)