Skip to content

Commit b25e013

Browse files
committed
feat(plugin): transform all queries instead of select only
1 parent 0d673b6 commit b25e013

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs, QueryResult, RootOperationNode, UnknownRow } from 'kysely'
2-
import type { QueryId } from 'kysely/dist/cjs/util/query-id'
32
import { SerializeParametersTransformer } from './sqlite-serialize-transformer'
43
import type { Deserializer, Serializer } from './sqlite-serialize'
54
import { defaultDeserializer } from './sqlite-serialize'
@@ -30,7 +29,6 @@ export interface SqliteSerializePluginOptions {
3029
export class SqliteSerializePlugin implements KyselyPlugin {
3130
private serializeParametersTransformer: SerializeParametersTransformer
3231
private deserializer: Deserializer
33-
private ctx: WeakMap<QueryId, string>
3432

3533
/**
3634
* _**THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY !!!**_
@@ -96,17 +94,16 @@ export class SqliteSerializePlugin implements KyselyPlugin {
9694
public constructor({ deserializer, serializer }: SqliteSerializePluginOptions = {}) {
9795
this.serializeParametersTransformer = new SerializeParametersTransformer(serializer)
9896
this.deserializer = deserializer || defaultDeserializer
99-
this.ctx = new WeakMap()
10097
}
10198

102-
public transformQuery({ node, queryId }: PluginTransformQueryArgs): RootOperationNode {
103-
if (node.kind === 'SelectQueryNode') {
104-
this.ctx.set(queryId, node.kind)
105-
}
99+
public transformQuery({ node }: PluginTransformQueryArgs): RootOperationNode {
106100
return this.serializeParametersTransformer.transformNode(node)
107101
}
108102

109103
private async parseResult(rows: any[]) {
104+
if (!rows.length) {
105+
return []
106+
}
110107
return await Promise.all(rows.map(async (row) => {
111108
const deserializedRow = { ...row }
112109
for (const key in deserializedRow) {
@@ -117,16 +114,11 @@ export class SqliteSerializePlugin implements KyselyPlugin {
117114
}
118115

119116
public async transformResult(
120-
{ result, queryId }: PluginTransformResultArgs,
117+
{ result }: PluginTransformResultArgs,
121118
): Promise<QueryResult<UnknownRow>> {
122-
const { rows } = result
123-
const data = this.ctx.get(queryId)
124-
this.ctx.delete(queryId)
125-
return (rows && data === 'SelectQueryNode')
126-
? {
127-
...result,
128-
rows: await this.parseResult(rows),
129-
}
130-
: result
119+
return {
120+
...result,
121+
rows: await this.parseResult(result.rows),
122+
}
131123
}
132124
}

0 commit comments

Comments
 (0)