1
- import type { KyselyPlugin , PluginTransformQueryArgs , PluginTransformResultArgs , QueryResult , RootOperationNode , UnknownRow } from 'kysely'
1
+ import type {
2
+ KyselyPlugin ,
3
+ PluginTransformQueryArgs ,
4
+ PluginTransformResultArgs ,
5
+ QueryResult ,
6
+ RootOperationNode ,
7
+ UnknownRow ,
8
+ } from 'kysely'
2
9
import type { QueryId } from 'kysely/dist/esm/util/query-id'
3
10
import { SerializeParametersTransformer } from './serialize-transformer'
4
11
import type { Deserializer , Serializer } from './serializer'
@@ -14,15 +21,15 @@ export interface SerializePluginOptions {
14
21
*/
15
22
deserializer ?: Deserializer
16
23
/**
17
- * only transform select query or raw sql return
24
+ * node kink to skip transform
18
25
*/
19
- selectOrRawOnly ?: boolean
26
+ skipNodeKind ?: Array < RootOperationNode [ 'kind' ] >
20
27
}
21
28
22
29
export class SerializePlugin implements KyselyPlugin {
23
30
private transformer : SerializeParametersTransformer
24
31
private deserializer : Deserializer
25
- private only : boolean | undefined
32
+ private skipNodeSet ?: Set < RootOperationNode [ 'kind' ] >
26
33
private ctx ?: WeakSet < QueryId >
27
34
28
35
/**
@@ -89,42 +96,39 @@ export class SerializePlugin implements KyselyPlugin {
89
96
public constructor ( options : SerializePluginOptions = { } ) {
90
97
const {
91
98
deserializer = defaultDeserializer ,
92
- selectOrRawOnly,
93
99
serializer = defaultSerializer ,
100
+ skipNodeKind = [ ] ,
94
101
} = options
95
102
96
103
this . transformer = new SerializeParametersTransformer ( serializer )
97
104
this . deserializer = deserializer
98
- this . only = selectOrRawOnly
99
-
100
- if ( selectOrRawOnly ) {
105
+ if ( skipNodeKind . length ) {
106
+ this . skipNodeSet = new Set ( skipNodeKind )
101
107
this . ctx = new WeakSet ( )
102
108
}
103
109
}
104
110
105
111
public transformQuery ( { node, queryId } : PluginTransformQueryArgs ) : RootOperationNode {
106
- if ( this . only && [ 'SelectQueryNode' , 'RawNode' ] . includes ( node . kind ) ) {
112
+ if ( this . skipNodeSet ?. has ( node . kind ) ) {
107
113
this . ctx ?. add ( queryId )
114
+ return node
108
115
}
109
116
return this . transformer . transformNode ( node )
110
117
}
111
118
112
119
public async transformResult (
113
120
{ result, queryId } : PluginTransformResultArgs ,
114
121
) : Promise < QueryResult < UnknownRow > > {
115
- const parsedResult = {
116
- ...result ,
117
- rows : result . rows . map ( row => Object . fromEntries (
118
- Object . entries ( row ) . map ( ( [ key , value ] ) =>
119
- ( [ key , this . deserializer ( value ) ] ) ,
120
- ) ,
121
- ) ) ,
122
- }
123
- if ( ! this . only ) {
124
- return parsedResult
125
- }
126
- this . ctx ?. delete ( queryId )
127
- return parsedResult
122
+ return this . ctx ?. has ( queryId )
123
+ ? result
124
+ : {
125
+ ...result ,
126
+ rows : result . rows . map ( row => Object . fromEntries (
127
+ Object . entries ( row ) . map ( ( [ key , value ] ) =>
128
+ ( [ key , this . deserializer ( value ) ] ) ,
129
+ ) ,
130
+ ) ) ,
131
+ }
128
132
}
129
133
}
130
134
0 commit comments