Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions deco-llm/server/db/postgres.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* PostgreSQL Database Module
*
* Generic SQL runner using the DATABASE binding.
*/

import type { Env } from "../main.ts";

/**
* Run a SQL query using the DATABASE binding
* @param env - The environment containing the DATABASE binding
* @param sql - SQL query with ? placeholders
* @param params - Parameters to substitute for ? placeholders
* @returns The query results as an array of rows
*/
export async function runSQL<T = unknown>(
env: Env,
sql: string,
params: unknown[] = [],
): Promise<T[]> {
// Defensive: some DATABASE bindings may interpolate params into SQL literals.
// Ensure single quotes inside string params don't break the query.
const sanitizedParams = params.map((p) => {
if (typeof p === "string") return p.replaceAll("'", "''");
return p;
});
const response =
await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Missing optional chaining on DATABASE. If state exists but DATABASE is undefined, accessing .DATABASES_RUN_SQL() will throw.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At deco-llm/server/db/postgres.ts, line 28:

<comment>Missing optional chaining on `DATABASE`. If `state` exists but `DATABASE` is undefined, accessing `.DATABASES_RUN_SQL()` will throw.</comment>

<file context>
@@ -0,0 +1,33 @@
+    return p;
+  });
+  const response =
+    await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
+      sql,
+      params: sanitizedParams,
</file context>
Suggested change
await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
await env.MESH_REQUEST_CONTEXT?.state?.DATABASE?.DATABASES_RUN_SQL({

sql,
params: sanitizedParams,
});
return (response.result[0]?.results ?? []) as T[];
}
Loading
Loading