@syncular/server 0.82.0 → 0.82.1
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.
- package/dist/authoritative-query.js +3 -1
- package/dist/operations.js +3 -6
- package/dist/postgres-storage.js +6 -6
- package/dist/relational-rows.d.ts +3 -1
- package/dist/relational-rows.js +4 -2
- package/package.json +2 -2
- package/src/authoritative-query.ts +3 -1
- package/src/operations.ts +3 -7
- package/src/postgres-storage.ts +11 -6
- package/src/relational-rows.ts +4 -2
|
@@ -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 = [];
|
package/dist/operations.js
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
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
|
package/dist/postgres-storage.js
CHANGED
|
@@ -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
|
|
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
|
: []),
|
|
@@ -1547,7 +1547,7 @@ class PostgresTransaction {
|
|
|
1547
1547
|
partition, idempotency_key, type, version, payload,
|
|
1548
1548
|
source_client_id, source_client_commit_id, source_commit_seq,
|
|
1549
1549
|
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)`, [
|
|
1550
|
+
) VALUES ($1,$2,$3,$4,$5::text::jsonb,$6,$7,$8,$9,$9,'pending',0,$10)`, [
|
|
1551
1551
|
this.#partition,
|
|
1552
1552
|
reaction.idempotencyKey,
|
|
1553
1553
|
reaction.type,
|
|
@@ -2265,7 +2265,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2265
2265
|
}
|
|
2266
2266
|
async failReaction(partition, idempotencyKey, update) {
|
|
2267
2267
|
const result = await this.#exec.query(`UPDATE sync_reactions
|
|
2268
|
-
SET status=$4, available_at_ms=$5, last_failure=$6,
|
|
2268
|
+
SET status=$4, available_at_ms=$5, last_failure=$6::text::jsonb,
|
|
2269
2269
|
lease_owner=NULL, lease_expires_at_ms=NULL
|
|
2270
2270
|
WHERE partition=$1 AND idempotency_key=$2
|
|
2271
2271
|
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
|
/**
|
package/dist/relational-rows.js
CHANGED
|
@@ -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((
|
|
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.
|
|
3
|
+
"version": "0.82.1",
|
|
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.
|
|
71
|
+
"@syncular/core": "0.82.1"
|
|
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
|
-
|
|
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(
|
|
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 =
|
package/src/postgres-storage.ts
CHANGED
|
@@ -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
|
|
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) =>
|
|
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(
|
|
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
|
: []),
|
|
@@ -2278,7 +2283,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
2278
2283
|
partition, idempotency_key, type, version, payload,
|
|
2279
2284
|
source_client_id, source_client_commit_id, source_commit_seq,
|
|
2280
2285
|
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)`,
|
|
2286
|
+
) VALUES ($1,$2,$3,$4,$5::text::jsonb,$6,$7,$8,$9,$9,'pending',0,$10)`,
|
|
2282
2287
|
[
|
|
2283
2288
|
this.#partition,
|
|
2284
2289
|
reaction.idempotencyKey,
|
|
@@ -3352,7 +3357,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
3352
3357
|
): Promise<boolean> {
|
|
3353
3358
|
const result = await this.#exec.query(
|
|
3354
3359
|
`UPDATE sync_reactions
|
|
3355
|
-
SET status=$4, available_at_ms=$5, last_failure=$6,
|
|
3360
|
+
SET status=$4, available_at_ms=$5, last_failure=$6::text::jsonb,
|
|
3356
3361
|
lease_owner=NULL, lease_expires_at_ms=NULL
|
|
3357
3362
|
WHERE partition=$1 AND idempotency_key=$2
|
|
3358
3363
|
AND status='leased' AND lease_owner=$3
|
package/src/relational-rows.ts
CHANGED
|
@@ -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((
|
|
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
|