@syncular/server 0.57.0 → 0.59.0

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.
Files changed (91) hide show
  1. package/README.md +31 -22
  2. package/dist/admin.js +8 -4
  3. package/dist/blob-handlers.d.ts +25 -31
  4. package/dist/blob-handlers.js +63 -41
  5. package/dist/blob-store.d.ts +42 -24
  6. package/dist/blob-store.js +0 -0
  7. package/dist/bucket-blob-store.d.ts +12 -8
  8. package/dist/bucket-blob-store.js +54 -45
  9. package/dist/bucket-segment-store.js +18 -16
  10. package/dist/content-encoding.d.ts +5 -2
  11. package/dist/content-encoding.js +42 -13
  12. package/dist/context.d.ts +16 -0
  13. package/dist/context.js +21 -1
  14. package/dist/d1-storage.d.ts +14 -98
  15. package/dist/d1-storage.js +223 -1111
  16. package/dist/errors.js +8 -0
  17. package/dist/events.d.ts +1 -1
  18. package/dist/handler.js +135 -103
  19. package/dist/local-authority.d.ts +2 -0
  20. package/dist/local-authority.js +17 -3
  21. package/dist/object-bucket.d.ts +2 -0
  22. package/dist/operations-realtime.js +69 -50
  23. package/dist/pg-executor-pglite.d.ts +5 -6
  24. package/dist/pg-executor-pglite.js +22 -14
  25. package/dist/pg-executor.d.ts +4 -1
  26. package/dist/postgres-storage.d.ts +1 -1
  27. package/dist/postgres-storage.js +182 -105
  28. package/dist/pull.d.ts +28 -20
  29. package/dist/pull.js +85 -87
  30. package/dist/push.js +272 -147
  31. package/dist/realtime.d.ts +68 -28
  32. package/dist/realtime.js +297 -136
  33. package/dist/relational-rows.d.ts +27 -15
  34. package/dist/relational-rows.js +38 -22
  35. package/dist/s3-blob-store.d.ts +11 -12
  36. package/dist/s3-blob-store.js +20 -13
  37. package/dist/s3-object-bucket.d.ts +8 -1
  38. package/dist/s3-object-bucket.js +16 -3
  39. package/dist/schema.d.ts +14 -0
  40. package/dist/schema.js +23 -0
  41. package/dist/scopes.d.ts +6 -2
  42. package/dist/scopes.js +22 -4
  43. package/dist/seed.js +1 -2
  44. package/dist/segment-download.js +18 -4
  45. package/dist/signed-url.d.ts +2 -2
  46. package/dist/signed-url.js +9 -2
  47. package/dist/sigv4.d.ts +8 -2
  48. package/dist/sigv4.js +8 -4
  49. package/dist/sqlite-blob-store.d.ts +1 -1
  50. package/dist/sqlite-blob-store.js +23 -22
  51. package/dist/sqlite-dialect.d.ts +150 -47
  52. package/dist/sqlite-dialect.js +987 -88
  53. package/dist/sqlite-lease-store.js +13 -18
  54. package/dist/sqlite-storage.d.ts +9 -62
  55. package/dist/sqlite-storage.js +293 -1117
  56. package/dist/storage.d.ts +32 -12
  57. package/package.json +2 -2
  58. package/src/admin.ts +16 -2
  59. package/src/blob-handlers.ts +91 -72
  60. package/src/blob-store.ts +0 -0
  61. package/src/bucket-blob-store.ts +75 -48
  62. package/src/bucket-segment-store.ts +20 -15
  63. package/src/content-encoding.ts +48 -16
  64. package/src/context.ts +32 -1
  65. package/src/d1-storage.ts +326 -1692
  66. package/src/errors.ts +8 -0
  67. package/src/events.ts +1 -1
  68. package/src/handler.ts +178 -127
  69. package/src/local-authority.ts +17 -3
  70. package/src/object-bucket.ts +2 -0
  71. package/src/operations-realtime.ts +85 -61
  72. package/src/pg-executor-pglite.ts +27 -23
  73. package/src/pg-executor.ts +5 -1
  74. package/src/postgres-storage.ts +231 -138
  75. package/src/pull.ts +133 -147
  76. package/src/push.ts +356 -205
  77. package/src/realtime.ts +380 -179
  78. package/src/relational-rows.ts +54 -27
  79. package/src/s3-blob-store.ts +23 -13
  80. package/src/s3-object-bucket.ts +20 -2
  81. package/src/schema.ts +31 -0
  82. package/src/scopes.ts +23 -3
  83. package/src/seed.ts +1 -2
  84. package/src/segment-download.ts +25 -4
  85. package/src/signed-url.ts +10 -4
  86. package/src/sigv4.ts +17 -4
  87. package/src/sqlite-blob-store.ts +27 -26
  88. package/src/sqlite-dialect.ts +1607 -114
  89. package/src/sqlite-lease-store.ts +12 -23
  90. package/src/sqlite-storage.ts +360 -1811
  91. package/src/storage.ts +33 -12
@@ -1,17 +1,13 @@
1
1
  var _a;
2
- import { validateCommitPruneQuery } from './prune.js';
3
- import { StorageQueryError } from './storage-errors.js';
4
- import { clientRoundState, PARTITION_TOUCH_INTERVAL_MS, rowsLiveAfterDelete, } from './storage.js';
5
2
  /**
6
3
  * Cloudflare D1 server storage for Workers deployments.
7
4
  *
8
5
  * D1 *is* SQLite exposed over an async, statement-at-a-time API
9
6
  * (`prepare(sql).bind(...).all()` / `.first()` / `.run()`, plus `batch([…])`
10
- * for atomic multi-statement writes). So this storage shares the schema and
11
- * value codecs with `SqliteServerStorage` via `sqlite-dialect.ts` — same
12
- * DDL, same `?` placeholders, same JSON-scope / base64-push-result
13
- * serialization — and differs only in the execution shape (async, no
14
- * interactive transaction).
7
+ * for atomic multi-statement writes). The statements it shares with
8
+ * `SqliteServerStorage` live in `SqliteFamilyStorage` (`sqlite-dialect.ts`);
9
+ * this class adds the execution seam, the published-schema guard, the
10
+ * resumable schema migration and the buffered transaction.
15
11
  *
16
12
  * ## Transaction model (the one real divergence)
17
13
  *
@@ -44,38 +40,20 @@ import { clientRoundState, PARTITION_TOUCH_INTERVAL_MS, rowsLiveAfterDelete, } f
44
40
  * rather than a lock D1 does not expose.
45
41
  */
46
42
  import { decodeRow } from '@syncular/core';
47
- import { bindAuthoritativePartition, prepareAuthoritativeQuery, } from './authoritative-query.js';
48
- import { syncError } from './errors.js';
49
- import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, commitWindowPageSql, LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL, deleteRowSql, deleteSqliteRowScopesSql, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, SYNC_COLUMN_VERSIONS_COLUMN, SYNC_PARTITION_COLUMN, SYNC_PAYLOAD_COLUMN, SYNC_ROW_ID_COLUMN, SYNC_SCOPES_COLUMN, SYNC_VERSION_COLUMN, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowScopesSql, selectRowSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, upsertSql, upsertValues, } from './relational-rows.js';
43
+ import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
50
44
  import { matchesEffective } from './scopes.js';
51
- import { asUint8Array, collectCommitWindowPage, deserializePushResult, placeholders, ERASE_SCOPE_VALUE_HISTORY_SQLITE_SQL, eraseScopeValueHistoryParams, LIST_SCOPE_VALUE_ROWS_SQLITE_SQL, serializePushResult, sqliteDdlStatements, SYNC_CLIENT_ROUND_COLUMNS, toStoredChange, toStoredRow, } from './sqlite-dialect.js';
52
- import { isD1ConstraintError, StorageConstraintError } from './storage-errors.js';
45
+ import { asUint8Array, deserializePushResult, placeholders, SqliteFamilyStorage, SqliteFamilyTransaction, SQLITE_ADDED_COLUMNS, sqliteDdlStatements, toStoredRow, toTombstone, } from './sqlite-dialect.js';
46
+ import { isD1ConstraintError, StorageConstraintError, StorageQueryError, } from './storage-errors.js';
53
47
  import { assertScopeIndexedScan, resolveIndexRowScan } from './storage-query.js';
54
- function toStoredReaction(record) {
55
- return {
56
- idempotencyKey: record.idempotency_key,
57
- type: record.type,
58
- version: record.version,
59
- payload: JSON.parse(record.payload),
60
- sourceClientId: record.source_client_id,
61
- sourceClientCommitId: record.source_client_commit_id,
62
- sourceCommitSeq: record.source_commit_seq,
63
- createdAtMs: record.created_at_ms,
64
- maxAttempts: record.max_attempts,
65
- status: record.status,
66
- attempts: record.attempts,
67
- availableAtMs: record.available_at_ms,
68
- ...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
69
- ...(record.lease_expires_at_ms !== null
70
- ? { leaseExpiresAtMs: record.lease_expires_at_ms }
71
- : {}),
72
- ...(record.completed_at_ms !== null
73
- ? { completedAtMs: record.completed_at_ms }
74
- : {}),
75
- ...(record.last_failure !== null
76
- ? { lastFailure: JSON.parse(record.last_failure) }
77
- : {}),
78
- };
48
+ /** The rows of one `db.batch` result entry (`{ results }` per statement). */
49
+ function batchRows(result) {
50
+ if (typeof result !== 'object' ||
51
+ result === null ||
52
+ !('results' in result) ||
53
+ !Array.isArray(result.results)) {
54
+ throw new Error('D1 batch returned an invalid result');
55
+ }
56
+ return result.results;
79
57
  }
80
58
  function relationalValuesEqual(left, right) {
81
59
  if (left instanceof Uint8Array && right instanceof Uint8Array) {
@@ -92,39 +70,24 @@ async function queryAuthoritativeOn(db, partition, prepared) {
92
70
  .prepare('SELECT max_commit_seq FROM sync_partitions WHERE partition=?')
93
71
  .bind(partition),
94
72
  ]);
95
- const rowsResult = results[0];
96
- const cursorResult = results[1];
97
- if (typeof rowsResult !== 'object' ||
98
- rowsResult === null ||
99
- !('results' in rowsResult) ||
100
- !Array.isArray(rowsResult.results) ||
101
- typeof cursorResult !== 'object' ||
102
- cursorResult === null ||
103
- !('results' in cursorResult) ||
104
- !Array.isArray(cursorResult.results)) {
105
- throw new Error('D1 registered query returned an invalid batch result');
106
- }
107
- const cursor = cursorResult.results[0];
108
- const maxCommitSeq = typeof cursor === 'object' &&
109
- cursor !== null &&
110
- 'max_commit_seq' in cursor &&
111
- typeof cursor.max_commit_seq === 'number'
112
- ? cursor.max_commit_seq
113
- : 0;
73
+ const cursor = batchRows(results[1])[0];
114
74
  return {
115
- rows: rowsResult.results.filter((row) => typeof row === 'object' && row !== null),
116
- maxCommitSeq,
75
+ rows: batchRows(results[0]).filter((row) => typeof row === 'object' && row !== null),
76
+ maxCommitSeq: typeof cursor === 'object' &&
77
+ cursor !== null &&
78
+ 'max_commit_seq' in cursor &&
79
+ typeof cursor.max_commit_seq === 'number'
80
+ ? cursor.max_commit_seq
81
+ : 0,
117
82
  };
118
83
  }
119
- class D1Transaction {
84
+ class D1Transaction extends SqliteFamilyTransaction {
120
85
  #db;
121
- #partition;
122
86
  #resolveTable;
123
87
  /** Storage-owned relation rewrite, bound to this transaction's partition. */
124
88
  #prepareQuery;
125
89
  #pushApplySerialized;
126
90
  #buffer = [];
127
- #open = true;
128
91
  /** Live snapshot of `max_commit_seq`, advanced within this transaction. */
129
92
  #maxCommitSeq;
130
93
  #pushApplyCheckpoint;
@@ -146,99 +109,132 @@ class D1Transaction {
146
109
  */
147
110
  #pending = new Map();
148
111
  #clearedTombstones = new Set();
112
+ /**
113
+ * `prefetchRows` snapshot keyed `tbl\u0000rowId`: the stored row and its §5
114
+ * tombstone, `undefined` when absent. Nothing persists before `commit()`,
115
+ * so the snapshot stays exact; the `#pending` overlay still wins for rows
116
+ * this transaction wrote.
117
+ */
118
+ #prefetched = new Map();
149
119
  constructor(db, partition, resolveTable, prepareQuery, pushApplySerialized) {
120
+ super(partition);
150
121
  this.#db = db;
151
- this.#partition = partition;
152
122
  this.#resolveTable = resolveTable;
153
123
  this.#prepareQuery = prepareQuery;
154
124
  this.#pushApplySerialized = pushApplySerialized;
155
125
  }
156
- #assertOpen() {
157
- if (!this.#open)
158
- throw new Error('transaction already finished');
159
- }
160
- #buffer_(sql, params) {
126
+ run(sql, params) {
161
127
  this.#buffer.push({ sql, params });
162
128
  }
129
+ async all(sql, params) {
130
+ return (await this.#db
131
+ .prepare(sql)
132
+ .bind(...params)
133
+ .all()).results;
134
+ }
135
+ table(name) {
136
+ return this.#resolveTable(name);
137
+ }
163
138
  static #key(table, rowId) {
164
139
  return `${table}\u0000${rowId}`;
165
140
  }
166
141
  async getRow(table, rowId) {
167
- this.#assertOpen();
142
+ this.assertOpen();
168
143
  const pending = this.#pending.get(_a.#key(table, rowId));
169
144
  if (pending !== undefined) {
170
145
  return pending.kind === 'row' ? pending.row : undefined;
171
146
  }
147
+ const prefetched = this.#prefetched.get(_a.#key(table, rowId));
148
+ if (prefetched !== undefined)
149
+ return prefetched.row;
172
150
  const record = await this.#db
173
- .prepare(selectRowSql(this.#resolveTable(table), 'sqlite'))
174
- .bind(this.#partition, rowId)
151
+ .prepare(selectRowSql(this.table(table), 'sqlite'))
152
+ .bind(this.partition, rowId)
175
153
  .first();
176
154
  return record === null ? undefined : toStoredRow(record);
177
155
  }
178
- async getRows(table, rowIds) {
179
- this.#assertOpen();
180
- // One lookup per id: `getRow` owns the buffered-write overlay, and a
181
- // mutable id list cannot share one bound statement across D1's limit.
182
- const rows = new Map();
183
- for (const rowId of new Set(rowIds)) {
184
- const row = await this.getRow(table, rowId);
185
- if (row !== undefined)
186
- rows.set(rowId, row);
187
- }
188
- return rows;
189
- }
190
- async getTombstoneSeq(table, rowId) {
191
- this.#assertOpen();
192
- if (this.#clearedTombstones.has(_a.#key(table, rowId))) {
193
- return undefined;
194
- }
156
+ async getTombstone(table, rowId) {
157
+ this.assertOpen();
195
158
  // Persisted read, no #pending overlay: a delete buffered by this commit
196
159
  // records its tombstone at appendCommit, matching the SQLite/Postgres
197
160
  // adapters where the tombstone insert also runs at commit append.
198
- const record = await this.#db
199
- .prepare('SELECT commit_seq FROM sync_tombstones WHERE partition=? AND tbl=? AND row_id=?')
200
- .bind(this.#partition, table, rowId)
201
- .first();
202
- return record?.commit_seq;
161
+ const key = _a.#key(table, rowId);
162
+ if (this.#clearedTombstones.has(key))
163
+ return undefined;
164
+ const prefetched = this.#prefetched.get(key);
165
+ return prefetched === undefined
166
+ ? super.getTombstone(table, rowId)
167
+ : prefetched.tombstone;
168
+ }
169
+ /**
170
+ * Read every named row and its tombstone in one `db.batch` (two statements
171
+ * per table, ids bound as a JSON array), so a multi-operation push pays one
172
+ * round trip instead of up to two per operation.
173
+ */
174
+ async prefetchRows(targets) {
175
+ this.assertOpen();
176
+ const specs = targets
177
+ .map(({ table, rowIds }) => ({
178
+ table,
179
+ ids: [...new Set(rowIds)].filter((rowId) => !this.#prefetched.has(_a.#key(table, rowId))),
180
+ }))
181
+ .filter(({ ids }) => ids.length > 0);
182
+ if (specs.length === 0)
183
+ return;
184
+ const results = await this.#db.batch(specs.flatMap(({ table, ids }) => {
185
+ const bound = JSON.stringify(ids);
186
+ return [
187
+ this.#db
188
+ .prepare(selectRowsSql(this.table(table), 'sqlite'))
189
+ .bind(this.partition, bound),
190
+ this.#db
191
+ .prepare('SELECT row_id, commit_seq, server_version, scopes FROM sync_tombstones WHERE partition=? AND tbl=? AND row_id IN (SELECT value FROM json_each(?))')
192
+ .bind(this.partition, table, bound),
193
+ ];
194
+ }));
195
+ specs.forEach(({ table, ids }, i) => {
196
+ const rows = new Map(batchRows(results[2 * i]).map((record) => [
197
+ record.row_id,
198
+ toStoredRow(record),
199
+ ]));
200
+ const tombstones = new Map(batchRows(results[2 * i + 1]).map((record) => [record.row_id, toTombstone(record)]));
201
+ for (const rowId of ids) {
202
+ this.#prefetched.set(_a.#key(table, rowId), {
203
+ row: rows.get(rowId),
204
+ tombstone: tombstones.get(rowId),
205
+ });
206
+ }
207
+ });
203
208
  }
204
209
  async clearTombstone(table, rowId) {
205
- this.#assertOpen();
210
+ await super.clearTombstone(table, rowId);
206
211
  this.#clearedTombstones.add(_a.#key(table, rowId));
207
- this.#buffer_('DELETE FROM sync_tombstones WHERE partition=? AND tbl=? AND row_id=?', [this.#partition, table, rowId]);
208
212
  }
209
213
  async getPushResult(clientId, clientCommitId) {
210
- this.#assertOpen();
214
+ this.assertOpen();
211
215
  // D1 reads run in autocommit; the push layer's duplicate re-check happens
212
216
  // before this transaction buffers any write, so a direct read is exact.
213
- const record = await this.#db
214
- .prepare('SELECT result FROM sync_push_results WHERE partition=? AND client_id=? AND client_commit_id=?')
215
- .bind(this.#partition, clientId, clientCommitId)
216
- .first();
217
- if (record === null)
218
- return undefined;
219
- try {
220
- return deserializePushResult(record.result);
221
- }
222
- catch {
223
- throw syncError('sync.idempotency_cache_miss', 'persisted push result unreadable (§6.3)');
224
- }
217
+ const [record] = await this.all('SELECT result FROM sync_push_results WHERE partition=? AND client_id=? AND client_commit_id=?', [this.partition, clientId, clientCommitId]);
218
+ return record === undefined
219
+ ? undefined
220
+ : deserializePushResult(record.result);
225
221
  }
226
222
  async scanRows(query) {
227
- this.#assertOpen();
223
+ this.assertOpen();
228
224
  const firstVariable = assertScopeIndexedScan(query);
229
225
  const firstValues = query.scopeFilter[firstVariable] ?? [];
230
226
  if (firstValues.length === 0)
231
227
  return [];
232
228
  const pendingForTable = [...this.#pending.entries()].filter(([key]) => key.startsWith(`${query.table}\u0000`));
233
229
  const persistedLimit = query.limit + pendingForTable.length;
234
- const sql = scanRowPageSql(this.#resolveTable(query.table), firstValues.length, 'sqlite');
230
+ const sql = scanRowPageSql(this.table(query.table), firstValues.length, 'sqlite');
235
231
  const persisted = [];
236
232
  let afterRowId = query.afterRowId ?? '';
237
233
  const batchSize = Math.max(64, persistedLimit);
238
234
  while (persisted.length < persistedLimit) {
239
235
  const { results: records } = await this.#db
240
236
  .prepare(sql)
241
- .bind(this.#partition, query.table, firstVariable, ...firstValues, afterRowId, batchSize, this.#partition)
237
+ .bind(this.partition, query.table, firstVariable, JSON.stringify(firstValues), afterRowId, batchSize, this.partition)
242
238
  .all();
243
239
  if (records.length === 0)
244
240
  break;
@@ -272,12 +268,12 @@ class D1Transaction {
272
268
  .slice(0, query.limit);
273
269
  }
274
270
  async scanRowsByIndex(query) {
275
- this.#assertOpen();
276
- const table = this.#resolveTable(query.table);
271
+ this.assertOpen();
272
+ const table = this.table(query.table);
277
273
  const index = resolveIndexRowScan(table, query);
278
274
  const pendingForTable = [...this.#pending.entries()].filter(([key]) => key.startsWith(`${query.table}\u0000`));
279
275
  const persistedLimit = query.limit + pendingForTable.length;
280
- const statement = indexRowPageStatement(table, index, query.values, this.#partition, query.afterRowId, persistedLimit, 'sqlite');
276
+ const statement = indexRowPageStatement(table, index, query.values, this.partition, query.afterRowId, persistedLimit, 'sqlite');
281
277
  const { results: records } = await this.#db
282
278
  .prepare(statement.sql)
283
279
  .bind(...statement.params)
@@ -309,7 +305,7 @@ class D1Transaction {
309
305
  .slice(0, query.limit);
310
306
  }
311
307
  async queryAuthoritative(query) {
312
- this.#assertOpen();
308
+ this.assertOpen();
313
309
  // D1 buffers writes until commit() and cannot overlay them on generated
314
310
  // SQL, so a read over a written table would return committed state
315
311
  // without this transaction's staged rows. Refuse it instead.
@@ -318,10 +314,10 @@ class D1Transaction {
318
314
  throw new StorageQueryError('sync.storage.query_over_staged_writes');
319
315
  }
320
316
  }
321
- return queryAuthoritativeOn(this.#db, this.#partition, this.#prepareQuery(query));
317
+ return queryAuthoritativeOn(this.#db, this.partition, this.#prepareQuery(query));
322
318
  }
323
319
  async preparePushApply(clientId, clientCommitId, runningSchemaVersion, _targets = []) {
324
- this.#assertOpen();
320
+ this.assertOpen();
325
321
  if (!this.#pushApplySerialized) {
326
322
  throw new Error('D1 push apply requires externally serialized partition writes');
327
323
  }
@@ -338,7 +334,7 @@ class D1Transaction {
338
334
  return Promise.reject(new StorageQueryError('sync.storage.checkpoint_unsupported'));
339
335
  }
340
336
  async readServeGate(_runningSchemaVersion) {
341
- this.#assertOpen();
337
+ this.assertOpen();
342
338
  const marker = await this.#db
343
339
  .prepare('SELECT schema_version FROM sync_schema_meta WHERE id=1')
344
340
  .first();
@@ -349,7 +345,7 @@ class D1Transaction {
349
345
  };
350
346
  }
351
347
  async commitRejectedPushResult(clientId, clientCommitId, result) {
352
- this.#assertOpen();
348
+ this.assertOpen();
353
349
  const checkpoint = this.#pushApplyCheckpoint;
354
350
  if (checkpoint === undefined) {
355
351
  throw new Error('push rejection requires its apply checkpoint');
@@ -408,7 +404,7 @@ class D1Transaction {
408
404
  });
409
405
  const persisted = await this.#db
410
406
  .prepare(sql)
411
- .bind(this.#partition, ...bind, row.rowId)
407
+ .bind(this.partition, ...bind, row.rowId)
412
408
  .first();
413
409
  if (persisted === null)
414
410
  continue;
@@ -425,9 +421,8 @@ class D1Transaction {
425
421
  }
426
422
  }
427
423
  async upsertRow(table, row, context) {
428
- this.#assertOpen();
429
- const compiled = this.#resolveTable(table);
430
- await this.#assertNoUniqueCollision(compiled, row, context?.opIndex);
424
+ this.assertOpen();
425
+ await this.#assertNoUniqueCollision(this.table(table), row, context?.opIndex);
431
426
  if (context?.opIndex !== undefined) {
432
427
  this.#applicationOpIndexes.add(context.opIndex);
433
428
  }
@@ -435,63 +430,25 @@ class D1Transaction {
435
430
  kind: 'row',
436
431
  row,
437
432
  });
438
- const p = this.#partition;
439
- // Scope index maintenance: delete only the old keys the new scope map no
440
- // longer carries, then insert the new keys. Keys present in both are left
441
- // alone, so the `INSERT OR IGNORE` below finds them and writes nothing.
442
- // This DELETE precedes the row upsert in the same atomic batch, and its
443
- // subquery reads the row's pre-upsert scope map.
444
- const scopesJson = JSON.stringify(row.scopes);
445
- this.#buffer_(`${deleteSqliteRowScopesSql(compiled)}
446
- AND (var, value) NOT IN (
447
- SELECT key, value FROM json_each(?)
448
- )`, [p, table, row.rowId, p, row.rowId, scopesJson]);
449
- this.#buffer_(upsertSql(compiled, 'sqlite'), upsertValues(compiled, p, row, 'sqlite'));
450
- for (const [variable, value] of Object.entries(row.scopes)) {
451
- this.#buffer_('INSERT OR IGNORE INTO sync_row_scopes(partition, tbl, var, value, row_id) VALUES (?,?,?,?,?)', [p, table, variable, value, row.rowId]);
452
- }
433
+ this.writeRow(table, row);
453
434
  }
454
435
  async deleteRow(table, rowId) {
455
- this.#assertOpen();
436
+ await super.deleteRow(table, rowId);
456
437
  this.#pending.set(_a.#key(table, rowId), { kind: 'deleted' });
457
- const p = this.#partition;
458
- const compiled = this.#resolveTable(table);
459
- this.#buffer_(deleteSqliteRowScopesSql(compiled), [
460
- p,
461
- table,
462
- rowId,
463
- p,
464
- rowId,
465
- ]);
466
- this.#buffer_(deleteRowSql(compiled, 'sqlite'), [p, rowId]);
467
- // §5.9.4: a deleted row references no blobs.
468
- this.#buffer_('DELETE FROM sync_blob_refs WHERE partition=? AND tbl=? AND row_id=?', [p, table, rowId]);
469
- }
470
- async setBlobRefs(table, rowId, blobIds) {
471
- this.#assertOpen();
472
- const p = this.#partition;
473
- this.#buffer_('DELETE FROM sync_blob_refs WHERE partition=? AND tbl=? AND row_id=?', [p, table, rowId]);
474
- for (const blobId of blobIds) {
475
- this.#buffer_('INSERT OR IGNORE INTO sync_blob_refs(partition, tbl, row_id, blob_id) VALUES (?,?,?,?)', [p, table, rowId, blobId]);
476
- }
477
438
  }
478
- async appendCommit(commit) {
479
- this.#assertOpen();
480
- const p = this.#partition;
439
+ async appendCommitRow(commit) {
440
+ const p = this.partition;
481
441
  // Read the live counter once, then advance it in memory for this tx; the
482
442
  // increment is buffered and lands atomically at commit() (see header for
483
443
  // the concurrency posture).
484
444
  if (this.#maxCommitSeq === undefined) {
485
- const row = await this.#db
486
- .prepare('SELECT max_commit_seq FROM sync_partitions WHERE partition=?')
487
- .bind(p)
488
- .first();
445
+ const [row] = await this.all('SELECT max_commit_seq FROM sync_partitions WHERE partition=?', [p]);
489
446
  this.#maxCommitSeq = row?.max_commit_seq ?? 0;
490
447
  }
491
448
  const commitSeq = this.#maxCommitSeq + 1;
492
449
  this.#maxCommitSeq = commitSeq;
493
- this.#buffer_('INSERT INTO sync_partitions(partition, max_commit_seq) VALUES (?,?) ON CONFLICT(partition) DO UPDATE SET max_commit_seq=excluded.max_commit_seq', [p, commitSeq]);
494
- this.#buffer_('INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms) VALUES (?,?,?,?,?,?)', [
450
+ this.run('INSERT INTO sync_partitions(partition, max_commit_seq) VALUES (?,?) ON CONFLICT(partition) DO UPDATE SET max_commit_seq=excluded.max_commit_seq', [p, commitSeq]);
451
+ this.run('INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms) VALUES (?,?,?,?,?,?)', [
495
452
  p,
496
453
  commitSeq,
497
454
  commit.clientId,
@@ -499,70 +456,19 @@ class D1Transaction {
499
456
  commit.actorId,
500
457
  commit.createdAtMs,
501
458
  ]);
502
- const liveAfterDelete = rowsLiveAfterDelete(commit.changes);
503
- commit.changes.forEach((change, idx) => {
504
- this.#buffer_('INSERT INTO sync_changes(partition, commit_seq, idx, tbl, row_id, op, row_version, scopes, payload) VALUES (?,?,?,?,?,?,?,?,?)', [
505
- p,
506
- commitSeq,
507
- idx,
508
- change.table,
509
- change.rowId,
510
- change.op === 'upsert' ? 1 : 2,
511
- change.rowVersion ?? null,
512
- JSON.stringify(change.scopes),
513
- change.payload ?? null,
514
- ]);
515
- if (change.op === 'delete' &&
516
- !liveAfterDelete.has(`${change.table}\u0000${change.rowId}`)) {
517
- // §5 delete precedence: every applied delete leaves a tombstone,
518
- // pruned with the commit log (§4.6). A delete that the same commit
519
- // follows with an upsert is a §2.2 scope move, not a deletion.
520
- this.#buffer_(`INSERT INTO sync_tombstones(partition, tbl, row_id, commit_seq) VALUES (?,?,?,?)
521
- ON CONFLICT(partition, tbl, row_id) DO UPDATE SET commit_seq=excluded.commit_seq`, [p, change.table, change.rowId, commitSeq]);
522
- }
523
- for (const [variable, value] of Object.entries(change.scopes)) {
524
- this.#buffer_('INSERT OR IGNORE INTO sync_change_scopes(partition, tbl, var, value, commit_seq) VALUES (?,?,?,?,?)', [p, change.table, variable, value, commitSeq]);
525
- }
526
- });
527
459
  return commitSeq;
528
460
  }
529
- async putPushResult(clientId, clientCommitId, result) {
530
- this.#assertOpen();
531
- this.#buffer_('INSERT OR IGNORE INTO sync_push_results(partition, client_id, client_commit_id, result) VALUES (?,?,?,?)', [this.#partition, clientId, clientCommitId, serializePushResult(result)]);
532
- }
533
- async enqueueReactions(reactions) {
534
- this.#assertOpen();
535
- for (const reaction of reactions) {
536
- this.#buffer_(`INSERT INTO sync_reactions(
537
- partition, idempotency_key, type, version, payload,
538
- source_client_id, source_client_commit_id, source_commit_seq,
539
- created_at_ms, available_at_ms, status, attempts, max_attempts
540
- ) VALUES (?,?,?,?,?,?,?,?,?,?,'pending',0,?)`, [
541
- this.#partition,
542
- reaction.idempotencyKey,
543
- reaction.type,
544
- reaction.version,
545
- JSON.stringify(reaction.payload),
546
- reaction.sourceClientId,
547
- reaction.sourceClientCommitId,
548
- reaction.sourceCommitSeq,
549
- reaction.createdAtMs,
550
- reaction.createdAtMs,
551
- reaction.maxAttempts,
552
- ]);
553
- }
554
- }
555
461
  async commit() {
556
- this.#assertOpen();
462
+ this.assertOpen();
557
463
  if (this.#buffer.length === 0) {
558
- this.#open = false;
464
+ this.open = false;
559
465
  return;
560
466
  }
561
467
  const statements = this.#buffer.map((entry) => this.#db.prepare(entry.sql).bind(...entry.params));
562
468
  // One atomic D1 batch — the §6.4 all-or-nothing commit.
563
469
  try {
564
470
  await this.#db.batch(statements);
565
- this.#open = false;
471
+ this.open = false;
566
472
  }
567
473
  catch (error) {
568
474
  if (isD1ConstraintError(error)) {
@@ -577,18 +483,13 @@ class D1Transaction {
577
483
  }
578
484
  }
579
485
  async rollback() {
580
- if (!this.#open)
486
+ if (!this.open)
581
487
  return;
582
- this.#open = false;
488
+ this.open = false;
583
489
  this.#buffer.length = 0;
584
490
  }
585
491
  }
586
492
  _a = D1Transaction;
587
- /**
588
- * D1 caps bound parameters at 100 per statement; the relational upsert
589
- * binds one per app column plus the five `_sync_*` meta columns.
590
- */
591
- const D1_MAX_BIND_PARAMS = 100;
592
493
  /**
593
494
  * The core tables `D1ServerStorage` reads or writes outside schema migration.
594
495
  * `migrateSchema` creates them in its core phase, which runs only when the
@@ -609,12 +510,14 @@ const D1_CORE_TABLES = [
609
510
  'sync_blob_refs',
610
511
  'sync_tombstones',
611
512
  ];
612
- export class D1ServerStorage {
513
+ export class D1ServerStorage extends SqliteFamilyStorage {
613
514
  #db;
614
515
  #pushApplySerialized;
615
- /** Set by `ensureSchema`: app-table lookup for the relational row store. */
616
- #tables;
617
- #schemaVersion;
516
+ /**
517
+ * D1 caps bound parameters at 100 per statement; the relational upsert
518
+ * binds one per app column plus the five `_sync_*` meta columns.
519
+ */
520
+ maxBindParams = 100;
618
521
  /**
619
522
  * Page reads waiting for the end of the current microtask turn. A pull
620
523
  * starts the window read (or first snapshot page) of every subscription
@@ -623,35 +526,80 @@ export class D1ServerStorage {
623
526
  */
624
527
  #pendingReads;
625
528
  constructor(db, options = {}) {
529
+ super();
626
530
  this.#db = db;
627
531
  this.#pushApplySerialized =
628
532
  options.pushApplySerialized === true ||
629
533
  options.commitValidationSerialized === true;
630
534
  }
535
+ async all(sql, params) {
536
+ return (await this.#db
537
+ .prepare(sql)
538
+ .bind(...params)
539
+ .all()).results;
540
+ }
541
+ /** Queue one read into the current turn's `db.batch` (see `#pendingReads`). */
542
+ guardedAll(sql, params) {
543
+ let pending = this.#pendingReads;
544
+ if (pending === undefined) {
545
+ const created = {
546
+ db: this.#schemaDatabase(),
547
+ entries: [],
548
+ };
549
+ pending = created;
550
+ this.#pendingReads = created;
551
+ queueMicrotask(() => {
552
+ this.#pendingReads = undefined;
553
+ created.db.batch(created.entries.map((entry) => entry.statement)).then((results) => {
554
+ created.entries.forEach((entry, i) => {
555
+ try {
556
+ entry.resolve(batchRows(results[i]));
557
+ }
558
+ catch (error) {
559
+ entry.reject(error);
560
+ }
561
+ });
562
+ }, (error) => {
563
+ for (const entry of created.entries)
564
+ entry.reject(error);
565
+ });
566
+ });
567
+ }
568
+ const target = pending;
569
+ const statement = target.db.prepare(sql).bind(...params);
570
+ return new Promise((resolve, reject) => {
571
+ target.entries.push({
572
+ statement,
573
+ resolve: (rows) => resolve(rows),
574
+ reject,
575
+ });
576
+ });
577
+ }
578
+ async atomic(statements) {
579
+ const results = await this.#db.batch(statements.map(({ sql, params }) => this.#db.prepare(sql).bind(...params)));
580
+ return results.map(batchRows);
581
+ }
631
582
  /** Apply the schema DDL (idempotent). Call once before use. */
632
583
  async migrate() {
633
584
  for (const statement of sqliteDdlStatements()) {
634
585
  await this.#db.exec(`${statement.replace(/\s+/g, ' ')};`);
635
586
  }
636
- const { results } = await this.#db
637
- .prepare('PRAGMA table_info("sync_clients")')
638
- .all();
639
- if (!results.some((column) => column.name === 'wire_version')) {
640
- await this.#db.exec('ALTER TABLE sync_clients ADD COLUMN wire_version INTEGER NOT NULL DEFAULT 1;');
641
- }
642
- for (const [name, definition] of Object.entries(SYNC_CLIENT_ROUND_COLUMNS)) {
643
- if (!results.some((column) => column.name === name)) {
644
- await this.#db.exec(`ALTER TABLE sync_clients ADD COLUMN ${name} ${definition};`);
645
- }
587
+ for (const sql of await this.#addedColumnDdl()) {
588
+ await this.#db.exec(`${sql};`);
646
589
  }
647
590
  }
648
- /** Resolve a table's compiled schema; row operations require `ensureSchema`. */
649
- table(name) {
650
- const table = this.#tables?.get(name);
651
- if (table === undefined) {
652
- throw new Error(`unknown table ${JSON.stringify(name)} — ensureSchema(schema) must run before row operations`);
653
- }
654
- return table;
591
+ /**
592
+ * `ALTER TABLE` statements for the `SQLITE_ADDED_COLUMNS` this database
593
+ * lacks, read in one statement.
594
+ */
595
+ async #addedColumnDdl() {
596
+ const { results } = await this.#db
597
+ .prepare('SELECT t.value AS tbl, c.name AS name FROM json_each(?) AS t, pragma_table_info(t.value) AS c')
598
+ .bind(JSON.stringify([
599
+ ...new Set(SQLITE_ADDED_COLUMNS.map(([table]) => table)),
600
+ ]))
601
+ .all();
602
+ return SQLITE_ADDED_COLUMNS.filter(([table, name]) => !results.some((column) => column.tbl === table && column.name === name)).map(([table, name, definition]) => `ALTER TABLE ${table} ADD COLUMN ${name} ${definition}`);
655
603
  }
656
604
  async ensureSchema(schema, checkpoints) {
657
605
  if (checkpoints !== undefined && checkpoints.length > 0) {
@@ -659,7 +607,7 @@ export class D1ServerStorage {
659
607
  // visibility here; refuse instead of accepting a weaker guarantee.
660
608
  throw new StorageQueryError('sync.storage.checkpoint_unsupported');
661
609
  }
662
- if (this.#schemaVersion === schema.version) {
610
+ if (this.schemaVersion === schema.version) {
663
611
  await this.#schemaDatabase().batch([]);
664
612
  return;
665
613
  }
@@ -677,7 +625,7 @@ export class D1ServerStorage {
677
625
  }
678
626
  const tables = [...schema.tables.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
679
627
  for (const table of tables) {
680
- if (tableColumnNames(table).length > D1_MAX_BIND_PARAMS) {
628
+ if (tableColumnNames(table).length > this.maxBindParams) {
681
629
  throw new Error('D1 caps statements at 100 bound parameters');
682
630
  }
683
631
  }
@@ -740,14 +688,11 @@ export class D1ServerStorage {
740
688
  primaryKeyPosition: column.pk,
741
689
  })), 'sqlite');
742
690
  }
743
- // A database from before the round-state columns gains them in place.
691
+ // A database from before a core-table column gains it in place.
744
692
  statementsExecuted++;
745
- const clientColumns = await this.#db
746
- .prepare('PRAGMA table_info("sync_clients")')
747
- .all();
748
- const missingRoundColumns = Object.entries(SYNC_CLIENT_ROUND_COLUMNS).filter(([name]) => !clientColumns.results.some((column) => column.name === name));
749
- if (missingRoundColumns.length > 0) {
750
- await batch(missingRoundColumns.map(([name, definition]) => this.#db.prepare(`ALTER TABLE sync_clients ADD COLUMN ${name} ${definition}`)));
693
+ const added = await this.#addedColumnDdl();
694
+ if (added.length > 0) {
695
+ await batch(added.map((sql) => this.#db.prepare(sql)));
751
696
  }
752
697
  // Server-only indexes reconcile at every open of a database already at
753
698
  // this version: they never reach a client, so no version bump is needed.
@@ -764,8 +709,8 @@ export class D1ServerStorage {
764
709
  if (reconcile.length > 0) {
765
710
  await batch(reconcile.map((sql) => this.#db.prepare(sql)));
766
711
  }
767
- this.#tables = schema.tables;
768
- this.#schemaVersion = schema.version;
712
+ this.tables = schema.tables;
713
+ this.schemaVersion = schema.version;
769
714
  return { complete: true, statementsExecuted };
770
715
  }
771
716
  if (marker !== null && marker.schema_version > schema.version) {
@@ -861,17 +806,14 @@ export class D1ServerStorage {
861
806
  break;
862
807
  continue;
863
808
  }
809
+ // The column read, the checkpoint pair, and every missing column.
864
810
  if (remaining < 4)
865
811
  break;
866
812
  statementsExecuted++;
867
- const columns = await this.#db
868
- .prepare('PRAGMA table_info("sync_clients")')
869
- .all();
870
- const missingColumns = Object.entries({
871
- wire_version: 'INTEGER NOT NULL DEFAULT 1',
872
- ...SYNC_CLIENT_ROUND_COLUMNS,
873
- }).filter(([name]) => !columns.results.some((column) => column.name === name));
874
- if (!(await save({ ...state, phase: 'prepare', offset: 0 }, missingColumns.map(([name, definition]) => this.#db.prepare(`ALTER TABLE sync_clients ADD COLUMN ${name} ${definition}`)))))
813
+ const added = await this.#addedColumnDdl();
814
+ if (remaining < 3 + added.length)
815
+ break;
816
+ if (!(await save({ ...state, phase: 'prepare', offset: 0 }, added.map((sql) => this.#db.prepare(sql)))))
875
817
  break;
876
818
  }
877
819
  else if (state.phase === 'prepare') {
@@ -971,8 +913,8 @@ export class D1ServerStorage {
971
913
  .bind(schema.version, layoutsOf(schema)),
972
914
  ], true)))
973
915
  break;
974
- this.#tables = schema.tables;
975
- this.#schemaVersion = schema.version;
916
+ this.tables = schema.tables;
917
+ this.schemaVersion = schema.version;
976
918
  return { complete: true, statementsExecuted };
977
919
  }
978
920
  const table = schema.tables.get(plan.table);
@@ -1010,52 +952,11 @@ export class D1ServerStorage {
1010
952
  return { complete: false, statementsExecuted };
1011
953
  }
1012
954
  /** Every protected read or write shares a transaction with its schema check. */
1013
- /** Queue one read into the current turn's `db.batch` (see `#pendingReads`). */
1014
- #batchedAll(sql, params) {
1015
- let pending = this.#pendingReads;
1016
- if (pending === undefined) {
1017
- const created = {
1018
- db: this.#schemaDatabase(),
1019
- entries: [],
1020
- };
1021
- pending = created;
1022
- this.#pendingReads = created;
1023
- queueMicrotask(() => {
1024
- this.#pendingReads = undefined;
1025
- created.db.batch(created.entries.map((entry) => entry.statement)).then((results) => {
1026
- created.entries.forEach((entry, i) => {
1027
- const result = results[i];
1028
- if (typeof result !== 'object' ||
1029
- result === null ||
1030
- !('results' in result) ||
1031
- !Array.isArray(result.results)) {
1032
- entry.reject(new Error('D1 query returned an invalid batch result'));
1033
- }
1034
- else {
1035
- entry.resolve(result.results);
1036
- }
1037
- });
1038
- }, (error) => {
1039
- for (const entry of created.entries)
1040
- entry.reject(error);
1041
- });
1042
- });
1043
- }
1044
- const target = pending;
1045
- const statement = target.db.prepare(sql).bind(...params);
1046
- return new Promise((resolve, reject) => {
1047
- target.entries.push({
1048
- statement,
1049
- resolve: (rows) => resolve(rows),
1050
- reject,
1051
- });
1052
- });
1053
- }
1054
955
  #schemaDatabase() {
1055
- if (this.#schemaVersion === undefined)
956
+ if (this.schemaVersion === undefined)
1056
957
  throw new StorageQueryError('sync.storage.schema_changed');
1057
958
  const db = this.#db;
1058
- const version = this.#schemaVersion;
959
+ const version = this.schemaVersion;
1059
960
  const sources = new WeakMap();
1060
961
  const execute = async (statements) => {
1061
962
  try {
@@ -1086,14 +987,9 @@ export class D1ServerStorage {
1086
987
  const prepare = (sql, params = []) => {
1087
988
  const source = db.prepare(sql).bind(...params);
1088
989
  const all = async () => {
1089
- const result = (await execute([source]))[0];
1090
- if (typeof result !== 'object' ||
1091
- result === null ||
1092
- !('results' in result) ||
1093
- !Array.isArray(result.results)) {
1094
- throw new Error('D1 query returned an invalid batch result');
1095
- }
1096
- return { results: result.results };
990
+ return {
991
+ results: batchRows((await execute([source]))[0]),
992
+ };
1097
993
  };
1098
994
  const statement = {
1099
995
  bind: (...values) => prepare(sql, values),
@@ -1114,180 +1010,27 @@ export class D1ServerStorage {
1114
1010
  })),
1115
1011
  };
1116
1012
  }
1117
- async touchPartition(partition, authenticatedAtMs, initialLogEpoch) {
1118
- if (initialLogEpoch.length === 0) {
1119
- throw new Error('initial log epoch must be non-empty');
1120
- }
1121
- const select = this.#db
1122
- .prepare(`SELECT log_epoch, epoch_required, last_authenticated_at_ms
1123
- FROM sync_partition_registry WHERE partition=?`)
1124
- .bind(partition);
1125
- let row = await select.first();
1126
- if (row === null ||
1127
- authenticatedAtMs - row.last_authenticated_at_ms >=
1128
- PARTITION_TOUCH_INTERVAL_MS) {
1129
- await this.#db
1130
- .prepare(`INSERT INTO sync_partition_registry(
1131
- partition, log_epoch, last_authenticated_at_ms
1132
- ) VALUES (?,?,?)
1133
- ON CONFLICT(partition) DO UPDATE SET
1134
- last_authenticated_at_ms=excluded.last_authenticated_at_ms`)
1135
- .bind(partition, initialLogEpoch, authenticatedAtMs)
1136
- .run();
1137
- row = await select.first();
1138
- }
1139
- if (row === null)
1140
- throw new Error('partition registry write did not persist');
1141
- return {
1142
- partition,
1143
- logEpoch: row.log_epoch,
1144
- epochRequired: row.epoch_required === 1,
1145
- lastAuthenticatedAtMs: row.last_authenticated_at_ms,
1146
- };
1147
- }
1148
- async rotatePartitionLogEpoch(partition, logEpoch, authenticatedAtMs) {
1149
- if (logEpoch.length === 0)
1150
- throw new Error('log epoch must be non-empty');
1151
- await this.#db.batch([
1152
- this.#db
1153
- .prepare(`INSERT INTO sync_partition_registry(
1154
- partition, log_epoch, epoch_required, last_authenticated_at_ms
1155
- ) VALUES (?,?,1,?)
1156
- ON CONFLICT(partition) DO UPDATE SET
1157
- log_epoch=excluded.log_epoch,
1158
- epoch_required=1,
1159
- last_authenticated_at_ms=excluded.last_authenticated_at_ms`)
1160
- .bind(partition, logEpoch, authenticatedAtMs),
1161
- this.#db
1162
- .prepare('DELETE FROM sync_clients WHERE partition=?')
1163
- .bind(partition),
1164
- ]);
1165
- return {
1166
- partition,
1167
- logEpoch,
1168
- epochRequired: true,
1169
- lastAuthenticatedAtMs: authenticatedAtMs,
1170
- };
1171
- }
1172
- async listPartitionRegistry() {
1173
- const { results } = await this.#db
1174
- .prepare(`SELECT partition, log_epoch, epoch_required, last_authenticated_at_ms
1175
- FROM sync_partition_registry ORDER BY partition`)
1176
- .all();
1177
- return results.map((row) => ({
1178
- partition: row.partition,
1179
- logEpoch: row.log_epoch,
1180
- epochRequired: row.epoch_required === 1,
1181
- lastAuthenticatedAtMs: row.last_authenticated_at_ms,
1182
- }));
1183
- }
1184
1013
  async begin(partition) {
1185
- const tables = this.#tables;
1014
+ const tables = this.tables;
1186
1015
  const db = this.#schemaDatabase();
1187
1016
  return new D1Transaction(db, partition, (name) => {
1188
1017
  const table = tables?.get(name);
1189
1018
  if (table === undefined)
1190
1019
  throw new StorageQueryError('sync.storage.schema_changed');
1191
1020
  return table;
1192
- }, (query) => this.#prepareAuthoritativeQuery(partition, query), this.#pushApplySerialized);
1193
- }
1194
- async getMaxCommitSeq(partition) {
1195
- const row = await this.#db
1196
- .prepare('SELECT max_commit_seq FROM sync_partitions WHERE partition=?')
1197
- .bind(partition)
1198
- .first();
1199
- return row?.max_commit_seq ?? 0;
1021
+ }, (query) => this.prepareAuthoritativeQuery(partition, query), this.#pushApplySerialized);
1200
1022
  }
1201
1023
  async queryAuthoritative(partition, query, _checkpoints) {
1202
1024
  // D1 declares no checkpoints. `#schemaDatabase` prepends a guard that
1203
1025
  // re-checks the published marker inside this same batch, so condition 2
1204
1026
  // is already evaluated with the query (§2.4 D1 schema readiness).
1205
1027
  const db = this.#schemaDatabase();
1206
- return queryAuthoritativeOn(db, partition, this.#prepareAuthoritativeQuery(partition, query));
1207
- }
1208
- #prepareAuthoritativeQuery(partition, query) {
1209
- if (this.#tables === undefined) {
1210
- throw new Error('ensureSchema(schema) must run before registered queries');
1211
- }
1212
- return bindAuthoritativePartition(prepareAuthoritativeQuery(query.plan, query.params, query.tables, this.#tables), partition);
1213
- }
1214
- async getPartitionLogEpoch(partition) {
1215
- const row = await this.#db
1216
- .prepare('SELECT log_epoch FROM sync_partition_registry WHERE partition=?')
1217
- .bind(partition)
1218
- .first();
1219
- return row?.log_epoch;
1220
- }
1221
- async getHorizonSeq(partition) {
1222
- const row = await this.#db
1223
- .prepare('SELECT horizon_seq FROM sync_partitions WHERE partition=?')
1224
- .bind(partition)
1225
- .first();
1226
- return row?.horizon_seq ?? 0;
1227
- }
1228
- async setHorizonSeq(partition, seq) {
1229
- await this.#db
1230
- .prepare('INSERT INTO sync_partitions(partition, horizon_seq) VALUES (?,?) ON CONFLICT(partition) DO UPDATE SET horizon_seq=max(horizon_seq,excluded.horizon_seq)')
1231
- .bind(partition, seq)
1232
- .run();
1028
+ return queryAuthoritativeOn(db, partition, this.prepareAuthoritativeQuery(partition, query));
1233
1029
  }
1234
1030
  async pruneCommitsThrough(partition, query) {
1235
- validateCommitPruneQuery(query);
1236
1031
  if (!this.#pushApplySerialized)
1237
1032
  throw new Error('D1 pruning requires externally serialized partition writes');
1238
- const epochGuard = 'EXISTS (SELECT 1 FROM sync_partition_registry WHERE partition=? AND log_epoch=?)';
1239
- const horizon = '(SELECT horizon_seq FROM sync_partitions WHERE partition=?)';
1240
- const results = await this.#db.batch([
1241
- this.#db
1242
- .prepare(`SELECT log_epoch, coalesce(${horizon},0) AS previous_horizon_seq FROM sync_partition_registry WHERE partition=?`)
1243
- .bind(partition, partition),
1244
- this.#db
1245
- .prepare(`INSERT INTO sync_partitions(partition,horizon_seq) SELECT ?,? WHERE ${epochGuard}
1246
- ON CONFLICT(partition) DO UPDATE SET horizon_seq=max(horizon_seq,excluded.horizon_seq)`)
1247
- .bind(partition, query.throughSeq, partition, query.logEpoch),
1248
- this.#db
1249
- .prepare(`SELECT horizon_seq, (SELECT count(*) FROM sync_commits WHERE partition=? AND commit_seq<=${horizon}) AS removed_commits FROM sync_partitions WHERE partition=?`)
1250
- .bind(partition, partition, partition),
1251
- ...[
1252
- 'sync_commits',
1253
- 'sync_changes',
1254
- 'sync_change_scopes',
1255
- 'sync_tombstones',
1256
- ].map((table) => this.#db
1257
- .prepare(`DELETE FROM ${table} WHERE partition=? AND commit_seq<=${horizon} AND ${epochGuard}`)
1258
- .bind(partition, partition, partition, query.logEpoch)),
1259
- ]);
1260
- const [before, after] = [results[0], results[2]].map((result) => {
1261
- if (typeof result !== 'object' ||
1262
- result === null ||
1263
- !('results' in result) ||
1264
- !Array.isArray(result.results)) {
1265
- throw new Error('D1 pruning returned an invalid batch result');
1266
- }
1267
- const row = result.results[0];
1268
- return typeof row === 'object' && row !== null
1269
- ? row
1270
- : undefined;
1271
- });
1272
- if (before?.log_epoch !== query.logEpoch)
1273
- throw new StorageQueryError('sync.storage.prune_epoch_mismatch');
1274
- if (typeof before.previous_horizon_seq !== 'number' ||
1275
- typeof after?.horizon_seq !== 'number' ||
1276
- typeof after.removed_commits !== 'number') {
1277
- throw new Error('D1 pruning returned invalid horizon metadata');
1278
- }
1279
- return {
1280
- previousHorizonSeq: before.previous_horizon_seq,
1281
- horizonSeq: after.horizon_seq,
1282
- removedCommits: after.removed_commits,
1283
- };
1284
- }
1285
- async getCommitSeqBefore(partition, createdBeforeMs) {
1286
- const row = await this.#db
1287
- .prepare('SELECT max(commit_seq) AS seq FROM sync_commits WHERE partition=? AND created_at_ms<?')
1288
- .bind(partition, createdBeforeMs)
1289
- .first();
1290
- return row?.seq ?? 0;
1033
+ return super.pruneCommitsThrough(partition, query);
1291
1034
  }
1292
1035
  async readCheckpoints(_partition) {
1293
1036
  // D1 supports no declared backfill checkpoints in this release: migrations
@@ -1319,640 +1062,9 @@ export class D1ServerStorage {
1319
1062
  async advanceCheckpoint(_partition, _name, _ownerEpoch, _watermark, _observedRows, _nowMs) {
1320
1063
  throw new StorageQueryError('sync.storage.checkpoint_unsupported');
1321
1064
  }
1322
- async sourceCoverageSeq(partition, tables) {
1323
- if (tables.length === 0)
1324
- return 0;
1325
- const tableParams = tables.map(() => '?').join(',');
1326
- const row = await this.#db
1327
- .prepare(`SELECT max(commit_seq) AS seq FROM sync_changes
1328
- WHERE partition=? AND tbl IN (${tableParams})`)
1329
- .bind(partition, ...tables)
1330
- .first();
1331
- return row?.seq ?? 0;
1332
- }
1333
- async hasSourceChangesAbove(partition, tables, seq) {
1334
- // Read the horizon before the window scan: a horizon past `seq` means the
1335
- // history that would answer the question is gone, so an empty scan proves
1336
- // nothing and must not be reported as `clean`.
1337
- const horizon = await this.getHorizonSeq(partition);
1338
- if (tables.length === 0)
1339
- return 'clean';
1340
- const tableParams = tables.map(() => '?').join(',');
1341
- const hit = await this.#db
1342
- .prepare(`SELECT 1 AS hit FROM sync_changes
1343
- WHERE partition=? AND tbl IN (${tableParams}) AND commit_seq>?
1344
- LIMIT 1`)
1345
- .bind(partition, ...tables, seq)
1346
- .first();
1347
- if (hit !== null)
1348
- return 'changed';
1349
- return horizon > seq ? 'unverifiable' : 'clean';
1350
- }
1351
- async getRow(partition, table, rowId) {
1352
- const db = this.#schemaDatabase();
1353
- const record = await db
1354
- .prepare(selectRowSql(this.table(table), 'sqlite'))
1355
- .bind(partition, rowId)
1356
- .first();
1357
- return record === null ? undefined : toStoredRow(record);
1358
- }
1359
- async getPushResult(partition, clientId, clientCommitId) {
1360
- const db = this.#schemaDatabase();
1361
- const record = await db
1362
- .prepare('SELECT result FROM sync_push_results WHERE partition=? AND client_id=? AND client_commit_id=?')
1363
- .bind(partition, clientId, clientCommitId)
1364
- .first();
1365
- if (record === null)
1366
- return undefined;
1367
- try {
1368
- return deserializePushResult(record.result);
1369
- }
1370
- catch {
1371
- throw syncError('sync.idempotency_cache_miss', 'persisted push result unreadable (§6.3)');
1372
- }
1373
- }
1374
- async claimReactions(partition, query) {
1375
- if (query.types.length === 0 || query.limit <= 0)
1376
- return [];
1377
- if (query.types.length + 7 > D1_MAX_BIND_PARAMS) {
1378
- throw new Error('D1 reaction claim exceeds the bound-parameter limit');
1379
- }
1380
- const typeParams = query.types.map(() => '?').join(',');
1381
- const { results } = await this.#db
1382
- .prepare(`UPDATE sync_reactions
1383
- SET status='leased', attempts=attempts+1,
1384
- lease_owner=?, lease_expires_at_ms=?, completed_at_ms=NULL
1385
- WHERE (partition, idempotency_key) IN (
1386
- SELECT partition, idempotency_key
1387
- FROM sync_reactions
1388
- WHERE partition=? AND type IN (${typeParams})
1389
- AND ((status='pending' AND available_at_ms<=?)
1390
- OR (status='leased' AND lease_expires_at_ms<=?))
1391
- ORDER BY CASE WHEN status='leased' THEN lease_expires_at_ms
1392
- ELSE available_at_ms END,
1393
- created_at_ms, idempotency_key
1394
- LIMIT ?
1395
- )
1396
- RETURNING *`)
1397
- .bind(query.leaseOwner, Math.min(Number.MAX_SAFE_INTEGER, query.nowMs + query.leaseDurationMs), partition, ...query.types, query.nowMs, query.nowMs, query.limit)
1398
- .all();
1399
- return results
1400
- .map(toStoredReaction)
1401
- .sort((a, b) => a.createdAtMs - b.createdAtMs ||
1402
- a.idempotencyKey.localeCompare(b.idempotencyKey));
1403
- }
1404
- async completeReaction(partition, idempotencyKey, leaseOwner, completedAtMs) {
1405
- const record = await this.#db
1406
- .prepare(`UPDATE sync_reactions
1407
- SET status='completed', completed_at_ms=?,
1408
- lease_owner=NULL, lease_expires_at_ms=NULL
1409
- WHERE partition=? AND idempotency_key=?
1410
- AND status='leased' AND lease_owner=?
1411
- RETURNING idempotency_key`)
1412
- .bind(completedAtMs, partition, idempotencyKey, leaseOwner)
1413
- .first();
1414
- return record !== null;
1415
- }
1416
- async extendReactionLease(partition, idempotencyKey, leaseOwner, leaseExpiresAtMs) {
1417
- const record = await this.#db
1418
- .prepare(`UPDATE sync_reactions SET lease_expires_at_ms=?
1419
- WHERE partition=? AND idempotency_key=?
1420
- AND status='leased' AND lease_owner=?
1421
- RETURNING idempotency_key`)
1422
- .bind(leaseExpiresAtMs, partition, idempotencyKey, leaseOwner)
1423
- .first();
1424
- return record !== null;
1425
- }
1426
- async failReaction(partition, idempotencyKey, update) {
1427
- const record = await this.#db
1428
- .prepare(`UPDATE sync_reactions
1429
- SET status=?, available_at_ms=?, last_failure=?,
1430
- lease_owner=NULL, lease_expires_at_ms=NULL
1431
- WHERE partition=? AND idempotency_key=?
1432
- AND status='leased' AND lease_owner=?
1433
- RETURNING idempotency_key`)
1434
- .bind(update.retryAtMs === undefined ? 'dead-letter' : 'pending', update.retryAtMs ?? update.failure.atMs, JSON.stringify(update.failure), partition, idempotencyKey, update.leaseOwner)
1435
- .first();
1436
- return record !== null;
1437
- }
1438
- async retryReaction(partition, idempotencyKey, nowMs) {
1439
- const record = await this.#db
1440
- .prepare(`UPDATE sync_reactions
1441
- SET status='pending', attempts=0, available_at_ms=?,
1442
- last_failure=NULL, lease_owner=NULL, lease_expires_at_ms=NULL,
1443
- completed_at_ms=NULL
1444
- WHERE partition=? AND idempotency_key=? AND status='dead-letter'
1445
- RETURNING idempotency_key`)
1446
- .bind(nowMs, partition, idempotencyKey)
1447
- .first();
1448
- return record !== null;
1449
- }
1450
- async getReaction(partition, idempotencyKey) {
1451
- const record = await this.#db
1452
- .prepare('SELECT * FROM sync_reactions WHERE partition=? AND idempotency_key=?')
1453
- .bind(partition, idempotencyKey)
1454
- .first();
1455
- return record === null ? undefined : toStoredReaction(record);
1456
- }
1457
- async listReactions(partition, query) {
1458
- const where = ['partition=?'];
1459
- const params = [partition];
1460
- if (query.statuses !== undefined && query.statuses.length > 0) {
1461
- where.push(`status IN (${query.statuses.map(() => '?').join(',')})`);
1462
- params.push(...query.statuses);
1463
- }
1464
- if (query.types !== undefined && query.types.length > 0) {
1465
- where.push(`type IN (${query.types.map(() => '?').join(',')})`);
1466
- params.push(...query.types);
1467
- }
1468
- if (params.length + 1 > D1_MAX_BIND_PARAMS) {
1469
- throw new Error('D1 reaction list exceeds the bound-parameter limit');
1470
- }
1471
- params.push(query.limit);
1472
- const { results } = await this.#db
1473
- .prepare(`SELECT * FROM sync_reactions WHERE ${where.join(' AND ')}
1474
- ORDER BY created_at_ms DESC, idempotency_key DESC LIMIT ?`)
1475
- .bind(...params)
1476
- .all();
1477
- return results.map(toStoredReaction);
1478
- }
1479
- async pruneReactions(partition, query) {
1480
- if (query.limit <= 0)
1481
- return { completed: 0, deadLetter: 0 };
1482
- const { results } = await this.#db
1483
- .prepare(`DELETE FROM sync_reactions
1484
- WHERE partition=? AND idempotency_key IN (
1485
- SELECT idempotency_key FROM sync_reactions
1486
- WHERE partition=?
1487
- AND ((status='completed' AND completed_at_ms IS NOT NULL
1488
- AND completed_at_ms<?)
1489
- OR (status='dead-letter' AND available_at_ms<?))
1490
- ORDER BY CASE WHEN status='completed' THEN completed_at_ms
1491
- ELSE available_at_ms END,
1492
- idempotency_key
1493
- LIMIT ?
1494
- )
1495
- AND ((status='completed' AND completed_at_ms IS NOT NULL
1496
- AND completed_at_ms<?)
1497
- OR (status='dead-letter' AND available_at_ms<?))
1498
- RETURNING status`)
1499
- .bind(partition, partition, query.completedBeforeMs, query.deadLetterBeforeMs, query.limit, query.completedBeforeMs, query.deadLetterBeforeMs)
1500
- .all();
1501
- return {
1502
- completed: results.filter((row) => row.status === 'completed').length,
1503
- deadLetter: results.filter((row) => row.status === 'dead-letter').length,
1504
- };
1505
- }
1506
- async latestScopedChangeSeq(partition, query) {
1507
- const firstVariable = Object.keys(query.scopeFilter).sort()[0];
1508
- const values = firstVariable === undefined
1509
- ? []
1510
- : (query.scopeFilter[firstVariable] ?? []);
1511
- if (firstVariable === undefined || values.length === 0)
1512
- return 0;
1513
- // Concurrent reads of a pull share one `db.batch` round trip.
1514
- const [row] = await this.#batchedAll(LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL, [partition, query.table, firstVariable, JSON.stringify(values)]);
1515
- return row?.seq ?? 0;
1516
- }
1517
- async readCommitWindow(partition, query) {
1518
- const variables = Object.keys(query.scopeFilter).sort();
1519
- const firstVariable = variables[0];
1520
- if (firstVariable === undefined)
1521
- return [];
1522
- const firstValues = query.scopeFilter[firstVariable] ?? [];
1523
- if (firstValues.length === 0)
1524
- return [];
1525
- // Candidates via the inverted index (one variable) LEFT JOINed to the
1526
- // commit meta + the table's changes — one statement per page, never two
1527
- // per candidate (see `commitWindowPageSql`), and concurrent pages share
1528
- // one D1 round trip. Exact multi-variable verification against the
1529
- // stored scope map in `collectCommitWindowPage`.
1530
- const sql = commitWindowPageSql(firstValues.length, 'sqlite');
1531
- const commits = [];
1532
- let deliveredChanges = 0;
1533
- let afterSeq = query.afterSeq;
1534
- const batchSize = Math.max(64, query.limitChanges);
1535
- while (deliveredChanges < query.limitChanges) {
1536
- const records = await this.#batchedAll(sql, [
1537
- partition,
1538
- query.table,
1539
- firstVariable,
1540
- ...firstValues,
1541
- afterSeq,
1542
- query.throughSeq,
1543
- batchSize,
1544
- partition,
1545
- partition,
1546
- query.table,
1547
- ]);
1548
- if (records.length === 0)
1549
- break;
1550
- const page = collectCommitWindowPage(records, query.scopeFilter, query.limitChanges - deliveredChanges);
1551
- commits.push(...page.commits);
1552
- deliveredChanges += page.delivered;
1553
- afterSeq = page.lastSeq;
1554
- if (page.candidateCount < batchSize)
1555
- break;
1556
- }
1557
- return commits;
1558
- }
1559
- async scanRows(partition, query) {
1560
- const firstVariable = assertScopeIndexedScan(query);
1561
- const firstValues = query.scopeFilter[firstVariable] ?? [];
1562
- if (firstValues.length === 0)
1563
- return [];
1564
- // Candidates via the inverted index LEFT JOINed to the row table — one
1565
- // statement per page, never one per row (see `scanRowPageSql`), and
1566
- // concurrent pages share one D1 round trip.
1567
- // Exact multi-variable verification against the stored scope map below.
1568
- const sql = scanRowPageSql(this.table(query.table), firstValues.length, 'sqlite');
1569
- const rows = [];
1570
- let afterRowId = query.afterRowId ?? '';
1571
- const batchSize = Math.max(64, query.limit);
1572
- while (rows.length < query.limit) {
1573
- const records = await this.#batchedAll(sql, [
1574
- partition,
1575
- query.table,
1576
- firstVariable,
1577
- ...firstValues,
1578
- afterRowId,
1579
- batchSize,
1580
- partition,
1581
- ]);
1582
- if (records.length === 0)
1583
- break;
1584
- for (const record of records) {
1585
- afterRowId = record.row_id;
1586
- // NULL payload: an index candidate whose row vanished — it still
1587
- // advances the keyset cursor (LEFT JOIN contract) but yields no row.
1588
- if (record.payload === null)
1589
- continue;
1590
- const stored = toStoredRow(record);
1591
- if (!matchesEffective(stored.scopes, query.scopeFilter))
1592
- continue;
1593
- rows.push(stored);
1594
- if (rows.length >= query.limit)
1595
- break;
1596
- }
1597
- if (records.length < batchSize)
1598
- break;
1599
- }
1600
- return rows;
1601
- }
1602
- async scanRowsByIndex(partition, query) {
1603
- const db = this.#schemaDatabase();
1604
- const table = this.table(query.table);
1605
- const index = resolveIndexRowScan(table, query);
1606
- const statement = indexRowPageStatement(table, index, query.values, partition, query.afterRowId, query.limit, 'sqlite');
1607
- const { results } = await db
1608
- .prepare(statement.sql)
1609
- .bind(...statement.params)
1610
- .all();
1611
- return results.map(toStoredRow);
1612
- }
1613
- async getClientRecord(partition, clientId) {
1614
- const record = await this.#db
1615
- .prepare('SELECT client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms, last_error_code, consecutive_failures FROM sync_clients WHERE partition=? AND client_id=?')
1616
- .bind(partition, clientId)
1617
- .first();
1618
- if (record === null)
1619
- return undefined;
1620
- return {
1621
- clientId: record.client_id,
1622
- actorId: record.actor_id,
1623
- wireVersion: record.wire_version,
1624
- cursor: record.cursor,
1625
- updatedAtMs: record.updated_at_ms,
1626
- subscriptions: JSON.parse(record.subscriptions),
1627
- ...clientRoundState(record.last_round_at_ms, record.last_error_code, record.consecutive_failures),
1628
- };
1629
- }
1630
- async putClientRecord(partition, record, options) {
1631
- // Update the existing record in place, so the table keeps one row per
1632
- // (partition, client_id). Every non-key field is written from the new
1633
- // record; a delete-and-insert would drop and recreate the row instead.
1634
- // `keepRoundOutcome` leaves the round-state columns alone (§1.5, §4.5).
1635
- await this.#db
1636
- .prepare(`INSERT INTO sync_clients(partition, client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms) VALUES (?,?,?,?,?,?,?,?)
1637
- ON CONFLICT(partition, client_id) DO UPDATE SET
1638
- actor_id=excluded.actor_id,
1639
- wire_version=excluded.wire_version,
1640
- cursor=excluded.cursor,
1641
- subscriptions=excluded.subscriptions,
1642
- updated_at_ms=excluded.updated_at_ms,
1643
- last_round_at_ms=excluded.last_round_at_ms${options?.keepRoundOutcome === true
1644
- ? ''
1645
- : `,
1646
- last_error_code=NULL,
1647
- consecutive_failures=0`}`)
1648
- .bind(partition, record.clientId, record.actorId, record.wireVersion, record.cursor, JSON.stringify(record.subscriptions), record.updatedAtMs, record.updatedAtMs)
1649
- .run();
1650
- }
1651
- async recordClientRoundFailure(partition, clientId, actorId, errorCode, atMs) {
1652
- await this.#db
1653
- .prepare(`UPDATE sync_clients
1654
- SET last_round_at_ms=?, last_error_code=?,
1655
- consecutive_failures=consecutive_failures+1
1656
- WHERE partition=? AND client_id=? AND actor_id=?`)
1657
- .bind(atMs, errorCode, partition, clientId, actorId)
1658
- .run();
1659
- }
1660
- async advanceClientCursor(partition, clientId, actorId, logEpoch, cursor, updatedAtMs) {
1661
- await this.#db
1662
- .prepare(`UPDATE sync_clients
1663
- SET cursor=MAX(cursor, ?), updated_at_ms=MAX(updated_at_ms, ?)
1664
- WHERE partition=? AND client_id=? AND actor_id=?
1665
- AND EXISTS (SELECT 1 FROM sync_partition_registry
1666
- WHERE partition=sync_clients.partition AND log_epoch=?)`)
1667
- .bind(cursor, updatedAtMs, partition, clientId, actorId, logEpoch)
1668
- .run();
1669
- }
1670
- async updateClientCursor(partition, clientId, cursor, updatedAtMs) {
1671
- await this.#db
1672
- .prepare(`UPDATE sync_clients
1673
- SET cursor=MAX(cursor, ?), updated_at_ms=MAX(updated_at_ms, ?)
1674
- WHERE partition=? AND client_id=?`)
1675
- .bind(cursor, updatedAtMs, partition, clientId)
1676
- .run();
1677
- }
1678
- async getActiveClientCursorFloor(partition, cutoffMs) {
1679
- const row = await this.#db
1680
- .prepare('SELECT MIN(cursor) AS cursor FROM sync_clients WHERE partition=? AND updated_at_ms>=?')
1681
- .bind(partition, cutoffMs)
1682
- .first();
1683
- return row.cursor;
1684
- }
1685
- async listClientCursors(partition) {
1686
- const { results } = await this.#db
1687
- .prepare('SELECT client_id, cursor, updated_at_ms FROM sync_clients WHERE partition=?')
1688
- .bind(partition)
1689
- .all();
1690
- return results.map((r) => ({
1691
- clientId: r.client_id,
1692
- cursor: r.cursor,
1693
- updatedAtMs: r.updated_at_ms,
1694
- }));
1695
- }
1696
- async listRowsReferencingBlob(partition, blobId) {
1697
- const db = this.#schemaDatabase();
1698
- const { results: refs } = await db
1699
- .prepare('SELECT tbl, row_id FROM sync_blob_refs WHERE partition=? AND blob_id=?')
1700
- .bind(partition, blobId)
1701
- .all();
1702
- const out = [];
1703
- for (const ref of refs) {
1704
- const compiled = this.#tables?.get(ref.tbl);
1705
- if (compiled === undefined)
1706
- continue; // table no longer in the schema
1707
- const row = await db
1708
- .prepare(selectRowScopesSql(compiled, 'sqlite'))
1709
- .bind(partition, ref.row_id)
1710
- .first();
1711
- if (row === null)
1712
- continue;
1713
- out.push({
1714
- table: ref.tbl,
1715
- rowId: ref.row_id,
1716
- scopes: JSON.parse(row.scopes),
1717
- });
1718
- }
1719
- return out;
1720
- }
1721
- async listReferencedBlobIds(partition) {
1722
- const { results } = await this.#db
1723
- .prepare('SELECT DISTINCT blob_id FROM sync_blob_refs WHERE partition=?')
1724
- .bind(partition)
1725
- .all();
1726
- return results.map((r) => r.blob_id);
1727
- }
1728
- async listScopeValueRows(partition, query) {
1729
- const { results } = await this.#db
1730
- .prepare(LIST_SCOPE_VALUE_ROWS_SQLITE_SQL)
1731
- .bind(partition, query.table, query.variable, query.value, query.limit)
1732
- .all();
1733
- return results.map((row) => row.row_id);
1734
- }
1735
1065
  async eraseScopeValueHistory(partition, query) {
1736
1066
  if (!this.#pushApplySerialized)
1737
1067
  throw new Error('D1 erasure requires externally serialized partition writes');
1738
- const params = eraseScopeValueHistoryParams(partition, query);
1739
- const results = await this.#db.batch(ERASE_SCOPE_VALUE_HISTORY_SQLITE_SQL.map((sql) => this.#db.prepare(sql).bind(...params)));
1740
- let removed = 0;
1741
- for (const result of results) {
1742
- if (typeof result !== 'object' ||
1743
- result === null ||
1744
- !('results' in result) ||
1745
- !Array.isArray(result.results)) {
1746
- throw new Error('D1 erasure returned an invalid batch result');
1747
- }
1748
- removed += result.results.length;
1749
- }
1750
- return removed;
1751
- }
1752
- // -- admin/console read surface --------------------------------------------
1753
- async listClientRecords(partition) {
1754
- const { results } = await this.#db
1755
- .prepare('SELECT client_id, actor_id, wire_version, cursor, subscriptions, updated_at_ms, last_round_at_ms, last_error_code, consecutive_failures FROM sync_clients WHERE partition=? ORDER BY updated_at_ms DESC')
1756
- .bind(partition)
1757
- .all();
1758
- return results.map((record) => ({
1759
- clientId: record.client_id,
1760
- actorId: record.actor_id,
1761
- wireVersion: record.wire_version,
1762
- cursor: record.cursor,
1763
- updatedAtMs: record.updated_at_ms,
1764
- subscriptions: JSON.parse(record.subscriptions),
1765
- ...clientRoundState(record.last_round_at_ms, record.last_error_code, record.consecutive_failures),
1766
- }));
1767
- }
1768
- async listCommitMetadata(partition, query) {
1769
- // Newest-first window, resumed by `afterSeq` (exclusive lower bound) and
1770
- // ended by `beforeSeq` (exclusive upper bound) when paging older. An
1771
- // omitted bound is the largest safe commitSeq, so one statement serves
1772
- // both the bounded and unbounded read.
1773
- const beforeSeq = query.beforeSeq ?? Number.MAX_SAFE_INTEGER;
1774
- const { results: rows } = query.table
1775
- ? await this.#db
1776
- .prepare(`SELECT c.commit_seq, c.client_id, c.client_commit_id, c.actor_id, c.created_at_ms
1777
- FROM sync_commits c
1778
- WHERE c.partition=? AND c.commit_seq>? AND c.commit_seq<?
1779
- AND EXISTS (SELECT 1 FROM sync_changes ch
1780
- WHERE ch.partition=c.partition AND ch.commit_seq=c.commit_seq AND ch.tbl=?)
1781
- ORDER BY c.commit_seq DESC LIMIT ?`)
1782
- .bind(partition, query.afterSeq, beforeSeq, query.table, query.limit)
1783
- .all()
1784
- : await this.#db
1785
- .prepare(`SELECT commit_seq, client_id, client_commit_id, actor_id, created_at_ms
1786
- FROM sync_commits
1787
- WHERE partition=? AND commit_seq>? AND commit_seq<?
1788
- ORDER BY commit_seq DESC LIMIT ?`)
1789
- .bind(partition, query.afterSeq, beforeSeq, query.limit)
1790
- .all();
1791
- const out = [];
1792
- // `changeSample`: extra statement(s) for the whole page — the first
1793
- // `sample` changes (change-index order) of every listed commit.
1794
- const sampleCount = query.changeSample !== undefined &&
1795
- Number.isInteger(query.changeSample) &&
1796
- query.changeSample > 0
1797
- ? query.changeSample
1798
- : 0;
1799
- const samples = new Map();
1800
- if (sampleCount > 0 && rows.length > 0) {
1801
- const seqs = rows.map((row) => row.commit_seq);
1802
- // D1 caps the bound parameters of one statement, so a page larger than
1803
- // that cap splits into a few chunk statements, never one per commit.
1804
- const chunkSize = D1_MAX_BIND_PARAMS - 2;
1805
- for (let offset = 0; offset < seqs.length; offset += chunkSize) {
1806
- const chunk = seqs.slice(offset, offset + chunkSize);
1807
- const { results: changes } = await this.#db
1808
- .prepare(`SELECT commit_seq, tbl, row_id, op FROM sync_changes
1809
- WHERE partition=? AND commit_seq IN (${placeholders(chunk.length)}) AND idx<?
1810
- ORDER BY commit_seq, idx`)
1811
- .bind(partition, ...chunk, sampleCount)
1812
- .all();
1813
- for (const change of changes) {
1814
- const sample = samples.get(change.commit_seq) ?? [];
1815
- sample.push({
1816
- table: change.tbl,
1817
- rowId: change.row_id,
1818
- op: change.op === 1 ? 'upsert' : 'delete',
1819
- });
1820
- samples.set(change.commit_seq, sample);
1821
- }
1822
- }
1823
- }
1824
- for (const row of rows) {
1825
- const { results: changes } = await this.#db
1826
- .prepare('SELECT tbl, count(*) AS n FROM sync_changes WHERE partition=? AND commit_seq=? GROUP BY tbl')
1827
- .bind(partition, row.commit_seq)
1828
- .all();
1829
- out.push({
1830
- commitSeq: row.commit_seq,
1831
- clientId: row.client_id,
1832
- clientCommitId: row.client_commit_id,
1833
- actorId: row.actor_id,
1834
- createdAtMs: row.created_at_ms,
1835
- changeCount: changes.reduce((sum, c) => sum + c.n, 0),
1836
- tables: changes.map((c) => c.tbl),
1837
- ...(sampleCount > 0
1838
- ? { sample: samples.get(row.commit_seq) ?? [] }
1839
- : {}),
1840
- });
1841
- }
1842
- return out;
1843
- }
1844
- async getCommit(partition, commitSeq) {
1845
- const meta = await this.#db
1846
- .prepare('SELECT client_id, client_commit_id, actor_id, created_at_ms FROM sync_commits WHERE partition=? AND commit_seq=?')
1847
- .bind(partition, commitSeq)
1848
- .first();
1849
- if (meta === null)
1850
- return undefined;
1851
- const { results: changeRows } = await this.#db
1852
- .prepare('SELECT tbl, row_id, op, row_version, scopes, payload FROM sync_changes WHERE partition=? AND commit_seq=? ORDER BY idx')
1853
- .bind(partition, commitSeq)
1854
- .all();
1855
- const changes = changeRows.map(toStoredChange);
1856
- return {
1857
- commitSeq,
1858
- clientId: meta.client_id,
1859
- clientCommitId: meta.client_commit_id,
1860
- actorId: meta.actor_id,
1861
- createdAtMs: meta.created_at_ms,
1862
- changeCount: changes.length,
1863
- tables: [...new Set(changes.map((change) => change.table))].sort(),
1864
- changes,
1865
- };
1866
- }
1867
- async listRowChanges(partition, query) {
1868
- // Retained-log read for one row: no dedicated index (the write path pays
1869
- // nothing for it), bounded by retention (§4.6).
1870
- const { results: rows } = await this.#db
1871
- .prepare(`SELECT ch.commit_seq, ch.op, ch.row_version, c.actor_id, c.client_id, c.created_at_ms
1872
- FROM sync_changes ch
1873
- JOIN sync_commits c
1874
- ON c.partition=ch.partition AND c.commit_seq=ch.commit_seq
1875
- WHERE ch.partition=? AND ch.tbl=? AND ch.row_id=?
1876
- ORDER BY ch.commit_seq DESC, ch.idx DESC LIMIT ?`)
1877
- .bind(partition, query.table, query.rowId, query.limit)
1878
- .all();
1879
- return rows.map((row) => ({
1880
- commitSeq: row.commit_seq,
1881
- op: row.op === 1 ? 'upsert' : 'delete',
1882
- ...(row.row_version !== null ? { rowVersion: row.row_version } : {}),
1883
- actorId: row.actor_id,
1884
- clientId: row.client_id,
1885
- createdAtMs: row.created_at_ms,
1886
- }));
1887
- }
1888
- async listRows(partition, query) {
1889
- // Keyset page over the row table's (partition, rowId) primary key: an
1890
- // index range read, no scope filter, no per-row lookup.
1891
- const { results } = await this.#schemaDatabase()
1892
- .prepare(`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
1893
- FROM ${quoteIdent(this.table(query.table).name)}
1894
- WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=? AND ${quoteIdent(SYNC_ROW_ID_COLUMN)}>?
1895
- ORDER BY ${quoteIdent(SYNC_ROW_ID_COLUMN)} LIMIT ?`)
1896
- .bind(partition, query.afterRowId ?? '', query.limit)
1897
- .all();
1898
- return results.map(toStoredRow);
1899
- }
1900
- async countRows(partition, table) {
1901
- const record = await this.#schemaDatabase()
1902
- .prepare(`SELECT count(*) AS n FROM ${quoteIdent(this.table(table).name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=?`)
1903
- .bind(partition)
1904
- .first();
1905
- return record?.n ?? 0;
1906
- }
1907
- async listPushResults(partition, clientCommitId) {
1908
- // Retained outcomes for one clientCommitId: no dedicated index, ordered
1909
- // by client so the read is deterministic.
1910
- const { results } = await this.#schemaDatabase()
1911
- .prepare('SELECT client_id, result FROM sync_push_results WHERE partition=? AND client_commit_id=? ORDER BY client_id')
1912
- .bind(partition, clientCommitId)
1913
- .all();
1914
- return results.map((record) => {
1915
- try {
1916
- return {
1917
- clientId: record.client_id,
1918
- result: deserializePushResult(record.result),
1919
- };
1920
- }
1921
- catch {
1922
- throw syncError('sync.idempotency_cache_miss', 'persisted push result unreadable (§6.3)');
1923
- }
1924
- });
1925
- }
1926
- async scopeActivity(partition, query) {
1927
- const { results: rows } = await this.#db
1928
- .prepare(`SELECT DISTINCT commit_seq, tbl FROM sync_change_scopes
1929
- WHERE partition=? AND var=? AND value=?
1930
- ORDER BY commit_seq DESC LIMIT ?`)
1931
- .bind(partition, query.variable, query.value, query.limit)
1932
- .all();
1933
- const out = [];
1934
- for (const row of rows) {
1935
- const meta = await this.#db
1936
- .prepare('SELECT actor_id, created_at_ms FROM sync_commits WHERE partition=? AND commit_seq=?')
1937
- .bind(partition, row.commit_seq)
1938
- .first();
1939
- if (meta === null)
1940
- continue;
1941
- const count = await this.#db
1942
- .prepare('SELECT count(*) AS n FROM sync_changes WHERE partition=? AND commit_seq=? AND tbl=?')
1943
- .bind(partition, row.commit_seq, row.tbl)
1944
- .first();
1945
- out.push({
1946
- commitSeq: row.commit_seq,
1947
- table: row.tbl,
1948
- createdAtMs: meta.created_at_ms,
1949
- actorId: meta.actor_id,
1950
- changeCount: count?.n ?? 0,
1951
- });
1952
- }
1953
- return out;
1954
- }
1955
- async listPartitions() {
1956
- return (await this.listPartitionRegistry()).map((entry) => entry.partition);
1068
+ return super.eraseScopeValueHistory(partition, query);
1957
1069
  }
1958
1070
  }