public commit

This commit is contained in:
dswbx
2024-11-16 12:01:47 +01:00
commit 90f80c4280
582 changed files with 49291 additions and 0 deletions

View 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;
}),
});
}
}

View 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;
}),
});
}
}

View 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[];
}
}