-
Notifications
You must be signed in to change notification settings - Fork 0
/
offsetLimitTypeCastingPlugin.ts
36 lines (29 loc) · 1.33 KB
/
offsetLimitTypeCastingPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { KyselyPlugin, LimitNode, OffsetNode, OperationNodeTransformer, PluginTransformQueryArgs, PluginTransformResultArgs, QueryResult, RootOperationNode, SelectQueryNode, UnknownRow } from 'kysely'
const isValidNumber = (value: unknown): value is number => typeof value === 'number' && !Number.isNaN(value)
class OffsetLimitTypeCastingTransformer extends OperationNodeTransformer {
protected override transformSelectQuery(
node: SelectQueryNode,
): SelectQueryNode {
if (isValidNumber(node.limit?.limit.value)) {
node = SelectQueryNode.cloneWithLimit(node, LimitNode.create(`${node.limit?.limit.value}` as unknown as number))
}
if (isValidNumber(node.offset?.offset.value)) {
node = SelectQueryNode.cloneWithOffset(node, OffsetNode.create(`${node.offset?.offset.value}` as unknown as number))
}
return super.transformSelectQuery(node)
}
}
export class OffsetLimitTypeCastingPlugin implements KyselyPlugin {
readonly #transformer: OffsetLimitTypeCastingTransformer
constructor() {
this.#transformer = new OffsetLimitTypeCastingTransformer()
}
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
return this.#transformer.transformNode(args.node)
}
async transformResult(
args: PluginTransformResultArgs,
): Promise<QueryResult<UnknownRow>> {
return args.result
}
}