Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
return operators[operator](comparisonRight)
}
// HACK: Can't be `{}` or `undefined`, because that doesn't work with `or` conditions
const hackAlwaysTrueCondition: LokiRawQuery = { _fakeAlwaysTrue: { $eq: undefined } }
const encodeWhereDescription: (WhereDescription | On) => LokiRawQuery = ({ left, comparison }) =>
// HACK: If this is a column comparison condition, ignore it (assume it evaluates to true)
// The column comparison will actually be performed during the refining pass with a matcher func
has('column', comparison.right)
? hackAlwaysTrueCondition
: objOf(left, encodeComparison(comparison))
const typeEq = propEq('type')
const encodeCondition: Condition => LokiRawQuery = condition =>
(cond([
[typeEq('and'), encodeAnd],
[typeEq('or'), encodeOr],
[typeEq('where'), encodeWhereDescription],
[typeEq('on'), encodeWhereDescription],
]): any)(condition)
const encodeAndOr: LokiKeyword => (And | Or) => LokiRawQuery = op =>
pipe(
prop('conditions'),
map(encodeCondition),
objOf(op),
)
[T, objOf('$and')],
]): any)
const encodeConditions: (Where[] | On[]) => LokiRawQuery = pipe(
conditions => map(encodeCondition, conditions),
concatRawQueries,
)
const encodeMapKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
always(columnName('id')),
prop('foreignKey'),
)
const encodeJoinKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
prop('key'),
always(columnName('id')),
)
const encodeOriginalConditions: (On[]) => Where[] = map(({ left, comparison }) => ({
type: 'where',
left,
comparison,
}))
const encodeJoin: (AssociationArgs, On[]) => LokiJoin = ([table, associationInfo], conditions) => ({
table,
query: encodeConditions(conditions),
originalConditions: encodeOriginalConditions(conditions),
mapKey: encodeMapKey(associationInfo),
joinKey: encodeJoinKey(associationInfo),
[has('value'), prop('value')],
[has('values'), prop('values')],
[has('column'), arg => element._raw[arg.column]],
])
const encodeWhereDescription: WhereDescription => Matcher<*> = description => element => {
const left = element._raw[description.left]
const { comparison } = description
const operator = operators[comparison.operator]
const getRight = getComparisonRightFor(element)
const right = getRight(comparison.right)
return operator(left, right)
}
const typeEq = propEq('type')
const encodeWhere: Where => Matcher<*> = where =>
(cond([
[typeEq('and'), encodeAnd],
[typeEq('or'), encodeOr],
[typeEq('where'), encodeWhereDescription],
]): any)(where)
const encodeAnd: And => Matcher<*> = pipe(
prop('conditions'),
map(encodeWhere),
allPass,
)
const encodeOr: Or => Matcher<*> = pipe(
prop('conditions'),
comparison: _valueOrComparison(valueOrComparison),
}
}
const whereDescription: WhereDescription = (leftOrWhereDescription: any)
return {
type: 'on',
table,
left: whereDescription.left,
comparison: whereDescription.comparison,
}
}
const syncStatusColumn = columnName('_status')
const getJoins: (Condition[]) => [On[], Where[]] = (partition(propEq('type', 'on')): any)
const whereNotDeleted = where(syncStatusColumn, notEq('deleted'))
const joinsWithoutDeleted = pipe(
map(prop('table')),
uniq,
map(table => on(table, syncStatusColumn, notEq('deleted'))),
)
export function buildQueryDescription(conditions: Condition[]): QueryDescription {
const [join, whereConditions] = getJoins(conditions)
return { join, where: whereConditions }
}
export function queryWithoutDeleted(query: QueryDescription): QueryDescription {
const { join, where: whereConditions } = query