From b92fafbc6d2a009c195036c41cb9f5e66af36591 Mon Sep 17 00:00:00 2001 From: Igor Terzic Date: Thu, 23 Jan 2020 17:53:53 -0800 Subject: [PATCH] [BUGFIX] fix importing from @ember-data/store Dividing Record Data into packages (https://github.com/emberjs/data/pull/6513) configured the St ore class inside of the `-ember-data` metapackage. This broke importing the Store directly from `@ember-da ta/store`, which this commit fixes. (cherry-picked from commit 3e3677f6cfb3c8715697b302aed522d07926287f) --- packages/-ember-data/addon/store.ts | 22 +----- .../integration/store/package-import-test.js | 70 +++++++++++++++++++ .../addon/-private/system/ds-model-store.ts | 59 +++++++++++----- 3 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 packages/-ember-data/tests/integration/store/package-import-test.js diff --git a/packages/-ember-data/addon/store.ts b/packages/-ember-data/addon/store.ts index d6c2c79c4d3..fa482881caf 100644 --- a/packages/-ember-data/addon/store.ts +++ b/packages/-ember-data/addon/store.ts @@ -1,21 +1 @@ -import Store from '@ember-data/store'; -import { RecordData } from '@ember-data/record-data/-private'; -import { identifierCacheFor } from '@ember-data/store/-private'; -import { IDENTIFIERS } from '@ember-data/canary-features'; - -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 a79406e3213..208469925df 100644 --- a/packages/store/addon/-private/system/ds-model-store.ts +++ b/packages/store/addon/-private/system/ds-model-store.ts @@ -1,26 +1,36 @@ /** @module @ember-data/store */ -import CoreStore from './core-store'; import { assert } from '@ember/debug'; -import normalizeModelName from './normalize-model-name'; import { assign } from '@ember/polyfills'; import { DEBUG } from '@glimmer/env'; -import { isPresent } from '@ember/utils'; import { deprecate } from '@ember/application/deprecations'; + +import { CUSTOM_MODEL_CLASS, IDENTIFIERS } from '@ember-data/canary-features'; +import { HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra'; +import { isPresent } from '@ember/utils'; import EmberError from '@ember/error'; import { get } from '@ember/object'; -import ShimModelClass, { getShimClass } from './model/shim-model-class'; import { setOwner, getOwner } from '@ember/application'; -import { DSModel } from '../ts-interfaces/ds-model'; -import NotificationManager from './record-notification-manager'; -import { StableRecordIdentifier } from '../ts-interfaces/identifier'; -import { DSModelSchemaDefinitionService, getModelFactory } from './schema-definition-service'; -import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features'; -import RecordDataRecordWrapper from '../ts-interfaces/record-data-record-wrapper'; -import { SchemaDefinitionService } from '../ts-interfaces/schema-definition-service'; -import { RelationshipsSchema } from '../ts-interfaces/record-data-schemas'; + +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; +type StableRecordIdentifier = import('../ts-interfaces/identifier').StableRecordIdentifier; +type NotificationManager = import('./record-notification-manager').default; +type DSModel = import('../ts-interfaces/ds-model').DSModel; +type ShimModelClass = import('./model/shim-model-class').default; type DSModelClass = import('@ember-data/model').default; /** @@ -132,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} @@ -188,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) {