Skip to content

Commit

Permalink
[BUGFIX #4497] query/queryRecord/filter now support adapter options
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Mar 13, 2017
1 parent 2ebeb89 commit c2783fb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
18 changes: 14 additions & 4 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1255,11 +1255,16 @@ Store = Service.extend({
assert(`You need to pass a query hash to the store's query method`, query);
assert(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`, typeof modelName === 'string');

let options = {};

options.adapterOptions = query.adapterOptions;
delete query.adapterOptions;

let normalizedModelName = normalizeModelName(modelName);
return this._query(normalizedModelName, query);
return this._query(normalizedModelName, query, null, options);
},

_query(modelName, query, array) {
_query(modelName, query, array, options) {
let token = heimdall.start('store._query');
assert(`You need to pass a model name to the store's query method`, isPresent(modelName));
assert(`You need to pass a query hash to the store's query method`, query);
Expand All @@ -1277,7 +1282,7 @@ Store = Service.extend({
assert(`You tried to load a query but you have no adapter (for ${modelName})`, adapter);
assert(`You tried to load a query but your adapter does not implement 'query'`, typeof adapter.query === 'function');

let pA = promiseArray(_query(adapter, this, modelName, query, array));
let pA = promiseArray(_query(adapter, this, modelName, query, array, options));
instrument(() => {
pA.finally(() => { heimdall.stop(token); });
});
Expand Down Expand Up @@ -1386,10 +1391,15 @@ Store = Service.extend({

let adapter = this.adapterFor(normalizedModelName);

let options = {};

options.adapterOptions = query.adapterOptions;
delete query.adapterOptions;

assert(`You tried to make a query but you have no adapter (for ${normalizedModelName})`, adapter);
assert(`You tried to make a query but your adapter does not implement 'queryRecord'`, typeof adapter.queryRecord === 'function');

return promiseObject(_queryRecord(adapter, this, modelName, query).then(internalModel => {
return promiseObject(_queryRecord(adapter, this, modelName, query, options).then(internalModel => {
// the promise returned by store.queryRecord is expected to resolve with
// an instance of DS.Model
if (internalModel) {
Expand Down
8 changes: 4 additions & 4 deletions addon/-private/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ export function _findAll(adapter, store, modelName, sinceToken, options) {
}, null, 'DS: Extract payload of findAll ${modelName}');
}

export function _query(adapter, store, modelName, query, recordArray) {
export function _query(adapter, store, modelName, query, recordArray, options) {
let modelClass = store.modelFor(modelName); // adapter.query needs the class
let promise = adapter.query(store, modelClass, query, recordArray);
let promise = adapter.query(store, modelClass, query, recordArray, options);

let serializerToken = heimdall.start('initial-serializerFor-lookup');
let serializer = serializerForAdapter(store, adapter, modelName);
Expand All @@ -165,9 +165,9 @@ export function _query(adapter, store, modelName, query, recordArray) {
}, null, `DS: Extract payload of query ${modelName}`);
}

export function _queryRecord(adapter, store, modelName, query) {
export function _queryRecord(adapter, store, modelName, query, options) {
let modelClass = store.modelFor(modelName); // adapter.queryRecord needs the class
let promise = adapter.queryRecord(store, modelClass, query);
let promise = adapter.queryRecord(store, modelClass, query, options);
let serializer = serializerForAdapter(store, adapter, modelName);
let label = `DS: Handle Adapter#queryRecord of ${modelName}`;

Expand Down
42 changes: 42 additions & 0 deletions tests/integration/adapter/store-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,48 @@ test("store.findRecord should pass adapterOptions to adapter.findRecord", functi
});
});

test("store.query should pass adapterOptions to adapter.query ", function(assert) {
assert.expect(2);

env.adapter.query = function(store, type, query, array, options) {
assert.ok(!('adapterOptions' in query));
assert.deepEqual(options.adapterOptions, { query: { embed: true } });
return [];
};

return run(() => {
return store.query('person', { adapterOptions: { query: { embed: true } } });
});
});

test("store.filter should pass adapterOptions to adapter.query", function(assert) {
assert.expect(2);

env.adapter.query = function(store, type, query, array, options) {
assert.ok(!('adapterOptions' in query));
assert.deepEqual(options.adapterOptions, { query: { embed: true } });
return [];
};

return run(() => {
return store.filter('person', { adapterOptions: { query: { embed: true } } }, () => {});
});
});

test("store.queryRecord should pass adapterOptions to adapter.queryRecord", function(assert) {
assert.expect(2);

env.adapter.queryRecord = function(store, type, query, snapshot) {
assert.ok(!('adapterOptions' in query));
assert.deepEqual(snapshot.adapterOptions, { query: { embed: true } });
return { id: 1 };
};

return run(() => {
return store.queryRecord('person', { adapterOptions: { query: { embed: true } } });
});
});

test("store.findRecord should pass 'include' to adapter.findRecord", function(assert) {
assert.expect(1);

Expand Down

0 comments on commit c2783fb

Please sign in to comment.