Skip to content

Commit

Permalink
[BUGFIX] fix importing from @ember-data/store
Browse files Browse the repository at this point in the history
Dividing Record Data into packages (#6513) configured the Store class
inside of the `-ember-data` metapackage. This broke importing the Store directly from `@ember-data/store`,
which this commit fixes.
  • Loading branch information
igorT committed Jan 12, 2020
1 parent 7ea3d45 commit 3e3677f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 26 deletions.
22 changes: 1 addition & 21 deletions packages/-ember-data/addon/store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
import { IDENTIFIERS } from '@ember-data/canary-features';
import { RecordData } from '@ember-data/record-data/-private';
import Store from '@ember-data/store';
import { identifierCacheFor } from '@ember-data/store/-private';

type RecordDataStoreWrapper = import('@ember-data/store/-private/ts-interfaces/record-data-store-wrapper').RecordDataStoreWrapper;

export default class DefaultStore extends Store {
createRecordDataFor(modelName: string, id: string | null, clientId: string, storeWrapper: RecordDataStoreWrapper) {
if (IDENTIFIERS) {
let identifier = identifierCacheFor(this).getOrCreateRecordIdentifier({
type: modelName,
id,
lid: clientId,
});
return new RecordData(identifier, storeWrapper);
} else {
return new RecordData(modelName, id, clientId, storeWrapper);
}
}
}
export { default } from '@ember-data/store';
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { get } from '@ember/object';
import { settled } from '@ember/test-helpers';

import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model, { attr } from '@ember-data/model';
import Store from '@ember-data/store';

class Person extends Model {
@attr()
name;
}

class CustomStore extends Store {}

module('integration/store/package-import', function(hooks) {
setupTest(hooks);

let store;

hooks.beforeEach(function() {
let { owner } = this;

owner.register('model:person', Person);
owner.unregister('service:store');
owner.register('service:store', CustomStore);
store = owner.lookup('service:store');
});

test('Store push works with an import from @ember-data/store', async function(assert) {
store.push({
data: [
{
type: 'person',
id: '1',
attributes: {
name: 'Scumbag Dale',
},
},
{
type: 'person',
id: '2',
attributes: {
name: 'Scumbag Katz',
},
},
],
});

let all = store.peekAll('person');
assert.equal(get(all, 'length'), 2);

store.push({
data: [
{
type: 'person',
id: '3',
attributes: {
name: 'Scumbag Bryn',
},
},
],
});

await settled();
assert.equal(get(all, 'length'), 3);
});
});
33 changes: 28 additions & 5 deletions packages/store/addon/-private/system/ds-model-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ import { assign } from '@ember/polyfills';
import { isPresent } from '@ember/utils';
import { DEBUG } from '@glimmer/env';

import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features';
import { CUSTOM_MODEL_CLASS, IDENTIFIERS } from '@ember-data/canary-features';
import { HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';

import { identifierCacheFor } from '../identifiers/cache';
import CoreStore from './core-store';
import notifyChanges from './model/notify-changes';
import { getShimClass } from './model/shim-model-class';
import normalizeModelName from './normalize-model-name';
import { DSModelSchemaDefinitionService, getModelFactory } from './schema-definition-service';

type RecordDataStoreWrapper = import('./store/record-data-store-wrapper').default;

const RecordData = HAS_RECORD_DATA_PACKAGE ? require('@ember-data/record-data/-private').RecordData : null;

type RelationshipsSchema = import('../ts-interfaces/record-data-schemas').RelationshipsSchema;
type SchemaDefinitionService = import('../ts-interfaces/schema-definition-service').SchemaDefinitionService;
type RecordDataRecordWrapper = import('../ts-interfaces/record-data-record-wrapper').RecordDataRecordWrapper;
Expand Down Expand Up @@ -136,14 +142,31 @@ class Store extends CoreStore {
record.destroy();
}

createRecordDataFor(modelName: string, id: string | null, clientId: string, storeWrapper: RecordDataStoreWrapper) {
if (HAS_RECORD_DATA_PACKAGE) {
if (IDENTIFIERS) {
let identifier = identifierCacheFor(this).getOrCreateRecordIdentifier({
type: modelName,
id,
lid: clientId,
});
return new RecordData(identifier, storeWrapper);
} else {
return new RecordData(modelName, id, clientId, storeWrapper);
}
} else {
throw new Error(`Expected store.createRecordDataFor to be implemented but it wasn't`);
}
}

/**
Returns the model class for the particular `modelName`.
The class of a model might be useful if you want to get a list of all the
relationship names of the model, see
[`relationshipNames`](/ember-data/release/classes/Model?anchor=relationshipNames)
for example.
@method modelFor
@param {String} modelName
@return {Model}
Expand Down Expand Up @@ -192,10 +215,10 @@ class Store extends CoreStore {
This exists for legacy support for the RESTSerializer,
which due to how it must guess whether a key is a model
must query for whether a match exists.
We should investigate an RFC to make this public or removing
this requirement.
@private
*/
_hasModelFor(modelName) {
Expand Down

0 comments on commit 3e3677f

Please sign in to comment.