diff --git a/src/lib/normalize-statement.ts b/src/lib/normalize-statement.ts index c280c82..65871f3 100644 --- a/src/lib/normalize-statement.ts +++ b/src/lib/normalize-statement.ts @@ -1,4 +1,4 @@ -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'; @@ -6,7 +6,17 @@ import { sqlTag } from './sql-tag.js'; function isDrizzleStatement( statement: ReturningStatement ): statement is RunnableQuery { - 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( @@ -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.'); } }