Function
setIdentifierGenerationMethod (method) public
Defined in ../packages/store/src/-types/q/identifier.ts:8
- method
Configures how unique identifier lid strings are generated by @ember-data/store.
This configuration MUST occur prior to the store instance being created.
Takes a method which can expect to receive various data as its first argument and the name of a bucket as its second argument.
Currently there are two buckets, 'record' and 'document'.
Resource (Record
) Identity
If the bucket is record
the method must return a unique (to at-least
the given bucket) string identifier for the given data as a string to be
used as the lid
of an Identifier
token.
This method will only be called by either getOrCreateRecordIdentifier
or
createIdentifierForNewRecord
when an identifier for the supplied data
is not already known via lid
or type + id
combo and one needs to be
generated or retrieved from a proprietary cache.
data
will be the same data argument provided to getOrCreateRecordIdentifier
and in the createIdentifierForNewRecord
case will be an object with
only type
as a key.
import { setIdentifierGenerationMethod } from '@ember-data/store';
export function initialize(applicationInstance) {
// note how `count` here is now scoped to the application instance
// for our generation method by being inside the closure provided
// by the initialize function
let count = 0;
setIdentifierGenerationMethod((resource, bucket) => {
return resource.lid || `my-key-${count++}`;
});
}
export default {
name: 'configure-ember-data-identifiers',
initialize
};
Document Identity
If the bucket is document
the method will receive the associated
immutable request
passed to store.request
as its first argument
and should return a unique string for the given request if the document
should be cached, and null
if it should not be cached.
Note, the request result will still be passed to the cache via Cache.put
,
but caches should take this as a signal that the document should not itself
be cached, while its contents may still be used to update other cache state.
The presence of cacheOptions.key
on the request will take precedence
for the document cache key, and this method will not be called if it is
present.
The default method implementation for this bucket is to return null
for all requests whose method is not GET
, and to return the url
for
those where it is.
This means that queries via POST
MUST provide cacheOptions.key
or
implement this hook.
⚠️ Caution: Requests that do not have a method
assigned are assumed to be GET