Skip to content

Commit

Permalink
feat(hydra-cli): support multiple orderBy clauses (Joystream#378)
Browse files Browse the repository at this point in the history
* feat(hydra-cli): update orderBy to accept multiple fields

affects: @dzlzv/hydra-cli

* test(hydra-cli): bump warthog version

affects: @dzlzv/hydra-cli

* fix(hydra-cli): interfaces enable orderby multiple fields

affects: @dzlzv/hydra-cli

* fix(hydra-cli): bug fix

affects: @dzlzv/hydra-cli
  • Loading branch information
metmirr authored and dzhelezov committed May 7, 2021
1 parent 6a95a99 commit 7348f03
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/hydra-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
"mustache": "^4.0.1",
"pluralize": "^8.0.0",
"tslib": "1.11.2",
"warthog": "/~https://github.com/metmirr/warthog/releases/download/v2.26.0/warthog-v2.26.0.tgz"
"warthog": "/~https://github.com/metmirr/warthog/releases/download/v2.30.0/warthog-v2.30.0.tgz"
},
"queryNodeDependencies": {
"lodash": "^4.17.20",
"pg-listen": "^1.7.0",
"warthog": "/~https://github.com/metmirr/warthog/releases/download/v2.26.0/warthog-v2.26.0.tgz",
"warthog": "/~https://github.com/metmirr/warthog/releases/download/v2.30.0/warthog-v2.30.0.tgz",
"typeorm": "^0.2.31"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/hydra-cli/src/templates/entities/resolver.ts.mst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class {{className}}ConnectionWhereArgs extends ConnectionPageInputOptions
where?: {{className}}WhereInput;

@Field(() => {{className}}OrderByEnum, { nullable: true })
orderBy?: {{className}}OrderByEnum;
orderBy?: [{{className}}OrderByEnum];
}

{{! -- Pagination objects }}
Expand Down
2 changes: 1 addition & 1 deletion packages/hydra-cli/src/templates/entities/service.ts.mst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class {{className}}Service extends BaseService<{{className}}> {

async find<W extends WhereInput>(
where?: any,
orderBy?: string,
orderBy?: string | string[],
limit?: number,
offset?: number,
fields?: string[]
Expand Down
23 changes: 14 additions & 9 deletions packages/hydra-cli/src/templates/interfaces/service.ts.mst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service } from 'typedi';
import { getConnection } from 'typeorm';
import { BaseService, WhereInput } from 'warthog';
import { orderBy } from 'lodash';
import { isArray, orderBy } from 'lodash';

{{#subclasses}}
import { {{className}}Service } from '../{{kebabName}}/{{kebabName}}.service'
Expand All @@ -19,7 +19,7 @@ export class {{className}}Service {
{{/subclasses}}
) {}

async find<W extends WhereInput>(where?: any, ob?: string, limit?: number, offset?: number, fields?: string[]): Promise<{{className}}[]> {
async find<W extends WhereInput>(where?: any, ob?: string | string[], limit?: number, offset?: number, fields?: string[]): Promise<{{className}}[]> {
let _limit = limit || 50;
let _offset = offset || 0;

Expand Down Expand Up @@ -47,14 +47,19 @@ export class {{className}}Service {

let collect: any[] = [{{#subclasses}}...{{camelNamePlural}}, {{/subclasses}}];
if (ob) {
const directions: ('asc' | 'desc')[] = []
const attrs: string[] = []
if (!isArray(ob)) {
ob = [ob]
}
ob.map(o => {
// NB: copied from warthog's BaseService
// TODO: allow multiple sorts
const parts = ob.toString().split('_');
// TODO: ensure attr is one of the properties on the model
const attr = parts[0];
const direction: 'asc' | 'desc' = parts[1] as 'asc' | 'desc';

collect = orderBy(collect, [attr], [direction]);
const parts = o.toString().split('_');
const direction: 'asc' | 'desc' = parts[1] as 'asc' | 'desc';
directions.push(direction)
attrs.push(parts[0])
})
collect = orderBy(collect, attrs, directions);
}

const _end = Math.min(collect.length, _limit + _offset);
Expand Down
28 changes: 21 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11934,12 +11934,12 @@ pg-pool@^2.0.10:
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-2.0.10.tgz#842ee23b04e86824ce9d786430f8365082d81c4a"
integrity sha512-qdwzY92bHf3nwzIUcj+zJ0Qo5lpG/YxchahxIN8+ZVmXqkahKXsnl2aiJPHLYN9o5mB/leG+Xh6XKxtP7e0sjg==

pg-pool@^3.3.0:
pg-pool@^3.1.1, pg-pool@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-3.3.0.tgz#12d5c7f65ea18a6e99ca9811bd18129071e562fc"
integrity sha512-0O5huCql8/D6PIRFAlmccjphLYWC+JIzvUhSzXSpGaf+tjTZc4nn+Lr7mLXBbFJfvwbP0ywDv73EiaBsxn7zdg==

pg-protocol@^1.2.0, pg-protocol@^1.5.0:
pg-protocol@^1.2.0, pg-protocol@^1.2.2, pg-protocol@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/pg-protocol/-/pg-protocol-1.5.0.tgz#b5dd452257314565e2d54ab3c132adc46565a6a0"
integrity sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==
Expand All @@ -11955,6 +11955,20 @@ pg-types@^2.1.0, pg-types@^2.2.0:
postgres-date "~1.0.4"
postgres-interval "^1.1.0"

pg@8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/pg/-/pg-8.0.3.tgz#b220ee468a1819e1c7e9ca9878f8ae50ba8e1952"
integrity sha512-fvcNXn4o/iq4jKq15Ix/e58q3jPSmzOp6/8C3CaHoSR/bsxdg+1FXfDRePdtE/zBb3++TytvOrS1hNef3WC/Kg==
dependencies:
buffer-writer "2.0.0"
packet-reader "1.0.0"
pg-connection-string "0.1.3"
pg-pool "^3.1.1"
pg-protocol "^1.2.2"
pg-types "^2.1.0"
pgpass "1.x"
semver "4.3.2"

pg@^7.12.1:
version "7.18.2"
resolved "https://registry.yarnpkg.com/pg/-/pg-7.18.2.tgz#4e219f05a00aff4db6aab1ba02f28ffa4513b0bb"
Expand Down Expand Up @@ -14460,7 +14474,7 @@ typeorm-typedi-extensions@^0.2.3:
resolved "https://registry.yarnpkg.com/typeorm-typedi-extensions/-/typeorm-typedi-extensions-0.2.3.tgz#94fca2656206d771bf6d2242f5aab570511188e8"
integrity sha512-T9i1NvRZNjPn9Jb8oT772ihfn6PwdqDVpzPCtKSqjkZGOgXrCkdyD3dDrzfMaoWJ1afU58bVx2CMb95FzT42Ow==

typeorm@^0.2.25, typeorm@^0.2.26, typeorm@^0.2.31:
typeorm@^0.2.25, typeorm@^0.2.26:
version "0.2.32"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.32.tgz#544dbfdfe0cd0887548d9bcbd28527ea4f4b3c9b"
integrity sha512-LOBZKZ9As3f8KRMPCUT2H0JZbZfWfkcUnO3w/1BFAbL/X9+cADTF6bczDGGaKVENJ3P8SaKheKmBgpt5h1x+EQ==
Expand Down Expand Up @@ -14835,9 +14849,9 @@ warthog@^2.20.0:
typeorm-typedi-extensions "^0.2.3"
typescript "^3.9.7"

"warthog@/~https://github.com/metmirr/warthog/releases/download/v2.26.0/warthog-v2.26.0.tgz":
version "2.26.0"
resolved "/~https://github.com/metmirr/warthog/releases/download/v2.26.0/warthog-v2.26.0.tgz#0a76a8710c392992e7695b45ca0a8efc81ad4c9a"
"warthog@/~https://github.com/metmirr/warthog/releases/download/v2.30.0/warthog-v2.30.0.tgz":
version "2.30.0"
resolved "/~https://github.com/metmirr/warthog/releases/download/v2.30.0/warthog-v2.30.0.tgz#24a0b975f2ad5cba17a934752ac07052e856b49c"
dependencies:
"@types/app-root-path" "^1.2.4"
"@types/bn.js" "^4.11.6"
Expand Down Expand Up @@ -14888,7 +14902,7 @@ warthog@^2.20.0:
mkdirp "^0.5.1"
node-emoji "^1.10.0"
open "^7.0.0"
pg "^7.12.1"
pg "8.0.3"
pgtools "^0.3.0"
prettier "^1.19.1"
reflect-metadata "^0.1.13"
Expand Down

0 comments on commit 7348f03

Please sign in to comment.