Skip to content

Commit 8a31bb2

Browse files
committed
feat(plugin): custom skip node
1 parent eed80ea commit 8a31bb2

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

packages/plugin-serialize/src/serialize-plugin.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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'
29
import type { QueryId } from 'kysely/dist/esm/util/query-id'
310
import { SerializeParametersTransformer } from './serialize-transformer'
411
import type { Deserializer, Serializer } from './serializer'
@@ -14,15 +21,15 @@ export interface SerializePluginOptions {
1421
*/
1522
deserializer?: Deserializer
1623
/**
17-
* only transform select query or raw sql return
24+
* node kink to skip transform
1825
*/
19-
selectOrRawOnly?: boolean
26+
skipNodeKind?: Array<RootOperationNode['kind']>
2027
}
2128

2229
export class SerializePlugin implements KyselyPlugin {
2330
private transformer: SerializeParametersTransformer
2431
private deserializer: Deserializer
25-
private only: boolean | undefined
32+
private skipNodeSet?: Set<RootOperationNode['kind']>
2633
private ctx?: WeakSet<QueryId>
2734

2835
/**
@@ -89,42 +96,39 @@ export class SerializePlugin implements KyselyPlugin {
8996
public constructor(options: SerializePluginOptions = {}) {
9097
const {
9198
deserializer = defaultDeserializer,
92-
selectOrRawOnly,
9399
serializer = defaultSerializer,
100+
skipNodeKind = [],
94101
} = options
95102

96103
this.transformer = new SerializeParametersTransformer(serializer)
97104
this.deserializer = deserializer
98-
this.only = selectOrRawOnly
99-
100-
if (selectOrRawOnly) {
105+
if (skipNodeKind.length) {
106+
this.skipNodeSet = new Set(skipNodeKind)
101107
this.ctx = new WeakSet()
102108
}
103109
}
104110

105111
public transformQuery({ node, queryId }: PluginTransformQueryArgs): RootOperationNode {
106-
if (this.only && ['SelectQueryNode', 'RawNode'].includes(node.kind)) {
112+
if (this.skipNodeSet?.has(node.kind)) {
107113
this.ctx?.add(queryId)
114+
return node
108115
}
109116
return this.transformer.transformNode(node)
110117
}
111118

112119
public async transformResult(
113120
{ result, queryId }: PluginTransformResultArgs,
114121
): 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+
}
128132
}
129133
}
130134

0 commit comments

Comments
 (0)