Skip to content

Commit

Permalink
normalizeStatement fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 20, 2024
1 parent 7d7d88f commit 5b5dc82
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lib/normalize-statement.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { isSQLWrapper, type Query as DrizzleQuery } from 'drizzle-orm';
import { isSQLWrapper } from 'drizzle-orm';
import type { RunnableQuery } from 'drizzle-orm/runnable-query';
import type { ReturningStatement, Statement } from '../types.js';
import { sqlTag } from './sql-tag.js';

function isDrizzleStatement<Result = unknown>(
statement: ReturningStatement<Result>
): statement is RunnableQuery<Result[], 'sqlite'> {
return '_' in statement && isSQLWrapper(statement);
return isSQLWrapper(statement);
}

function isStatement(statement: unknown): statement is Statement {
return (
typeof statement === 'object' &&
statement !== null &&
'sql' in statement === true &&
typeof statement.sql === 'string' &&
'params' in statement === true
);
}

export function normalizeStatement(
Expand All @@ -18,9 +28,15 @@ export function normalizeStatement(

if (isDrizzleStatement(statement)) {
if ('toSQL' in statement && typeof statement.toSQL === 'function') {
return statement.toSQL() as DrizzleQuery;
const drizzleStatement = statement.toSQL();

if (isStatement(drizzleStatement)) {
return drizzleStatement;
} else {
throw new Error('The passed Drizzle statement could not be parsed.');
}
} else {
throw new Error('The passed Drizzle statement could not be parsed.');
throw new Error('The passed statement could not be parsed.');
}
}

Expand Down

0 comments on commit 5b5dc82

Please sign in to comment.