Skip to content

Commit

Permalink
Merge pull request #3235 from csantero/store-find-query
Browse files Browse the repository at this point in the history
rename store.findQuery to store.query
  • Loading branch information
fivetanley committed Jun 7, 2015
2 parents dfd692a + a631f01 commit 3a4f84a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion packages/ember-data/lib/adapters/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ var RestAdapter = Adapter.extend(BuildURLMixin, {
For example:
```js
store.find('posts', {sort: 'price', category: 'pets'});
store.query('posts', {sort: 'price', category: 'pets'});
```
will generate a requests like this `/posts?category=pets&sort=price`, even if the
Expand Down
4 changes: 1 addition & 3 deletions packages/ember-data/lib/system/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ var Adapter = Ember.Object.extend({
findAll: null,

/**
This method is called when you call `find` on the store with a
query object as the second parameter (i.e. `store.find('person', {
page: 1 })`).
This method is called when you call `query` on the store.
Example
Expand Down
99 changes: 51 additions & 48 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
_findHasMany,
_findBelongsTo,
_findAll,
_findQuery
_query
} from "ember-data/system/store/finders";

import coerceId from "ember-data/system/coerce-id";
Expand Down Expand Up @@ -473,47 +473,6 @@ Store = Service.extend({
returns the values. The promise will resolve into all records of this type
present in the store, even if the server only returns a subset of them.
---
To find a record by a query, call `find` with a hash as the second
parameter:
```javascript
store.find('person', { page: 1 });
```
By passing an object `{page: 1}` as an argument to the find method, it
delegates to the adapter's findQuery method. The adapter then makes
a call to the server, transforming the object `{page: 1}` as parameters
that are sent along, and will return a RecordArray when the promise
resolves.
Exposing queries this way seems preferable to creating an abstract query
language for all server-side queries, and then require all adapters to
implement them.
The call made to the server, using a Rails backend, will look something like this:
```
Started GET "/api/v1/person?page=1"
Processing by Api::V1::PersonsController#index as HTML
Parameters: {"page"=>"1"}
```
If you do something like this:
```javascript
store.find('person', {ids: [1, 2, 3]});
```
The call to the server, using a Rails backend, will look something like this:
```
Started GET "/api/v1/person?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"
Processing by Api::V1::PersonsController#index as HTML
Parameters: {"ids"=>["1", "2", "3"]}
```
@method find
@param {String} modelName
@param {(Object|String|Integer|null)} id
Expand All @@ -531,7 +490,8 @@ Store = Service.extend({

// We are passed a query instead of an id.
if (Ember.typeOf(id) === 'object') {
return this.findQuery(modelName, id);
Ember.deprecate('Calling store.find() with a query object is deprecated. Use store.query() instead.');
return this.query(modelName, id);
}

return this.findByRecord(modelName, coerceId(id), preload);
Expand Down Expand Up @@ -983,16 +943,37 @@ Store = Service.extend({
language for all server-side queries, and then require all adapters to
implement them.
The call made to the server, using a Rails backend, will look something like this:
```
Started GET "/api/v1/person?page=1"
Processing by Api::V1::PersonsController#index as HTML
Parameters: {"page"=>"1"}
```
If you do something like this:
```javascript
store.query('person', {ids: [1, 2, 3]});
```
The call to the server, using a Rails backend, will look something like this:
```
Started GET "/api/v1/person?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"
Processing by Api::V1::PersonsController#index as HTML
Parameters: {"ids"=>["1", "2", "3"]}
```
This method returns a promise, which is resolved with a `RecordArray`
once the server returns.
@method findQuery
@private
@method query
@param {String} modelName
@param {any} query an opaque query to be used by the adapter
@return {Promise} promise
*/
findQuery: function(modelName, query) {
query: function(modelName, query) {
Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var typeClass = this.modelFor(modelName);
var array = this.recordArrayManager
Expand All @@ -1003,7 +984,29 @@ Store = Service.extend({
Ember.assert("You tried to load a query but you have no adapter (for " + typeClass + ")", adapter);
Ember.assert("You tried to load a query but your adapter does not implement `findQuery`", typeof adapter.findQuery === 'function');

return promiseArray(_findQuery(adapter, this, typeClass, query, array));
return promiseArray(_query(adapter, this, typeClass, query, array));
},

/**
This method delegates a query to the adapter. This is the one place where
adapter-level semantics are exposed to the application.
Exposing queries this way seems preferable to creating an abstract query
language for all server-side queries, and then require all adapters to
implement them.
This method returns a promise, which is resolved with a `RecordArray`
once the server returns.
@method query
@param {String} modelName
@param {any} query an opaque query to be used by the adapter
@return {Promise} promise
@deprecated Use `store.query instead`
*/
findQuery: function(modelName, query) {
Ember.deprecate('store#findQuery is deprecated. You should use store#query instead.');
return this.query(modelName, query);
},

/**
Expand Down Expand Up @@ -1186,7 +1189,7 @@ Store = Service.extend({

// allow an optional server query
if (hasQuery) {
promise = this.findQuery(modelName, query);
promise = this.query(modelName, query);
} else if (arguments.length === 2) {
filter = query;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function _findAll(adapter, store, typeClass, sinceToken) {
}, null, "DS: Extract payload of findAll " + typeClass);
}

export function _findQuery(adapter, store, typeClass, query, recordArray) {
export function _query(adapter, store, typeClass, query, recordArray) {
var modelName = typeClass.modelName;
var promise = adapter.findQuery(store, typeClass, query, recordArray);
var serializer = serializerForAdapter(store, adapter, modelName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test("When a query is made, the adapter should receive a record array it can pop
return Ember.RSVP.resolve([{ id: 1, name: "Peter Wagenet" }, { id: 2, name: "Brohuda Katz" }]);
};

store.find('person', { page: 1 }).then(async(function(queryResults) {
store.query('person', { page: 1 }).then(async(function(queryResults) {
equal(get(queryResults, 'length'), 2, "the record array has a length of 2 after the results are loaded");
equal(get(queryResults, 'isLoaded'), true, "the record array's `isLoaded` property should be true");

Expand Down
20 changes: 10 additions & 10 deletions packages/ember-data/tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ test("findQuery - if `sortQueryParams` option is not provided, query params are
return run(Ember.RSVP, 'resolve', { posts: [{ id: 1, name: "Rails is very expensive sushi" }] });
};

store.findQuery('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
// Noop
}));
});
Expand All @@ -977,7 +977,7 @@ test("findQuery - passes buildURL the requestType", function() {
return run(Ember.RSVP, 'resolve', { posts: [{ id: 1, name: "Rails is very expensive sushi" }] });
};

store.findQuery('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
// NOOP
}));
});
Expand All @@ -995,7 +995,7 @@ test("findQuery - if `sortQueryParams` is falsey, query params are not sorted at

adapter.sortQueryParams = null;

store.findQuery('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
// Noop
}));
});
Expand All @@ -1022,7 +1022,7 @@ test("findQuery - if `sortQueryParams` is a custom function, query params passed
return newQueryParams;
};

store.findQuery('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(async(function() {
// Noop
}));
});
Expand All @@ -1033,7 +1033,7 @@ test("findQuery - payload 'meta' is accessible on the record array", function()
posts: [{ id: 1, name: "Rails is very expensive sushi" }]
});

store.findQuery('post', { page: 2 }).then(async(function(posts) {
store.query('post', { page: 2 }).then(async(function(posts) {
equal(
posts.get('meta.offset'),
5,
Expand All @@ -1048,7 +1048,7 @@ test("findQuery - each record array can have it's own meta object", function() {
posts: [{ id: 1, name: "Rails is very expensive sushi" }]
});

store.findQuery('post', { page: 2 }).then(async(function(posts) {
store.query('post', { page: 2 }).then(async(function(posts) {
equal(
posts.get('meta.offset'),
5,
Expand All @@ -1058,7 +1058,7 @@ test("findQuery - each record array can have it's own meta object", function() {
meta: { offset: 1 },
posts: [{ id: 1, name: "Rails is very expensive sushi" }]
});
store.findQuery('post', { page: 1 }).then(async(function(newPosts) {
store.query('post', { page: 1 }).then(async(function(newPosts) {
equal(newPosts.get('meta.offset'), 1, 'new array has correct metadata');
equal(posts.get('meta.offset'), 5, 'metadata on the old array hasnt been clobbered');
}));
Expand All @@ -1073,7 +1073,7 @@ test("findQuery - returning an array populates the array", function() {
{ id: 2, name: "The Parley Letter" }]
});

store.findQuery('post', { page: 1 }).then(async(function(posts) {
store.query('post', { page: 1 }).then(async(function(posts) {
equal(passedUrl, '/posts');
equal(passedVerb, 'GET');
deepEqual(passedHash.data, { page: 1 });
Expand Down Expand Up @@ -1111,7 +1111,7 @@ test("findQuery - returning sideloaded data loads the data", function() {
comments: [{ id: 1, name: "FIRST" }]
});

store.findQuery('post', { page: 1 }).then(async(function(posts) {
store.query('post', { page: 1 }).then(async(function(posts) {
var comment = store.getById('comment', 1);

deepEqual(comment.getProperties('id', 'name'), { id: "1", name: "FIRST" });
Expand All @@ -1129,7 +1129,7 @@ test("findQuery - data is normalized through custom serializers", function() {
{ _ID_: 2, _NAME_: "The Parley Letter" }]
});

store.findQuery('post', { page: 1 }).then(async(function(posts) {
store.query('post', { page: 1 }).then(async(function(posts) {
var post1 = store.getById('post', 1);
var post2 = store.getById('post', 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ test("Records loaded multiple times and retrieved in recordArray are ready to se
}]);
};

run(store, 'findQuery', 'person', { q: 'bla' }).then(async(function(people) {
var people2 = store.findQuery('person', { q: 'bla2' });
run(store, 'query', 'person', { q: 'bla' }).then(async(function(people) {
var people2 = store.query('person', { q: 'bla2' });

return Ember.RSVP.hash({ people: people, people2: people2 });
})).then(async(function(results) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/tests/integration/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ test("destroying the store correctly cleans everything up", function() {
var adapterPopulatedPeople, filterdPeople;

run(function() {
adapterPopulatedPeople = store.find('person', {
adapterPopulatedPeople = store.query('person', {
someCrazy: 'query'
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/tests/unit/record-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ test("an AdapterPopulatedRecordArray knows if it's loaded or not", function() {
};

run(function() {
store.find('person', { page: 1 }).then(function(people) {
store.query('person', { page: 1 }).then(function(people) {
equal(get(people, 'isLoaded'), true, "The array is now loaded");
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/tests/unit/store/adapter-interop-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ test("loadMany takes an optional Object and passes it on to the Adapter", functi
});

run(function() {
store.find('person', passedQuery);
store.query('person', passedQuery);
});
});

Expand Down Expand Up @@ -250,7 +250,7 @@ test("Find with query calls the correct extract", function() {
env.registry.register('serializer:application', ApplicationSerializer);

run(function() {
store.find('person', passedQuery);
store.query('person', passedQuery);
});
equal(callCount, 1, 'extractFindQuery was called');
});
Expand Down

0 comments on commit 3a4f84a

Please sign in to comment.