@powersync/service-module-mongodb-storage 0.0.0-dev-20250813080357 → 0.0.0-dev-20250819134004

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.
@@ -4,7 +4,7 @@ import { event_types } from '@powersync/service-types';
4
4
  import { PowerSyncMongo } from './implementation/db.js';
5
5
  import { logger } from '@powersync/lib-services-framework';
6
6
 
7
- export class MongoReportStorage implements storage.ReportStorageFactory {
7
+ export class MongoReportStorage implements storage.ReportStorage {
8
8
  private readonly client: mongo.MongoClient;
9
9
  public readonly db: PowerSyncMongo;
10
10
 
@@ -12,6 +12,94 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
12
12
  this.client = db.client;
13
13
  this.db = db;
14
14
  }
15
+ async deleteOldConnectionData(data: event_types.DeleteOldConnectionData): Promise<void> {
16
+ const { date } = data;
17
+ const result = await this.db.sdk_report_events.deleteMany({
18
+ connected_at: { $lt: date },
19
+ $or: [
20
+ { disconnected_at: { $exists: true } },
21
+ { jwt_exp: { $lt: new Date() }, disconnected_at: { $exists: false } }
22
+ ]
23
+ });
24
+ if (result.deletedCount > 0) {
25
+ logger.info(
26
+ `TTL from ${date.toISOString()}: ${result.deletedCount} MongoDB documents have been removed from sdk_report_events.`
27
+ );
28
+ }
29
+ }
30
+
31
+ async getClientConnectionReports(
32
+ data: event_types.ClientConnectionReportRequest
33
+ ): Promise<event_types.ClientConnectionReport> {
34
+ const { start, end } = data;
35
+ const result = await this.db.sdk_report_events
36
+ .aggregate<event_types.ClientConnectionReport>([
37
+ {
38
+ $match: {
39
+ connected_at: { $lte: end, $gte: start }
40
+ }
41
+ },
42
+ this.connectionsFacetPipeline(),
43
+ this.connectionsProjectPipeline()
44
+ ])
45
+ .toArray();
46
+ return result[0];
47
+ }
48
+
49
+ async reportClientConnection(data: event_types.ClientConnectionBucketData): Promise<void> {
50
+ const updateFilter = this.updateDocFilter(data.user_id, data.client_id!);
51
+ await this.db.sdk_report_events.findOneAndUpdate(
52
+ updateFilter,
53
+ {
54
+ $set: data,
55
+ $unset: {
56
+ disconnected_at: ''
57
+ }
58
+ },
59
+ {
60
+ upsert: true
61
+ }
62
+ );
63
+ }
64
+ async reportClientDisconnection(data: event_types.ClientDisconnectionEventData): Promise<void> {
65
+ const { connected_at, user_id, client_id } = data;
66
+ await this.db.sdk_report_events.findOneAndUpdate(
67
+ {
68
+ client_id,
69
+ user_id,
70
+ connected_at
71
+ },
72
+ {
73
+ $set: {
74
+ disconnected_at: data.disconnected_at
75
+ },
76
+ $unset: {
77
+ jwt_exp: ''
78
+ }
79
+ }
80
+ );
81
+ }
82
+ async getConnectedClients(data: event_types.ClientConnectionsRequest): Promise<event_types.ClientConnectionReport> {
83
+ const timeframeFilter = this.listConnectionsDateRange(data);
84
+ const result = await this.db.sdk_report_events
85
+ .aggregate<event_types.ClientConnectionReport>([
86
+ {
87
+ $match: {
88
+ disconnected_at: { $exists: false },
89
+ jwt_exp: { $gt: new Date() },
90
+ ...timeframeFilter
91
+ }
92
+ },
93
+ this.connectionsFacetPipeline(),
94
+ this.connectionsProjectPipeline()
95
+ ])
96
+ .toArray();
97
+ return result[0];
98
+ }
99
+
100
+ async [Symbol.asyncDispose]() {
101
+ // No-op
102
+ }
15
103
 
16
104
  private parseJsDate(date: Date) {
17
105
  const year = date.getFullYear();
@@ -27,7 +115,7 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
27
115
  };
28
116
  }
29
117
 
30
- private sdkFacetPipeline() {
118
+ private connectionsFacetPipeline() {
31
119
  return {
32
120
  $facet: {
33
121
  unique_users: [
@@ -67,7 +155,7 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
67
155
  };
68
156
  }
69
157
 
70
- private sdkProjectPipeline() {
158
+ private connectionsProjectPipeline() {
71
159
  return {
72
160
  $project: {
73
161
  users: { $ifNull: [{ $arrayElemAt: ['$unique_users.count', 0] }, 0] },
@@ -82,7 +170,7 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
82
170
  return {
83
171
  user_id: userId,
84
172
  client_id: clientId,
85
- connect_at: {
173
+ connected_at: {
86
174
  // Need to create a new date here to sett the time to 00:00:00
87
175
  $gte: new Date(year, month, today),
88
176
  $lt: new Date(year, month, nextDay)
@@ -90,7 +178,7 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
90
178
  };
91
179
  }
92
180
 
93
- private listConnectionsDateRange(data: event_types.ListCurrentConnectionsRequest) {
181
+ private listConnectionsDateRange(data: event_types.ClientConnectionsRequest) {
94
182
  const { range } = data;
95
183
  if (!range) {
96
184
  return undefined;
@@ -98,96 +186,10 @@ export class MongoReportStorage implements storage.ReportStorageFactory {
98
186
  const endDate = data.range?.end ? new Date(data.range.end) : new Date();
99
187
  const startDate = new Date(range.start);
100
188
  return {
101
- connect_at: {
189
+ connected_at: {
102
190
  $lte: endDate,
103
191
  $gte: startDate
104
192
  }
105
193
  };
106
194
  }
107
-
108
- async deleteOldSdkData(data: event_types.DeleteOldSdkData): Promise<void> {
109
- const { date } = data;
110
- const result = await this.db.sdk_report_events.deleteMany({
111
- connect_at: { $lt: date },
112
- $or: [{ disconnect_at: { $exists: true } }, { jwt_exp: { $lt: new Date() }, disconnect_at: { $exists: false } }]
113
- });
114
- if (result.deletedCount > 0) {
115
- logger.info(
116
- `TTL from ${date.toISOString()}: ${result.deletedCount} MongoDB documents have been removed from sdk_report_events.`
117
- );
118
- }
119
- }
120
-
121
- async scrapeSdkData(data: event_types.ScrapeSdkDataRequest): Promise<event_types.ListCurrentConnections> {
122
- const { start, end } = data;
123
- const result = await this.db.sdk_report_events
124
- .aggregate<event_types.ListCurrentConnections>([
125
- {
126
- $match: {
127
- connect_at: { $lte: end, $gte: start }
128
- }
129
- },
130
- this.sdkFacetPipeline(),
131
- this.sdkProjectPipeline()
132
- ])
133
- .toArray();
134
- return result[0];
135
- }
136
-
137
- async reportSdkConnect(data: event_types.SdkConnectBucketData): Promise<void> {
138
- const updateFilter = this.updateDocFilter(data.user_id, data.client_id!);
139
- await this.db.sdk_report_events.findOneAndUpdate(
140
- updateFilter,
141
- {
142
- $set: data,
143
- $unset: {
144
- disconnect_at: ''
145
- }
146
- },
147
- {
148
- upsert: true
149
- }
150
- );
151
- }
152
- async reportSdkDisconnect(data: event_types.SdkDisconnectEventData): Promise<void> {
153
- const { connect_at, user_id, client_id } = data;
154
- await this.db.sdk_report_events.findOneAndUpdate(
155
- {
156
- client_id,
157
- user_id,
158
- connect_at
159
- },
160
- {
161
- $set: {
162
- disconnect_at: data.disconnect_at
163
- },
164
- $unset: {
165
- jwt_exp: ''
166
- }
167
- }
168
- );
169
- }
170
- async listCurrentConnections(
171
- data: event_types.ListCurrentConnectionsRequest
172
- ): Promise<event_types.ListCurrentConnections> {
173
- const timeframeFilter = this.listConnectionsDateRange(data);
174
- const result = await this.db.sdk_report_events
175
- .aggregate<event_types.ListCurrentConnections>([
176
- {
177
- $match: {
178
- disconnect_at: { $exists: false },
179
- jwt_exp: { $gt: new Date() },
180
- ...timeframeFilter
181
- }
182
- },
183
- this.sdkFacetPipeline(),
184
- this.sdkProjectPipeline()
185
- ])
186
- .toArray();
187
- return result[0];
188
- }
189
-
190
- async [Symbol.asyncDispose]() {
191
- // No-op
192
- }
193
195
  }
@@ -807,7 +807,7 @@ export class MongoBucketBatch
807
807
  }
808
808
 
809
809
  async keepalive(lsn: string): Promise<boolean> {
810
- if (this.last_checkpoint_lsn != null && lsn <= this.last_checkpoint_lsn) {
810
+ if (this.last_checkpoint_lsn != null && lsn < this.last_checkpoint_lsn) {
811
811
  // No-op
812
812
  return false;
813
813
  }
@@ -6,7 +6,7 @@ import { MongoBucketStorage } from '../MongoBucketStorage.js';
6
6
  import { PowerSyncMongo } from './db.js';
7
7
  import { MongoReportStorage } from '../MongoReportStorage.js';
8
8
 
9
- export class MongoStorageProvider implements storage.BucketStorageProvider {
9
+ export class MongoStorageProvider implements storage.StorageProvider {
10
10
  get type() {
11
11
  return lib_mongo.MONGO_CONNECTION_TYPE;
12
12
  }
@@ -8,11 +8,11 @@ import {
8
8
  BucketParameterDocument,
9
9
  BucketStateDocument,
10
10
  CheckpointEventDocument,
11
+ ClientConnectionDocument,
11
12
  CurrentDataDocument,
12
13
  CustomWriteCheckpointDocument,
13
14
  IdSequenceDocument,
14
15
  InstanceDocument,
15
- SdkConnectDocument,
16
16
  SourceTableDocument,
17
17
  SyncRuleDocument,
18
18
  WriteCheckpointDocument
@@ -38,7 +38,7 @@ export class PowerSyncMongo {
38
38
  readonly locks: mongo.Collection<lib_mongo.locks.Lock>;
39
39
  readonly bucket_state: mongo.Collection<BucketStateDocument>;
40
40
  readonly checkpoint_events: mongo.Collection<CheckpointEventDocument>;
41
- readonly sdk_report_events: mongo.Collection<SdkConnectDocument>;
41
+ readonly sdk_report_events: mongo.Collection<ClientConnectionDocument>;
42
42
 
43
43
  readonly client: mongo.MongoClient;
44
44
  readonly db: mongo.Db;
@@ -221,4 +221,4 @@ export interface InstanceDocument {
221
221
  _id: string;
222
222
  }
223
223
 
224
- export interface SdkConnectDocument extends event_types.SdkConnectDocument {}
224
+ export interface ClientConnectionDocument extends event_types.ClientConnection {}
@@ -10,9 +10,21 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 1`] = `
10
10
  "checksum": -93886621,
11
11
  "count": 2,
12
12
  "priority": 3,
13
+ "subscriptions": [
14
+ {
15
+ "default": 0,
16
+ },
17
+ ],
13
18
  },
14
19
  ],
15
20
  "last_op_id": "2",
21
+ "streams": [
22
+ {
23
+ "errors": [],
24
+ "is_default": true,
25
+ "name": "mybucket",
26
+ },
27
+ ],
16
28
  "write_checkpoint": undefined,
17
29
  },
18
30
  },
@@ -46,6 +58,11 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
46
58
  "checksum": 499012468,
47
59
  "count": 4,
48
60
  "priority": 3,
61
+ "subscriptions": [
62
+ {
63
+ "default": 0,
64
+ },
65
+ ],
49
66
  },
50
67
  ],
51
68
  "write_checkpoint": undefined,
@@ -105,9 +122,21 @@ exports[`sync - mongodb > expiring token 1`] = `
105
122
  "checksum": 0,
106
123
  "count": 0,
107
124
  "priority": 3,
125
+ "subscriptions": [
126
+ {
127
+ "default": 0,
128
+ },
129
+ ],
108
130
  },
109
131
  ],
110
132
  "last_op_id": "0",
133
+ "streams": [
134
+ {
135
+ "errors": [],
136
+ "is_default": true,
137
+ "name": "mybucket",
138
+ },
139
+ ],
111
140
  "write_checkpoint": undefined,
112
141
  },
113
142
  },
@@ -137,9 +166,21 @@ exports[`sync - mongodb > sends checkpoint complete line for empty checkpoint 1`
137
166
  "checksum": -1221282404,
138
167
  "count": 1,
139
168
  "priority": 3,
169
+ "subscriptions": [
170
+ {
171
+ "default": 0,
172
+ },
173
+ ],
140
174
  },
141
175
  ],
142
176
  "last_op_id": "1",
177
+ "streams": [
178
+ {
179
+ "errors": [],
180
+ "is_default": true,
181
+ "name": "mybucket",
182
+ },
183
+ ],
143
184
  "write_checkpoint": undefined,
144
185
  },
145
186
  },
@@ -193,15 +234,37 @@ exports[`sync - mongodb > sync buckets in order 1`] = `
193
234
  "checksum": 920318466,
194
235
  "count": 1,
195
236
  "priority": 2,
237
+ "subscriptions": [
238
+ {
239
+ "default": 0,
240
+ },
241
+ ],
196
242
  },
197
243
  {
198
244
  "bucket": "b1[]",
199
245
  "checksum": -1382098757,
200
246
  "count": 1,
201
247
  "priority": 1,
248
+ "subscriptions": [
249
+ {
250
+ "default": 1,
251
+ },
252
+ ],
202
253
  },
203
254
  ],
204
255
  "last_op_id": "2",
256
+ "streams": [
257
+ {
258
+ "errors": [],
259
+ "is_default": true,
260
+ "name": "b0",
261
+ },
262
+ {
263
+ "errors": [],
264
+ "is_default": true,
265
+ "name": "b1",
266
+ },
267
+ ],
205
268
  "write_checkpoint": undefined,
206
269
  },
207
270
  },
@@ -267,9 +330,21 @@ exports[`sync - mongodb > sync global data 1`] = `
267
330
  "checksum": -93886621,
268
331
  "count": 2,
269
332
  "priority": 3,
333
+ "subscriptions": [
334
+ {
335
+ "default": 0,
336
+ },
337
+ ],
270
338
  },
271
339
  ],
272
340
  "last_op_id": "2",
341
+ "streams": [
342
+ {
343
+ "errors": [],
344
+ "is_default": true,
345
+ "name": "mybucket",
346
+ },
347
+ ],
273
348
  "write_checkpoint": undefined,
274
349
  },
275
350
  },
@@ -319,21 +394,53 @@ exports[`sync - mongodb > sync interrupts low-priority buckets on new checkpoint
319
394
  "checksum": -659831575,
320
395
  "count": 2000,
321
396
  "priority": 2,
397
+ "subscriptions": [
398
+ {
399
+ "default": 0,
400
+ },
401
+ ],
322
402
  },
323
403
  {
324
404
  "bucket": "b0b[]",
325
405
  "checksum": -659831575,
326
406
  "count": 2000,
327
407
  "priority": 2,
408
+ "subscriptions": [
409
+ {
410
+ "default": 1,
411
+ },
412
+ ],
328
413
  },
329
414
  {
330
415
  "bucket": "b1[]",
331
416
  "checksum": -1096116670,
332
417
  "count": 1,
333
418
  "priority": 1,
419
+ "subscriptions": [
420
+ {
421
+ "default": 2,
422
+ },
423
+ ],
334
424
  },
335
425
  ],
336
426
  "last_op_id": "4001",
427
+ "streams": [
428
+ {
429
+ "errors": [],
430
+ "is_default": true,
431
+ "name": "b0a",
432
+ },
433
+ {
434
+ "errors": [],
435
+ "is_default": true,
436
+ "name": "b0b",
437
+ },
438
+ {
439
+ "errors": [],
440
+ "is_default": true,
441
+ "name": "b1",
442
+ },
443
+ ],
337
444
  "write_checkpoint": undefined,
338
445
  },
339
446
  },
@@ -380,18 +487,33 @@ exports[`sync - mongodb > sync interrupts low-priority buckets on new checkpoint
380
487
  "checksum": 883076828,
381
488
  "count": 2001,
382
489
  "priority": 2,
490
+ "subscriptions": [
491
+ {
492
+ "default": 0,
493
+ },
494
+ ],
383
495
  },
384
496
  {
385
497
  "bucket": "b0b[]",
386
498
  "checksum": 883076828,
387
499
  "count": 2001,
388
500
  "priority": 2,
501
+ "subscriptions": [
502
+ {
503
+ "default": 1,
504
+ },
505
+ ],
389
506
  },
390
507
  {
391
508
  "bucket": "b1[]",
392
509
  "checksum": 1841937527,
393
510
  "count": 2,
394
511
  "priority": 1,
512
+ "subscriptions": [
513
+ {
514
+ "default": 2,
515
+ },
516
+ ],
395
517
  },
396
518
  ],
397
519
  "write_checkpoint": undefined,
@@ -466,9 +588,21 @@ exports[`sync - mongodb > sync legacy non-raw data 1`] = `
466
588
  "checksum": -852817836,
467
589
  "count": 1,
468
590
  "priority": 3,
591
+ "subscriptions": [
592
+ {
593
+ "default": 0,
594
+ },
595
+ ],
469
596
  },
470
597
  ],
471
598
  "last_op_id": "1",
599
+ "streams": [
600
+ {
601
+ "errors": [],
602
+ "is_default": true,
603
+ "name": "mybucket",
604
+ },
605
+ ],
472
606
  "write_checkpoint": undefined,
473
607
  },
474
608
  },
@@ -514,9 +648,21 @@ exports[`sync - mongodb > sync updates to data query only 1`] = `
514
648
  "checksum": 0,
515
649
  "count": 0,
516
650
  "priority": 3,
651
+ "subscriptions": [
652
+ {
653
+ "default": 0,
654
+ },
655
+ ],
517
656
  },
518
657
  ],
519
658
  "last_op_id": "1",
659
+ "streams": [
660
+ {
661
+ "errors": [],
662
+ "is_default": true,
663
+ "name": "by_user",
664
+ },
665
+ ],
520
666
  "write_checkpoint": undefined,
521
667
  },
522
668
  },
@@ -540,6 +686,11 @@ exports[`sync - mongodb > sync updates to data query only 2`] = `
540
686
  "checksum": 1418351250,
541
687
  "count": 1,
542
688
  "priority": 3,
689
+ "subscriptions": [
690
+ {
691
+ "default": 0,
692
+ },
693
+ ],
543
694
  },
544
695
  ],
545
696
  "write_checkpoint": undefined,
@@ -582,9 +733,21 @@ exports[`sync - mongodb > sync updates to global data 1`] = `
582
733
  "checksum": 0,
583
734
  "count": 0,
584
735
  "priority": 3,
736
+ "subscriptions": [
737
+ {
738
+ "default": 0,
739
+ },
740
+ ],
585
741
  },
586
742
  ],
587
743
  "last_op_id": "0",
744
+ "streams": [
745
+ {
746
+ "errors": [],
747
+ "is_default": true,
748
+ "name": "mybucket",
749
+ },
750
+ ],
588
751
  "write_checkpoint": undefined,
589
752
  },
590
753
  },
@@ -608,6 +771,11 @@ exports[`sync - mongodb > sync updates to global data 2`] = `
608
771
  "checksum": 920318466,
609
772
  "count": 1,
610
773
  "priority": 3,
774
+ "subscriptions": [
775
+ {
776
+ "default": 0,
777
+ },
778
+ ],
611
779
  },
612
780
  ],
613
781
  "write_checkpoint": undefined,
@@ -652,6 +820,11 @@ exports[`sync - mongodb > sync updates to global data 3`] = `
652
820
  "checksum": -93886621,
653
821
  "count": 2,
654
822
  "priority": 3,
823
+ "subscriptions": [
824
+ {
825
+ "default": 0,
826
+ },
827
+ ],
655
828
  },
656
829
  ],
657
830
  "write_checkpoint": undefined,
@@ -690,6 +863,13 @@ exports[`sync - mongodb > sync updates to parameter query + data 1`] = `
690
863
  "checkpoint": {
691
864
  "buckets": [],
692
865
  "last_op_id": "0",
866
+ "streams": [
867
+ {
868
+ "errors": [],
869
+ "is_default": true,
870
+ "name": "by_user",
871
+ },
872
+ ],
693
873
  "write_checkpoint": undefined,
694
874
  },
695
875
  },
@@ -713,6 +893,11 @@ exports[`sync - mongodb > sync updates to parameter query + data 2`] = `
713
893
  "checksum": 1418351250,
714
894
  "count": 1,
715
895
  "priority": 3,
896
+ "subscriptions": [
897
+ {
898
+ "default": 0,
899
+ },
900
+ ],
716
901
  },
717
902
  ],
718
903
  "write_checkpoint": undefined,
@@ -751,6 +936,13 @@ exports[`sync - mongodb > sync updates to parameter query only 1`] = `
751
936
  "checkpoint": {
752
937
  "buckets": [],
753
938
  "last_op_id": "0",
939
+ "streams": [
940
+ {
941
+ "errors": [],
942
+ "is_default": true,
943
+ "name": "by_user",
944
+ },
945
+ ],
754
946
  "write_checkpoint": undefined,
755
947
  },
756
948
  },
@@ -774,6 +966,11 @@ exports[`sync - mongodb > sync updates to parameter query only 2`] = `
774
966
  "checksum": 0,
775
967
  "count": 0,
776
968
  "priority": 3,
969
+ "subscriptions": [
970
+ {
971
+ "default": 0,
972
+ },
973
+ ],
777
974
  },
778
975
  ],
779
976
  "write_checkpoint": undefined,