Skip to content

Commit

Permalink
feat: allow to set some default fetching options to a query.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Jul 22, 2020
1 parent 188ea40 commit ac753ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 12 additions & 0 deletions spec/suite/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ describe("Query", function() {

});

describe(".fetchOptions()", function() {

it("gets/sets the fetching options", function() {

expect(this.query.fetchOptions().return).toBe('entity');
this.query.fetchOptions({ return: 'array' });
expect(this.query.fetchOptions().return).toBe('array');

});

});

describe(".get()", function() {

it("finds all records", function(done) {
Expand Down
27 changes: 23 additions & 4 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class Query {
*/
this._page = [];


/**
* Default fetch options.
*
* @var Object
*/
this._fetchOptions = {};

var schema = this.schema();

/**
Expand Down Expand Up @@ -138,6 +146,20 @@ class Query {
return this._statement;
}

/**
* Get/set default fetch options.
*
* @param Object|null $fetchOptions The fetching options.
* @return Object.
*/
fetchOptions(fetchOptions) {
if (arguments.length) {
this._fetchOptions = fetchOptions;
return this;
}
return Object.assign({ return: 'entity' }, this._fetchOptions);
}

/**
* Executes the query and returns the result.
*
Expand All @@ -146,10 +168,7 @@ class Query {
*/
get(options) {
return co(function*(){
var defaults = {
return: 'entity'
};
options = extend({}, defaults, options);
options = extend({}, this.fetchOptions(), options);

this._applyHas();
this._applyLimit();
Expand Down

0 comments on commit ac753ed

Please sign in to comment.