Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Ember Data ID Generation #19428

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ui/app/initializers/ember-data-identifiers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { setIdentifierGenerationMethod } from '@ember-data/store';
import { dasherize } from '@ember/string';
import { v4 as uuidv4 } from 'uuid';

export function initialize() {
// see this GH issue for more information https://github.com/emberjs/data/issues/8106
// Ember Data uses uuidv4 library to generate ids which relies on the crypto API which is no available in unsecure contexts
// the suggested polyfill was added in 4.6.2 so until we upgrade we need to define our own id generation method
// https://api.emberjs.com/ember-data/4.5/classes/IdentifierCache/methods/getOrCreateRecordIdentifier?anchor=getOrCreateRecordIdentifier
// the uuid library was brought in to replace other usages of crypto in the app so it is safe to use in unsecure contexts
setIdentifierGenerationMethod((resource) => {
return resource.lid || uuidv4();
// adapted from defaultGenerationMethod -- https://github.com/emberjs/data/blob/v4.5.0/packages/store/addon/-private/identifier-cache.ts#LL82-L94C2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this!

setIdentifierGenerationMethod((data) => {
if (data.lid) {
return data.lid;
}
if (data.id) {
return `@lid:${dasherize(data.type)}-${data.id}`;
}
return uuidv4();
});
}

Expand Down