1
1
import type { KyselyPlugin , PluginTransformQueryArgs , PluginTransformResultArgs , QueryResult , RootOperationNode , UnknownRow } from 'kysely'
2
+ import type { QueryId } from 'kysely/dist/cjs/util/query-id'
2
3
import { SerializeParametersTransformer } from './sqlite-serialize-transformer'
3
4
import type { Deserializer , Serializer } from './sqlite-serialize'
4
5
import { defaultDeserializer } from './sqlite-serialize'
@@ -24,11 +25,17 @@ export interface SqliteSerializePluginOptions {
24
25
* @param parameter unknown
25
26
*/
26
27
deserializer ?: Deserializer
28
+ /**
29
+ * only transform select query or raw sql return
30
+ */
31
+ selectOrRawOnly ?: boolean
27
32
}
28
33
29
34
export class SqliteSerializePlugin implements KyselyPlugin {
30
35
private serializeParametersTransformer : SerializeParametersTransformer
31
36
private deserializer : Deserializer
37
+ private only : boolean
38
+ private ctx ?: WeakSet < QueryId >
32
39
33
40
/**
34
41
* _**THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY !!!**_
@@ -91,12 +98,16 @@ export class SqliteSerializePlugin implements KyselyPlugin {
91
98
* })
92
99
* ```
93
100
*/
94
- public constructor ( { deserializer, serializer } : SqliteSerializePluginOptions = { } ) {
101
+ public constructor ( { selectOrRawOnly , deserializer, serializer } : SqliteSerializePluginOptions = { } ) {
95
102
this . serializeParametersTransformer = new SerializeParametersTransformer ( serializer )
96
103
this . deserializer = deserializer || defaultDeserializer
104
+ this . only = selectOrRawOnly || false
97
105
}
98
106
99
- public transformQuery ( { node } : PluginTransformQueryArgs ) : RootOperationNode {
107
+ public transformQuery ( { node, queryId } : PluginTransformQueryArgs ) : RootOperationNode {
108
+ if ( this . only && ( node . kind === 'SelectQueryNode' || node . kind === 'RawNode' ) ) {
109
+ this . ctx ?. add ( queryId )
110
+ }
100
111
return this . serializeParametersTransformer . transformNode ( node )
101
112
}
102
113
@@ -114,11 +125,19 @@ export class SqliteSerializePlugin implements KyselyPlugin {
114
125
}
115
126
116
127
public async transformResult (
117
- { result } : PluginTransformResultArgs ,
128
+ { result, queryId } : PluginTransformResultArgs ,
118
129
) : Promise < QueryResult < UnknownRow > > {
119
- return {
130
+ const parse = async ( ) => ( {
120
131
...result ,
121
132
rows : await this . parseResult ( result . rows ) ,
133
+ } )
134
+ if ( ! this . only ) {
135
+ return await parse ( )
136
+ }
137
+ if ( ! this . ctx ?. has ( queryId ) ) {
138
+ return result
122
139
}
140
+ this . ctx ?. delete ( queryId )
141
+ return await parse ( )
123
142
}
124
143
}
0 commit comments