@powersync/service-module-mongodb-storage 0.0.0-dev-20250804065653 → 0.0.0-dev-20250811145516

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.
@@ -6,6 +6,7 @@ import { storage, utils } from '@powersync/service-core';
6
6
 
7
7
  import { PowerSyncMongo } from './db.js';
8
8
  import { BucketDataDocument } from './models.js';
9
+ import { ServiceAssertionError } from '@powersync/lib-services-framework';
9
10
 
10
11
  export function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T> {
11
12
  let filter = {
@@ -116,3 +117,15 @@ export const connectMongoForTests = (url: string, isCI: boolean) => {
116
117
  });
117
118
  return new PowerSyncMongo(client);
118
119
  };
120
+
121
+ export function setSessionSnapshotTime(session: mongo.ClientSession, time: bson.Timestamp) {
122
+ // This is a workaround for the lack of direct support for snapshot reads in the MongoDB driver.
123
+ if (!session.snapshotEnabled) {
124
+ throw new ServiceAssertionError(`Session must be a snapshot session`);
125
+ }
126
+ if ((session as any).snapshotTime == null) {
127
+ (session as any).snapshotTime = time;
128
+ } else {
129
+ throw new ServiceAssertionError(`Session snapshotTime is already set`);
130
+ }
131
+ }
@@ -0,0 +1,188 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`SDK reporting storage > Should create a sdk event if its after a day 1`] = `
4
+ [
5
+ {
6
+ "client_id": "client_week",
7
+ "sdk": "powersync-js/1.24.5",
8
+ "user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
9
+ "user_id": "user_week",
10
+ },
11
+ {
12
+ "client_id": "client_week",
13
+ "sdk": "powersync-js/1.24.5",
14
+ "user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
15
+ "user_id": "user_week",
16
+ },
17
+ ]
18
+ `;
19
+
20
+ exports[`SDK reporting storage > Should delete rows older than specified range 1`] = `
21
+ {
22
+ "sdks": [
23
+ {
24
+ "clients": 1,
25
+ "sdk": "powersync-dart/1.6.4",
26
+ "users": 1,
27
+ },
28
+ {
29
+ "clients": 1,
30
+ "sdk": "powersync-js/1.21.1",
31
+ "users": 1,
32
+ },
33
+ {
34
+ "clients": 1,
35
+ "sdk": "powersync-js/1.21.2",
36
+ "users": 1,
37
+ },
38
+ {
39
+ "clients": 1,
40
+ "sdk": "powersync-js/1.21.4",
41
+ "users": 1,
42
+ },
43
+ ],
44
+ "users": 4,
45
+ }
46
+ `;
47
+
48
+ exports[`SDK reporting storage > Should show SDK scrape data for user over the past day 1`] = `
49
+ {
50
+ "sdks": [
51
+ {
52
+ "clients": 1,
53
+ "sdk": "powersync-dart/1.6.4",
54
+ "users": 1,
55
+ },
56
+ {
57
+ "clients": 1,
58
+ "sdk": "powersync-js/1.21.1",
59
+ "users": 1,
60
+ },
61
+ {
62
+ "clients": 1,
63
+ "sdk": "powersync-js/1.21.4",
64
+ "users": 1,
65
+ },
66
+ ],
67
+ "users": 3,
68
+ }
69
+ `;
70
+
71
+ exports[`SDK reporting storage > Should show SDK scrape data for user over the past month 1`] = `
72
+ {
73
+ "sdks": [
74
+ {
75
+ "clients": 1,
76
+ "sdk": "powersync-dart/1.6.4",
77
+ "users": 1,
78
+ },
79
+ {
80
+ "clients": 1,
81
+ "sdk": "powersync-js/1.21.1",
82
+ "users": 1,
83
+ },
84
+ {
85
+ "clients": 1,
86
+ "sdk": "powersync-js/1.21.2",
87
+ "users": 1,
88
+ },
89
+ {
90
+ "clients": 1,
91
+ "sdk": "powersync-js/1.21.4",
92
+ "users": 1,
93
+ },
94
+ {
95
+ "clients": 1,
96
+ "sdk": "powersync-js/1.23.6",
97
+ "users": 1,
98
+ },
99
+ {
100
+ "clients": 1,
101
+ "sdk": "powersync-js/1.23.7",
102
+ "users": 1,
103
+ },
104
+ {
105
+ "clients": 1,
106
+ "sdk": "powersync-js/1.24.5",
107
+ "users": 1,
108
+ },
109
+ ],
110
+ "users": 7,
111
+ }
112
+ `;
113
+
114
+ exports[`SDK reporting storage > Should show SDK scrape data for user over the past week 1`] = `
115
+ {
116
+ "sdks": [
117
+ {
118
+ "clients": 1,
119
+ "sdk": "powersync-dart/1.6.4",
120
+ "users": 1,
121
+ },
122
+ {
123
+ "clients": 1,
124
+ "sdk": "powersync-js/1.21.1",
125
+ "users": 1,
126
+ },
127
+ {
128
+ "clients": 1,
129
+ "sdk": "powersync-js/1.21.2",
130
+ "users": 1,
131
+ },
132
+ {
133
+ "clients": 1,
134
+ "sdk": "powersync-js/1.21.4",
135
+ "users": 1,
136
+ },
137
+ ],
138
+ "users": 4,
139
+ }
140
+ `;
141
+
142
+ exports[`SDK reporting storage > Should show connected users with start range 1`] = `
143
+ {
144
+ "sdks": [
145
+ {
146
+ "clients": 1,
147
+ "sdk": "powersync-dart/1.6.4",
148
+ "users": 1,
149
+ },
150
+ ],
151
+ "users": 1,
152
+ }
153
+ `;
154
+
155
+ exports[`SDK reporting storage > Should show connected users with start range and end range 1`] = `
156
+ {
157
+ "sdks": [
158
+ {
159
+ "clients": 1,
160
+ "sdk": "powersync-js/1.21.1",
161
+ "users": 1,
162
+ },
163
+ ],
164
+ "users": 1,
165
+ }
166
+ `;
167
+
168
+ exports[`SDK reporting storage > Should update a connected sdk event and make it disconnected 1`] = `
169
+ [
170
+ {
171
+ "client_id": "client_three",
172
+ "sdk": "powersync-js/1.21.2",
173
+ "user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
174
+ "user_id": "user_three",
175
+ },
176
+ ]
177
+ `;
178
+
179
+ exports[`SDK reporting storage > Should update a sdk event if its within a day 1`] = `
180
+ [
181
+ {
182
+ "client_id": "client_one",
183
+ "sdk": "powersync-dart/1.6.4",
184
+ "user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
185
+ "user_id": "user_one",
186
+ },
187
+ ]
188
+ `;
@@ -2,9 +2,11 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
2
2
  import { INITIALIZED_MONGO_REPORT_STORAGE_FACTORY } from './util.js';
3
3
  import { event_types } from '@powersync/service-types';
4
4
 
5
- function removeVolatileFields(sdks: event_types.SdkConnectDocument[]): Partial<event_types.SdkConnectDocument>[] {
6
- return sdks.map((sdk) => {
7
- const { id, disconnect_at, connect_at, jwt_exp, ...rest } = sdk;
5
+ function removeVolatileFields(
6
+ sdks: event_types.SdkConnectDocument[]
7
+ ): Partial<event_types.SdkConnectDocument & { _id: string }>[] {
8
+ return sdks.map((sdk: Partial<event_types.SdkConnectDocument & { _id: string }>) => {
9
+ const { _id, disconnect_at, connect_at, jwt_exp, ...rest } = sdk;
8
10
  return {
9
11
  ...rest
10
12
  };
@@ -188,7 +190,7 @@ describe('SDK reporting storage', async () => {
188
190
  expect(sdk).toHaveLength(1);
189
191
  expect(new Date(sdk[0].connect_at)).toEqual(newConnectAt);
190
192
  expect(new Date(sdk[0].jwt_exp!)).toEqual(jwtExp);
191
- expect(sdk[0].disconnect_at).toBeNull();
193
+ expect(sdk[0].disconnect_at).toBeUndefined();
192
194
  const cleaned = removeVolatileFields(sdk);
193
195
  expect(cleaned).toMatchSnapshot();
194
196
  });
@@ -206,12 +208,13 @@ describe('SDK reporting storage', async () => {
206
208
  await factory.reportSdkDisconnect({
207
209
  disconnect_at: disconnectAt,
208
210
  jwt_exp: jwtExp,
209
- client_id: user_one.client_id,
210
- user_id: user_one.user_id,
211
- user_agent: user_one.user_agent
211
+ client_id: user_three.client_id,
212
+ user_id: user_three.user_id,
213
+ user_agent: user_three.user_agent,
214
+ connect_at: user_three.connect_at
212
215
  });
213
216
 
214
- const sdk = await factory.db.sdk_report_events.find({ user_id: user_one.user_id }).toArray();
217
+ const sdk = await factory.db.sdk_report_events.find({ user_id: user_three.user_id }).toArray();
215
218
  expect(sdk).toHaveLength(1);
216
219
  expect(new Date(sdk[0].disconnect_at!)).toEqual(disconnectAt);
217
220
  const cleaned = removeVolatileFields(sdk);
@@ -231,7 +234,7 @@ describe('SDK reporting storage', async () => {
231
234
  user_agent: user_week.user_agent
232
235
  });
233
236
 
234
- const sdk = await factory.db.sdk_report_events.find({ user_id: user_one.user_id }).toArray();
237
+ const sdk = await factory.db.sdk_report_events.find({ user_id: user_week.user_id }).toArray();
235
238
  expect(sdk).toHaveLength(2);
236
239
  const cleaned = removeVolatileFields(sdk);
237
240
  expect(cleaned).toMatchSnapshot();
@@ -3,3 +3,5 @@ import { describe } from 'vitest';
3
3
  import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
4
4
 
5
5
  describe('Mongo Sync Bucket Storage Compact', () => register.registerCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY));
6
+ describe('Mongo Sync Parameter Storage Compact', () =>
7
+ register.registerParameterCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY));