Skip to content

Commit

Permalink
fix: augmented instanceof checks with fallback checks (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg authored Sep 5, 2024
1 parent d804a22 commit aae47e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
22 changes: 21 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectQueryBuilder } from 'typeorm'
import { FindOperator, Repository, SelectQueryBuilder } from 'typeorm'
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'

/**
Expand Down Expand Up @@ -227,3 +227,23 @@ const isoDateRegExp = new RegExp(
export function isISODate(str: string): boolean {
return isoDateRegExp.test(str)
}

export function isRepository<T>(repo: unknown | Repository<T>): repo is Repository<T> {
if (repo instanceof Repository) return true
try {
if (Object.getPrototypeOf(repo).constructor.name === 'Repository') return true
return typeof repo === 'object' && !('connection' in repo) && 'manager' in repo
} catch {
return false
}
}

export function isFindOperator<T>(value: unknown | FindOperator<T>): value is FindOperator<T> {
if (value instanceof FindOperator) return true
try {
if (Object.getPrototypeOf(value).constructor.name === 'FindOperator') return true
return typeof value === 'object' && '_type' in value && '_value' in value
} catch {
return false
}
}
23 changes: 12 additions & 11 deletions src/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mapKeys } from 'lodash'
import { stringify } from 'querystring'
import {
Brackets,
FindOperator,
FindOptionsRelationByString,
FindOptionsRelations,
FindOptionsUtils,
Expand All @@ -15,21 +14,23 @@ import {
import { WherePredicateOperator } from 'typeorm/query-builder/WhereClause'
import { OrmUtils } from 'typeorm/util/OrmUtils'
import { PaginateQuery } from './decorator'
import { FilterOperator, FilterSuffix, addFilter } from './filter'
import { addFilter, FilterOperator, FilterSuffix } from './filter'
import {
Column,
Order,
RelationColumn,
SortBy,
checkIsEmbedded,
checkIsRelation,
Column,
extractVirtualProperty,
fixColumnAlias,
getPropertiesByColumnName,
getQueryUrlComponents,
includesAllPrimaryKeyColumns,
isEntityKey,
isFindOperator,
isRepository,
Order,
positiveNumberOrDefault,
RelationColumn,
SortBy,
} from './helper'

const logger: Logger = new Logger('nestjs-paginate')
Expand Down Expand Up @@ -116,7 +117,7 @@ function flattenWhereAndTransform<T>(
if (obj.hasOwnProperty(key)) {
const joinedKey = parentKey ? `${parentKey}${separator}${key}` : key

if (typeof value === 'object' && value !== null && !(value instanceof FindOperator)) {
if (typeof value === 'object' && value !== null && !isFindOperator(value)) {
return flattenWhereAndTransform(queryBuilder, value as FindOptionsWhere<T>, separator, joinedKey)
} else {
const property = getPropertiesByColumnName(joinedKey)
Expand Down Expand Up @@ -210,9 +211,9 @@ export async function paginate<T extends ObjectLiteral>(

let [items, totalItems]: [T[], number] = [[], 0]

const queryBuilder = repo instanceof Repository ? repo.createQueryBuilder('__root') : repo
const queryBuilder = isRepository(repo) ? repo.createQueryBuilder('__root') : repo

if (repo instanceof Repository && !config.relations && config.loadEagerRelations === true) {
if (isRepository(repo) && !config.relations && config.loadEagerRelations === true) {
if (!config.relations) {
FindOptionsUtils.joinEagerRelations(queryBuilder, queryBuilder.alias, repo.metadata)
}
Expand Down Expand Up @@ -254,7 +255,7 @@ export async function paginate<T extends ObjectLiteral>(
createQueryBuilderRelations(queryBuilder.alias, relations)
}

const dbType = (repo instanceof Repository ? repo.manager : repo).connection.options.type
const dbType = (isRepository(repo) ? repo.manager : repo).connection.options.type
const isMariaDbOrMySql = (dbType: string) => dbType === 'mariadb' || dbType === 'mysql'
const isMMDb = isMariaDbOrMySql(dbType)

Expand Down Expand Up @@ -327,7 +328,7 @@ export async function paginate<T extends ObjectLiteral>(
queryBuilder.select(cols)
}

if (config.where && repo instanceof Repository) {
if (config.where && isRepository(repo)) {
const baseWhereStr = generateWhereStatement(queryBuilder, config.where)
queryBuilder.andWhere(`(${baseWhereStr})`)
}
Expand Down

0 comments on commit aae47e6

Please sign in to comment.