Skip to content

Commit

Permalink
fixes for Objection 2.x (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Vignesh M <[email protected]>

Co-authored-by: Igor Savin <[email protected]>
Co-authored-by: Vignesh M <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2020
1 parent 6910d55 commit 34309ce
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ In addition to the filter parameters, there is a set of query parameters that ha

| Special parameter | Explanation |
|-----------------------------------|----------------------------------------------------------------------------------------------------------|
| `eager=[children, parent.movies]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. |
| `eager=[children, parent.movies]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. That pass to [withGraphFetched](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#withgraphfetched). |
| `join=[parent, parent.movies]` | Which relations to join and fetch eagerly for the result models. An objection.js relation expression. That pass to [withGraphJoined](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#withgraphjoined). |
| `orderBy=firstName` | Sort the result by certain property. |
| `orderByDesc=firstName` | Sort the result by certain property in descending order. |
| `rangeStart=10` | The start of the result range (inclusive). The result will be `{total: 12343, results: [ ... ]}`. |
Expand Down
23 changes: 20 additions & 3 deletions lib/FindQueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const QueryParameter = require('./QueryParameter');

const SPECIAL_PARAMETERS = Object.freeze({
eager: 'eager',
join: 'join',
rangeEnd: 'rangeEnd',
rangeStart: 'rangeStart',
orderBy: 'orderBy',
Expand Down Expand Up @@ -78,7 +79,8 @@ const SPECIAL_PARAMETERS = Object.freeze({
*
* | Special parameter | Explanation |
* |-------------------------------|----------------------------------------------------------------------------------------------|
* | `eager=[pets, parent.movies]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. |
* | `eager=[pets, parent.movies]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. That pass to `withGraphFetched`. |
* | `join=[owner]` | Which relations to fetch eagerly for the result models. An objection.js relation expression. That pass to `withGraphJoined`. |
* | `orderBy=firstName` | Sort the result by certain property. |
* | `orderByDesc=firstName` | Sort the result by certain property in descending order. |
* | `rangeStart=10` | The start of the result range. The result will be `{total: 12343, results: [ ... ]}`. |
Expand Down Expand Up @@ -338,6 +340,7 @@ class FindQueryBuilder {
this._buildOrderBy(params, builder);
this._buildRange(params, builder);
this._buildEager(params, builder);
this._buildJoin(params, builder);

return builder;
}
Expand Down Expand Up @@ -499,10 +502,24 @@ class FindQueryBuilder {
}

if (this._allowEager) {
builder.allowEager(this._allowEager);
builder.allowGraph(this._allowEager);
}

builder.eager(eager.value);
builder.withGraphFetched(eager.value);
}

_buildJoin(params, builder) {
let join = _.find(params, { specialParameter: 'join' });

if (!join) {
return;
}

if (this._allowEager) {
builder.allowGraph(this._allowEager);
}

builder.withGraphJoined(join.value);
}

_parsePropertyRefs(refs) {
Expand Down
6 changes: 2 additions & 4 deletions lib/PropertyRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ class PropertyRef {

const rel = this.relation;
if (rel && !rel.isOneToOne()) {
const subQuery = rel.findQuery(rel.relatedModelClass.query(), {
ownerIds: rel.ownerProp.refs(builder),
});
const subQuery = rel.ownerModelClass.relatedQuery(rel.name).alias(rel.relatedModelClass.name);
subQuery[whereMethod].apply(subQuery, filter.args);

builder.whereExists(subQuery.toKnexQuery().select(1));
builder.whereExists(subQuery.select(1));
} else {
builder[whereMethod].apply(builder, filter.args);
}
Expand Down
37 changes: 31 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
"mocha": "6.2.3",
"mysql": "^2.18.1",
"nyc": "^15.0.1",
"objection": "^1.6.11",
"objection": "^2.0.0",
"pg": "^7.18.2",
"prettier": "^2.0.4",
"sqlite3": "^4.1.1",
"typescript": "3.8.3"
},
"peerDependencies": {
"objection": ">=1.6.11 <2.0.0"
"objection": ">=2.0.0 <3.0.0"
},
"engines": {
"node": ">=8"
Expand Down
3 changes: 1 addition & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ describe('integration tests', () => {
return objectionFind(Animal)
.build({
orderByAsc: ['owner:parent:lastName'],
eager: 'owner.[parent]',
join: 'owner.[parent]',
})
.eagerAlgorithm(Model.JoinEagerAlgorithm)
.then(function (result) {
const names = _.map(
_.reject(result, (pet) => _.includes(pet.name, 'P0')),
Expand Down

0 comments on commit 34309ce

Please sign in to comment.