@powersync/service-module-mongodb-storage 0.0.0-dev-20250829094737 → 0.0.0-dev-20250901073220

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 (63) hide show
  1. package/CHANGELOG.md +22 -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.d.ts +3 -0
  6. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js +36 -0
  7. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js.map +1 -0
  8. package/dist/storage/MongoBucketStorage.js +1 -1
  9. package/dist/storage/MongoBucketStorage.js.map +1 -1
  10. package/dist/storage/MongoReportStorage.d.ts +18 -0
  11. package/dist/storage/MongoReportStorage.js +154 -0
  12. package/dist/storage/MongoReportStorage.js.map +1 -0
  13. package/dist/storage/implementation/MongoBucketBatch.js +1 -1
  14. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  15. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  16. package/dist/storage/implementation/MongoStorageProvider.d.ts +1 -1
  17. package/dist/storage/implementation/MongoStorageProvider.js +7 -3
  18. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  19. package/dist/storage/implementation/MongoSyncBucketStorage.js +1 -1
  20. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  21. package/dist/storage/implementation/PersistedBatch.js +1 -1
  22. package/dist/storage/implementation/PersistedBatch.js.map +1 -1
  23. package/dist/storage/implementation/db.d.ts +6 -1
  24. package/dist/storage/implementation/db.js +16 -0
  25. package/dist/storage/implementation/db.js.map +1 -1
  26. package/dist/storage/implementation/models.d.ts +3 -0
  27. package/dist/storage/storage-index.d.ts +3 -2
  28. package/dist/storage/storage-index.js +3 -2
  29. package/dist/storage/storage-index.js.map +1 -1
  30. package/dist/utils/test-utils.d.ts +11 -0
  31. package/dist/utils/test-utils.js +40 -0
  32. package/dist/utils/test-utils.js.map +1 -0
  33. package/dist/{storage/implementation → utils}/util.d.ts +1 -6
  34. package/dist/{storage/implementation → utils}/util.js +0 -15
  35. package/dist/utils/util.js.map +1 -0
  36. package/dist/utils/utils-index.d.ts +2 -0
  37. package/dist/utils/utils-index.js +3 -0
  38. package/dist/utils/utils-index.js.map +1 -0
  39. package/package.json +7 -7
  40. package/src/index.ts +1 -0
  41. package/src/migrations/db/migrations/1752661449910-connection-reporting.ts +58 -0
  42. package/src/storage/MongoBucketStorage.ts +1 -1
  43. package/src/storage/MongoReportStorage.ts +177 -0
  44. package/src/storage/implementation/MongoBucketBatch.ts +1 -1
  45. package/src/storage/implementation/MongoCompactor.ts +0 -1
  46. package/src/storage/implementation/MongoStorageProvider.ts +9 -4
  47. package/src/storage/implementation/MongoSyncBucketStorage.ts +2 -1
  48. package/src/storage/implementation/PersistedBatch.ts +1 -1
  49. package/src/storage/implementation/db.ts +18 -0
  50. package/src/storage/implementation/models.ts +3 -0
  51. package/src/storage/storage-index.ts +3 -2
  52. package/src/utils/test-utils.ts +55 -0
  53. package/src/{storage/implementation → utils}/util.ts +2 -18
  54. package/src/utils/utils-index.ts +2 -0
  55. package/test/src/__snapshots__/connection-report-storage.test.ts.snap +215 -0
  56. package/test/src/connection-report-storage.test.ts +133 -0
  57. package/test/src/util.ts +6 -2
  58. package/tsconfig.tsbuildinfo +1 -1
  59. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.d.ts +0 -7
  60. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js +0 -18
  61. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js.map +0 -1
  62. package/dist/storage/implementation/util.js.map +0 -1
  63. package/src/storage/implementation/MongoTestStorageFactoryGenerator.ts +0 -28
@@ -0,0 +1,133 @@
1
+ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
2
+ import { INITIALIZED_MONGO_REPORT_STORAGE_FACTORY } from './util.js';
3
+ import { register, ReportUserData } from '@powersync/service-core-tests';
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();
10
+
11
+ function removeVolatileFields(
12
+ sdks: event_types.ClientConnection[]
13
+ ): Partial<event_types.ClientConnection & { _id: string }>[] {
14
+ return sdks.map((sdk: Partial<event_types.ClientConnection & { _id: string }>) => {
15
+ const { _id, disconnected_at, connected_at, jwt_exp, ...rest } = sdk;
16
+ return {
17
+ ...rest
18
+ };
19
+ });
20
+ }
21
+
22
+ async function loadData(data: ReportUserData, factory: MongoReportStorage) {
23
+ await factory.db.connection_report_events.insertMany(Object.values(data));
24
+ }
25
+
26
+ async function deleteData(factory: MongoReportStorage) {
27
+ await factory.db.connection_report_events.deleteMany();
28
+ }
29
+
30
+ beforeAll(async () => {
31
+ await loadData(userData, factory);
32
+ });
33
+ afterAll(async () => {
34
+ await deleteData(factory);
35
+ });
36
+
37
+ describe('Report storage tests', async () => {
38
+ await register.registerReportTests(factory);
39
+ });
40
+
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);
50
+
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
58
+ });
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();
64
+ });
65
+
66
+ it('Should update a connection report if its within a day', async () => {
67
+ const newConnectAt = new Date(
68
+ dates.now.getFullYear(),
69
+ dates.now.getMonth(),
70
+ dates.now.getDate(),
71
+ dates.now.getHours(),
72
+ dates.now.getMinutes() + 20
73
+ );
74
+ const jwtExp = new Date(newConnectAt.getFullYear(), newConnectAt.getMonth(), newConnectAt.getDate() + 1);
75
+ await factory.reportClientConnection({
76
+ sdk: userData.user_one.sdk,
77
+ connected_at: newConnectAt,
78
+ jwt_exp: jwtExp,
79
+ client_id: userData.user_one.client_id,
80
+ user_id: userData.user_one.user_id,
81
+ user_agent: userData.user_one.user_agent
82
+ });
83
+
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();
87
+ expect(sdk).toHaveLength(1);
88
+ expect(new Date(sdk[0].connected_at)).toEqual(newConnectAt);
89
+ expect(new Date(sdk[0].jwt_exp!)).toEqual(jwtExp);
90
+ expect(sdk[0].disconnected_at).toBeUndefined();
91
+ const cleaned = removeVolatileFields(sdk);
92
+ expect(cleaned).toMatchSnapshot();
93
+ });
94
+
95
+ it('Should update a connected connection report and make it disconnected', async () => {
96
+ const disconnectAt = new Date(
97
+ dates.now.getFullYear(),
98
+ dates.now.getMonth(),
99
+ dates.now.getDate(),
100
+ dates.now.getHours(),
101
+ dates.now.getMinutes() + 20
102
+ );
103
+ const jwtExp = new Date(disconnectAt.getFullYear(), disconnectAt.getMonth(), disconnectAt.getDate() + 1);
104
+
105
+ await factory.reportClientDisconnection({
106
+ disconnected_at: disconnectAt,
107
+ jwt_exp: jwtExp,
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
112
+ });
113
+
114
+ const sdk = await factory.db.connection_report_events.find({ user_id: userData.user_three.user_id }).toArray();
115
+ expect(sdk).toHaveLength(1);
116
+ expect(new Date(sdk[0].disconnected_at!)).toEqual(disconnectAt);
117
+ const cleaned = removeVolatileFields(sdk);
118
+ expect(cleaned).toMatchSnapshot();
119
+ });
120
+
121
+ it('Should delete rows older than specified range', async () => {
122
+ await deleteData(factory);
123
+ await loadData(userData, factory);
124
+ await factory.deleteOldConnectionData({
125
+ date: dates.weekAgo
126
+ });
127
+ const sdk = await factory.getClientConnectionReports({
128
+ start: dates.monthAgo,
129
+ end: dates.now
130
+ });
131
+ expect(sdk).toMatchSnapshot();
132
+ });
133
+ });
package/test/src/util.ts CHANGED
@@ -1,8 +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
+ export const INITIALIZED_MONGO_STORAGE_FACTORY = mongoTestStorageFactoryGenerator({
5
+ url: env.MONGO_TEST_URL,
6
+ isCI: env.CI
7
+ });
4
8
 
5
- export const INITIALIZED_MONGO_STORAGE_FACTORY = MongoTestStorageFactoryGenerator({
9
+ export const INITIALIZED_MONGO_REPORT_STORAGE_FACTORY = mongoTestReportStorageFactoryGenerator({
6
10
  url: env.MONGO_TEST_URL,
7
11
  isCI: env.CI
8
12
  });