From 97cba1a0ff93dbb8a18c07bc68b238eda025e7ef Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Thu, 16 Mar 2023 17:44:01 +0700 Subject: [PATCH] perf: get rid of objection shim for tableRef --- src/helpers/field_ref.ts | 6 ++++++ src/paginators/cursor.ts | 5 +++-- src/resolver/field.ts | 9 +++------ src/resolver/model.ts | 7 ++++--- src/shim.d.ts | 8 -------- 5 files changed, 16 insertions(+), 19 deletions(-) create mode 100644 src/helpers/field_ref.ts delete mode 100644 src/shim.d.ts diff --git a/src/helpers/field_ref.ts b/src/helpers/field_ref.ts new file mode 100644 index 0000000..bf21cb4 --- /dev/null +++ b/src/helpers/field_ref.ts @@ -0,0 +1,6 @@ +import { AnyQueryBuilder, Model, ref } from "objection" + +// Get rid of this once https://github.com/Vincit/objection.js/issues/2364 is fixed +export function field_ref(query: AnyQueryBuilder, field: string) { + return ref(field).from(query.tableRefFor(query.modelClass() as typeof Model)) +} diff --git a/src/paginators/cursor.ts b/src/paginators/cursor.ts index b05f024..2dc8441 100644 --- a/src/paginators/cursor.ts +++ b/src/paginators/cursor.ts @@ -1,5 +1,6 @@ -import { Model, QueryBuilder, raw, ref } from "objection" +import { Model, QueryBuilder, raw } from "objection" +import { field_ref } from "../helpers/field_ref" import { PaginatorFn } from "." export interface CursorPaginatorOptions { @@ -85,7 +86,7 @@ export function CursorPaginator( // Set query order query.clearOrder() fields.forEach((field) => { - const f = ref(field.name).from(query.tableRef()) + const f = field_ref(query, field.name) // TODO: prevent potential name clash with aliases like .as(`_${table_ref}_order_key_0`) query.select(f).orderBy(f, field.desc ? "desc" : "asc") }) diff --git a/src/resolver/field.ts b/src/resolver/field.ts index 9cf2af4..ede6e96 100644 --- a/src/resolver/field.ts +++ b/src/resolver/field.ts @@ -1,6 +1,7 @@ import { ResolveTree } from "graphql-parse-resolve-info" -import { Model, QueryBuilder, ref } from "objection" +import { Model, QueryBuilder } from "objection" +import { field_ref } from "../helpers/field_ref" import { async_run_after } from "../helpers/run_after" import { ResolverContext, ResolveTreeFn } from "./graph" @@ -38,11 +39,7 @@ export function FieldResolver( if (select) { select(query, resolve_opts) } else { - query.select( - ref(model_field || field) - .from(query.tableRef()) - .as(field) - ) + query.select(field_ref(query, model_field || field).as(field)) } if (clean) { const context = query.context() diff --git a/src/resolver/model.ts b/src/resolver/model.ts index 8cdddb3..7ddf86f 100644 --- a/src/resolver/model.ts +++ b/src/resolver/model.ts @@ -4,10 +4,10 @@ import { ModelClass, ModelConstructor, QueryBuilder, - ref, RelationMappings, } from "objection" +import { field_ref } from "../helpers/field_ref" import { FieldResolver, FieldResolverFn } from "./field" import { ResolverContext, ResolveTreeFn } from "./graph" import { RelationResolver } from "./relation" @@ -124,11 +124,12 @@ export function ModelResolver( // This avoid automatic "select *" generated by objection. if (!query.has((QueryBuilder as any).SelectSelector)) { query.select( - ref( + field_ref( + query, typeof ThisModel.idColumn === "string" ? ThisModel.idColumn : ThisModel.idColumn[0] - ).from(query.tableRef()) + ) ) } diff --git a/src/shim.d.ts b/src/shim.d.ts deleted file mode 100644 index 5d3552b..0000000 --- a/src/shim.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import "objection" - -declare module "objection" { - interface QueryBuilder { - // Not sure why it's missing in official typings - tableRef(): string - } -}