ember-addon-ember-data-model-query

0.1.2 • Public • Published

Ember-addon-ember-data-model-query

This addon provides a way to add query parameters when handling has-many relationships and depaginate them.

Compatibility

  • Ember.js v2.18 or above
  • Ember CLI v2.13 or above

Installation

  • ember install ember-addon-ember-data-model-query

or add them to the package.json and run npm install


Usage

The addon gives the user two mixins adapter-actions and model-actions. Both mixins need to be present in the model and adapter of the type to work properly.

import AdapterMixin from 'ember-addon-ember-data-model-query/mixins/adapter-actions';
 
export default DS.RESTAdapter.extend(AdapterMixin, {
});
import ModelMixin from 'ember-addon-ember-data-model-query/mixins/model-actions'
 
export default DS.Model.extend(ModelMixin, {
});

The adapter is presented of two functions: findHasMany and getDepaginated. The override of the findHasMany give the user the ability to set queryParams to the relationship get request and/or get it depaginated. Currently it works with sirenJSON and the meta property returned from the API.

The getDepaginated method on the other hand exposes the depaginated logic used in the findHasMany. Example usage:

Lets say in the model you have this property customFoo

import ModelMixin from 'ember-addon-ember-data-model-query/mixins/model-actions'
 
export default DS.Model.extend(ModelMixin, {
  foo: hasMany('foo'),
  customFoo: computed('foo', function() {
    //Here we want to filter the foo relationship and only return handful. 
    //The problem we are solving is that the API may have a custom QueryParam set to filter the foo and we would like to use it. But in order to do so, we need to build a proper URL based on the relationship of `foo`
    const adapter = this.store.adapterFor(this.constructor.modelName);
    const promise = adapter.getCustomFoo({ $filter: 'custom eq true' }, { depaginated: true }, this);
 
    return PromiseManyArray.create({
      promise: resolve(promise, 'customFooFetch')
    });
  })
});

Now in the adapter:

import AdapterMixin from 'ember-addon-ember-data-model-query/mixins/adapter-actions';
 
export default DS.RESTAdapter.extend(AdapterMixin, {
    getCustomFoo(queryParams = {}, options = {}, model) {
        const url = `${prefix}/parentModel/${model.get('id')}/foo`;
 
        return this.getDepaginated(url, parameters).then(rawResponse => {
            //Now here we have the raw response from the API and we need to normalize it and push it to the store.
        });
    }
});

In order to keep the functionality of ember-data findHasMany to use the addon you need to customize your relationship properties in the model. Currently in the options you need to pass { depaginated: true }.

import ModelMixin from 'ember-addon-ember-data-model-query/mixins/model-actions'
 
export default DS.Model.extend(ModelMixin, {
  foo: hasMany('foo', { depaginated: true })
});

Future ideas

  • Add option to cache the result for hasMany relationship

Readme

Keywords

Package Sidebar

Install

npm i ember-addon-ember-data-model-query

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

10.3 kB

Total Files

9

Last publish

Collaborators

  • fourth-sj