Skip to content

append() doesn't work in neon-serverless driver #10

@toshimo

Description

@toshimo

Description

When using waddler/neon-serverless, SQL queries built with append() fail with a syntax error. The toSQL() method returns the correct SQL, but the query sent to the database only contains the initial SQL fragment.

Reproduction

import { waddler } from "waddler/neon-serverless";

const sql = waddler({ connection: "postgresql://...@...-pooler.neon.tech/..." });

// Works: direct template literal
await sql`WITH cte AS (SELECT 1 as num) SELECT * FROM cte`;  // ✅ OK

// Fails: using append()
const query = sql`WITH`;
query.append(sql` cte AS (SELECT 1 as num)`);
query.append(sql` SELECT * FROM cte`);

console.log(query.toSQL());
// → { sql: "WITH cte AS (SELECT 1 as num) SELECT * FROM cte", params: [] }  ✅ Correct

console.log((query as any).queryConfig.text);
// → "WITH"  ❌ Still the initial value!

await query;
// Error: syntax error at end of input (position: 5)

Root Cause

In NeonServerlessSQLTemplate, queryConfig.text is set once in the constructor and never updated after append() calls:

// Constructor - sets queryConfig.text once
constructor(sqlWrapper, client, dialect, configOptions, options) {
  // ...
  const query = this.sqlWrapper.getQuery(this.dialect).sql;
  this.queryConfig = {
    rowMode: "array",
    text: query,  // ← Fixed at construction time
    types: pgTypeConfig,
  };
}

// execute() - uses stale queryConfig
async execute() {
  const { sql: query, params } = this.sqlWrapper.getQuery(this.dialect);
  // query is correct here, but queryConfig.text is stale
  const queryResult = await this.client.query(this.queryConfig, params);
  // ↑ Sends the old SQL text
}

Proposed Fix

The fix is minimal - just update queryConfig.text and rawQueryConfig.text in execute() before sending the query:

async execute() {
  const { sql: query, params } = this.sqlWrapper.getQuery(this.dialect);
  this.queryConfig.text = query;      // ← Add this
  this.rawQueryConfig.text = query;   // ← Add this
  // ... rest unchanged
}

Happy to open a PR if this approach looks good.

Environment

  • waddler version: 0.1.1
  • Driver: waddler/neon-serverless
  • Database: Neon PostgreSQL (pooler connection)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions