diff --git a/packages/-ember-data/addon/store.ts b/packages/-ember-data/addon/store.ts index 6cbddc5f7e8..fa482881caf 100644 --- a/packages/-ember-data/addon/store.ts +++ b/packages/-ember-data/addon/store.ts @@ -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'; diff --git a/packages/-ember-data/tests/integration/store/package-import-test.js b/packages/-ember-data/tests/integration/store/package-import-test.js new file mode 100644 index 00000000000..5d664f9c8df --- /dev/null +++ b/packages/-ember-data/tests/integration/store/package-import-test.js @@ -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); + }); +}); diff --git a/packages/store/addon/-private/system/ds-model-store.ts b/packages/store/addon/-private/system/ds-model-store.ts index 37a326b5bd3..737c07350a8 100644 --- a/packages/store/addon/-private/system/ds-model-store.ts +++ b/packages/store/addon/-private/system/ds-model-store.ts @@ -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; @@ -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} @@ -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) {