@powersync/service-module-mongodb-storage 0.0.0-dev-20250820110726 → 0.0.0-dev-20250827072023

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 (55) hide show
  1. package/CHANGELOG.md +11 -7
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +1 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js +0 -12
  6. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js.map +1 -1
  7. package/dist/storage/MongoBucketStorage.js +1 -1
  8. package/dist/storage/MongoBucketStorage.js.map +1 -1
  9. package/dist/storage/MongoReportStorage.d.ts +2 -3
  10. package/dist/storage/MongoReportStorage.js +8 -25
  11. package/dist/storage/MongoReportStorage.js.map +1 -1
  12. package/dist/storage/implementation/MongoBucketBatch.js +1 -1
  13. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  14. package/dist/storage/implementation/MongoSyncBucketStorage.js +1 -1
  15. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  16. package/dist/storage/implementation/PersistedBatch.js +1 -1
  17. package/dist/storage/implementation/PersistedBatch.js.map +1 -1
  18. package/dist/storage/storage-index.d.ts +2 -2
  19. package/dist/storage/storage-index.js +2 -2
  20. package/dist/storage/storage-index.js.map +1 -1
  21. package/dist/utils/test-utils.d.ts +11 -0
  22. package/dist/utils/test-utils.js +40 -0
  23. package/dist/utils/test-utils.js.map +1 -0
  24. package/dist/{storage/implementation → utils}/util.d.ts +1 -6
  25. package/dist/{storage/implementation → utils}/util.js +0 -15
  26. package/dist/utils/util.js.map +1 -0
  27. package/dist/utils/utils-index.d.ts +2 -0
  28. package/dist/utils/utils-index.js +3 -0
  29. package/dist/utils/utils-index.js.map +1 -0
  30. package/package.json +8 -8
  31. package/src/index.ts +1 -0
  32. package/src/migrations/db/migrations/1752661449910-connection-reporting.ts +0 -12
  33. package/src/storage/MongoBucketStorage.ts +1 -1
  34. package/src/storage/MongoReportStorage.ts +11 -29
  35. package/src/storage/implementation/MongoBucketBatch.ts +1 -1
  36. package/src/storage/implementation/MongoSyncBucketStorage.ts +1 -1
  37. package/src/storage/implementation/PersistedBatch.ts +1 -1
  38. package/src/storage/storage-index.ts +2 -2
  39. package/src/utils/test-utils.ts +55 -0
  40. package/src/{storage/implementation → utils}/util.ts +1 -17
  41. package/src/utils/utils-index.ts +2 -0
  42. package/test/src/__snapshots__/connection-report-storage.test.ts.snap +63 -46
  43. package/test/src/__snapshots__/storage_sync.test.ts.snap +12 -11
  44. package/test/src/connection-report-storage.test.ts +69 -192
  45. package/test/src/util.ts +3 -5
  46. package/tsconfig.tsbuildinfo +1 -1
  47. package/dist/storage/implementation/MongoTestReportStorageFactoryGenerator.d.ts +0 -7
  48. package/dist/storage/implementation/MongoTestReportStorageFactoryGenerator.js +0 -13
  49. package/dist/storage/implementation/MongoTestReportStorageFactoryGenerator.js.map +0 -1
  50. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.d.ts +0 -7
  51. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js +0 -18
  52. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js.map +0 -1
  53. package/dist/storage/implementation/util.js.map +0 -1
  54. package/src/storage/implementation/MongoTestReportStorageFactoryGenerator.ts +0 -22
  55. package/src/storage/implementation/MongoTestStorageFactoryGenerator.ts +0 -28
@@ -39,7 +39,7 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
39
39
  "bucket": "mybucket[]",
40
40
  "data": [
41
41
  {
42
- "checksum": -93886621n,
42
+ "checksum": -93886621,
43
43
  "op": "CLEAR",
44
44
  "op_id": "2",
45
45
  },
@@ -74,7 +74,7 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
74
74
  "bucket": "mybucket[]",
75
75
  "data": [
76
76
  {
77
- "checksum": 1859363232n,
77
+ "checksum": 1859363232,
78
78
  "data": "{"id":"t1","description":"Test 1b"}",
79
79
  "object_id": "t1",
80
80
  "object_type": "test",
@@ -83,7 +83,7 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
83
83
  "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
84
84
  },
85
85
  {
86
- "checksum": 3028503153n,
86
+ "checksum": 3028503153,
87
87
  "data": "{"id":"t2","description":"Test 2b"}",
88
88
  "object_id": "t2",
89
89
  "object_type": "test",
@@ -203,6 +203,7 @@ exports[`sync - mongodb > sends checkpoint complete line for empty checkpoint 1`
203
203
  "next_after": "1",
204
204
  },
205
205
  },
206
+ null,
206
207
  {
207
208
  "checkpoint_complete": {
208
209
  "last_op_id": "1",
@@ -274,7 +275,7 @@ exports[`sync - mongodb > sync buckets in order 1`] = `
274
275
  "bucket": "b1[]",
275
276
  "data": [
276
277
  {
277
- "checksum": 2912868539n,
278
+ "checksum": 2912868539,
278
279
  "data": "{"id":"earlier","description":"Test 2"}",
279
280
  "object_id": "earlier",
280
281
  "object_type": "test",
@@ -299,7 +300,7 @@ exports[`sync - mongodb > sync buckets in order 1`] = `
299
300
  "bucket": "b0[]",
300
301
  "data": [
301
302
  {
302
- "checksum": 920318466n,
303
+ "checksum": 920318466,
303
304
  "data": "{"id":"t1","description":"Test 1"}",
304
305
  "object_id": "t1",
305
306
  "object_type": "test",
@@ -354,7 +355,7 @@ exports[`sync - mongodb > sync global data 1`] = `
354
355
  "bucket": "mybucket[]",
355
356
  "data": [
356
357
  {
357
- "checksum": 920318466n,
358
+ "checksum": 920318466,
358
359
  "data": "{"id":"t1","description":"Test 1"}",
359
360
  "object_id": "t1",
360
361
  "object_type": "test",
@@ -363,7 +364,7 @@ exports[`sync - mongodb > sync global data 1`] = `
363
364
  "subkey": "e5aa2ddc-1328-58fa-a000-0b5ed31eaf1a",
364
365
  },
365
366
  {
366
- "checksum": 3280762209n,
367
+ "checksum": 3280762209,
367
368
  "data": "{"id":"t2","description":"Test 2"}",
368
369
  "object_id": "t2",
369
370
  "object_type": "test",
@@ -702,7 +703,7 @@ exports[`sync - mongodb > sync updates to data query only 2`] = `
702
703
  "bucket": "by_user["user1"]",
703
704
  "data": [
704
705
  {
705
- "checksum": 1418351250n,
706
+ "checksum": 1418351250,
706
707
  "data": "{"id":"list1","user_id":"user1","name":"User 1"}",
707
708
  "object_id": "list1",
708
709
  "object_type": "lists",
@@ -787,7 +788,7 @@ exports[`sync - mongodb > sync updates to global data 2`] = `
787
788
  "bucket": "mybucket[]",
788
789
  "data": [
789
790
  {
790
- "checksum": 920318466n,
791
+ "checksum": 920318466,
791
792
  "data": "{"id":"t1","description":"Test 1"}",
792
793
  "object_id": "t1",
793
794
  "object_type": "test",
@@ -836,7 +837,7 @@ exports[`sync - mongodb > sync updates to global data 3`] = `
836
837
  "bucket": "mybucket[]",
837
838
  "data": [
838
839
  {
839
- "checksum": 3280762209n,
840
+ "checksum": 3280762209,
840
841
  "data": "{"id":"t2","description":"Test 2"}",
841
842
  "object_id": "t2",
842
843
  "object_type": "test",
@@ -909,7 +910,7 @@ exports[`sync - mongodb > sync updates to parameter query + data 2`] = `
909
910
  "bucket": "by_user["user1"]",
910
911
  "data": [
911
912
  {
912
- "checksum": 1418351250n,
913
+ "checksum": 1418351250,
913
914
  "data": "{"id":"list1","user_id":"user1","name":"User 1"}",
914
915
  "object_id": "list1",
915
916
  "object_type": "lists",
@@ -1,6 +1,12 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'vitest';
2
2
  import { INITIALIZED_MONGO_REPORT_STORAGE_FACTORY } from './util.js';
3
+ import { register, ReportUserData } from '@powersync/service-core-tests';
3
4
  import { event_types } from '@powersync/service-types';
5
+ import { MongoReportStorage } from '@module/storage/MongoReportStorage.js';
6
+
7
+ const userData = register.REPORT_TEST_USERS;
8
+ const dates = register.REPORT_TEST_DATES;
9
+ const factory = await INITIALIZED_MONGO_REPORT_STORAGE_FACTORY();
4
10
 
5
11
  function removeVolatileFields(
6
12
  sdks: event_types.ClientConnection[]
@@ -13,181 +19,71 @@ function removeVolatileFields(
13
19
  });
14
20
  }
15
21
 
16
- describe('SDK reporting storage', async () => {
17
- const factory = await INITIALIZED_MONGO_REPORT_STORAGE_FACTORY();
18
- const now = new Date();
19
- const nowAdd5minutes = new Date(
20
- now.getFullYear(),
21
- now.getMonth(),
22
- now.getDate(),
23
- now.getHours(),
24
- now.getMinutes() + 5
25
- );
26
- const nowLess5minutes = new Date(
27
- now.getFullYear(),
28
- now.getMonth(),
29
- now.getDate(),
30
- now.getHours(),
31
- now.getMinutes() - 5
32
- );
33
- const dayAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1, now.getHours());
34
- const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
35
- const weekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
36
- const monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
37
- const user_one = {
38
- user_id: 'user_one',
39
- client_id: 'client_one',
40
- connected_at: now,
41
- sdk: 'powersync-dart/1.6.4',
42
- user_agent: 'powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android',
43
- jwt_exp: nowAdd5minutes
44
- };
45
- const user_two = {
46
- user_id: 'user_two',
47
- client_id: 'client_two',
48
- connected_at: nowLess5minutes,
49
- sdk: 'powersync-js/1.21.1',
50
- user_agent: 'powersync-js/1.21.0 powersync-web Chromium/138 linux',
51
- jwt_exp: nowAdd5minutes
52
- };
53
- const user_three = {
54
- user_id: 'user_three',
55
- client_id: 'client_three',
56
- connected_at: yesterday,
57
- sdk: 'powersync-js/1.21.2',
58
- user_agent: 'powersync-js/1.21.0 powersync-web Firefox/141 linux',
59
- disconnected_at: yesterday
60
- };
61
-
62
- const user_four = {
63
- user_id: 'user_four',
64
- client_id: 'client_four',
65
- connected_at: now,
66
- sdk: 'powersync-js/1.21.4',
67
- user_agent: 'powersync-js/1.21.0 powersync-web Firefox/141 linux',
68
- jwt_exp: nowLess5minutes
69
- };
70
-
71
- const user_week = {
72
- user_id: 'user_week',
73
- client_id: 'client_week',
74
- connected_at: weekAgo,
75
- sdk: 'powersync-js/1.24.5',
76
- user_agent: 'powersync-js/1.21.0 powersync-web Firefox/141 linux',
77
- disconnected_at: weekAgo
78
- };
79
-
80
- const user_month = {
81
- user_id: 'user_month',
82
- client_id: 'client_month',
83
- connected_at: monthAgo,
84
- sdk: 'powersync-js/1.23.6',
85
- user_agent: 'powersync-js/1.23.0 powersync-web Firefox/141 linux',
86
- disconnected_at: monthAgo
87
- };
22
+ async function loadData(data: ReportUserData, factory: MongoReportStorage) {
23
+ await factory.db.connection_report_events.insertMany(Object.values(data));
24
+ }
88
25
 
89
- const user_expired = {
90
- user_id: 'user_expired',
91
- client_id: 'client_expired',
92
- connected_at: monthAgo,
93
- sdk: 'powersync-js/1.23.7',
94
- user_agent: 'powersync-js/1.23.0 powersync-web Firefox/141 linux',
95
- jwt_exp: monthAgo
96
- };
26
+ async function deleteData(factory: MongoReportStorage) {
27
+ await factory.db.connection_report_events.deleteMany();
28
+ }
97
29
 
98
- async function loadData() {
99
- await factory.db.connection_report_events.insertMany([
100
- user_one,
101
- user_two,
102
- user_three,
103
- user_four,
104
- user_week,
105
- user_month,
106
- user_expired
107
- ]);
108
- }
30
+ beforeAll(async () => {
31
+ await loadData(userData, factory);
32
+ });
33
+ afterAll(async () => {
34
+ await deleteData(factory);
35
+ });
109
36
 
110
- function deleteData() {
111
- return factory.db.connection_report_events.deleteMany();
112
- }
37
+ describe('Report storage tests', async () => {
38
+ await register.registerReportTests(factory);
39
+ });
113
40
 
114
- beforeAll(async () => {
115
- await loadData();
116
- });
41
+ describe('Connection reporting storage', async () => {
42
+ it('Should create a connection report if its after a day', async () => {
43
+ const newConnectAt = new Date(
44
+ dates.now.getFullYear(),
45
+ dates.now.getMonth(),
46
+ dates.now.getDate() + 1,
47
+ dates.now.getHours()
48
+ );
49
+ const jwtExp = new Date(newConnectAt.getFullYear(), newConnectAt.getMonth(), newConnectAt.getDate() + 1);
117
50
 
118
- afterAll(async () => {
119
- await deleteData();
120
- });
121
- it('Should show connected users with start range', async () => {
122
- const current = await factory.getConnectedClients({
123
- range: {
124
- start: new Date(
125
- now.getFullYear(),
126
- now.getMonth(),
127
- now.getDate(),
128
- now.getHours(),
129
- now.getMinutes() - 1
130
- ).toISOString()
131
- }
132
- });
133
- expect(current).toMatchSnapshot();
134
- });
135
- it('Should show connected users with start range and end range', async () => {
136
- const current = await factory.getConnectedClients({
137
- range: {
138
- end: nowLess5minutes.toISOString(),
139
- start: new Date(
140
- now.getFullYear(),
141
- now.getMonth(),
142
- now.getDate(),
143
- now.getHours(),
144
- now.getMinutes() - 6
145
- ).toISOString()
146
- }
147
- });
148
- expect(current).toMatchSnapshot();
149
- });
150
- it('Should show connection report data for user over the past month', async () => {
151
- const sdk = await factory.getClientConnectionReports({
152
- start: monthAgo,
153
- end: now
154
- });
155
- expect(sdk).toMatchSnapshot();
156
- });
157
- it('Should show connection report data for user over the past week', async () => {
158
- const sdk = await factory.getClientConnectionReports({
159
- start: weekAgo,
160
- end: now
161
- });
162
- expect(sdk).toMatchSnapshot();
163
- });
164
- it('Should show connection report data for user over the past day', async () => {
165
- const sdk = await factory.getClientConnectionReports({
166
- start: dayAgo,
167
- end: now
51
+ await factory.reportClientConnection({
52
+ sdk: userData.user_week.sdk,
53
+ connected_at: newConnectAt,
54
+ jwt_exp: jwtExp,
55
+ client_id: userData.user_week.client_id,
56
+ user_id: userData.user_week.user_id,
57
+ user_agent: userData.user_week.user_agent
168
58
  });
169
- expect(sdk).toMatchSnapshot();
59
+
60
+ const sdk = await factory.db.connection_report_events.find({ user_id: userData.user_week.user_id }).toArray();
61
+ expect(sdk).toHaveLength(2);
62
+ const cleaned = removeVolatileFields(sdk);
63
+ expect(cleaned).toMatchSnapshot();
170
64
  });
171
65
 
172
66
  it('Should update a connection report if its within a day', async () => {
173
67
  const newConnectAt = new Date(
174
- now.getFullYear(),
175
- now.getMonth(),
176
- now.getDate(),
177
- now.getHours(),
178
- now.getMinutes() + 20
68
+ dates.now.getFullYear(),
69
+ dates.now.getMonth(),
70
+ dates.now.getDate(),
71
+ dates.now.getHours(),
72
+ dates.now.getMinutes() + 20
179
73
  );
180
74
  const jwtExp = new Date(newConnectAt.getFullYear(), newConnectAt.getMonth(), newConnectAt.getDate() + 1);
181
75
  await factory.reportClientConnection({
182
- sdk: user_one.sdk,
76
+ sdk: userData.user_one.sdk,
183
77
  connected_at: newConnectAt,
184
78
  jwt_exp: jwtExp,
185
- client_id: user_one.client_id,
186
- user_id: user_one.user_id,
187
- user_agent: user_one.user_agent
79
+ client_id: userData.user_one.client_id,
80
+ user_id: userData.user_one.user_id,
81
+ user_agent: userData.user_one.user_agent
188
82
  });
189
83
 
190
- const sdk = await factory.db.connection_report_events.find({ user_id: user_one.user_id }).toArray();
84
+ const sdk = await factory.db.connection_report_events
85
+ .find({ user_id: userData.user_one.user_id, client_id: userData.user_one.client_id })
86
+ .toArray();
191
87
  expect(sdk).toHaveLength(1);
192
88
  expect(new Date(sdk[0].connected_at)).toEqual(newConnectAt);
193
89
  expect(new Date(sdk[0].jwt_exp!)).toEqual(jwtExp);
@@ -198,58 +94,39 @@ describe('SDK reporting storage', async () => {
198
94
 
199
95
  it('Should update a connected connection report and make it disconnected', async () => {
200
96
  const disconnectAt = new Date(
201
- now.getFullYear(),
202
- now.getMonth(),
203
- now.getDate(),
204
- now.getHours(),
205
- now.getMinutes() + 20
97
+ dates.now.getFullYear(),
98
+ dates.now.getMonth(),
99
+ dates.now.getDate(),
100
+ dates.now.getHours(),
101
+ dates.now.getMinutes() + 20
206
102
  );
207
103
  const jwtExp = new Date(disconnectAt.getFullYear(), disconnectAt.getMonth(), disconnectAt.getDate() + 1);
208
104
 
209
105
  await factory.reportClientDisconnection({
210
106
  disconnected_at: disconnectAt,
211
107
  jwt_exp: jwtExp,
212
- client_id: user_three.client_id,
213
- user_id: user_three.user_id,
214
- user_agent: user_three.user_agent,
215
- connected_at: user_three.connected_at
108
+ client_id: userData.user_three.client_id,
109
+ user_id: userData.user_three.user_id,
110
+ user_agent: userData.user_three.user_agent,
111
+ connected_at: userData.user_three.connected_at
216
112
  });
217
113
 
218
- const sdk = await factory.db.connection_report_events.find({ user_id: user_three.user_id }).toArray();
114
+ const sdk = await factory.db.connection_report_events.find({ user_id: userData.user_three.user_id }).toArray();
219
115
  expect(sdk).toHaveLength(1);
220
116
  expect(new Date(sdk[0].disconnected_at!)).toEqual(disconnectAt);
221
117
  const cleaned = removeVolatileFields(sdk);
222
118
  expect(cleaned).toMatchSnapshot();
223
119
  });
224
120
 
225
- it('Should create a connection report if its after a day', async () => {
226
- const newConnectAt = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, now.getHours());
227
- const jwtExp = new Date(newConnectAt.getFullYear(), newConnectAt.getMonth(), newConnectAt.getDate() + 1);
228
-
229
- await factory.reportClientConnection({
230
- sdk: user_week.sdk,
231
- connected_at: newConnectAt,
232
- jwt_exp: jwtExp,
233
- client_id: user_week.client_id,
234
- user_id: user_week.user_id,
235
- user_agent: user_week.user_agent
236
- });
237
-
238
- const sdk = await factory.db.connection_report_events.find({ user_id: user_week.user_id }).toArray();
239
- expect(sdk).toHaveLength(2);
240
- const cleaned = removeVolatileFields(sdk);
241
- expect(cleaned).toMatchSnapshot();
242
- });
243
-
244
121
  it('Should delete rows older than specified range', async () => {
245
- await deleteData();
246
- await loadData();
122
+ await deleteData(factory);
123
+ await loadData(userData, factory);
247
124
  await factory.deleteOldConnectionData({
248
- date: weekAgo
125
+ date: dates.weekAgo
249
126
  });
250
127
  const sdk = await factory.getClientConnectionReports({
251
- start: monthAgo,
252
- end: now
128
+ start: dates.monthAgo,
129
+ end: dates.now
253
130
  });
254
131
  expect(sdk).toMatchSnapshot();
255
132
  });
package/test/src/util.ts CHANGED
@@ -1,14 +1,12 @@
1
1
  import { env } from './env.js';
2
+ import { mongoTestReportStorageFactoryGenerator, mongoTestStorageFactoryGenerator } from '@module/utils/test-utils.js';
2
3
 
3
- import { MongoTestStorageFactoryGenerator } from '@module/storage/implementation/MongoTestStorageFactoryGenerator.js';
4
- import { MongoTestReportStorageFactoryGenerator } from '@module/storage/implementation/MongoTestReportStorageFactoryGenerator.js';
5
-
6
- export const INITIALIZED_MONGO_STORAGE_FACTORY = MongoTestStorageFactoryGenerator({
4
+ export const INITIALIZED_MONGO_STORAGE_FACTORY = mongoTestStorageFactoryGenerator({
7
5
  url: env.MONGO_TEST_URL,
8
6
  isCI: env.CI
9
7
  });
10
8
 
11
- export const INITIALIZED_MONGO_REPORT_STORAGE_FACTORY = MongoTestReportStorageFactoryGenerator({
9
+ export const INITIALIZED_MONGO_REPORT_STORAGE_FACTORY = mongoTestReportStorageFactoryGenerator({
12
10
  url: env.MONGO_TEST_URL,
13
11
  isCI: env.CI
14
12
  });