data icon indicating copy to clipboard operation
data copied to clipboard

Enable creation of new record with a pre-assigned lid

Open robbytx opened this issue 7 months ago • 0 comments

From #ember-data discord

Problem

In scenarios dealing with multiple stores, it can be desirable for each store to represent the same new record, before that record has received a persistent id from the server.

In the original store, the application typically calls store.createRecord('example', ...), which calls identifierCache.createIdentifierForNewRecord({ type: 'example', id: null }), which delegates to the configured/default identifier generation method, which generates a lid value to uniquely identify the record.

However, if the code syncing the state across (in our case, syncing across tabs) to another store uses store.createRecord in the same manner, then record in this store is generated with a different lid value that has no relationship to the original lid.

As seen in my multi-store-id-issue repro, this creates problems with the shared NEW_IDENTIFIERS map in the default identifier generation method, when the record identifiers in each store are updated to the same persistent id.

Workaround

To workaround this issue, the "code syncing the state across" must utilize the IdentifierCache and Cache API directly rather than relying on the existing Store APIs, in order to create a record with a pre-assigned lid.

Request

The suggestion here is to introduce some Store (or other simple) API that provides the facility to create a record in a new state while specifying a specific lid to use.

robbytx avatar Jun 06 '25 18:06 robbytx