Skip to content

Instantly share code, notes, and snippets.

@arisris
Created November 2, 2023 14:18
Show Gist options
  • Save arisris/40bdc35c621559995ec449dc4fe9fb31 to your computer and use it in GitHub Desktop.
Save arisris/40bdc35c621559995ec449dc4fe9fb31 to your computer and use it in GitHub Desktop.
Simple Kysely Plugins to prefix table name
import {
type KyselyPlugin,
type PluginTransformQueryArgs,
type RootOperationNode,
type PluginTransformResultArgs,
type QueryResult,
type UnknownRow,
OperationNodeTransformer,
TableNode,
} from "kysely";
export class KyselyTablePrefixed implements KyselyPlugin {
#transformer: TablePrefixedTransformers;
constructor(prefix: string) {
this.#transformer = new TablePrefixedTransformers(prefix);
}
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
return this.#transformer.transformNode(args.node);
}
async transformResult(
args: PluginTransformResultArgs
): Promise<QueryResult<UnknownRow>> {
return args.result;
}
}
class TablePrefixedTransformers extends OperationNodeTransformer {
#prefix: string;
constructor(prefix: string) {
super();
this.#prefix = prefix;
}
protected transformTable(node: TableNode): TableNode {
return {
...node,
table: {
...node.table,
identifier: {
...node.table.identifier,
name: this.#prefix + node.table.identifier.name,
},
},
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment