<commit_before>import { QueryBuilder as ObjectionQueryBuilder } from 'objection'

import './ModelNotFoundError'

export class QueryBuilder extends ObjectionQueryBuilder {
	static registeredFilters = { }

	_cyclicalEagerProtection = [ ]
	_allowEager = true

	static registerFilter(name, filter) {
		this.registeredFilters[name] = filter
	}

	subset(limit, offset = 0) {
		Log.error('subset is deprecated and will be removed in 0.8.')

		if(typeof limit === 'object') {
			offset = limit.offset || 0
			limit = limit.limit
		}

		return this.limit(limit).offset(offset)
	}

	paginate(req, perPage = 25, { query, param } = { }) {
		let page = 1

		if(!param.isNil) {
			page = Number.parseInt(req.params[param]) || 1
		} else {
			page = Number.parseInt(req.query[query || 'page']) || 1
		}

		page = Math.max(1, page)

		const start = (page - 1) * perPage
		const end = start + perPage

		return this.range(start, end).runAfter(result => {
			result.totalPages = Math.ceil(result.total / perPage)
			result.perPage = perPage
			result.page = page
			result.start = start
			result.end = Math.min(end, result.total)

			return result
		})
	}

	orFail() {
		return this.runAfter(result => {
			if(
				result.isNil ||
				(Array.isArray(result) && result.length === 0) ||
				(Array.isArray(result.results) && result.results.length === 0)
			) {
				throw new ModelNotFoundError(this._modelClass)
			}

			return result
		})
	}

	withoutEager() {
		this._allowEager = false
		this._eagerExpression = null
		this._eagerFilterExpressions = [ ]
		return this
	}

	eager(exp, filters) {
		if(!this._allowEager) {
			return this
		}

		if(!exp.isNil) {
			filters = Object.assign({ }, this.constructor.registeredFilters, filters || { })
		}

		return super.eager(exp, filters)
	}

	clone() {
		const builder = super.clone()
		builder._cyclicalEagerProtection = [ ].concat(this._cyclicalEagerProtection)
		builder._allowEager = this._allowEager
		return builder
	}

	childQueryOf(query) {
		super.childQueryOf(query)
<commit_msg>in QueryBuilder.js eager() do not re-assign filters if exp is already RelationExpression class<commit_after>import { QueryBuilder as ObjectionQueryBuilder } from 'objection'

import './ModelNotFoundError'

export class QueryBuilder extends ObjectionQueryBuilder {
	static registeredFilters = { }

	_cyclicalEagerProtection = [ ]
	_allowEager = true

	static registerFilter(name, filter) {
		this.registeredFilters[name] = filter
	}

	subset(limit, offset = 0) {
		Log.error('subset is deprecated and will be removed in 0.8.')

		if(typeof limit === 'object') {
			offset = limit.offset || 0
			limit = limit.limit
		}

		return this.limit(limit).offset(offset)
	}

	paginate(req, perPage = 25, { query, param } = { }) {
		let page = 1

		if(!param.isNil) {
			page = Number.parseInt(req.params[param]) || 1
		} else {
			page = Number.parseInt(req.query[query || 'page']) || 1
		}

		page = Math.max(1, page)

		const start = (page - 1) * perPage
		const end = start + perPage

		return this.range(start, end).runAfter(result => {
			result.totalPages = Math.ceil(result.total / perPage)
			result.perPage = perPage
			result.page = page
			result.start = start
			result.end = Math.min(end, result.total)

			return result
		})
	}

	orFail() {
		return this.runAfter(result => {
			if(
				result.isNil ||
				(Array.isArray(result) && result.length === 0) ||
				(Array.isArray(result.results) && result.results.length === 0)
			) {
				throw new ModelNotFoundError(this._modelClass)
			}

			return result
		})
	}

	withoutEager() {
		this._allowEager = false
		this._eagerExpression = null
		this._eagerFilterExpressions = [ ]
		return this
	}

	eager(exp, filters) {
		if(!this._allowEager) {
			return this
		}

		if(typeof exp === 'string') {
			filters = Object.assign({ }, this.constructor.registeredFilters, filters || { })
		}

		return super.eager(exp, filters)
	}

	clone() {
		const builder = super.clone()
		builder._cyclicalEagerProtection = [ ].concat(this._cyclicalEagerProtection)
		builder._allowEager = this._allowEager
		return builder
	}

	childQueryOf(query) {
		super.childQueryOf(query)

{'fraction_non_alphanumeric': 0.1314133591481123, 'fraction_numerical': 0.0062923523717328175, 'mean_word_length': 3.49727965179543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5302871', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1363, 'n_words': 294}