Skip to content

Commit

Permalink
perf: get rid of objection shim for tableRef
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Mar 16, 2023
1 parent 4f8fae7 commit 97cba1a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/helpers/field_ref.ts
Original file line number Diff line number Diff line change
@@ -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))
}
5 changes: 3 additions & 2 deletions src/paginators/cursor.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -85,7 +86,7 @@ export function CursorPaginator<M extends Model>(
// 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")
})
Expand Down
9 changes: 3 additions & 6 deletions src/resolver/field.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -38,11 +39,7 @@ export function FieldResolver<M extends Model>(
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()
Expand Down
7 changes: 4 additions & 3 deletions src/resolver/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -124,11 +124,12 @@ export function ModelResolver<M extends Model = Model>(
// 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())
)
)
}

Expand Down
8 changes: 0 additions & 8 deletions src/shim.d.ts

This file was deleted.

0 comments on commit 97cba1a

Please sign in to comment.