Skip to content

upsertMany with multiple updates with the same id throws away updates #3429

Closed
@johannaljunggren

Description

@johannaljunggren

I'm using upsertMany to push multiple updates with the same id. If the id previously didn't exist only the last update in the array of updates is added to the store. Example using the books example from the createEntityAdapter documentation https://redux-toolkit.js.org/api/createEntityAdapter:

const booksAdapter = createEntityAdapter({
  selectId: (book) => book.customId,
  sortComparer: (a, b) => (a.customId > b.customId ? 1 : -1)
});

const booksSlice = createSlice({
  name: "books",
  initialState: booksAdapter.getInitialState(),
  reducers: {
    booksUpserted: booksAdapter.upsertMany,
  }
});

const { booksUpserted } = booksSlice.actions;

const store = configureStore({
  reducer: {
    books: booksSlice.reducer
  }
});

store.dispatch(booksUpserted([
    { customId: "a", title: "First" },
    { customId: "a", author: "Author" },
    { customId: "a", description: "Description" }
  ])
);

After dispatching dispatching I expected the state to look like this:
{"ids":["a"],"entities":{"a":{"customId":"a", title: "First", author: "Author", "description":"Description"}}}
Instead it looks like this:
{"ids":["a"],"entities":{"a":{"customId":"a","description":"Description"}}}
I created a codesandbox example to demonstrate the issue https://codesandbox.io/s/upsertmany-test-lwmfy4?file=/src/App.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions