mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
58
apps/web-react/app/db/empty-values-noop-plugin.ts
Normal file
58
apps/web-react/app/db/empty-values-noop-plugin.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
type InsertQueryNode,
|
||||
type KyselyPlugin,
|
||||
type PluginTransformQueryArgs,
|
||||
type PluginTransformResultArgs,
|
||||
type QueryResult,
|
||||
RawNode,
|
||||
type RootOperationNode,
|
||||
SelectionNode,
|
||||
SelectQueryNode,
|
||||
type UnknownRow,
|
||||
ValuesNode,
|
||||
WhereNode,
|
||||
} from "kysely";
|
||||
|
||||
/**
|
||||
* Makes inserting an empty array of values a no-op instead of a syntax error.
|
||||
* Kysely compiles `.values([])` into invalid SQL, so without this plugin every
|
||||
* dynamic multi-row insert would need a length check before it. The empty
|
||||
* insert is rewritten into `INSERT INTO "T" SELECT * FROM "T" WHERE 0` which
|
||||
* inserts zero rows and returns zero rows for any `returning` clause.
|
||||
*/
|
||||
export class EmptyValuesNoopPlugin implements KyselyPlugin {
|
||||
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
||||
if (args.node.kind !== "InsertQueryNode" || !isEmptyInsert(args.node)) {
|
||||
return args.node;
|
||||
}
|
||||
|
||||
const { columns: _columns, ...node } = args.node;
|
||||
|
||||
return Object.freeze({
|
||||
...node,
|
||||
values: selectNothingFrom(args.node),
|
||||
});
|
||||
}
|
||||
|
||||
async transformResult(
|
||||
args: PluginTransformResultArgs,
|
||||
): Promise<QueryResult<UnknownRow>> {
|
||||
return args.result;
|
||||
}
|
||||
}
|
||||
|
||||
function isEmptyInsert(node: InsertQueryNode) {
|
||||
return (
|
||||
node.values !== undefined &&
|
||||
ValuesNode.is(node.values) &&
|
||||
node.values.values.length === 0
|
||||
);
|
||||
}
|
||||
|
||||
function selectNothingFrom(node: InsertQueryNode): SelectQueryNode {
|
||||
return Object.freeze({
|
||||
...SelectQueryNode.createFrom([node.into!]),
|
||||
selections: Object.freeze([SelectionNode.createSelectAll()]),
|
||||
where: WhereNode.create(RawNode.createWithSql("0")),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user