@@ -11,27 +11,72 @@ import { SerializeParametersTransformer } from './serialize-transformer'
11
11
import type { Deserializer , Serializer } from './serializer'
12
12
import { defaultDeserializer , defaultSerializer } from './serializer'
13
13
14
- export interface SerializePluginOptions {
14
+ export interface BaseSerializePluginOptions {
15
15
/**
16
16
* serialize params
17
17
*/
18
- serializer ? : Serializer
18
+ serializer : Serializer
19
19
/**
20
20
* deserialize params
21
21
*/
22
- deserializer ? : Deserializer
22
+ deserializer : Deserializer
23
23
/**
24
24
* node kind to skip transform
25
25
*/
26
- skipNodeKind ? : Array < RootOperationNode [ 'kind' ] >
26
+ skipNodeKind : Array < RootOperationNode [ 'kind' ] >
27
27
}
28
28
29
- export class SerializePlugin implements KyselyPlugin {
29
+ export class BaseSerializePlugin implements KyselyPlugin {
30
30
private transformer : SerializeParametersTransformer
31
31
private deserializer : Deserializer
32
32
private skipNodeSet ?: Set < RootOperationNode [ 'kind' ] >
33
33
private ctx ?: WeakSet < QueryId >
34
34
35
+ /**
36
+ * Base class for {@link SerializePlugin}, without default options
37
+ */
38
+ public constructor ( { deserializer, serializer, skipNodeKind } : BaseSerializePluginOptions ) {
39
+ this . transformer = new SerializeParametersTransformer ( serializer )
40
+ this . deserializer = deserializer
41
+ if ( skipNodeKind . length ) {
42
+ this . skipNodeSet = new Set ( skipNodeKind )
43
+ this . ctx = new WeakSet ( )
44
+ }
45
+ }
46
+
47
+ public transformQuery ( { node, queryId } : PluginTransformQueryArgs ) : RootOperationNode {
48
+ if ( this . skipNodeSet ?. has ( node . kind ) ) {
49
+ this . ctx ?. add ( queryId )
50
+ return node
51
+ }
52
+ return this . transformer . transformNode ( node )
53
+ }
54
+
55
+ public async transformResult (
56
+ { result, queryId } : PluginTransformResultArgs ,
57
+ ) : Promise < QueryResult < UnknownRow > > {
58
+ return this . ctx ?. has ( queryId )
59
+ ? result
60
+ : { ...result , rows : this . parseRows ( result . rows ) }
61
+ }
62
+
63
+ private parseRows ( rows : UnknownRow [ ] ) : UnknownRow [ ] {
64
+ const result : UnknownRow [ ] = [ ]
65
+ for ( const row of rows ) {
66
+ const parsedRow : UnknownRow = { }
67
+ for ( const [ key , value ] of Object . entries ( row ) ) {
68
+ parsedRow [ key ] = this . deserializer ( value )
69
+ }
70
+ result . push ( parsedRow )
71
+ }
72
+ return result
73
+ }
74
+ }
75
+
76
+ interface SerializePluginOptions extends Partial < BaseSerializePluginOptions > {
77
+ }
78
+
79
+ export class SerializePlugin extends BaseSerializePlugin {
35
80
/**
36
81
* _**THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY !!!**_
37
82
*
@@ -94,50 +139,14 @@ export class SerializePlugin implements KyselyPlugin {
94
139
* ```
95
140
*/
96
141
public constructor ( options : SerializePluginOptions = { } ) {
97
- const {
98
- deserializer = defaultDeserializer ,
99
- serializer = defaultSerializer ,
100
- skipNodeKind = [ ] ,
101
- } = options
102
-
103
- this . transformer = new SerializeParametersTransformer ( serializer )
104
- this . deserializer = deserializer
105
- if ( skipNodeKind . length ) {
106
- this . skipNodeSet = new Set ( skipNodeKind )
107
- this . ctx = new WeakSet ( )
108
- }
109
- }
110
-
111
- public transformQuery ( { node, queryId } : PluginTransformQueryArgs ) : RootOperationNode {
112
- if ( this . skipNodeSet ?. has ( node . kind ) ) {
113
- this . ctx ?. add ( queryId )
114
- return node
115
- }
116
- return this . transformer . transformNode ( node )
117
- }
118
-
119
- public async transformResult (
120
- { result, queryId } : PluginTransformResultArgs ,
121
- ) : Promise < QueryResult < UnknownRow > > {
122
- return this . ctx ?. has ( queryId )
123
- ? result
124
- : { ...result , rows : this . parseRows ( result . rows ) }
125
- }
126
-
127
- private parseRows ( rows : UnknownRow [ ] ) : UnknownRow [ ] {
128
- const result : UnknownRow [ ] = [ ]
129
- for ( const row of rows ) {
130
- const parsedRow : UnknownRow = { }
131
- for ( const [ key , value ] of Object . entries ( row ) ) {
132
- parsedRow [ key ] = this . deserializer ( value )
133
- }
134
- result . push ( parsedRow )
135
- }
136
- return result
142
+ options . deserializer ??= defaultDeserializer
143
+ options . serializer ??= defaultSerializer
144
+ options . skipNodeKind ??= [ ]
145
+ super ( options as BaseSerializePluginOptions )
137
146
}
138
147
}
139
148
140
149
/**
141
- * @deprecated prefer to use {@link SerializePlugin}
150
+ * @deprecated use {@link SerializePlugin} instead
142
151
*/
143
152
export const SqliteSerializePlugin = SerializePlugin
0 commit comments