Skip to content

Commit ed54554

Browse files
committed
feat(plugin): add BaseSerializePlugin
1 parent 84ff3f1 commit ed54554

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

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

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,72 @@ import { SerializeParametersTransformer } from './serialize-transformer'
1111
import type { Deserializer, Serializer } from './serializer'
1212
import { defaultDeserializer, defaultSerializer } from './serializer'
1313

14-
export interface SerializePluginOptions {
14+
export interface BaseSerializePluginOptions {
1515
/**
1616
* serialize params
1717
*/
18-
serializer?: Serializer
18+
serializer: Serializer
1919
/**
2020
* deserialize params
2121
*/
22-
deserializer?: Deserializer
22+
deserializer: Deserializer
2323
/**
2424
* node kind to skip transform
2525
*/
26-
skipNodeKind?: Array<RootOperationNode['kind']>
26+
skipNodeKind: Array<RootOperationNode['kind']>
2727
}
2828

29-
export class SerializePlugin implements KyselyPlugin {
29+
export class BaseSerializePlugin implements KyselyPlugin {
3030
private transformer: SerializeParametersTransformer
3131
private deserializer: Deserializer
3232
private skipNodeSet?: Set<RootOperationNode['kind']>
3333
private ctx?: WeakSet<QueryId>
3434

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 {
3580
/**
3681
* _**THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY !!!**_
3782
*
@@ -94,50 +139,14 @@ export class SerializePlugin implements KyselyPlugin {
94139
* ```
95140
*/
96141
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)
137146
}
138147
}
139148

140149
/**
141-
* @deprecated prefer to use {@link SerializePlugin}
150+
* @deprecated use {@link SerializePlugin} instead
142151
*/
143152
export const SqliteSerializePlugin = SerializePlugin

packages/plugin-serialize/src/serializer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export const defaultDeserializer: Deserializer = (parameter) => {
3131
} else if (dateRegex.test(parameter)) {
3232
return new Date(parameter)
3333
} else if (
34-
(parameter[0] === '{' && parameter[parameter.length - 1] === '}')
35-
|| (parameter[0] === '[' && parameter[parameter.length - 1] === ']')
34+
(parameter.startsWith('{') && parameter.endsWith('}'))
35+
|| (parameter.startsWith('[') && parameter.endsWith(']'))
3636
) {
3737
try {
3838
return JSON.parse(parameter)

0 commit comments

Comments
 (0)