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

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
5 participants

@mmun mmun added the T-ember-data label Oct 12, 2015

text/0000-my-feature.md
+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).

This comment has been minimized.

@bmac

bmac Oct 12, 2015

Member

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'
    }
  }
});
@bmac

bmac Oct 12, 2015

Member

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'
    }
  }
});

This comment has been minimized.

@HeroicEric

HeroicEric Oct 12, 2015

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

@HeroicEric

HeroicEric Oct 12, 2015

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

This comment has been minimized.

@HeroicEric

HeroicEric Oct 14, 2015

@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?

@HeroicEric

HeroicEric Oct 14, 2015

@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?

+if any query parameters are passed but I think the better default is to rely on
+loading the data in the background to make sure that we eventually end up with
+all of the data that we need. If a user wants to make sure that they always have
+fresh data, they can do so by setting the `reload` option to `true`.

This comment has been minimized.

@bmac

bmac Oct 12, 2015

Member

cc @wycats. I believe you had some thoughts on how this case should be handled.

@bmac

bmac Oct 12, 2015

Member

cc @wycats. I believe you had some thoughts on how this case should be handled.

@bmac bmac referenced this pull request in emberjs/data Oct 13, 2015

Closed

Improvement ides for Ember Data #1776

@mwpastore

This comment has been minimized.

Show comment
Hide comment
@mwpastore

mwpastore Oct 19, 2015

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'
    }
  })
});

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

This comment has been minimized.

Show comment
Hide comment
@bmac

bmac Nov 4, 2015

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@mwpastore

mwpastore Nov 4, 2015

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?

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?

@luxferresum

This comment has been minimized.

Show comment
Hide comment
@luxferresum

luxferresum Nov 19, 2015

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.

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

Allow `include` query parameter with store.findRecord & store.findAll
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

Allow `include` query parameter with store.findRecord & store.findAll
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

Allow `include` query parameter with store.findRecord & store.findAll
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

Allow `include` query parameter with store.findRecord & store.findAll
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

Add `include` param support for create/update/delete operations
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

This comment has been minimized.

Show comment
Hide comment

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