Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(db): keep every query working on the Neon 1.x driver

Nicholasfaace2e32225
diff --git a/api/_lib/db.js b/api/_lib/db.js
index ab88a7c2a..0bbe9e32b 100644
--- a/api/_lib/db.js
+++ b/api/_lib/db.js
@@ -62,6 +62,8 @@ function stripNul(v) {
// Brand identifying a composable fragment produced by this wrapper.
const FRAGMENT = Symbol('neonSqlFragment');
+// Builds (once) and returns the fragment's underlying Neon query object.
+const NATIVE = Symbol('neonSqlNative');
function isFragment(v) {
return v != null && typeof v === 'object' && v[FRAGMENT] === true;
@@ -95,7 +97,7 @@ function composeFragment(strings, values) {
// A fragment is a lazy, composable stand-in for a NeonQueryPromise. It holds the
// raw template pieces so a parent query can splice it, and only builds the
-// underlying Neon query (via the function form, which prepares params and stays
+// underlying Neon query (via `client.query`, which prepares params and stays
// lazy until awaited) the first time it is executed, inspected for its
// `parameterizedQuery`, or read for `opts` — i.e. when used standalone or inside
// `sql.transaction([...])`.
@@ -106,7 +108,7 @@ function makeFragment(strings, values) {
const { query, params } = composeFragment(strings, values);
const { sql: client, err } = getSqlSafe();
if (err) throw err;
- native = client(query, params);
+ native = client.query(query, params);
}
return native;
};
@@ -129,7 +131,8 @@ function makeFragment(strings, values) {
[Symbol.toStringTag]: 'NeonQueryPromise',
strings,
values,
- get parameterizedQuery() { return toNative().parameterizedQuery; },
+ [NATIVE]: toNative,
+ get parameterizedQuery() { return toNative().queryData; },
get opts() { return toNative().opts; },
then(onFulfilled, onRejected) { return settle().then(onFulfilled, onRejected); },
catch(onRejected) { return settle().catch(onRejected); },
@@ -339,11 +342,28 @@ export async function isStoragePressured() {
}
}
+// Neon 1.x `transaction()` only accepts its own query objects (an instanceof
+// check), so a fragment is unwrapped to the native query it builds before the
+// batch is handed over. Accepts both Neon forms: an array of queries, or a
+// function that receives the transaction client and returns one.
+function unwrapQueries(queries) {
+ return Array.isArray(queries) ? queries.map((q) => (isFragment(q) ? q[NATIVE]() : q)) : queries;
+}
+
+function transaction(queriesOrFn, opts) {
+ const client = getSql();
+ const batch = typeof queriesOrFn === 'function'
+ ? (txn) => unwrapQueries(queriesOrFn(txn))
+ : unwrapQueries(queriesOrFn);
+ return client.transaction(batch, opts);
+}
+
export const sql = /** @type {ReturnType<typeof import('@neondatabase/serverless').neon>} */ (new Proxy(function () {}, {
apply(_t, _this, args) {
- // Neon dispatches on the first argument: a string is the ordinary
- // function form `sql(queryText, params, opts)`; anything else is a
- // tagged-template call where the first arg is the strings array.
+ // A string first argument is the function form `sql(queryText, params, opts)`;
+ // anything else is a tagged-template call where the first arg is the strings
+ // array. Neon 1.x rejects the function form on the client itself, so both
+ // paths build their query through `client.query`, which it still accepts.
if (typeof args[0] === 'string') {
const [queryText, params, opts] = args;
const safeParams = Array.isArray(params) ? params.map(stripNul) : params;
@@ -352,11 +372,12 @@ export const sql = /** @type {ReturnType<typeof import('@neondatabase/serverless
// the same way the tagged-template fragment path does.
const { sql: client, err } = getSqlSafe();
if (err) return Promise.reject(err);
- return client(queryText, safeParams, opts);
+ return client.query(queryText, safeParams, opts);
}
return makeFragment(args[0], args.slice(1));
},
get(_t, prop) {
+ if (prop === 'transaction') return transaction;
return getSql()[prop];
},
}));
diff --git a/scripts/apply-siwx-migration.mjs b/scripts/apply-siwx-migration.mjs
index fdaeae896..3bb7918b5 100644
--- a/scripts/apply-siwx-migration.mjs
+++ b/scripts/apply-siwx-migration.mjs
@@ -58,7 +58,7 @@ const statements = text
.filter((s) => s.length > 0);
for (const s of statements) {
- await sql(s, [], {});
+ await sql.query(s, [], {});
console.log('OK:', s.slice(0, 80).replace(/\s+/g, ' '));
}
console.log('siwx tables ready');
diff --git a/scripts/run-migrations.mjs b/scripts/run-migrations.mjs
index 900423927..941cf14c8 100644
--- a/scripts/run-migrations.mjs
+++ b/scripts/run-migrations.mjs
@@ -161,7 +161,7 @@ async function main() {
for (const stmt of statements) {
const body = stmt.replace(/--[^\n]*/g, '').trim();
if (body) {
- await sql(stmt);
+ await sql.query(stmt);
}
}
const sha256 = createHash('sha256').update(sqlText).digest('hex');
diff --git a/tests/db-sql-compose.test.js b/tests/db-sql-compose.test.js
index 5bca35386..989183e9e 100644
--- a/tests/db-sql-compose.test.js
+++ b/tests/db-sql-compose.test.js
@@ -5,6 +5,7 @@
// positional parameter and emits invalid SQL (`… 2) $3 $4 …` → Postgres
// `syntax error at or near "$3"`). Our wrapper flattens fragments inline and
// renumbers placeholders. These tests assert the generated `parameterizedQuery`
+// (Neon 1.x `queryData`: params stay as bound values until the query runs)
// without touching a database — DATABASE_URL only needs to be a syntactically
// valid connection string so neon() can instantiate lazily.
@@ -21,7 +22,7 @@ describe('sql fragment composition', () => {
it('builds a plain parameterized query with no fragments', () => {
expect(sql`select * from t where id = ${5}`.parameterizedQuery).toEqual({
query: 'select * from t where id = $1',
- params: ['5'],
+ params: [5],
});
});
@@ -45,7 +46,7 @@ describe('sql fragment composition', () => {
`.parameterizedQuery;
// Every placeholder is sequential and contiguous — no bare `$N` gaps.
- expect(params).toEqual(['mainnet', '2', 'prime', '30', '50']);
+ expect(params).toEqual(['mainnet', 2, 'prime', 30, 50]);
expect(query).toContain('c.network = $1');
expect(query).toContain('o.ath_multiple >= $2');
expect(query).toContain('c.tier = $3');
@@ -74,15 +75,16 @@ describe('sql fragment composition', () => {
);
expect(sql`update t ${setClause} where id = ${'x'} returning *`.parameterizedQuery).toEqual({
query: 'update t set avatar_id = $1, price_amount = $2, price_currency = $3 where id = $4 returning *',
- params: ['av1', '100', 'USDC', 'x'],
+ params: ['av1', 100, 'USDC', 'x'],
});
});
it('keeps fragments compatible with sql.transaction([...])', () => {
const f = sql`insert into t(a) values (${1})`;
- // Neon's transaction() rejects anything whose toStringTag !== 'NeonQueryPromise'.
+ // The wrapper unwraps fragments to native Neon queries before transaction(),
+ // and the tag keeps code that sniffs for a Neon query recognising one.
expect(Object.prototype.toString.call(f)).toBe('[object NeonQueryPromise]');
- expect(f.parameterizedQuery).toEqual({ query: 'insert into t(a) values ($1)', params: ['1'] });
+ expect(f.parameterizedQuery).toEqual({ query: 'insert into t(a) values ($1)', params: [1] });
expect(f.opts).toBeUndefined();
});
@@ -101,9 +103,9 @@ describe('sql fragment composition', () => {
});
it('passes ordinary function-form calls straight through', () => {
- expect(sql('select $1::int', [7]).parameterizedQuery).toEqual({
+ expect(sql('select $1::int', [7]).queryData).toEqual({
query: 'select $1::int',
- params: ['7'],
+ params: [7],
});
});
});
@@ -113,7 +115,7 @@ describe('sqlValues — multi-row VALUES fragment', () => {
const { query, params } = sql`insert into t (a, b) values ${sqlValues([[1, 'x']])}`
.parameterizedQuery;
expect(query).toBe('insert into t (a, b) values ($1, $2)');
- expect(params).toEqual(['1', 'x']);
+ expect(params).toEqual([1, 'x']);
});
it('renders multiple rows with contiguous, renumbered placeholders', () => {
@@ -123,7 +125,7 @@ describe('sqlValues — multi-row VALUES fragment', () => {
[3, 'z'],
])}`.parameterizedQuery;
expect(query).toBe('insert into t (a, b) values ($1, $2), ($3, $4), ($5, $6)');
- expect(params).toEqual(['1', 'x', '2', 'y', '3', 'z']);
+ expect(params).toEqual([1, 'x', 2, 'y', 3, 'z']);
});
it('renumbers against a parent query that also binds params', () => {
@@ -148,7 +150,7 @@ describe('sqlValues — multi-row VALUES fragment', () => {
// exact failure mode (`syntax error at or near ":"`) the helper prevents.
expect(query).not.toContain(':');
expect(params[0]).toBe('x');
- expect(String(params[1])).toContain('2026-06-20');
+ expect(params[1]).toBe(ts);
});
it('rejects empty input and ragged rows', () => {