mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
public commit
This commit is contained in:
36
app/src/data/plugins/DeserializeJsonValuesPlugin.ts
Normal file
36
app/src/data/plugins/DeserializeJsonValuesPlugin.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type {
|
||||
KyselyPlugin,
|
||||
PluginTransformQueryArgs,
|
||||
PluginTransformResultArgs,
|
||||
QueryResult,
|
||||
RootOperationNode,
|
||||
UnknownRow,
|
||||
} from "kysely";
|
||||
|
||||
type KeyValueObject = { [key: string]: any };
|
||||
|
||||
export class DeserializeJsonValuesPlugin implements KyselyPlugin {
|
||||
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
||||
return args.node;
|
||||
}
|
||||
transformResult(
|
||||
args: PluginTransformResultArgs
|
||||
): Promise<QueryResult<UnknownRow>> {
|
||||
return Promise.resolve({
|
||||
...args.result,
|
||||
rows: args.result.rows.map((row: KeyValueObject) => {
|
||||
const result: KeyValueObject = {};
|
||||
for (const key in row) {
|
||||
try {
|
||||
// Attempt to parse the value as JSON
|
||||
result[key] = JSON.parse(row[key]);
|
||||
} catch (error) {
|
||||
// If parsing fails, keep the original value
|
||||
result[key] = row[key];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
31
app/src/data/plugins/FilterNumericKeysPlugin.ts
Normal file
31
app/src/data/plugins/FilterNumericKeysPlugin.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type {
|
||||
KyselyPlugin,
|
||||
PluginTransformQueryArgs,
|
||||
PluginTransformResultArgs,
|
||||
QueryResult,
|
||||
RootOperationNode,
|
||||
UnknownRow,
|
||||
} from "kysely";
|
||||
|
||||
type KeyValueObject = { [key: string]: any };
|
||||
|
||||
export class FilterNumericKeysPlugin implements KyselyPlugin {
|
||||
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
||||
return args.node;
|
||||
}
|
||||
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
|
||||
return Promise.resolve({
|
||||
...args.result,
|
||||
rows: args.result.rows.map((row: KeyValueObject) => {
|
||||
const filteredObj: KeyValueObject = {};
|
||||
for (const key in row) {
|
||||
if (Number.isNaN(+key)) {
|
||||
// Check if the key is not a number
|
||||
filteredObj[key] = row[key];
|
||||
}
|
||||
}
|
||||
return filteredObj;
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
23
app/src/data/plugins/KyselyPluginRunner.ts
Normal file
23
app/src/data/plugins/KyselyPluginRunner.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { KyselyPlugin, UnknownRow } from "kysely";
|
||||
|
||||
// @todo: add test
|
||||
export class KyselyPluginRunner {
|
||||
protected plugins: Set<KyselyPlugin>;
|
||||
|
||||
constructor(plugins: KyselyPlugin[] = []) {
|
||||
this.plugins = new Set(plugins);
|
||||
}
|
||||
|
||||
async transformResultRows<T>(rows: T[]): Promise<T[]> {
|
||||
let copy = rows;
|
||||
for (const plugin of this.plugins) {
|
||||
const res = await plugin.transformResult({
|
||||
queryId: "1" as any,
|
||||
result: { rows: copy as UnknownRow[] },
|
||||
});
|
||||
copy = res.rows as T[];
|
||||
}
|
||||
|
||||
return copy as T[];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user