Skip to content

Commit 58f3ec5

Browse files
committed
feat(plugin): add option for only transform select and raw sql
1 parent b25e013 commit 58f3ec5

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs, QueryResult, RootOperationNode, UnknownRow } from 'kysely'
2+
import type { QueryId } from 'kysely/dist/cjs/util/query-id'
23
import { SerializeParametersTransformer } from './sqlite-serialize-transformer'
34
import type { Deserializer, Serializer } from './sqlite-serialize'
45
import { defaultDeserializer } from './sqlite-serialize'
@@ -24,11 +25,17 @@ export interface SqliteSerializePluginOptions {
2425
* @param parameter unknown
2526
*/
2627
deserializer?: Deserializer
28+
/**
29+
* only transform select query or raw sql return
30+
*/
31+
selectOrRawOnly?: boolean
2732
}
2833

2934
export class SqliteSerializePlugin implements KyselyPlugin {
3035
private serializeParametersTransformer: SerializeParametersTransformer
3136
private deserializer: Deserializer
37+
private only: boolean
38+
private ctx?: WeakSet<QueryId>
3239

3340
/**
3441
* _**THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY !!!**_
@@ -91,12 +98,16 @@ export class SqliteSerializePlugin implements KyselyPlugin {
9198
* })
9299
* ```
93100
*/
94-
public constructor({ deserializer, serializer }: SqliteSerializePluginOptions = {}) {
101+
public constructor({ selectOrRawOnly, deserializer, serializer }: SqliteSerializePluginOptions = {}) {
95102
this.serializeParametersTransformer = new SerializeParametersTransformer(serializer)
96103
this.deserializer = deserializer || defaultDeserializer
104+
this.only = selectOrRawOnly || false
97105
}
98106

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+
}
100111
return this.serializeParametersTransformer.transformNode(node)
101112
}
102113

@@ -114,11 +125,19 @@ export class SqliteSerializePlugin implements KyselyPlugin {
114125
}
115126

116127
public async transformResult(
117-
{ result }: PluginTransformResultArgs,
128+
{ result, queryId }: PluginTransformResultArgs,
118129
): Promise<QueryResult<UnknownRow>> {
119-
return {
130+
const parse = async () => ({
120131
...result,
121132
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
122139
}
140+
this.ctx?.delete(queryId)
141+
return await parse()
123142
}
124143
}

0 commit comments

Comments
 (0)