@syncular/server 0.58.0 → 0.60.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 (103) hide show
  1. package/README.md +49 -36
  2. package/dist/admin.d.ts +32 -20
  3. package/dist/admin.js +46 -40
  4. package/dist/blob-handlers.d.ts +25 -31
  5. package/dist/blob-handlers.js +63 -41
  6. package/dist/blob-store.d.ts +42 -24
  7. package/dist/blob-store.js +0 -0
  8. package/dist/bucket-blob-store.d.ts +12 -8
  9. package/dist/bucket-blob-store.js +54 -45
  10. package/dist/bucket-segment-store.js +18 -16
  11. package/dist/content-encoding.d.ts +5 -2
  12. package/dist/content-encoding.js +42 -13
  13. package/dist/context.d.ts +19 -5
  14. package/dist/context.js +19 -1
  15. package/dist/d1-storage.d.ts +14 -98
  16. package/dist/d1-storage.js +249 -1119
  17. package/dist/errors.js +16 -0
  18. package/dist/events.d.ts +36 -18
  19. package/dist/frame-bytes.js +24 -6
  20. package/dist/handler.js +503 -208
  21. package/dist/local-authority.d.ts +2 -0
  22. package/dist/local-authority.js +24 -12
  23. package/dist/object-bucket.d.ts +2 -0
  24. package/dist/operations-realtime.js +69 -50
  25. package/dist/operations.d.ts +5 -2
  26. package/dist/operations.js +20 -29
  27. package/dist/pg-executor-pglite.d.ts +5 -6
  28. package/dist/pg-executor-pglite.js +22 -14
  29. package/dist/pg-executor.d.ts +4 -1
  30. package/dist/postgres-storage.d.ts +24 -19
  31. package/dist/postgres-storage.js +420 -262
  32. package/dist/pull.d.ts +70 -47
  33. package/dist/pull.js +270 -301
  34. package/dist/push.d.ts +12 -3
  35. package/dist/push.js +361 -173
  36. package/dist/realtime.d.ts +85 -39
  37. package/dist/realtime.js +355 -219
  38. package/dist/relational-rows.d.ts +21 -66
  39. package/dist/relational-rows.js +28 -109
  40. package/dist/s3-blob-store.d.ts +11 -12
  41. package/dist/s3-blob-store.js +20 -13
  42. package/dist/s3-object-bucket.d.ts +8 -1
  43. package/dist/s3-object-bucket.js +16 -3
  44. package/dist/schema.d.ts +14 -0
  45. package/dist/schema.js +23 -0
  46. package/dist/scopes.d.ts +6 -2
  47. package/dist/scopes.js +22 -4
  48. package/dist/seed.js +10 -16
  49. package/dist/segment-download.d.ts +4 -1
  50. package/dist/segment-download.js +22 -6
  51. package/dist/signed-url.d.ts +2 -2
  52. package/dist/signed-url.js +9 -2
  53. package/dist/sigv4.d.ts +8 -2
  54. package/dist/sigv4.js +8 -4
  55. package/dist/sqlite-blob-store.d.ts +1 -1
  56. package/dist/sqlite-blob-store.js +23 -22
  57. package/dist/sqlite-dialect.d.ts +164 -84
  58. package/dist/sqlite-dialect.js +1145 -150
  59. package/dist/sqlite-lease-store.js +13 -18
  60. package/dist/sqlite-storage.d.ts +9 -62
  61. package/dist/sqlite-storage.js +303 -1123
  62. package/dist/storage-query.d.ts +30 -1
  63. package/dist/storage-query.js +68 -0
  64. package/dist/storage.d.ts +185 -63
  65. package/dist/storage.js +15 -0
  66. package/package.json +2 -2
  67. package/src/admin.ts +99 -57
  68. package/src/blob-handlers.ts +91 -72
  69. package/src/blob-store.ts +0 -0
  70. package/src/bucket-blob-store.ts +75 -48
  71. package/src/bucket-segment-store.ts +20 -15
  72. package/src/content-encoding.ts +48 -16
  73. package/src/context.ts +33 -6
  74. package/src/d1-storage.ts +350 -1700
  75. package/src/errors.ts +16 -0
  76. package/src/events.ts +37 -18
  77. package/src/frame-bytes.ts +26 -6
  78. package/src/handler.ts +654 -261
  79. package/src/local-authority.ts +27 -14
  80. package/src/object-bucket.ts +2 -0
  81. package/src/operations-realtime.ts +85 -61
  82. package/src/operations.ts +44 -42
  83. package/src/pg-executor-pglite.ts +27 -23
  84. package/src/pg-executor.ts +5 -1
  85. package/src/postgres-storage.ts +557 -354
  86. package/src/pull.ts +406 -440
  87. package/src/push.ts +466 -220
  88. package/src/realtime.ts +447 -290
  89. package/src/relational-rows.ts +43 -121
  90. package/src/s3-blob-store.ts +23 -13
  91. package/src/s3-object-bucket.ts +20 -2
  92. package/src/schema.ts +31 -0
  93. package/src/scopes.ts +23 -3
  94. package/src/seed.ts +13 -20
  95. package/src/segment-download.ts +33 -7
  96. package/src/signed-url.ts +10 -4
  97. package/src/sigv4.ts +17 -4
  98. package/src/sqlite-blob-store.ts +27 -26
  99. package/src/sqlite-dialect.ts +1851 -199
  100. package/src/sqlite-lease-store.ts +12 -23
  101. package/src/sqlite-storage.ts +390 -1821
  102. package/src/storage-query.ts +102 -0
  103. package/src/storage.ts +215 -67
@@ -5,11 +5,12 @@
5
5
  * server with `attach` (§12.3).
6
6
  */
7
7
  import {
8
- decodeMessage,
8
+ decodeResponse,
9
9
  decodeRow,
10
- encodeMessage,
10
+ encodeRequest,
11
11
  encodeSparseRow,
12
12
  PROTOCOL_WIRE_VERSION,
13
+ registrationDigest,
13
14
  type PushCommitFrame,
14
15
  type PushOperation,
15
16
  type PushResultFrame,
@@ -102,6 +103,7 @@ export type LocalAuthorityAttachDetails =
102
103
  readonly retryable: boolean;
103
104
  }
104
105
  | { readonly reason: 'log_epoch_changed' }
106
+ | { readonly reason: 'undo_pending' }
105
107
  | {
106
108
  readonly reason: 'schema_floor';
107
109
  readonly requiredSchemaVersion: number;
@@ -284,22 +286,15 @@ export async function openLocalAuthority(
284
286
  transport: (request: Uint8Array) => Promise<Uint8Array>,
285
287
  frames: RequestFrame[],
286
288
  ): Promise<ResponseFrame[]> => {
287
- const message = decodeMessage(
289
+ const message = decodeResponse(
288
290
  await transport(
289
- encodeMessage({
291
+ encodeRequest({
290
292
  wireVersion: PROTOCOL_WIRE_VERSION,
291
293
  msgKind: 'request',
292
294
  frames,
293
295
  }),
294
296
  ),
295
297
  );
296
- if (message.msgKind !== 'response') {
297
- throw new LocalAuthorityAttachError({
298
- reason: 'request_failed',
299
- code: 'sync.invalid_request',
300
- retryable: false,
301
- });
302
- }
303
298
  const header = message.frames[0];
304
299
  if (header?.type === 'RESP_HEADER' && header.requiredSchemaVersion) {
305
300
  throw new LocalAuthorityAttachError({
@@ -335,6 +330,9 @@ export async function openLocalAuthority(
335
330
  limitSnapshotRows: 0,
336
331
  maxSnapshotPages: 0,
337
332
  accept: 0b0011,
333
+ cursor: -1,
334
+ registrationDigest: registrationDigest([]),
335
+ echoDigest: new Uint8Array(),
338
336
  },
339
337
  ])
340
338
  )[0];
@@ -431,7 +429,13 @@ export async function openLocalAuthority(
431
429
  uploading = false;
432
430
  }
433
431
  }
434
- writeMeta({ state: 'local', attach_id: null, sent: null, applied: null });
432
+ writeMeta({
433
+ state: 'local',
434
+ attach_id: null,
435
+ sent: null,
436
+ applied: null,
437
+ undo: null,
438
+ });
435
439
  state = 'local';
436
440
  },
437
441
  };
@@ -451,6 +455,10 @@ export async function openLocalAuthority(
451
455
  state = 'local';
452
456
  throw error;
453
457
  }
458
+ } else if (readMeta('undo') !== undefined) {
459
+ // §12.3: resuming after a started rollback would count `cached`
460
+ // uploads whose rows the rollback deleted.
461
+ throw new LocalAuthorityAttachError({ reason: 'undo_pending' });
454
462
  }
455
463
  const attachId = attachIdOf();
456
464
  const commits = planAttach(database, compiled, attachId);
@@ -479,7 +487,8 @@ export async function openLocalAuthority(
479
487
 
480
488
  /**
481
489
  * §12.3 abandon: settle the one commit whose outcome may be unknown, then
482
- * delete the rows of every applied commit, children before parents.
490
+ * delete the rows of every applied commit, children before parents, under
491
+ * commit ids fresh to this attempt (the persisted `undo` marker).
483
492
  */
484
493
  async function rollback(
485
494
  transport: (request: Uint8Array) => Promise<Uint8Array>,
@@ -505,6 +514,8 @@ export async function openLocalAuthority(
505
514
  op: 'delete',
506
515
  }),
507
516
  );
517
+ const attempt = ordinal('undo') + 1;
518
+ writeMeta({ undo: String(attempt) });
508
519
  for (
509
520
  let start = 0;
510
521
  start < deletes.length;
@@ -512,7 +523,8 @@ export async function openLocalAuthority(
512
523
  ) {
513
524
  const commit: PushCommitFrame = {
514
525
  type: 'PUSH_COMMIT',
515
- clientCommitId: `${attachId}:undo:${start / ATTACH_OPERATION_BUDGET}`,
526
+ clientCommitId: `${attachId}:undo:${attempt}:${start / ATTACH_OPERATION_BUDGET}`,
527
+ clientSeq: 0,
516
528
  operations: deletes.slice(start, start + ATTACH_OPERATION_BUDGET),
517
529
  };
518
530
  const result = await send(commit);
@@ -619,6 +631,7 @@ function planAttach(
619
631
  commits.push({
620
632
  type: 'PUSH_COMMIT',
621
633
  clientCommitId: `${attachId}:${commits.length}`,
634
+ clientSeq: 0,
622
635
  operations,
623
636
  });
624
637
  operations = [];
@@ -34,6 +34,8 @@ export interface BucketObject {
34
34
  export interface BucketObjectHead {
35
35
  readonly etag: string | undefined;
36
36
  readonly size: number | undefined;
37
+ /** Backend last-modified time (epoch ms): S3 `Last-Modified`, R2 `uploaded`. */
38
+ readonly lastModifiedMs: number | undefined;
37
39
  readonly metadata: Readonly<Record<string, string>>;
38
40
  }
39
41
 
@@ -4,7 +4,7 @@ import {
4
4
  type RemoteOperationRealtimeMessage,
5
5
  } from '@syncular/core';
6
6
  import type { RealtimeNotifier, SyncRequestContext } from './context';
7
- import { REMOTE_COMMAND_CLIENT_ID_PREFIX } from './context';
7
+ import { limitsOf, REMOTE_COMMAND_CLIENT_ID_PREFIX } from './context';
8
8
  import { reportError, SyncError, syncError } from './errors';
9
9
  import type {
10
10
  RegisteredRemoteQuery,
@@ -22,16 +22,24 @@ interface WatchState {
22
22
  readonly clientId: string;
23
23
  readonly operation: RegisteredRemoteQuery;
24
24
  readonly params: unknown;
25
- running: boolean;
26
- dirty: boolean;
27
25
  }
28
26
 
27
+ /**
28
+ * One socket's registered-query watches. A session runs one refresh at a
29
+ * time: `notify` queues each affected watch once, and the drain reruns the
30
+ * queue in order, so a commit costs at most one query per watch and never
31
+ * more than one query at a time.
32
+ */
29
33
  class WatchSession implements RemoteOperationWatchSession {
30
34
  readonly #ctx: SyncRequestContext;
31
35
  readonly #registry: RemoteOperationRegistry;
32
36
  readonly #send: (bytes: Uint8Array) => void;
33
37
  readonly #closed: () => void;
38
+ readonly #maxWatches: number;
34
39
  readonly #watches = new Map<string, WatchState>();
40
+ /** Watches waiting for a refresh, in arrival order, each at most once. */
41
+ readonly #pending = new Set<WatchState>();
42
+ #draining: Promise<void> | undefined;
35
43
  #isClosed = false;
36
44
 
37
45
  get partition(): string {
@@ -48,6 +56,7 @@ class WatchSession implements RemoteOperationWatchSession {
48
56
  this.#registry = registry;
49
57
  this.#send = send;
50
58
  this.#closed = closed;
59
+ this.#maxWatches = limitsOf(ctx).maxWatchesPerSession;
51
60
  }
52
61
 
53
62
  async receive(bytes: Uint8Array): Promise<void> {
@@ -80,6 +89,8 @@ class WatchSession implements RemoteOperationWatchSession {
80
89
  throw syncError('operation.invalid_request');
81
90
  }
82
91
  if (message.kind === 'unwatch') {
92
+ const state = this.#watches.get(message.watchId);
93
+ if (state !== undefined) this.#pending.delete(state);
83
94
  this.#watches.delete(message.watchId);
84
95
  return;
85
96
  }
@@ -122,13 +133,25 @@ class WatchSession implements RemoteOperationWatchSession {
122
133
  this.#sendError(message.watchId, syncError('operation.unknown'));
123
134
  return;
124
135
  }
136
+ if (this.#watches.size >= this.#maxWatches) {
137
+ this.#sendError(
138
+ message.watchId,
139
+ syncError(
140
+ 'sync.limit_exceeded',
141
+ 'watch session holds the maximum number of watches',
142
+ JSON.stringify({
143
+ limit: 'maxWatchesPerSession',
144
+ max: this.#maxWatches,
145
+ }),
146
+ ),
147
+ );
148
+ return;
149
+ }
125
150
  const state: WatchState = {
126
151
  watchId: message.watchId,
127
152
  clientId: message.clientId,
128
153
  operation,
129
154
  params: message.params,
130
- running: false,
131
- dirty: false,
132
155
  };
133
156
  this.#watches.set(message.watchId, state);
134
157
  await this.#refresh(state);
@@ -136,68 +159,68 @@ class WatchSession implements RemoteOperationWatchSession {
136
159
 
137
160
  notify(tables: ReadonlySet<string>): void {
138
161
  for (const state of this.#watches.values()) {
139
- if (!state.operation.tables.some((table) => tables.has(table))) continue;
140
- if (state.running) state.dirty = true;
141
- else void this.#refresh(state);
162
+ if (state.operation.tables.some((table) => tables.has(table))) {
163
+ void this.#refresh(state);
164
+ }
142
165
  }
143
166
  }
144
167
 
145
- async #refresh(state: WatchState): Promise<void> {
146
- if (this.#isClosed || this.#watches.get(state.watchId) !== state) return;
147
- if (state.running) {
148
- state.dirty = true;
149
- return;
168
+ /** Queue `state` and resolve when the session's queue has drained. */
169
+ #refresh(state: WatchState): Promise<void> {
170
+ this.#pending.add(state);
171
+ this.#draining ??= this.#drain();
172
+ return this.#draining;
173
+ }
174
+
175
+ async #drain(): Promise<void> {
176
+ try {
177
+ // A watch queued again while it runs is appended and runs once more.
178
+ for (const state of this.#pending) {
179
+ this.#pending.delete(state);
180
+ await this.#run(state);
181
+ }
182
+ } finally {
183
+ this.#draining = undefined;
150
184
  }
151
- state.running = true;
185
+ }
186
+
187
+ async #run(state: WatchState): Promise<void> {
188
+ if (this.#isClosed || this.#watches.get(state.watchId) !== state) return;
152
189
  try {
153
- do {
154
- state.dirty = false;
155
- try {
156
- const response = await state.operation.run(
157
- this.#ctx,
158
- state.clientId,
159
- state.params,
160
- );
161
- if (this.#isClosed || this.#watches.get(state.watchId) !== state) {
162
- return;
163
- }
164
- if (response.kind !== 'query') {
165
- throw syncError('operation.query_failed');
166
- }
167
- if (
168
- !this.#emit(
169
- encodeRemoteOperationRealtimeMessage({
170
- revision: 1,
171
- kind: 'snapshot',
172
- watchId: state.watchId,
173
- operationId: response.operationId,
174
- rows: response.rows,
175
- maxCommitSeq: response.maxCommitSeq,
176
- }),
177
- )
178
- )
179
- return;
180
- } catch (error) {
181
- if (this.#isClosed || this.#watches.get(state.watchId) !== state) {
182
- return;
183
- }
184
- if (!(error instanceof SyncError)) {
185
- reportError(error, this.#ctx.onError, 'operations');
186
- }
187
- this.#sendError(
188
- state.watchId,
189
- error instanceof SyncError
190
- ? error
191
- : syncError('operation.query_failed'),
192
- );
193
- }
194
- } while (
195
- state.dirty &&
196
- !this.#isClosed &&
197
- this.#watches.get(state.watchId) === state
190
+ const response = await state.operation.run(
191
+ this.#ctx,
192
+ state.clientId,
193
+ state.params,
194
+ );
195
+ if (this.#isClosed || this.#watches.get(state.watchId) !== state) {
196
+ return;
197
+ }
198
+ if (response.kind !== 'query') {
199
+ throw syncError('operation.query_failed');
200
+ }
201
+ this.#emit(
202
+ encodeRemoteOperationRealtimeMessage({
203
+ revision: 1,
204
+ kind: 'snapshot',
205
+ watchId: state.watchId,
206
+ operationId: response.operationId,
207
+ rows: response.rows,
208
+ maxCommitSeq: response.maxCommitSeq,
209
+ }),
210
+ );
211
+ } catch (error) {
212
+ if (this.#isClosed || this.#watches.get(state.watchId) !== state) {
213
+ return;
214
+ }
215
+ if (!(error instanceof SyncError)) {
216
+ reportError(error, this.#ctx.onError, 'operations');
217
+ }
218
+ this.#sendError(
219
+ state.watchId,
220
+ error instanceof SyncError
221
+ ? error
222
+ : syncError('operation.query_failed'),
198
223
  );
199
- } finally {
200
- state.running = false;
201
224
  }
202
225
  }
203
226
 
@@ -229,6 +252,7 @@ class WatchSession implements RemoteOperationWatchSession {
229
252
  if (this.#isClosed) return;
230
253
  this.#isClosed = true;
231
254
  this.#watches.clear();
255
+ this.#pending.clear();
232
256
  this.#closed();
233
257
  }
234
258
  }
package/src/operations.ts CHANGED
@@ -46,11 +46,14 @@ export interface RemoteQueryDependency {
46
46
  readonly scopeKeys?: readonly string[];
47
47
  }
48
48
 
49
+ /**
50
+ * The window units a scoped query reads (§4.8): the client's `WindowBase`
51
+ * (a variable and one value per other variable) and the variable's values.
52
+ */
49
53
  export interface RemoteQueryCoverage {
50
54
  readonly base: {
51
- readonly table: string;
52
55
  readonly variable: string;
53
- readonly fixedScopes?: Readonly<Record<string, readonly string[]>>;
56
+ readonly fixedScopes?: Readonly<Record<string, string>>;
54
57
  };
55
58
  readonly units: readonly string[];
56
59
  }
@@ -384,46 +387,48 @@ export function registerRemoteQuery<Params>(
384
387
  );
385
388
  }
386
389
  const coverage = descriptor.coverage(params);
387
- const coverageByTable = new Map<string, RemoteQueryCoverage>();
388
- for (const entry of coverage) {
389
- if (
390
- coverageByTable.has(entry.base.table) ||
391
- !descriptor.tables.includes(entry.base.table)
392
- ) {
393
- throw syncError(
394
- 'operation.invalid_request',
395
- 'scoped remote query has invalid generated scope coverage',
396
- );
397
- }
398
- coverageByTable.set(entry.base.table, entry);
399
- }
400
- if (descriptor.tables.some((table) => !coverageByTable.has(table))) {
390
+ // A unit names every variable of a table it lets a query read in
391
+ // full: a variable left open would read rows outside the grant, and
392
+ // the wildcard fence (§3.2) has no counterpart in a SQL read.
393
+ const exact = (
394
+ entry: RemoteQueryCoverage,
395
+ declared: ReadonlySet<string>,
396
+ ): boolean => {
397
+ const variables = new Set([
398
+ entry.base.variable,
399
+ ...Object.keys(entry.base.fixedScopes ?? {}),
400
+ ]);
401
+ return (
402
+ variables.size === declared.size &&
403
+ [...declared].every((variable) => variables.has(variable))
404
+ );
405
+ };
406
+ const declared = descriptor.tables.map(
407
+ (name) => schema.tables.get(name)?.declaredVariables,
408
+ );
409
+ if (
410
+ declared.some(
411
+ (variables) =>
412
+ variables === undefined ||
413
+ !coverage.some((entry) => exact(entry, variables)),
414
+ )
415
+ ) {
401
416
  throw syncError(
402
417
  'operation.invalid_request',
403
418
  'scoped remote query lacks complete generated scope coverage',
404
419
  );
405
420
  }
406
421
  for (const entry of coverage) {
407
- const table = schema.tables.get(entry.base.table);
408
422
  const fixedScopes = entry.base.fixedScopes ?? {};
409
- const coveredVariables = new Set([
410
- entry.base.variable,
411
- ...Object.keys(fixedScopes),
412
- ]);
413
423
  if (
414
- table === undefined ||
415
- Object.prototype.hasOwnProperty.call(
416
- fixedScopes,
417
- entry.base.variable,
418
- ) ||
419
- coveredVariables.size !== table.declaredVariables.size ||
420
- [...table.declaredVariables].some(
421
- (variable) => !coveredVariables.has(variable),
424
+ Object.hasOwn(fixedScopes, entry.base.variable) ||
425
+ !declared.some(
426
+ (variables) => variables !== undefined && exact(entry, variables),
422
427
  )
423
428
  ) {
424
429
  throw syncError(
425
430
  'operation.invalid_request',
426
- 'scoped remote query lacks complete generated scope coverage',
431
+ 'scoped remote query has invalid generated scope coverage',
427
432
  );
428
433
  }
429
434
  if (entry.units.length === 0) {
@@ -432,18 +437,15 @@ export function registerRemoteQuery<Params>(
432
437
  'scoped remote query has an empty scope unit',
433
438
  );
434
439
  }
435
- for (const value of entry.units) {
436
- if (!scopeAllowed(allowed, entry.base.variable, value)) {
437
- throw syncError('operation.forbidden');
438
- }
439
- }
440
- for (const [variable, values] of Object.entries(fixedScopes)) {
441
- if (
442
- values.length === 0 ||
443
- values.some((value) => !scopeAllowed(allowed, variable, value))
444
- ) {
445
- throw syncError('operation.forbidden');
446
- }
440
+ if (
441
+ entry.units.some(
442
+ (value) => !scopeAllowed(allowed, entry.base.variable, value),
443
+ ) ||
444
+ Object.entries(fixedScopes).some(
445
+ ([variable, value]) => !scopeAllowed(allowed, variable, value),
446
+ )
447
+ ) {
448
+ throw syncError('operation.forbidden');
447
449
  }
448
450
  }
449
451
  } else if (
@@ -9,21 +9,18 @@ import { retainRollbackFailure } from './storage-errors';
9
9
  * `PgExecutor` interface (see the server README).
10
10
  *
11
11
  * pglite is single-connection, so `transaction` runs `BEGIN`/`COMMIT`/
12
- * `ROLLBACK` on the one connection. Overlapping `transaction` calls are
13
- * serialized through a promise chain (mirroring `SqliteServerStorage`'s
14
- * begin() FIFO): a nested BEGIN on Postgres is a warning-level no-op, so
15
- * interleaved scopes would silently collapse into one SQL transaction and
16
- * break the push layer's serialization guarantee on this dev driver.
12
+ * `ROLLBACK` on the one connection. Transactions and pool-level `query` calls
13
+ * share one promise chain (mirroring `SqliteServerStorage`): a nested BEGIN
14
+ * on Postgres is a warning-level no-op, so interleaved scopes would silently
15
+ * collapse into one SQL transaction, and a pool-level statement issued while
16
+ * a transaction is open would read its uncommitted rows or join it (§6.4).
17
17
  */
18
18
  import type { PGlite } from '@electric-sql/pglite';
19
19
  import type { PgExecutor, PgQueryable, PgRow } from './pg-executor';
20
20
 
21
21
  /** Minimal structural type for a pglite instance (avoids a hard type dep). */
22
22
  interface PgliteLike {
23
- query<Row>(
24
- query: string,
25
- params?: unknown[],
26
- ): Promise<{ rows: Row[]; affectedRows?: number }>;
23
+ query<Row>(query: string, params?: unknown[]): Promise<{ rows: Row[] }>;
27
24
  exec(query: string): Promise<unknown>;
28
25
  close(): Promise<void>;
29
26
  }
@@ -35,10 +32,7 @@ function queryable(db: PgliteLike): PgQueryable {
35
32
  text,
36
33
  params ? [...params] : undefined,
37
34
  );
38
- return {
39
- rows: result.rows,
40
- rowCount: result.affectedRows ?? result.rows.length,
41
- };
35
+ return { rows: result.rows };
42
36
  },
43
37
  };
44
38
  }
@@ -46,18 +40,28 @@ function queryable(db: PgliteLike): PgQueryable {
46
40
  export function pgliteExecutor(db: PGlite | PgliteLike): PgExecutor {
47
41
  const like = db as PgliteLike;
48
42
  const q = queryable(like);
49
- // One BEGIN…COMMIT/ROLLBACK scope at a time on the single connection (see
50
- // the file header).
51
- let transactionTail: Promise<void> = Promise.resolve();
43
+ // One caller at a time on the single connection (see the file header).
44
+ let tail: Promise<void> = Promise.resolve();
45
+ const acquire = async (): Promise<() => void> => {
46
+ const previous = tail;
47
+ let release!: () => void;
48
+ tail = new Promise<void>((resolve) => {
49
+ release = resolve;
50
+ });
51
+ await previous;
52
+ return release;
53
+ };
52
54
  return {
53
- query: q.query,
55
+ async query<Row = PgRow>(text: string, params?: readonly unknown[]) {
56
+ const release = await acquire();
57
+ try {
58
+ return await q.query<Row>(text, params);
59
+ } finally {
60
+ release();
61
+ }
62
+ },
54
63
  async transaction<T>(fn: (client: PgQueryable) => Promise<T>): Promise<T> {
55
- const previous = transactionTail;
56
- let release!: () => void;
57
- transactionTail = new Promise<void>((resolve) => {
58
- release = resolve;
59
- });
60
- await previous;
64
+ const release = await acquire();
61
65
  try {
62
66
  await like.exec('BEGIN');
63
67
  try {
@@ -49,12 +49,16 @@ export type PgRow = Record<string, unknown>;
49
49
  * a time. An adapter does not need to queue calls itself, and must not
50
50
  * accept two in flight: node-postgres warns about that today and rejects it
51
51
  * in node-postgres 9.
52
+ *
53
+ * The result carries returned rows only. Drivers disagree on the affected-row
54
+ * count of a statement without result rows (Bun.sql reports none), so every
55
+ * write whose outcome matters ends in `RETURNING` and counts `rows`.
52
56
  */
53
57
  export interface PgQueryable {
54
58
  query<Row = PgRow>(
55
59
  text: string,
56
60
  params?: readonly unknown[],
57
- ): Promise<{ rows: Row[]; rowCount: number }>;
61
+ ): Promise<{ rows: Row[] }>;
58
62
  }
59
63
 
60
64
  /**