@powersync/service-module-mongodb-storage 0.0.0-dev-20250827091123 → 0.0.0-dev-20250828134335

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 (69) hide show
  1. package/CHANGELOG.md +28 -13
  2. package/dist/index.d.ts +0 -1
  3. package/dist/index.js +0 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/storage/MongoBucketStorage.js +1 -1
  6. package/dist/storage/MongoBucketStorage.js.map +1 -1
  7. package/dist/storage/implementation/MongoBucketBatch.d.ts +1 -1
  8. package/dist/storage/implementation/MongoBucketBatch.js +7 -4
  9. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  10. package/dist/storage/implementation/MongoCompactor.d.ts +16 -2
  11. package/dist/storage/implementation/MongoCompactor.js +204 -48
  12. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  13. package/dist/storage/implementation/MongoStorageProvider.d.ts +1 -1
  14. package/dist/storage/implementation/MongoStorageProvider.js +3 -7
  15. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  16. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +12 -1
  17. package/dist/storage/implementation/MongoSyncBucketStorage.js +196 -37
  18. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  19. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.d.ts +7 -0
  20. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js +18 -0
  21. package/dist/storage/implementation/MongoTestStorageFactoryGenerator.js.map +1 -0
  22. package/dist/storage/implementation/PersistedBatch.d.ts +1 -0
  23. package/dist/storage/implementation/PersistedBatch.js +13 -6
  24. package/dist/storage/implementation/PersistedBatch.js.map +1 -1
  25. package/dist/storage/implementation/db.d.ts +1 -6
  26. package/dist/storage/implementation/db.js +0 -16
  27. package/dist/storage/implementation/db.js.map +1 -1
  28. package/dist/storage/implementation/models.d.ts +14 -3
  29. package/dist/{utils → storage/implementation}/util.d.ts +35 -3
  30. package/dist/{utils → storage/implementation}/util.js +54 -0
  31. package/dist/storage/implementation/util.js.map +1 -0
  32. package/dist/storage/storage-index.d.ts +2 -3
  33. package/dist/storage/storage-index.js +2 -3
  34. package/dist/storage/storage-index.js.map +1 -1
  35. package/package.json +8 -8
  36. package/src/index.ts +0 -1
  37. package/src/storage/MongoBucketStorage.ts +1 -1
  38. package/src/storage/implementation/MongoBucketBatch.ts +8 -6
  39. package/src/storage/implementation/MongoCompactor.ts +239 -49
  40. package/src/storage/implementation/MongoStorageProvider.ts +4 -9
  41. package/src/storage/implementation/MongoSyncBucketStorage.ts +242 -38
  42. package/src/storage/implementation/MongoTestStorageFactoryGenerator.ts +28 -0
  43. package/src/storage/implementation/PersistedBatch.ts +14 -6
  44. package/src/storage/implementation/db.ts +0 -18
  45. package/src/storage/implementation/models.ts +15 -3
  46. package/src/{utils → storage/implementation}/util.ts +61 -3
  47. package/src/storage/storage-index.ts +2 -3
  48. package/test/src/__snapshots__/storage_sync.test.ts.snap +110 -0
  49. package/test/src/util.ts +2 -6
  50. package/tsconfig.tsbuildinfo +1 -1
  51. package/dist/migrations/db/migrations/1752661449910-connection-reporting.d.ts +0 -3
  52. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js +0 -36
  53. package/dist/migrations/db/migrations/1752661449910-connection-reporting.js.map +0 -1
  54. package/dist/storage/MongoReportStorage.d.ts +0 -18
  55. package/dist/storage/MongoReportStorage.js +0 -154
  56. package/dist/storage/MongoReportStorage.js.map +0 -1
  57. package/dist/utils/test-utils.d.ts +0 -11
  58. package/dist/utils/test-utils.js +0 -40
  59. package/dist/utils/test-utils.js.map +0 -1
  60. package/dist/utils/util.js.map +0 -1
  61. package/dist/utils/utils-index.d.ts +0 -2
  62. package/dist/utils/utils-index.js +0 -3
  63. package/dist/utils/utils-index.js.map +0 -1
  64. package/src/migrations/db/migrations/1752661449910-connection-reporting.ts +0 -58
  65. package/src/storage/MongoReportStorage.ts +0 -177
  66. package/src/utils/test-utils.ts +0 -55
  67. package/src/utils/utils-index.ts +0 -2
  68. package/test/src/__snapshots__/connection-report-storage.test.ts.snap +0 -215
  69. package/test/src/connection-report-storage.test.ts +0 -133
@@ -1 +1 @@
1
- {"version":3,"file":"db.js","sourceRoot":"","sources":["../../../src/storage/implementation/db.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gCAAgC,CAAC;AAE5D,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAyBrE,MAAM,OAAO,cAAc;IAChB,YAAY,CAAwC;IACpD,WAAW,CAAuC;IAClD,iBAAiB,CAA4C;IAC7D,cAAc,CAAuC;IACrD,UAAU,CAAqC;IAC/C,aAAa,CAAwC;IACrD,wBAAwB,CAAkD;IAC1E,iBAAiB,CAA4C;IAC7D,QAAQ,CAAqC;IAC7C,KAAK,CAAyC;IAC9C,YAAY,CAAwC;IACpD,iBAAiB,CAA4C;IAC7D,wBAAwB,CAA6C;IAErE,MAAM,CAAoB;IAC1B,EAAE,CAAW;IAEtB,YAAY,MAAyB,EAAE,OAA+B;QACpE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE;YACtC,GAAG,OAAO,CAAC,iCAAiC;SAC7C,CAAC,CAAC;QACH,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QAEb,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC,UAAU,CAAsB,cAAc,CAAC,CAAC;QACvE,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;QAChD,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC;QAC5D,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;QACtD,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC,UAAU,CA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,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;aACnE,OAAO,EAAE,CAAC;QACb,MAAM,UAAU,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;QAC1C,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;gBAChC,oEAAoE;gBACpE,MAAM,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC;YACpD,CAAC;iBAAM,CAAC;gBACN,iDAAiD;gBACjD,OAAO;YACT,CAAC;QACH,CAAC;QAED,MAAM,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,mBAAmB,EAAE;YAClD,MAAM,EAAE,IAAI;YACZ,sFAAsF;YACtF,mFAAmF;YACnF,iFAAiF;YACjF,4BAA4B;YAC5B,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,gBAAgB;YACjC,GAAG,EAAE,EAAE,CAAC,0BAA0B;SACnC,CAAC,CAAC;IACL,CAAC;CACF;AAED,MAAM,UAAU,oBAAoB,CAAC,MAA0B,EAAE,OAA0C;IACzG,OAAO,IAAI,cAAc,CACvB,SAAS,CAAC,iBAAiB,CAAC,MAAM,EAAE;QAClC,gBAAgB,EAAE,iBAAiB;QACnC,GAAG,OAAO;KACX,CAAC,EACF,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAC9B,CAAC;AACJ,CAAC"}
@@ -1,7 +1,6 @@
1
1
  import { InternalOpId, storage } from '@powersync/service-core';
2
2
  import { SqliteJsonValue } from '@powersync/service-sync-rules';
3
3
  import * as bson from 'bson';
4
- import { event_types } from '@powersync/service-types';
5
4
  /**
6
5
  * Replica id uniquely identifying a row on the source database.
7
6
  *
@@ -92,6 +91,20 @@ export interface BucketStateDocument {
92
91
  b: string;
93
92
  };
94
93
  last_op: bigint;
94
+ /**
95
+ * If set, this can be treated as "cache" of a checksum at a specific point.
96
+ * Can be updated periodically, for example by the compact job.
97
+ */
98
+ compacted_state?: {
99
+ op_id: InternalOpId;
100
+ count: number;
101
+ checksum: bigint;
102
+ bytes: number;
103
+ };
104
+ estimate_since_compact?: {
105
+ count: number;
106
+ bytes: number;
107
+ };
95
108
  }
96
109
  export interface IdSequenceDocument {
97
110
  _id: string;
@@ -190,5 +203,3 @@ export interface WriteCheckpointDocument {
190
203
  export interface InstanceDocument {
191
204
  _id: string;
192
205
  }
193
- export interface ClientConnectionDocument extends event_types.ClientConnection {
194
- }
@@ -1,7 +1,8 @@
1
1
  import * as bson from 'bson';
2
2
  import { mongo } from '@powersync/lib-service-mongodb';
3
- import { storage, utils } from '@powersync/service-core';
4
- import { BucketDataDocument } from '../storage/implementation/models.js';
3
+ import { PartialOrFullChecksum, storage, utils } from '@powersync/service-core';
4
+ import { PowerSyncMongo } from './db.js';
5
+ import { BucketDataDocument } from './models.js';
5
6
  export declare function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T>;
6
7
  export declare function generateSlotName(prefix: string, sync_rules_id: number): string;
7
8
  /**
@@ -15,10 +16,41 @@ export declare function generateSlotName(prefix: string, sync_rules_id: number):
15
16
  *
16
17
  * For this to be effective, set batchSize = limit in the find command.
17
18
  */
18
- export declare function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{
19
+ export declare function readSingleBatch<T>(cursor: mongo.AbstractCursor<T>): Promise<{
19
20
  data: T[];
20
21
  hasMore: boolean;
21
22
  }>;
22
23
  export declare function mapOpEntry(row: BucketDataDocument): utils.OplogEntry;
23
24
  export declare function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId): string;
25
+ /**
26
+ * Helper for unit tests
27
+ */
28
+ export declare const connectMongoForTests: (url: string, isCI: boolean) => PowerSyncMongo;
24
29
  export declare function setSessionSnapshotTime(session: mongo.ClientSession, time: bson.Timestamp): void;
30
+ export declare const CHECKSUM_QUERY_GROUP_STAGE: {
31
+ $group: {
32
+ _id: string;
33
+ checksum_total: {
34
+ $sum: {
35
+ $toLong: string;
36
+ };
37
+ };
38
+ count: {
39
+ $sum: number;
40
+ };
41
+ has_clear_op: {
42
+ $max: {
43
+ $cond: (number | {
44
+ $eq: string[];
45
+ })[];
46
+ };
47
+ };
48
+ last_op: {
49
+ $max: string;
50
+ };
51
+ };
52
+ };
53
+ /**
54
+ * Convert output of CHECKSUM_QUERY_GROUP_STAGE into a checksum.
55
+ */
56
+ export declare function checksumFromAggregate(doc: bson.Document): PartialOrFullChecksum;
@@ -1,7 +1,9 @@
1
1
  import * as bson from 'bson';
2
2
  import * as crypto from 'crypto';
3
3
  import * as uuid from 'uuid';
4
+ import { mongo } from '@powersync/lib-service-mongodb';
4
5
  import { storage, utils } from '@powersync/service-core';
6
+ import { PowerSyncMongo } from './db.js';
5
7
  import { ServiceAssertionError } from '@powersync/lib-services-framework';
6
8
  export function idPrefixFilter(prefix, rest) {
7
9
  let filter = {
@@ -94,6 +96,19 @@ export function replicaIdToSubkey(table, id) {
94
96
  return uuid.v5(repr, utils.ID_NAMESPACE);
95
97
  }
96
98
  }
99
+ /**
100
+ * Helper for unit tests
101
+ */
102
+ export const connectMongoForTests = (url, isCI) => {
103
+ // Short timeout for tests, to fail fast when the server is not available.
104
+ // Slightly longer timeouts for CI, to avoid arbitrary test failures
105
+ const client = new mongo.MongoClient(url, {
106
+ connectTimeoutMS: isCI ? 15_000 : 5_000,
107
+ socketTimeoutMS: isCI ? 15_000 : 5_000,
108
+ serverSelectionTimeoutMS: isCI ? 15_000 : 2_500
109
+ });
110
+ return new PowerSyncMongo(client);
111
+ };
97
112
  export function setSessionSnapshotTime(session, time) {
98
113
  // This is a workaround for the lack of direct support for snapshot reads in the MongoDB driver.
99
114
  if (!session.snapshotEnabled) {
@@ -106,4 +121,43 @@ export function setSessionSnapshotTime(session, time) {
106
121
  throw new ServiceAssertionError(`Session snapshotTime is already set`);
107
122
  }
108
123
  }
124
+ export const CHECKSUM_QUERY_GROUP_STAGE = {
125
+ $group: {
126
+ _id: '$_id.b',
127
+ // Historically, checksum may be stored as 'int' or 'double'.
128
+ // More recently, this should be a 'long'.
129
+ // $toLong ensures that we always sum it as a long, avoiding inaccuracies in the calculations.
130
+ checksum_total: { $sum: { $toLong: '$checksum' } },
131
+ count: { $sum: 1 },
132
+ has_clear_op: {
133
+ $max: {
134
+ $cond: [{ $eq: ['$op', 'CLEAR'] }, 1, 0]
135
+ }
136
+ },
137
+ last_op: { $max: '$_id.o' }
138
+ }
139
+ };
140
+ /**
141
+ * Convert output of CHECKSUM_QUERY_GROUP_STAGE into a checksum.
142
+ */
143
+ export function checksumFromAggregate(doc) {
144
+ const partialChecksum = Number(BigInt(doc.checksum_total) & 0xffffffffn) & 0xffffffff;
145
+ const bucket = doc._id;
146
+ if (doc.has_clear_op == 1) {
147
+ return {
148
+ // full checksum - replaces any previous one
149
+ bucket,
150
+ checksum: partialChecksum,
151
+ count: doc.count
152
+ };
153
+ }
154
+ else {
155
+ return {
156
+ // partial checksum - is added to a previous one
157
+ bucket,
158
+ partialCount: doc.count,
159
+ partialChecksum
160
+ };
161
+ }
162
+ }
109
163
  //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/storage/implementation/util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AACvD,OAAO,EAA0D,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEjH,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,MAAM,UAAU,cAAc,CAAI,MAAkB,EAAE,IAAiB;IACrE,IAAI,MAAM,GAAG;QACX,IAA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E,GAAG,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;aACzC;SACF;QACD,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC5B;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,GAAkB;IACtD,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,WAAW,CAAC,GAAG,UAAU,CAAC;IACtF,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC;IAEvB,IAAI,GAAG,CAAC,YAAY,IAAI,CAAC,EAAE,CAAC;QAC1B,OAAO;YACL,4CAA4C;YAC5C,MAAM;YACN,QAAQ,EAAE,eAAe;YACzB,KAAK,EAAE,GAAG,CAAC,KAAK;SACQ,CAAC;IAC7B,CAAC;SAAM,CAAC;QACN,OAAO;YACL,gDAAgD;YAChD,MAAM;YACN,YAAY,EAAE,GAAG,CAAC,KAAK;YACvB,eAAe;SACU,CAAC;IAC9B,CAAC;AACH,CAAC"}
@@ -7,9 +7,8 @@ export * from './implementation/MongoPersistedSyncRulesContent.js';
7
7
  export * from './implementation/MongoStorageProvider.js';
8
8
  export * from './implementation/MongoSyncBucketStorage.js';
9
9
  export * from './implementation/MongoSyncRulesLock.js';
10
+ export * from './implementation/MongoTestStorageFactoryGenerator.js';
10
11
  export * from './implementation/OperationBatch.js';
11
12
  export * from './implementation/PersistedBatch.js';
12
- export * from '../utils/util.js';
13
+ export * from './implementation/util.js';
13
14
  export * from './MongoBucketStorage.js';
14
- export * from './MongoReportStorage.js';
15
- export * as test_utils from '../utils/test-utils.js';
@@ -7,10 +7,9 @@ export * from './implementation/MongoPersistedSyncRulesContent.js';
7
7
  export * from './implementation/MongoStorageProvider.js';
8
8
  export * from './implementation/MongoSyncBucketStorage.js';
9
9
  export * from './implementation/MongoSyncRulesLock.js';
10
+ export * from './implementation/MongoTestStorageFactoryGenerator.js';
10
11
  export * from './implementation/OperationBatch.js';
11
12
  export * from './implementation/PersistedBatch.js';
12
- export * from '../utils/util.js';
13
+ export * from './implementation/util.js';
13
14
  export * from './MongoBucketStorage.js';
14
- export * from './MongoReportStorage.js';
15
- export * as test_utils from '../utils/test-utils.js';
16
15
  //# sourceMappingURL=storage-index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage-index.js","sourceRoot":"","sources":["../../src/storage/storage-index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sCAAsC,CAAC;AACrD,cAAc,qCAAqC,CAAC;AACpD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,oDAAoD,CAAC;AACnE,cAAc,0CAA0C,CAAC;AACzD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wCAAwC,CAAC;AACvD,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"storage-index.js","sourceRoot":"","sources":["../../src/storage/storage-index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sCAAsC,CAAC;AACrD,cAAc,qCAAqC,CAAC;AACpD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,oDAAoD,CAAC;AACnE,cAAc,0CAA0C,CAAC;AACzD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wCAAwC,CAAC;AACvD,cAAc,sDAAsD,CAAC;AACrE,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@powersync/service-module-mongodb-storage",
3
3
  "repository": "https://github.com/powersync-ja/powersync-service",
4
4
  "types": "dist/index.d.ts",
5
- "version": "0.0.0-dev-20250827091123",
5
+ "version": "0.0.0-dev-20250828134335",
6
6
  "main": "dist/index.js",
7
7
  "license": "FSL-1.1-ALv2",
8
8
  "type": "module",
@@ -27,15 +27,15 @@
27
27
  "lru-cache": "^10.2.2",
28
28
  "ts-codec": "^1.3.0",
29
29
  "uuid": "^11.1.0",
30
- "@powersync/lib-service-mongodb": "0.0.0-dev-20250827091123",
31
- "@powersync/lib-services-framework": "0.0.0-dev-20250827091123",
32
- "@powersync/service-core": "0.0.0-dev-20250827091123",
33
- "@powersync/service-types": "0.0.0-dev-20250827091123",
34
- "@powersync/service-jsonbig": "0.0.0-dev-20250827091123",
35
- "@powersync/service-sync-rules": "0.0.0-dev-20250827091123"
30
+ "@powersync/lib-service-mongodb": "0.0.0-dev-20250828134335",
31
+ "@powersync/lib-services-framework": "0.7.3",
32
+ "@powersync/service-core": "0.0.0-dev-20250828134335",
33
+ "@powersync/service-jsonbig": "0.17.11",
34
+ "@powersync/service-sync-rules": "0.29.0",
35
+ "@powersync/service-types": "0.13.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@powersync/service-core-tests": "0.0.0-dev-20250827091123"
38
+ "@powersync/service-core-tests": "0.0.0-dev-20250828134335"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsc -b",
package/src/index.ts CHANGED
@@ -5,4 +5,3 @@ export * as storage from './storage/storage-index.js';
5
5
 
6
6
  export * from './types/types.js';
7
7
  export * as types from './types/types.js';
8
- export * as utils from './utils/utils-index.js';
@@ -12,7 +12,7 @@ import { PowerSyncMongo } from './implementation/db.js';
12
12
  import { SyncRuleDocument } from './implementation/models.js';
13
13
  import { MongoPersistedSyncRulesContent } from './implementation/MongoPersistedSyncRulesContent.js';
14
14
  import { MongoSyncBucketStorage } from './implementation/MongoSyncBucketStorage.js';
15
- import { generateSlotName } from '../utils/util.js';
15
+ import { generateSlotName } from './implementation/util.js';
16
16
 
17
17
  export class MongoBucketStorage
18
18
  extends BaseObserver<storage.BucketStorageFactoryListener>
@@ -1,5 +1,5 @@
1
1
  import { mongo } from '@powersync/lib-service-mongodb';
2
- import { SqlEventDescriptor, SqliteRow, SqlSyncRules } from '@powersync/service-sync-rules';
2
+ import { SqlEventDescriptor, SqliteRow, SqliteValue, SqlSyncRules } from '@powersync/service-sync-rules';
3
3
  import * as bson from 'bson';
4
4
 
5
5
  import {
@@ -28,7 +28,7 @@ import { MongoIdSequence } from './MongoIdSequence.js';
28
28
  import { batchCreateCustomWriteCheckpoints } from './MongoWriteCheckpointAPI.js';
29
29
  import { cacheKey, OperationBatch, RecordOperation } from './OperationBatch.js';
30
30
  import { PersistedBatch } from './PersistedBatch.js';
31
- import { idPrefixFilter } from '../../utils/util.js';
31
+ import { idPrefixFilter } from './util.js';
32
32
 
33
33
  /**
34
34
  * 15MB
@@ -97,7 +97,7 @@ export class MongoBucketBatch
97
97
  private persisted_op: InternalOpId | null = null;
98
98
 
99
99
  /**
100
- * For tests only - not for persistence logic.
100
+ * Last written op, if any. This may not reflect a consistent checkpoint.
101
101
  */
102
102
  public last_flushed_op: InternalOpId | null = null;
103
103
 
@@ -319,7 +319,8 @@ export class MongoBucketBatch
319
319
  const record = operation.record;
320
320
  const beforeId = operation.beforeId;
321
321
  const afterId = operation.afterId;
322
- let after = record.after;
322
+ let sourceAfter = record.after;
323
+ let after = sourceAfter && this.sync_rules.applyRowContext(sourceAfter);
323
324
  const sourceTable = record.sourceTable;
324
325
 
325
326
  let existing_buckets: CurrentBucket[] = [];
@@ -367,7 +368,7 @@ export class MongoBucketBatch
367
368
  existing_lookups = result.lookups;
368
369
  if (this.storeCurrentData) {
369
370
  const data = deserializeBson((result.data as mongo.Binary).buffer) as SqliteRow;
370
- after = storage.mergeToast(after!, data);
371
+ after = storage.mergeToast<SqliteValue>(after!, data);
371
372
  }
372
373
  }
373
374
  } else if (record.tag == SaveOperationTag.DELETE) {
@@ -461,7 +462,8 @@ export class MongoBucketBatch
461
462
  if (sourceTable.syncData) {
462
463
  const { results: evaluated, errors: syncErrors } = this.sync_rules.evaluateRowWithErrors({
463
464
  record: after,
464
- sourceTable
465
+ sourceTable,
466
+ bucketIdTransformer: SqlSyncRules.versionedBucketIdTransformer(`${this.group_id}`)
465
467
  });
466
468
 
467
469
  for (let error of syncErrors) {