Skip to content

Commit

Permalink
Fix build error when drizzle-orm is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 31, 2024
1 parent 8017f99 commit 3e48665
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib/normalize-statement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isSQLWrapper } from 'drizzle-orm';
import type { RunnableQuery as DrizzleQuery } from 'drizzle-orm/runnable-query';
import type { SqliteRemoteResult } from 'drizzle-orm/sqlite-proxy';
import type { StatementInput, Statement } from '../types.js';
Expand All @@ -10,7 +9,12 @@ function isDrizzleStatement<Result = unknown>(
Result extends SqliteRemoteResult<unknown> ? any : Result[],
'sqlite'
> {
return isSQLWrapper(statement);
return (
typeof statement === 'object' &&
statement !== null &&
'getSQL' in statement &&
typeof statement.getSQL === 'function'
);
}

function isStatement(statement: unknown): statement is Statement {
Expand All @@ -29,15 +33,16 @@ export function normalizeStatement(statement: StatementInput): Statement {
}

if (isDrizzleStatement(statement)) {
if ('toSQL' in statement && typeof statement.toSQL === 'function') {
try {
if (!('toSQL' in statement && typeof statement.toSQL === 'function')) {
throw 1;
}
const drizzleStatement = statement.toSQL();

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

0 comments on commit 3e48665

Please sign in to comment.