@syncular/server 0.79.0 → 0.79.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.
- package/dist/postgres-storage.js +29 -17
- package/dist/relational-rows.d.ts +1 -1
- package/dist/relational-rows.js +2 -2
- package/package.json +2 -2
- package/src/postgres-storage.ts +40 -29
- package/src/relational-rows.ts +2 -2
package/dist/postgres-storage.js
CHANGED
|
@@ -344,11 +344,11 @@ async function readAdmissionOn(client, partition, clientId, clientCommitIds, run
|
|
|
344
344
|
partition,
|
|
345
345
|
runningSchemaVersion,
|
|
346
346
|
clientId,
|
|
347
|
-
|
|
347
|
+
JSON.stringify(clientCommitIds),
|
|
348
348
|
];
|
|
349
349
|
let statement = `SELECT ${SERVE_GATE_PROJECTION},
|
|
350
350
|
(SELECT jsonb_object_agg(client_commit_id, result) FROM sync_push_results
|
|
351
|
-
WHERE partition=$1 AND client_id=$3 AND client_commit_id
|
|
351
|
+
WHERE partition=$1 AND client_id=$3 AND client_commit_id IN (SELECT jsonb_array_elements_text($4::text::jsonb))) AS results,
|
|
352
352
|
(SELECT delete_floor FROM sync_partitions WHERE partition=$1) AS delete_floor`;
|
|
353
353
|
if (prefetch !== undefined && prefetch.specs.length > 0) {
|
|
354
354
|
params.push(JSON.stringify(prefetch.specs));
|
|
@@ -1008,7 +1008,7 @@ class PostgresTransaction {
|
|
|
1008
1008
|
// One statement for every id the admission read did not already answer.
|
|
1009
1009
|
// `#client` flushes this transaction's staged upserts first, so the rows
|
|
1010
1010
|
// are the final candidate state.
|
|
1011
|
-
const { rows: records } = await this.#client.query(selectRowsSql(this.#resolveTable(table), 'postgres'), [this.#partition, missing]);
|
|
1011
|
+
const { rows: records } = await this.#client.query(selectRowsSql(this.#resolveTable(table), 'postgres'), [this.#partition, JSON.stringify(missing)]);
|
|
1012
1012
|
for (const record of records)
|
|
1013
1013
|
rows.set(record.row_id, toStoredRow(record));
|
|
1014
1014
|
return rows;
|
|
@@ -1617,20 +1617,32 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
1617
1617
|
// `obj_description` and `to_regclass` of a missing table are NULL, so
|
|
1618
1618
|
// the probe never fails. A core table dropped out of band reapplies the
|
|
1619
1619
|
// DDL even under the current fingerprint.
|
|
1620
|
-
const
|
|
1620
|
+
const current = async (db) => {
|
|
1621
|
+
const { rows } = await db.query(`SELECT obj_description(to_regclass('sync_schema_meta'), 'pg_class') AS storage,
|
|
1621
1622
|
(SELECT count(*)::int
|
|
1622
1623
|
FROM jsonb_array_elements_text($1::text::jsonb) AS core(name)
|
|
1623
1624
|
WHERE to_regclass(core.name) IS NULL) AS missing`, [
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1625
|
+
JSON.stringify(statements.flatMap((statement) => /^CREATE TABLE IF NOT EXISTS (\w+)/.exec(statement)?.[1] ?? [])),
|
|
1626
|
+
]);
|
|
1627
|
+
return rows[0]?.storage === fingerprint && rows[0].missing === 0;
|
|
1628
|
+
};
|
|
1629
|
+
if (await current(this.#exec))
|
|
1627
1630
|
return;
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
//
|
|
1632
|
-
|
|
1633
|
-
|
|
1631
|
+
// Concurrent `CREATE TABLE IF NOT EXISTS` races in `pg_type` and fails
|
|
1632
|
+
// with `type ... already exists`. A transaction-scoped advisory lock
|
|
1633
|
+
// serializes the DDL across processes; the winner applies it and the
|
|
1634
|
+
// others find the fingerprint current once the lock frees.
|
|
1635
|
+
await this.#exec.transaction(async (tx) => {
|
|
1636
|
+
await tx.query("SELECT pg_advisory_xact_lock(hashtext('syncular.migrate'))");
|
|
1637
|
+
if (await current(tx))
|
|
1638
|
+
return;
|
|
1639
|
+
for (const statement of statements) {
|
|
1640
|
+
await tx.query(statement);
|
|
1641
|
+
}
|
|
1642
|
+
// The fingerprint is lowercase hex, safe to inline (COMMENT takes no
|
|
1643
|
+
// bound parameter).
|
|
1644
|
+
await tx.query(`COMMENT ON TABLE sync_schema_meta IS '${fingerprint}'`);
|
|
1645
|
+
});
|
|
1634
1646
|
}
|
|
1635
1647
|
/** Resolve a table's compiled schema; row operations require `ensureSchema`. */
|
|
1636
1648
|
table(name) {
|
|
@@ -2562,8 +2574,8 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2562
2574
|
for (const count of seqs.length === 0
|
|
2563
2575
|
? []
|
|
2564
2576
|
: (await this.#exec.query(`SELECT commit_seq, tbl, count(*) AS n FROM sync_changes
|
|
2565
|
-
WHERE partition=$1 AND commit_seq
|
|
2566
|
-
GROUP BY commit_seq, tbl ORDER BY commit_seq, tbl`, [partition, seqs])).rows) {
|
|
2577
|
+
WHERE partition=$1 AND commit_seq IN (SELECT jsonb_array_elements_text($2::text::jsonb)::bigint)
|
|
2578
|
+
GROUP BY commit_seq, tbl ORDER BY commit_seq, tbl`, [partition, JSON.stringify(seqs)])).rows) {
|
|
2567
2579
|
const seq = asNumber(count.commit_seq);
|
|
2568
2580
|
counts.set(seq, [...(counts.get(seq) ?? []), count]);
|
|
2569
2581
|
}
|
|
@@ -2571,8 +2583,8 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2571
2583
|
for (const change of sampleCount === 0 || seqs.length === 0
|
|
2572
2584
|
? []
|
|
2573
2585
|
: (await this.#exec.query(`SELECT commit_seq, tbl, row_id, op FROM sync_changes
|
|
2574
|
-
WHERE partition=$1 AND commit_seq
|
|
2575
|
-
ORDER BY commit_seq, idx`, [partition, seqs, sampleCount])).rows) {
|
|
2586
|
+
WHERE partition=$1 AND commit_seq IN (SELECT jsonb_array_elements_text($2::text::jsonb)::bigint) AND idx<$3
|
|
2587
|
+
ORDER BY commit_seq, idx`, [partition, JSON.stringify(seqs), sampleCount])).rows) {
|
|
2576
2588
|
const seq = asNumber(change.commit_seq);
|
|
2577
2589
|
samples.set(seq, [
|
|
2578
2590
|
...(samples.get(seq) ?? []),
|
|
@@ -190,7 +190,7 @@ export declare function upsertSql(table: CompiledTable, dialect: RelationalDiale
|
|
|
190
190
|
export declare function selectRowSql(table: CompiledTable, dialect: RelationalDialect): string;
|
|
191
191
|
/**
|
|
192
192
|
* SELECT many stored rows of one table by primary key in one statement.
|
|
193
|
-
* The ids bind as one parameter (Postgres `
|
|
193
|
+
* The ids bind as one JSON array parameter (Postgres `jsonb_array_elements_text`, SQLite
|
|
194
194
|
* a JSON array through `json_each`), so the statement count does not grow
|
|
195
195
|
* with the id count. Params: [partition, rowIds]; absent ids contribute no
|
|
196
196
|
* row.
|
package/dist/relational-rows.js
CHANGED
|
@@ -494,7 +494,7 @@ export function selectRowSql(table, dialect) {
|
|
|
494
494
|
}
|
|
495
495
|
/**
|
|
496
496
|
* SELECT many stored rows of one table by primary key in one statement.
|
|
497
|
-
* The ids bind as one parameter (Postgres `
|
|
497
|
+
* The ids bind as one JSON array parameter (Postgres `jsonb_array_elements_text`, SQLite
|
|
498
498
|
* a JSON array through `json_each`), so the statement count does not grow
|
|
499
499
|
* with the id count. Params: [partition, rowIds]; absent ids contribute no
|
|
500
500
|
* row.
|
|
@@ -502,7 +502,7 @@ export function selectRowSql(table, dialect) {
|
|
|
502
502
|
export function selectRowsSql(table, dialect) {
|
|
503
503
|
const [partition, rowIds] = dialect === 'sqlite'
|
|
504
504
|
? ['?', 'IN (SELECT value FROM json_each(?))']
|
|
505
|
-
: ['$1', '
|
|
505
|
+
: ['$1', ' IN (SELECT jsonb_array_elements_text($2::text::jsonb))'];
|
|
506
506
|
return `SELECT ${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, ${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, ${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, ${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, ${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=${partition} AND ${quoteIdent(SYNC_ROW_ID_COLUMN)}${rowIds}`;
|
|
507
507
|
}
|
|
508
508
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.79.
|
|
3
|
+
"version": "0.79.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.79.
|
|
71
|
+
"@syncular/core": "0.79.2"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/postgres-storage.ts
CHANGED
|
@@ -578,11 +578,11 @@ async function readAdmissionOn(
|
|
|
578
578
|
partition,
|
|
579
579
|
runningSchemaVersion,
|
|
580
580
|
clientId,
|
|
581
|
-
|
|
581
|
+
JSON.stringify(clientCommitIds),
|
|
582
582
|
];
|
|
583
583
|
let statement = `SELECT ${SERVE_GATE_PROJECTION},
|
|
584
584
|
(SELECT jsonb_object_agg(client_commit_id, result) FROM sync_push_results
|
|
585
|
-
WHERE partition=$1 AND client_id=$3 AND client_commit_id
|
|
585
|
+
WHERE partition=$1 AND client_id=$3 AND client_commit_id IN (SELECT jsonb_array_elements_text($4::text::jsonb))) AS results,
|
|
586
586
|
(SELECT delete_floor FROM sync_partitions WHERE partition=$1) AS delete_floor`;
|
|
587
587
|
if (prefetch !== undefined && prefetch.specs.length > 0) {
|
|
588
588
|
params.push(JSON.stringify(prefetch.specs));
|
|
@@ -1491,7 +1491,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1491
1491
|
// are the final candidate state.
|
|
1492
1492
|
const { rows: records } = await this.#client.query<RowRecord>(
|
|
1493
1493
|
selectRowsSql(this.#resolveTable(table), 'postgres'),
|
|
1494
|
-
[this.#partition, missing],
|
|
1494
|
+
[this.#partition, JSON.stringify(missing)],
|
|
1495
1495
|
);
|
|
1496
1496
|
for (const record of records) rows.set(record.row_id, toStoredRow(record));
|
|
1497
1497
|
return rows;
|
|
@@ -2299,32 +2299,43 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2299
2299
|
// `obj_description` and `to_regclass` of a missing table are NULL, so
|
|
2300
2300
|
// the probe never fails. A core table dropped out of band reapplies the
|
|
2301
2301
|
// DDL even under the current fingerprint.
|
|
2302
|
-
const
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2302
|
+
const current = async (db: PgQueryable): Promise<boolean> => {
|
|
2303
|
+
const { rows } = await db.query<{
|
|
2304
|
+
storage: string | null;
|
|
2305
|
+
missing: number;
|
|
2306
|
+
}>(
|
|
2307
|
+
`SELECT obj_description(to_regclass('sync_schema_meta'), 'pg_class') AS storage,
|
|
2307
2308
|
(SELECT count(*)::int
|
|
2308
2309
|
FROM jsonb_array_elements_text($1::text::jsonb) AS core(name)
|
|
2309
2310
|
WHERE to_regclass(core.name) IS NULL) AS missing`,
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2311
|
+
[
|
|
2312
|
+
JSON.stringify(
|
|
2313
|
+
statements.flatMap(
|
|
2314
|
+
(statement) =>
|
|
2315
|
+
/^CREATE TABLE IF NOT EXISTS (\w+)/.exec(statement)?.[1] ?? [],
|
|
2316
|
+
),
|
|
2315
2317
|
),
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
//
|
|
2324
|
-
//
|
|
2325
|
-
await this.#exec.
|
|
2326
|
-
|
|
2327
|
-
|
|
2318
|
+
],
|
|
2319
|
+
);
|
|
2320
|
+
return rows[0]?.storage === fingerprint && rows[0].missing === 0;
|
|
2321
|
+
};
|
|
2322
|
+
if (await current(this.#exec)) return;
|
|
2323
|
+
// Concurrent `CREATE TABLE IF NOT EXISTS` races in `pg_type` and fails
|
|
2324
|
+
// with `type ... already exists`. A transaction-scoped advisory lock
|
|
2325
|
+
// serializes the DDL across processes; the winner applies it and the
|
|
2326
|
+
// others find the fingerprint current once the lock frees.
|
|
2327
|
+
await this.#exec.transaction(async (tx) => {
|
|
2328
|
+
await tx.query(
|
|
2329
|
+
"SELECT pg_advisory_xact_lock(hashtext('syncular.migrate'))",
|
|
2330
|
+
);
|
|
2331
|
+
if (await current(tx)) return;
|
|
2332
|
+
for (const statement of statements) {
|
|
2333
|
+
await tx.query(statement);
|
|
2334
|
+
}
|
|
2335
|
+
// The fingerprint is lowercase hex, safe to inline (COMMENT takes no
|
|
2336
|
+
// bound parameter).
|
|
2337
|
+
await tx.query(`COMMENT ON TABLE sync_schema_meta IS '${fingerprint}'`);
|
|
2338
|
+
});
|
|
2328
2339
|
}
|
|
2329
2340
|
|
|
2330
2341
|
/** Resolve a table's compiled schema; row operations require `ensureSchema`. */
|
|
@@ -3803,9 +3814,9 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
3803
3814
|
n: unknown;
|
|
3804
3815
|
}>(
|
|
3805
3816
|
`SELECT commit_seq, tbl, count(*) AS n FROM sync_changes
|
|
3806
|
-
WHERE partition=$1 AND commit_seq
|
|
3817
|
+
WHERE partition=$1 AND commit_seq IN (SELECT jsonb_array_elements_text($2::text::jsonb)::bigint)
|
|
3807
3818
|
GROUP BY commit_seq, tbl ORDER BY commit_seq, tbl`,
|
|
3808
|
-
[partition, seqs],
|
|
3819
|
+
[partition, JSON.stringify(seqs)],
|
|
3809
3820
|
)
|
|
3810
3821
|
).rows) {
|
|
3811
3822
|
const seq = asNumber(count.commit_seq);
|
|
@@ -3822,9 +3833,9 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
3822
3833
|
op: unknown;
|
|
3823
3834
|
}>(
|
|
3824
3835
|
`SELECT commit_seq, tbl, row_id, op FROM sync_changes
|
|
3825
|
-
WHERE partition=$1 AND commit_seq
|
|
3836
|
+
WHERE partition=$1 AND commit_seq IN (SELECT jsonb_array_elements_text($2::text::jsonb)::bigint) AND idx<$3
|
|
3826
3837
|
ORDER BY commit_seq, idx`,
|
|
3827
|
-
[partition, seqs, sampleCount],
|
|
3838
|
+
[partition, JSON.stringify(seqs), sampleCount],
|
|
3828
3839
|
)
|
|
3829
3840
|
).rows) {
|
|
3830
3841
|
const seq = asNumber(change.commit_seq);
|
package/src/relational-rows.ts
CHANGED
|
@@ -610,7 +610,7 @@ export function selectRowSql(
|
|
|
610
610
|
|
|
611
611
|
/**
|
|
612
612
|
* SELECT many stored rows of one table by primary key in one statement.
|
|
613
|
-
* The ids bind as one parameter (Postgres `
|
|
613
|
+
* The ids bind as one JSON array parameter (Postgres `jsonb_array_elements_text`, SQLite
|
|
614
614
|
* a JSON array through `json_each`), so the statement count does not grow
|
|
615
615
|
* with the id count. Params: [partition, rowIds]; absent ids contribute no
|
|
616
616
|
* row.
|
|
@@ -622,7 +622,7 @@ export function selectRowsSql(
|
|
|
622
622
|
const [partition, rowIds] =
|
|
623
623
|
dialect === 'sqlite'
|
|
624
624
|
? ['?', 'IN (SELECT value FROM json_each(?))']
|
|
625
|
-
: ['$1', '
|
|
625
|
+
: ['$1', ' IN (SELECT jsonb_array_elements_text($2::text::jsonb))'];
|
|
626
626
|
return `SELECT ${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, ${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, ${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, ${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, ${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=${partition} AND ${quoteIdent(SYNC_ROW_ID_COLUMN)}${rowIds}`;
|
|
627
627
|
}
|
|
628
628
|
|