Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow query parameters to be specified when finding records via Ember Data's Store #99

Closed
wants to merge 2 commits into from

Conversation

HeroicEric
Copy link
Member

@mmun mmun added the T-ember-data RFCs that impact the ember-data library label Oct 12, 2015
they use, however, will need to be modified to pass the `query` (or all of
`options`) through to their corresponding adapter methods. An example of one of
the places that would require change is
[here](https://github.com/emberjs/data/blob/8f83fbf27acdd642cb824e3286832acebdac0da1/packages/ember-data/lib/system/store/finders.js#L19).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before adapterOptions were attached to the snapshot we considered passing adapterOptions into findRecord as an extra argument. In the end we decided to attach it to the snapshot in order to make it work with colaced requests.

I think we should follow the same pattern here and copy query into adapterOptions.query. This means

const articles = this.store.findAll('article', {
  query: {
    include: 'comments'
  }
});

would be equivalent to

const articles = this.store.findAll('article', {
  adapterOptions: {
    query: {
      include: 'comments'
    }
  }
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I wasn't aware that those were already being passed via the snapshot. I'll update the RFC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmac I just updated this to nest query under adapterOptions in the examples but I think I just realized that you were suggesting to keep query as the as option to findRecord but inside findRecord copy the query over to adapterOptions.

So the api would still be something like:

this.store.findRecord('post', 1, {
  query: {
    include: 'author'
  }
});

but that query would be copied over into adapterOptions before the snapshot is created and sent to the adapter. Is that what you meant?

@mwpastore
Copy link

It would also be useful to be able to supply adapterOptions when fetching associated records, e.g.:

this.store.peekRecord('foo', 1).get('bars', {
  adapterOptions: {
    include: 'quuxes'
  }
});

And/or when defining relationships:

// app/models/foo.js

import DS from 'ember-data';  

export default DS.Model.extend({
  bars: DS.hasMany('bar', {
    adapterOptions: {
      include: 'quuxes'
    }
  })
});

@bmac
Copy link
Member

bmac commented Nov 4, 2015

Hi @HeroicEric I'm a little weary about the potential for the query parameter to load a record in an incomplete state. My fear is that a record may be loaded into the store with fields missing when visiting one route then retrieved from the cache when the user navigates to a new route.

I would like to start with a more conservative API that only exposes the ability to specify the include parameter when making a find request.

const article = this.store.findRecord('article', 1, { include: 'comments' });
const articles = this.store.findAll('article', { include: 'comments' });
article.save({ include: 'comments' });
article.destroyRecord({ include: 'comments' });

This still has the potential for the same problem of expected relationship data not being loaded when loading a record from the cache, however, Ember Data has better tools (namely async relationships) for dealing with this case then it has for partially loaded fields.

@mwpastore
Copy link

That sounds great to me, @bmac. Thoughts on extending that functionality to relationship fetching? Have you seen the ember-data-has-many-query add-on?

@luxzeitlos
Copy link

I would love a official ember way to query data with a cache.
At the moment I've written my own hack here.

It hacks into the store and asks shouldReloadQuery on the adapter if the query result is cachable.

Something to notice: Think about how to handle with pagination and infinity-scroll.
If you query the first page, then load the second, you want to access both if you access the cache.

HeroicEric added a commit to HeroicEric/data that referenced this pull request Dec 8, 2015
Related RFC: emberjs/rfcs#99

These changes allow an `include` paremeter to be specified when using
`store.findRecord` and `store.findAll`:

```javascript
store.findRecord('post', 123, { include: 'comments' });
store.findAll('post', { include: 'comments' });
```

The value for `include` is copied into the `adapterOptions` that are
passed to the corresponding adapter function as part of the `snapshot`
or `snapshotRecordArray`. This value is then pulled from the snapshot
and used as a query parameter (`include`) when making an AJAX request
via `adapter.ajax`.
HeroicEric added a commit to HeroicEric/data that referenced this pull request Dec 12, 2015
Related RFC: emberjs/rfcs#99

These changes allow an `include` paremeter to be specified when using
`store.findRecord` and `store.findAll`:

```javascript
store.findRecord('post', 123, { include: 'comments' });
store.findAll('post', { include: 'comments' });
```

The value for `include` is copied into the `adapterOptions` that are
passed to the corresponding adapter function as part of the `snapshot`
or `snapshotRecordArray`. This value is then pulled from the snapshot
and used as a query parameter (`include`) when making an AJAX request
via `adapter.ajax`.
HeroicEric added a commit to HeroicEric/data that referenced this pull request Dec 15, 2015
Related RFC: emberjs/rfcs#99

These changes allow an `include` paremeter to be specified when using
`store.findRecord` and `store.findAll`:

```javascript
store.findRecord('post', 123, { include: 'comments' });
store.findAll('post', { include: 'comments' });
```

The value for `include` is copied into the `adapterOptions` that are
passed to the corresponding adapter function as part of the `snapshot`
or `snapshotRecordArray`. This value is then pulled from the snapshot
and used as a query parameter (`include`) when making an AJAX request
via `adapter.ajax`.
HeroicEric added a commit to HeroicEric/data that referenced this pull request Dec 16, 2015
Related RFC: emberjs/rfcs#99

These changes allow an `include` paremeter to be specified when using
`store.findRecord` and `store.findAll`:

```javascript
store.findRecord('post', 123, { include: 'comments' });
store.findAll('post', { include: 'comments' });
```

The value for `include` is copied onto the `DS.Snapshot` or
`DS.SnapshotRecordArray` that is passed to the corresponding adapter
function. This value is then pulled from the snapshot and used as a
query parameter (`include`) when making an AJAX request via
`adapter.ajax`.
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 25, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// Create
let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });

// POST /posts
//
// data: {
//   post: { title: 'Hurray' },
//   include: 'author'
// }
```

```javascript
// Update
let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });

// PUT /posts/123
//
// data: {
//   post: { title: 'Hurray' },
//   include: 'comments'
// }
```

```javascript
// Update
let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });

// DEL /posts/123
//
// data: {
//   post: { title: 'Hurray' },
//   include: 'comments'
// }
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 30, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 30, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 30, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 30, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request Apr 30, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
HeroicEric added a commit to HeroicEric/data that referenced this pull request May 1, 2016
Fixes emberjs#4344

This allows an `include` parameter for be specified when creating,
updating, or deleting a model.

This feature was originally discussed in
emberjs/rfcs#99 but was not part of the initial
implementation.

Examples:

```javascript
// POST /posts?include=author

let post = this.store.createRecord('post', { title: 'Hurray' });

post.save({ include: 'author' });
```

```javascript
// PUT /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

set(post, 'title', 'Hurray');

post.save({ include: 'comments' });
```

```javascript
// DEL /posts/123?include=comments

let post = this.store.peekRecord('post', 123);

post.deleteRecord();

post.save({ include: 'comments' });
```
@HeroicEric
Copy link
Member Author

This was added in emberjs/data#3976

@HeroicEric HeroicEric closed this Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-ember-data RFCs that impact the ember-data library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants