@syncular/server 0.82.0 → 0.82.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -82,10 +82,12 @@ export function prepareAuthoritativeQuery(plan, params, declaredTables, tables)
82
82
  throw new Error('registered query targets an unknown table');
83
83
  if (!table.materialize)
84
84
  throw new Error('registered query targets a non-materialized table');
85
+ // A `json` column reads as its JSON text on every storage: PostgreSQL
86
+ // stores it as JSONB, which its drivers would return parsed.
85
87
  return {
86
88
  start: relation.start,
87
89
  end: relation.end,
88
- text: `(SELECT ${table.columns.map((column) => quoteIdent(column.name)).join(', ')} FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?)${relation.alias === undefined ? ` AS ${quoteIdent(table.name)}` : ''}`,
90
+ text: `(SELECT ${table.columns.map((column) => (column.type === 'json' ? `CAST(${quoteIdent(column.name)} AS TEXT) AS ${quoteIdent(column.name)}` : quoteIdent(column.name))).join(', ')} FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?)${relation.alias === undefined ? ` AS ${quoteIdent(table.name)}` : ''}`,
89
91
  };
90
92
  });
91
93
  const bound = [];
@@ -57,15 +57,12 @@ function normalizeQueryRows(rows, columns) {
57
57
  throw syncError('operation.query_failed', 'registered query returned an invalid boolean');
58
58
  }
59
59
  break;
60
- case 'json': {
61
- const json = typeof value === 'string' ? value : JSON.stringify(value);
62
- if (json === undefined) {
60
+ case 'json':
61
+ if (typeof value !== 'string') {
63
62
  throw syncError('operation.query_failed', 'registered query returned invalid JSON');
64
63
  }
65
- JSON.parse(json);
66
- normalized[column.name] = json;
64
+ JSON.parse(value);
67
65
  break;
68
- }
69
66
  case 'bytes':
70
67
  case 'crdt': {
71
68
  const bytes = value instanceof Uint8Array
@@ -672,11 +672,11 @@ async function rewriteRowsOn(q, table, oldLayout, rekey) {
672
672
  // database) every statement is a round trip, and the bump holds the
673
673
  // migration lock for its whole duration. The page's rows bind as one
674
674
  // VALUES list with typed placeholders (`rewriteValues` order: projection
675
- // columns, re-keyed scopes, payload, partition, row id). The scopes bind as
676
- // text before the JSONB cast (see `scanRowBatchSql`).
675
+ // columns, re-keyed scopes, payload, partition, row id). The scopes and
676
+ // `json` columns bind as text before the JSONB cast (see `scanRowBatchSql`).
677
677
  const projected = table.materialize ? table.columns : [];
678
678
  const types = [
679
- ...projected.map((column) => columnSqlType(column, 'postgres')),
679
+ ...projected.map((column) => column.type === 'json' ? 'TEXT' : columnSqlType(column, 'postgres')),
680
680
  ...(rekey ? ['TEXT'] : []),
681
681
  'BYTEA',
682
682
  'TEXT',
@@ -684,7 +684,7 @@ async function rewriteRowsOn(q, table, oldLayout, rekey) {
684
684
  ];
685
685
  const payloadAt = projected.length + (rekey ? 1 : 0);
686
686
  const sets = [
687
- ...projected.map((column, i) => `${quoteIdent(column.name)}=v.c${i}`),
687
+ ...projected.map((column, i) => `${quoteIdent(column.name)}=v.c${i}${column.type === 'json' ? '::jsonb' : ''}`),
688
688
  ...(rekey
689
689
  ? [`${quoteIdent(SYNC_SCOPES_COLUMN)}=v.c${projected.length}::jsonb`]
690
690
  : []),
@@ -1153,6 +1153,26 @@ class PostgresTransaction {
1153
1153
  })));
1154
1154
  return cursors.find((cursor) => cursor !== undefined);
1155
1155
  }
1156
+ /**
1157
+ * The executor of a read over `tables`: it writes the pending writes first
1158
+ * only when one of them is a row of those tables or a change of the commit
1159
+ * log to them (§6.4). A read of other tables runs without them, so it takes
1160
+ * no savepoint and moves no write ahead of itself.
1161
+ */
1162
+ #readOf(tables) {
1163
+ return {
1164
+ query: (text, params) => {
1165
+ const appends = this.#stagedAppend === undefined
1166
+ ? this.#closedAppends
1167
+ : [...this.#closedAppends, this.#stagedAppend];
1168
+ return tables.some((table) => this.#staged.has(table) ||
1169
+ this.#closedRows.has(table) ||
1170
+ appends.some(({ commit }) => commit.changes.some((change) => change.table === table)))
1171
+ ? this.#client.query(text, params)
1172
+ : this.#serial.query(text, params);
1173
+ },
1174
+ };
1175
+ }
1156
1176
  /** The candidate row this transaction staged and has not yet written. */
1157
1177
  #stagedRow(table, rowId) {
1158
1178
  return (this.#staged.get(table)?.get(rowId)?.row ??
@@ -1211,7 +1231,10 @@ class PostgresTransaction {
1211
1231
  }
1212
1232
  if (specs.length === 0)
1213
1233
  return;
1214
- const { rows } = await this.#client.query(prefetchRowsBatchSql(tables), [this.#partition, JSON.stringify(specs)]);
1234
+ const { rows } = await this.#readOf(specs.map((spec) => spec.tbl)).query(prefetchRowsBatchSql(tables), [
1235
+ this.#partition,
1236
+ JSON.stringify(specs),
1237
+ ]);
1215
1238
  this.#recordPrefetched(rows);
1216
1239
  }
1217
1240
  /** Answer the following `getRow`/`getTombstone` calls from a read snapshot. */
@@ -1229,7 +1252,7 @@ class PostgresTransaction {
1229
1252
  const prefetched = this.#prefetchedTombstones.get(`${table}\u0000${rowId}`);
1230
1253
  if (prefetched !== undefined)
1231
1254
  return prefetched ?? undefined;
1232
- const { rows } = await this.#client.query('SELECT commit_seq, server_version, scopes FROM sync_tombstones WHERE partition=$1 AND tbl=$2 AND row_id=$3', [this.#partition, table, rowId]);
1255
+ const { rows } = await this.#readOf([table]).query('SELECT commit_seq, server_version, scopes FROM sync_tombstones WHERE partition=$1 AND tbl=$2 AND row_id=$3', [this.#partition, table, rowId]);
1233
1256
  const row = rows[0];
1234
1257
  return (toTombstone(row?.commit_seq, row?.server_version, row?.scopes) ??
1235
1258
  undefined);
@@ -1241,7 +1264,8 @@ class PostgresTransaction {
1241
1264
  }
1242
1265
  async scanRows(query) {
1243
1266
  this.#assertOpen();
1244
- const variable = await chooseScanVariable(this.#partition, query, this.#scanVariables, (probes, cap) => this.#client
1267
+ const client = this.#readOf([query.table]);
1268
+ const variable = await chooseScanVariable(this.#partition, query, this.#scanVariables, (probes, cap) => client
1245
1269
  .query(scanProbeSql('postgres'), [
1246
1270
  this.#partition,
1247
1271
  JSON.stringify(probes.map((probe) => ({ ...probe, lim: cap }))),
@@ -1255,7 +1279,7 @@ class PostgresTransaction {
1255
1279
  let afterRowId = query.afterRowId ?? '';
1256
1280
  const batchSize = Math.max(64, query.limit);
1257
1281
  while (rows.length < query.limit) {
1258
- const { rows: records } = await this.#client.query(sql, [
1282
+ const { rows: records } = await client.query(sql, [
1259
1283
  this.#partition,
1260
1284
  query.table,
1261
1285
  variable,
@@ -1283,16 +1307,19 @@ class PostgresTransaction {
1283
1307
  }
1284
1308
  scanRowsByIndex(query) {
1285
1309
  this.#assertOpen();
1286
- return scanRowsByIndexOn(this.#client, this.#resolveTable(query.table), this.#partition, query);
1310
+ return scanRowsByIndexOn(this.#readOf([query.table]), this.#resolveTable(query.table), this.#partition, query);
1287
1311
  }
1288
1312
  async queryAuthoritative(query) {
1289
1313
  this.#assertOpen();
1290
1314
  // Runs on this transaction's pinned client, so the read observes every
1291
- // staged row and never waits for a second pool connection.
1315
+ // staged row and never waits for a second pool connection. The prepared
1316
+ // statement reads exactly `query.tables`: every relation of the plan is
1317
+ // one of them, rewritten to a derived relation over its own table.
1292
1318
  const prepared = this.#prepareQuery(query);
1293
- const result = await this.#client.query(postgresPlaceholders(prepared.sql), prepared.params);
1319
+ const result = await this.#readOf(query.tables).query(postgresPlaceholders(prepared.sql), prepared.params);
1294
1320
  // Under the partition lock the cursor is the locked `max_commit_seq` plus
1295
- // this transaction's appends, which the query above flushed: no read.
1321
+ // this transaction's appends: no read. An append still held back changed
1322
+ // no table the query read, so the rows are those of that cursor.
1296
1323
  if (this.#nextCommitSeq !== undefined) {
1297
1324
  return { rows: result.rows, maxCommitSeq: this.#nextCommitSeq - 1 };
1298
1325
  }
@@ -1547,7 +1574,7 @@ class PostgresTransaction {
1547
1574
  partition, idempotency_key, type, version, payload,
1548
1575
  source_client_id, source_client_commit_id, source_commit_seq,
1549
1576
  created_at_ms, available_at_ms, status, attempts, max_attempts
1550
- ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$9,'pending',0,$10)`, [
1577
+ ) VALUES ($1,$2,$3,$4,$5::text::jsonb,$6,$7,$8,$9,$9,'pending',0,$10)`, [
1551
1578
  this.#partition,
1552
1579
  reaction.idempotencyKey,
1553
1580
  reaction.type,
@@ -2265,7 +2292,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
2265
2292
  }
2266
2293
  async failReaction(partition, idempotencyKey, update) {
2267
2294
  const result = await this.#exec.query(`UPDATE sync_reactions
2268
- SET status=$4, available_at_ms=$5, last_failure=$6,
2295
+ SET status=$4, available_at_ms=$5, last_failure=$6::text::jsonb,
2269
2296
  lease_owner=NULL, lease_expires_at_ms=NULL
2270
2297
  WHERE partition=$1 AND idempotency_key=$2
2271
2298
  AND status='leased' AND lease_owner=$3
@@ -179,7 +179,9 @@ export declare function upsertValues(table: CompiledTable, partition: string, ro
179
179
  /**
180
180
  * Upsert statement text (bind with `upsertValues`). PostgreSQL takes `rows`
181
181
  * rows in one statement, bound as consecutive `upsertValues` lists from
182
- * parameter `offset + 1` on; their row ids must be distinct.
182
+ * parameter `offset + 1` on; their row ids must be distinct. A JSONB column
183
+ * binds as text before the JSONB cast, so a driver that JSON-encodes a string
184
+ * bound to `jsonb` (Bun.sql) cannot store the document as a JSON string.
183
185
  */
184
186
  export declare function upsertSql(table: CompiledTable, dialect: RelationalDialect, rows?: number, offset?: number): string;
185
187
  /**
@@ -468,14 +468,16 @@ export function upsertValues(table, partition, row, dialect) {
468
468
  /**
469
469
  * Upsert statement text (bind with `upsertValues`). PostgreSQL takes `rows`
470
470
  * rows in one statement, bound as consecutive `upsertValues` lists from
471
- * parameter `offset + 1` on; their row ids must be distinct.
471
+ * parameter `offset + 1` on; their row ids must be distinct. A JSONB column
472
+ * binds as text before the JSONB cast, so a driver that JSON-encodes a string
473
+ * bound to `jsonb` (Bun.sql) cannot store the document as a JSON string.
472
474
  */
473
475
  export function upsertSql(table, dialect, rows = 1, offset = 0) {
474
476
  const names = tableColumnNames(table);
475
477
  const quoted = names.map((name) => quoteIdent(name)).join(', ');
476
478
  const values = dialect === 'sqlite'
477
479
  ? `(${new Array(names.length).fill('?').join(',')})`
478
- : Array.from({ length: rows }, (_, row) => `(${names.map((_, column) => `$${offset + row * names.length + column + 1}`).join(',')})`).join(',');
480
+ : Array.from({ length: rows }, (_, row) => `(${names.map((name, column) => `$${offset + row * names.length + column + 1}${name === SYNC_SCOPES_COLUMN || table.columns.some((c) => c.name === name && c.type === 'json') ? '::text::jsonb' : ''}`).join(',')})`).join(',');
479
481
  const updates = names
480
482
  .slice(2) // partition + row id are the conflict key
481
483
  .map((name) => `${quoteIdent(name)}=excluded.${quoteIdent(name)}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/server",
3
- "version": "0.82.0",
3
+ "version": "0.82.2",
4
4
  "description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -68,7 +68,7 @@
68
68
  "verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
69
69
  },
70
70
  "dependencies": {
71
- "@syncular/core": "0.82.0"
71
+ "@syncular/core": "0.82.2"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@electric-sql/pglite": "^0.5.4"
@@ -127,10 +127,12 @@ export function prepareAuthoritativeQuery(
127
127
  throw new Error('registered query targets an unknown table');
128
128
  if (!table.materialize)
129
129
  throw new Error('registered query targets a non-materialized table');
130
+ // A `json` column reads as its JSON text on every storage: PostgreSQL
131
+ // stores it as JSONB, which its drivers would return parsed.
130
132
  return {
131
133
  start: relation.start,
132
134
  end: relation.end,
133
- text: `(SELECT ${table.columns.map((column) => quoteIdent(column.name)).join(', ')} FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?)${relation.alias === undefined ? ` AS ${quoteIdent(table.name)}` : ''}`,
135
+ text: `(SELECT ${table.columns.map((column) => (column.type === 'json' ? `CAST(${quoteIdent(column.name)} AS TEXT) AS ${quoteIdent(column.name)}` : quoteIdent(column.name))).join(', ')} FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?)${relation.alias === undefined ? ` AS ${quoteIdent(table.name)}` : ''}`,
134
136
  };
135
137
  });
136
138
  const bound: (AuthoritativeQueryValue | typeof PARTITION_BIND)[] = [];
package/src/operations.ts CHANGED
@@ -282,19 +282,15 @@ function normalizeQueryRows(
282
282
  );
283
283
  }
284
284
  break;
285
- case 'json': {
286
- const json =
287
- typeof value === 'string' ? value : JSON.stringify(value);
288
- if (json === undefined) {
285
+ case 'json':
286
+ if (typeof value !== 'string') {
289
287
  throw syncError(
290
288
  'operation.query_failed',
291
289
  'registered query returned invalid JSON',
292
290
  );
293
291
  }
294
- JSON.parse(json);
295
- normalized[column.name] = json;
292
+ JSON.parse(value);
296
293
  break;
297
- }
298
294
  case 'bytes':
299
295
  case 'crdt': {
300
296
  const bytes =
@@ -1029,11 +1029,13 @@ async function rewriteRowsOn(
1029
1029
  // database) every statement is a round trip, and the bump holds the
1030
1030
  // migration lock for its whole duration. The page's rows bind as one
1031
1031
  // VALUES list with typed placeholders (`rewriteValues` order: projection
1032
- // columns, re-keyed scopes, payload, partition, row id). The scopes bind as
1033
- // text before the JSONB cast (see `scanRowBatchSql`).
1032
+ // columns, re-keyed scopes, payload, partition, row id). The scopes and
1033
+ // `json` columns bind as text before the JSONB cast (see `scanRowBatchSql`).
1034
1034
  const projected = table.materialize ? table.columns : [];
1035
1035
  const types = [
1036
- ...projected.map((column) => columnSqlType(column, 'postgres')),
1036
+ ...projected.map((column) =>
1037
+ column.type === 'json' ? 'TEXT' : columnSqlType(column, 'postgres'),
1038
+ ),
1037
1039
  ...(rekey ? ['TEXT'] : []),
1038
1040
  'BYTEA',
1039
1041
  'TEXT',
@@ -1041,7 +1043,10 @@ async function rewriteRowsOn(
1041
1043
  ];
1042
1044
  const payloadAt = projected.length + (rekey ? 1 : 0);
1043
1045
  const sets = [
1044
- ...projected.map((column, i) => `${quoteIdent(column.name)}=v.c${i}`),
1046
+ ...projected.map(
1047
+ (column, i) =>
1048
+ `${quoteIdent(column.name)}=v.c${i}${column.type === 'json' ? '::jsonb' : ''}`,
1049
+ ),
1045
1050
  ...(rekey
1046
1051
  ? [`${quoteIdent(SYNC_SCOPES_COLUMN)}=v.c${projected.length}::jsonb`]
1047
1052
  : []),
@@ -1743,6 +1748,33 @@ class PostgresTransaction implements StorageTransaction {
1743
1748
  return cursors.find((cursor) => cursor !== undefined);
1744
1749
  }
1745
1750
 
1751
+ /**
1752
+ * The executor of a read over `tables`: it writes the pending writes first
1753
+ * only when one of them is a row of those tables or a change of the commit
1754
+ * log to them (§6.4). A read of other tables runs without them, so it takes
1755
+ * no savepoint and moves no write ahead of itself.
1756
+ */
1757
+ #readOf(tables: readonly string[]): PgQueryable {
1758
+ return {
1759
+ query: (text, params) => {
1760
+ const appends =
1761
+ this.#stagedAppend === undefined
1762
+ ? this.#closedAppends
1763
+ : [...this.#closedAppends, this.#stagedAppend];
1764
+ return tables.some(
1765
+ (table) =>
1766
+ this.#staged.has(table) ||
1767
+ this.#closedRows.has(table) ||
1768
+ appends.some(({ commit }) =>
1769
+ commit.changes.some((change) => change.table === table),
1770
+ ),
1771
+ )
1772
+ ? this.#client.query(text, params)
1773
+ : this.#serial.query(text, params);
1774
+ },
1775
+ };
1776
+ }
1777
+
1746
1778
  /** The candidate row this transaction staged and has not yet written. */
1747
1779
  #stagedRow(table: string, rowId: string): StoredRow | undefined {
1748
1780
  return (
@@ -1817,10 +1849,12 @@ class PostgresTransaction implements StorageTransaction {
1817
1849
  specs.push({ tbl: target.table, ids });
1818
1850
  }
1819
1851
  if (specs.length === 0) return;
1820
- const { rows } = await this.#client.query<PrefetchRow>(
1821
- prefetchRowsBatchSql(tables),
1822
- [this.#partition, JSON.stringify(specs)],
1823
- );
1852
+ const { rows } = await this.#readOf(
1853
+ specs.map((spec) => spec.tbl),
1854
+ ).query<PrefetchRow>(prefetchRowsBatchSql(tables), [
1855
+ this.#partition,
1856
+ JSON.stringify(specs),
1857
+ ]);
1824
1858
  this.#recordPrefetched(rows);
1825
1859
  }
1826
1860
 
@@ -1852,7 +1886,7 @@ class PostgresTransaction implements StorageTransaction {
1852
1886
  this.#assertOpen();
1853
1887
  const prefetched = this.#prefetchedTombstones.get(`${table}\u0000${rowId}`);
1854
1888
  if (prefetched !== undefined) return prefetched ?? undefined;
1855
- const { rows } = await this.#client.query<{
1889
+ const { rows } = await this.#readOf([table]).query<{
1856
1890
  commit_seq: unknown;
1857
1891
  server_version: unknown;
1858
1892
  scopes: unknown;
@@ -1879,12 +1913,13 @@ class PostgresTransaction implements StorageTransaction {
1879
1913
 
1880
1914
  async scanRows(query: RowScanQuery): Promise<StoredRow[]> {
1881
1915
  this.#assertOpen();
1916
+ const client = this.#readOf([query.table]);
1882
1917
  const variable = await chooseScanVariable(
1883
1918
  this.#partition,
1884
1919
  query,
1885
1920
  this.#scanVariables,
1886
1921
  (probes, cap) =>
1887
- this.#client
1922
+ client
1888
1923
  .query<{ sub: unknown; n: unknown }>(scanProbeSql('postgres'), [
1889
1924
  this.#partition,
1890
1925
  JSON.stringify(probes.map((probe) => ({ ...probe, lim: cap }))),
@@ -1902,7 +1937,7 @@ class PostgresTransaction implements StorageTransaction {
1902
1937
  let afterRowId = query.afterRowId ?? '';
1903
1938
  const batchSize = Math.max(64, query.limit);
1904
1939
  while (rows.length < query.limit) {
1905
- const { rows: records } = await this.#client.query<RowRecord>(sql, [
1940
+ const { rows: records } = await client.query<RowRecord>(sql, [
1906
1941
  this.#partition,
1907
1942
  query.table,
1908
1943
  variable,
@@ -1927,7 +1962,7 @@ class PostgresTransaction implements StorageTransaction {
1927
1962
  scanRowsByIndex(query: IndexRowScanQuery): Promise<StoredRow[]> {
1928
1963
  this.#assertOpen();
1929
1964
  return scanRowsByIndexOn(
1930
- this.#client,
1965
+ this.#readOf([query.table]),
1931
1966
  this.#resolveTable(query.table),
1932
1967
  this.#partition,
1933
1968
  query,
@@ -1939,14 +1974,16 @@ class PostgresTransaction implements StorageTransaction {
1939
1974
  ): Promise<AuthoritativeQueryResult> {
1940
1975
  this.#assertOpen();
1941
1976
  // Runs on this transaction's pinned client, so the read observes every
1942
- // staged row and never waits for a second pool connection.
1977
+ // staged row and never waits for a second pool connection. The prepared
1978
+ // statement reads exactly `query.tables`: every relation of the plan is
1979
+ // one of them, rewritten to a derived relation over its own table.
1943
1980
  const prepared = this.#prepareQuery(query);
1944
- const result = await this.#client.query<Readonly<Record<string, unknown>>>(
1945
- postgresPlaceholders(prepared.sql),
1946
- prepared.params,
1947
- );
1981
+ const result = await this.#readOf(query.tables).query<
1982
+ Readonly<Record<string, unknown>>
1983
+ >(postgresPlaceholders(prepared.sql), prepared.params);
1948
1984
  // Under the partition lock the cursor is the locked `max_commit_seq` plus
1949
- // this transaction's appends, which the query above flushed: no read.
1985
+ // this transaction's appends: no read. An append still held back changed
1986
+ // no table the query read, so the rows are those of that cursor.
1950
1987
  if (this.#nextCommitSeq !== undefined) {
1951
1988
  return { rows: result.rows, maxCommitSeq: this.#nextCommitSeq - 1 };
1952
1989
  }
@@ -2278,7 +2315,7 @@ class PostgresTransaction implements StorageTransaction {
2278
2315
  partition, idempotency_key, type, version, payload,
2279
2316
  source_client_id, source_client_commit_id, source_commit_seq,
2280
2317
  created_at_ms, available_at_ms, status, attempts, max_attempts
2281
- ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$9,'pending',0,$10)`,
2318
+ ) VALUES ($1,$2,$3,$4,$5::text::jsonb,$6,$7,$8,$9,$9,'pending',0,$10)`,
2282
2319
  [
2283
2320
  this.#partition,
2284
2321
  reaction.idempotencyKey,
@@ -3352,7 +3389,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
3352
3389
  ): Promise<boolean> {
3353
3390
  const result = await this.#exec.query(
3354
3391
  `UPDATE sync_reactions
3355
- SET status=$4, available_at_ms=$5, last_failure=$6,
3392
+ SET status=$4, available_at_ms=$5, last_failure=$6::text::jsonb,
3356
3393
  lease_owner=NULL, lease_expires_at_ms=NULL
3357
3394
  WHERE partition=$1 AND idempotency_key=$2
3358
3395
  AND status='leased' AND lease_owner=$3
@@ -570,7 +570,9 @@ export function upsertValues(
570
570
  /**
571
571
  * Upsert statement text (bind with `upsertValues`). PostgreSQL takes `rows`
572
572
  * rows in one statement, bound as consecutive `upsertValues` lists from
573
- * parameter `offset + 1` on; their row ids must be distinct.
573
+ * parameter `offset + 1` on; their row ids must be distinct. A JSONB column
574
+ * binds as text before the JSONB cast, so a driver that JSON-encodes a string
575
+ * bound to `jsonb` (Bun.sql) cannot store the document as a JSON string.
574
576
  */
575
577
  export function upsertSql(
576
578
  table: CompiledTable,
@@ -586,7 +588,7 @@ export function upsertSql(
586
588
  : Array.from(
587
589
  { length: rows },
588
590
  (_, row) =>
589
- `(${names.map((_, column) => `$${offset + row * names.length + column + 1}`).join(',')})`,
591
+ `(${names.map((name, column) => `$${offset + row * names.length + column + 1}${name === SYNC_SCOPES_COLUMN || table.columns.some((c) => c.name === name && c.type === 'json') ? '::text::jsonb' : ''}`).join(',')})`,
590
592
  ).join(',');
591
593
  const updates = names
592
594
  .slice(2) // partition + row id are the conflict key