Skip to content

Commit

Permalink
misc cleanup
Browse files Browse the repository at this point in the history
* avoid needly internalModel -> type conversion, when the information is already present on the internalModel
* make asserts more consistent (ES6 interpolation etc)
* rename trueModelName to normalizedModelName
  • Loading branch information
stefanpenner committed Feb 22, 2017
1 parent e2f35a7 commit 77e4081
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 176 deletions.
271 changes: 137 additions & 134 deletions addon/-private/system/store.js

Large diffs are not rendered by default.

65 changes: 33 additions & 32 deletions addon/-private/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
serializerForAdapter
} from "ember-data/-private/system/store/serializers";

const Promise = Ember.RSVP.Promise;
const { Promise } = Ember.RSVP;

function payloadIsNotBlank(adapterPayload) {
if (Array.isArray(adapterPayload)) {
Expand All @@ -26,39 +26,40 @@ function payloadIsNotBlank(adapterPayload) {

export function _find(adapter, store, modelClass, id, internalModel, options) {
let snapshot = internalModel.createSnapshot(options);
let { modelName } = internalModel;
let promise = adapter.findRecord(store, modelClass, id, snapshot);
let serializer = serializerForAdapter(store, adapter, internalModel.type.modelName);
let label = "DS: Handle Adapter#findRecord of " + modelClass + " with id: " + id;
let serializer = serializerForAdapter(store, adapter, modelName);
let label = `DS: Handle Adapter#findRecord of '${modelName}' with id: '${id}'`;

promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));

return promise.then(function(adapterPayload) {
assert("You made a `findRecord` request for a " + modelClass.modelName + " with id " + id + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload));
return promise.then(adapterPayload => {
assert(`You made a 'findRecord' request for a '${modelName}' with id '${id}', but the adapter's response did not have any data`, payloadIsNotBlank(adapterPayload));
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, id, 'findRecord');
assert('Ember Data expected the primary data returned from a `findRecord` response to be an object but instead it found an array.', !Array.isArray(payload.data));
assert(`Ember Data expected the primary data returned from a 'findRecord' response to be an object but instead it found an array.`, !Array.isArray(payload.data));

warn(`You requested a record of type '${modelClass.modelName}' with id '${id}' but the adapter returned a payload with primary data having an id of '${payload.data.id}'. Use 'store.findRecord()' when the requested id is the same as the one returned by the adapter. In other cases use 'store.queryRecord()' instead http://emberjs.com/api/data/classes/DS.Store.html#method_queryRecord`, payload.data.id === id, {
warn(`You requested a record of type '${modelName}' with id '${id}' but the adapter returned a payload with primary data having an id of '${payload.data.id}'. Use 'store.findRecord()' when the requested id is the same as the one returned by the adapter. In other cases use 'store.queryRecord()' instead http://emberjs.com/api/data/classes/DS.Store.html#method_queryRecord`, payload.data.id === id, {
id: 'ds.store.findRecord.id-mismatch'
});

return store._push(payload);
}, function(error) {
}, error => {
internalModel.notFound();
if (internalModel.isEmpty()) {
internalModel.unloadRecord();
}

throw error;
}, "DS: Extract payload of '" + modelClass + "'");
}, `DS: Extract payload of '${modelName}'`);
}


export function _findMany(adapter, store, modelClass, ids, internalModels) {
let snapshots = Ember.A(internalModels).invoke('createSnapshot');
let { modelName } = modelClass; // TODO: pass in modelName, or something
let promise = adapter.findMany(store, modelClass, ids, snapshots);
let serializer = serializerForAdapter(store, adapter, modelClass.modelName);
let label = "DS: Handle Adapter#findMany of " + modelClass;
let serializer = serializerForAdapter(store, adapter, modelName);
let label = `DS: Handle Adapter#findMany of '${modelName}'`;

if (promise === undefined) {
throw new Error('adapter.findMany returned undefined, this was very likely a mistake');
Expand All @@ -67,58 +68,58 @@ export function _findMany(adapter, store, modelClass, ids, internalModels) {
promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));

return promise.then(function(adapterPayload) {
assert("You made a `findMany` request for " + modelClass.modelName + " records with ids " + ids + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload));
return promise.then(adapterPayload => {
assert(`You made a 'findMany' request for '${modelName}' records with ids '[${ids}]', but the adapter's response did not have any data`, payloadIsNotBlank(adapterPayload));
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findMany');
return store._push(payload);
}, null, "DS: Extract payload of " + modelClass);
}, null, `DS: Extract payload of ${modelName}`);
}

export function _findHasMany(adapter, store, internalModel, link, relationship) {
let snapshot = internalModel.createSnapshot();
let modelClass = store.modelFor(relationship.type);
let promise = adapter.findHasMany(store, snapshot, link, relationship);
let serializer = serializerForAdapter(store, adapter, relationship.type);
let label = "DS: Handle Adapter#findHasMany of " + internalModel + " : " + relationship.type;
let label = `DS: Handle Adapter#findHasMany of '${internalModel.modelName}' : '${relationship.type}'`;

promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));
promise = _guard(promise, _bind(_objectIsAlive, internalModel));

return promise.then(function(adapterPayload) {
assert("You made a `findHasMany` request for a " + internalModel.modelName + "'s `" + relationship.key + "` relationship, using link " + link + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload));
return promise.then(adapterPayload => {
assert(`You made a 'findHasMany' request for a ${internalModel.modelName}'s '${relationship.key}' relationship, using link '${link}' , but the adapter's response did not have any data`, payloadIsNotBlank(adapterPayload));
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findHasMany');
let internalModelArray = store._push(payload);

internalModelArray.meta = payload.meta;
return internalModelArray;
}, null, "DS: Extract payload of " + internalModel + " : hasMany " + relationship.type);
}, null, `DS: Extract payload of '${internalModel.modelName}' : hasMany '${relationship.type}'`);
}

export function _findBelongsTo(adapter, store, internalModel, link, relationship) {
let snapshot = internalModel.createSnapshot();
let modelClass = store.modelFor(relationship.type);
let promise = adapter.findBelongsTo(store, snapshot, link, relationship);
let serializer = serializerForAdapter(store, adapter, relationship.type);
let label = "DS: Handle Adapter#findBelongsTo of " + internalModel + " : " + relationship.type;
let label = `DS: Handle Adapter#findBelongsTo of ${internalModel.modelName} : ${relationship.type}`;

promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));
promise = _guard(promise, _bind(_objectIsAlive, internalModel));

return promise.then(function(adapterPayload) {
return promise.then(adapterPayload => {
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findBelongsTo');

if (!payload.data) {
return null;
}

return store._push(payload);
}, null, "DS: Extract payload of " + internalModel + " : " + relationship.type);
}, null, `DS: Extract payload of ${internalModel.modelName} : ${relationship.type}`);
}

export function _findAll(adapter, store, modelClass, sinceToken, options) {
let modelName = modelClass.modelName;
let modelName = modelClass.modelName; // TODO: pass in modelName
let recordArray = store.peekAll(modelName);
let snapshotArray = recordArray._createSnapshot(options);
let promise = adapter.findAll(store, modelClass, sinceToken, snapshotArray);
Expand All @@ -128,25 +129,25 @@ export function _findAll(adapter, store, modelClass, sinceToken, options) {
promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));

return promise.then(function(adapterPayload) {
assert("You made a `findAll` request for " + modelClass.modelName + " records, but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload));
return promise.then(adapterPayload => {
assert(`You made a 'findAll' request for '${modelName}' records, but the adapter's response did not have any data`, payloadIsNotBlank(adapterPayload));
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findAll');

store._push(payload);
store.didUpdateAll(modelName);

return store.peekAll(modelName);
}, null, "DS: Extract payload of findAll " + modelClass);
}, null, 'DS: Extract payload of findAll ${modelName}');
}

export function _query(adapter, store, modelClass, query, recordArray) {
let modelName = modelClass.modelName;
let modelName = modelClass.modelName; // TODO: name yo
let promise = adapter.query(store, modelClass, query, recordArray);

let serializerToken = heimdall.start('initial-serializerFor-lookup');
let serializer = serializerForAdapter(store, adapter, modelName);
heimdall.stop(serializerToken);
let label = 'DS: Handle Adapter#query of ' + modelClass;
let label = `DS: Handle Adapter#query of ${modelClass}`;

promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));
Expand All @@ -161,7 +162,7 @@ export function _query(adapter, store, modelClass, query, recordArray) {
recordArray._setInternalModels(internalModels, payload);

return recordArray;
}, null, 'DS: Extract payload of query ' + modelName);
}, null, `DS: Extract payload of query ${modelName}`);
}

export function _queryRecord(adapter, store, modelClass, query) {
Expand All @@ -173,13 +174,13 @@ export function _queryRecord(adapter, store, modelClass, query) {
promise = Promise.resolve(promise, label);
promise = _guard(promise, _bind(_objectIsAlive, store));

return promise.then(function(adapterPayload) {
return promise.then(adapterPayload => {
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'queryRecord');

assert("Expected the primary data returned by the serializer for a `queryRecord` response to be a single object or null but instead it was an array.", !Array.isArray(payload.data), {
assert(`Expected the primary data returned by the serializer for a 'queryRecord' response to be a single object or null but instead it was an array.`, !Array.isArray(payload.data), {
id: 'ds.store.queryRecord-array-response'
});

return store._push(payload);
}, null, "DS: Extract payload of queryRecord " + modelName);
}, null, `DS: Extract payload of queryRecord ${modelName}`);
}
4 changes: 2 additions & 2 deletions addon/-private/system/store/serializers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export function serializerForAdapter(store, adapter, type) {
export function serializerForAdapter(store, adapter, modelName) {
let serializer = adapter.serializer;

if (serializer === undefined) {
serializer = store.serializerFor(type);
serializer = store.serializerFor(modelName);
}

if (serializer === null || serializer === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/adapter/find-all-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ testInDebug('When all records are requested, assert the payload is not blank', (

assert.expectAssertion(() => {
run(() => store.findAll('person'));
}, /You made a `findAll` request for person records, but the adapter's response did not have any data/);
}, /You made a 'findAll' request for 'person' records, but the adapter's response did not have any data/);
});

test("isUpdating is true while records are fetched", function(assert) {
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/adapter/find-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ module("integration/adapter/find - Finding Records", {
testInDebug("It raises an assertion when `undefined` is passed as id (#1705)", (assert) => {
assert.expectAssertion(() => {
store.find('person', undefined);
}, "You cannot pass `undefined` as id to the store's find method");
}, `You cannot pass 'undefined' as id to the store's find method`);

assert.expectAssertion(() => {
store.find('person', null);
}, "You cannot pass `null` as id to the store's find method");
}, `You cannot pass 'null' as id to the store's find method`);
});

test("When a single record is requested, the adapter's find method should be called unless it's loaded.", (assert) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ testInDebug('When a single record is requested, and the payload is blank', (asse

assert.expectAssertion(() => {
run(() => store.findRecord('person', 'the-id'));
}, /You made a `findRecord` request for a person with id the-id, but the adapter's response did not have any data/);
}, /You made a 'findRecord' request for a 'person' with id 'the-id', but the adapter's response did not have any data/);
});

testInDebug('When multiple records are requested, and the payload is blank', (assert) => {
Expand All @@ -152,7 +152,7 @@ testInDebug('When multiple records are requested, and the payload is blank', (as
store.findRecord('person', '1');
store.findRecord('person', '2');
});
}, /You made a `findMany` request for person records with ids 1,2, but the adapter's response did not have any data/);
}, /You made a 'findMany' request for 'person' records with ids '\[1,2\]', but the adapter's response did not have any data/);
});

testInDebug("warns when returned record has different id", function(assert) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ test("findHasMany - passes buildURL the requestType", function(assert) {



test("findMany - returning sideloaded data loads the data", function(assert) {
test("findMany - returning sideloaded data loads the data (with JSONApi Links)", function(assert) {
adapter.shouldBackgroundReloadRecord = () => false;
Post.reopen({ comments: DS.hasMany('comment', { async: true }) });
adapter.coalesceFindRequests = true;
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ testInDebug('store#findRecord that returns an array should assert', assert => {
run(function() {
store.findRecord('car', 1);
});
}, /expected the primary data returned from a `findRecord` response to be an object but instead it found an array/);
}, /expected the primary data returned from a 'findRecord' response to be an object but instead it found an array/);
});

testInDebug('store#didSaveRecord should assert when the response to a save does not include the id', function(assert) {
Expand Down Expand Up @@ -910,7 +910,7 @@ testInDebug('store#queryRecord should assert when normalized payload of adapter
run(function() {
store.queryRecord('car', {});
});
}, /Expected the primary data returned by the serializer for a `queryRecord` response to be a single object or null but instead it was an array./);
}, /Expected the primary data returned by the serializer for a 'queryRecord' response to be a single object or null but instead it was an array./);
});


0 comments on commit 77e4081

Please sign in to comment.