@powersync/service-module-mongodb 0.0.0-dev-20241128134723 → 0.0.0-dev-20241219110735

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 (137) hide show
  1. package/CHANGELOG.md +69 -4
  2. package/dist/db/db-index.d.ts +1 -0
  3. package/dist/db/db-index.js +2 -0
  4. package/dist/db/db-index.js.map +1 -0
  5. package/dist/db/mongo.d.ts +35 -0
  6. package/dist/db/mongo.js +73 -0
  7. package/dist/db/mongo.js.map +1 -0
  8. package/dist/index.d.ts +2 -0
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/locks/MonogLocks.d.ts +36 -0
  12. package/dist/locks/MonogLocks.js +83 -0
  13. package/dist/locks/MonogLocks.js.map +1 -0
  14. package/dist/migrations/MonogMigrationAgent.d.ts +12 -0
  15. package/dist/migrations/MonogMigrationAgent.js +25 -0
  16. package/dist/migrations/MonogMigrationAgent.js.map +1 -0
  17. package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
  18. package/dist/migrations/db/migrations/1684951997326-init.js +30 -0
  19. package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
  20. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
  21. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
  22. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
  23. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
  24. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
  25. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
  26. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
  27. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +26 -0
  28. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
  29. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +3 -0
  30. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +28 -0
  31. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +1 -0
  32. package/dist/migrations/mongo-migration-store.d.ts +7 -0
  33. package/dist/migrations/mongo-migration-store.js +49 -0
  34. package/dist/migrations/mongo-migration-store.js.map +1 -0
  35. package/dist/module/MongoModule.js +15 -4
  36. package/dist/module/MongoModule.js.map +1 -1
  37. package/dist/replication/MongoManager.d.ts +1 -1
  38. package/dist/replication/MongoManager.js +3 -2
  39. package/dist/replication/MongoManager.js.map +1 -1
  40. package/dist/storage/MongoBucketStorage.d.ts +48 -0
  41. package/dist/storage/MongoBucketStorage.js +425 -0
  42. package/dist/storage/MongoBucketStorage.js.map +1 -0
  43. package/dist/storage/implementation/MongoBucketBatch.d.ts +72 -0
  44. package/dist/storage/implementation/MongoBucketBatch.js +681 -0
  45. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -0
  46. package/dist/storage/implementation/MongoCompactor.d.ts +40 -0
  47. package/dist/storage/implementation/MongoCompactor.js +310 -0
  48. package/dist/storage/implementation/MongoCompactor.js.map +1 -0
  49. package/dist/storage/implementation/MongoIdSequence.d.ts +12 -0
  50. package/dist/storage/implementation/MongoIdSequence.js +21 -0
  51. package/dist/storage/implementation/MongoIdSequence.js.map +1 -0
  52. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +9 -0
  53. package/dist/storage/implementation/MongoPersistedSyncRules.js +9 -0
  54. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
  55. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +20 -0
  56. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +26 -0
  57. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -0
  58. package/dist/storage/implementation/MongoStorageProvider.d.ts +6 -0
  59. package/dist/storage/implementation/MongoStorageProvider.js +34 -0
  60. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -0
  61. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +36 -0
  62. package/dist/storage/implementation/MongoSyncBucketStorage.js +529 -0
  63. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -0
  64. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +16 -0
  65. package/dist/storage/implementation/MongoSyncRulesLock.js +65 -0
  66. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -0
  67. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +20 -0
  68. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +104 -0
  69. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -0
  70. package/dist/storage/implementation/OperationBatch.d.ts +34 -0
  71. package/dist/storage/implementation/OperationBatch.js +119 -0
  72. package/dist/storage/implementation/OperationBatch.js.map +1 -0
  73. package/dist/storage/implementation/PersistedBatch.d.ts +46 -0
  74. package/dist/storage/implementation/PersistedBatch.js +223 -0
  75. package/dist/storage/implementation/PersistedBatch.js.map +1 -0
  76. package/dist/storage/implementation/config.d.ts +19 -0
  77. package/dist/storage/implementation/config.js +26 -0
  78. package/dist/storage/implementation/config.js.map +1 -0
  79. package/dist/storage/implementation/db.d.ts +36 -0
  80. package/dist/storage/implementation/db.js +47 -0
  81. package/dist/storage/implementation/db.js.map +1 -0
  82. package/dist/storage/implementation/models.d.ts +139 -0
  83. package/dist/storage/implementation/models.js +2 -0
  84. package/dist/storage/implementation/models.js.map +1 -0
  85. package/dist/storage/implementation/util.d.ts +58 -0
  86. package/dist/storage/implementation/util.js +196 -0
  87. package/dist/storage/implementation/util.js.map +1 -0
  88. package/dist/storage/storage-index.d.ts +14 -0
  89. package/dist/storage/storage-index.js +15 -0
  90. package/dist/storage/storage-index.js.map +1 -0
  91. package/dist/types/types.d.ts +3 -0
  92. package/dist/types/types.js +4 -1
  93. package/dist/types/types.js.map +1 -1
  94. package/package.json +11 -8
  95. package/src/db/db-index.ts +1 -0
  96. package/src/db/mongo.ts +81 -0
  97. package/src/index.ts +4 -0
  98. package/src/locks/MonogLocks.ts +147 -0
  99. package/src/migrations/MonogMigrationAgent.ts +39 -0
  100. package/src/migrations/db/migrations/1684951997326-init.ts +39 -0
  101. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
  102. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +105 -0
  103. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +38 -0
  104. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +40 -0
  105. package/src/migrations/mongo-migration-store.ts +62 -0
  106. package/src/module/MongoModule.ts +18 -4
  107. package/src/replication/MongoManager.ts +6 -2
  108. package/src/storage/MongoBucketStorage.ts +530 -0
  109. package/src/storage/implementation/MongoBucketBatch.ts +893 -0
  110. package/src/storage/implementation/MongoCompactor.ts +392 -0
  111. package/src/storage/implementation/MongoIdSequence.ts +24 -0
  112. package/src/storage/implementation/MongoPersistedSyncRules.ts +16 -0
  113. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +49 -0
  114. package/src/storage/implementation/MongoStorageProvider.ts +42 -0
  115. package/src/storage/implementation/MongoSyncBucketStorage.ts +612 -0
  116. package/src/storage/implementation/MongoSyncRulesLock.ts +88 -0
  117. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +146 -0
  118. package/src/storage/implementation/OperationBatch.ts +130 -0
  119. package/src/storage/implementation/PersistedBatch.ts +283 -0
  120. package/src/storage/implementation/config.ts +40 -0
  121. package/src/storage/implementation/db.ts +88 -0
  122. package/src/storage/implementation/models.ts +160 -0
  123. package/src/storage/implementation/util.ts +209 -0
  124. package/src/storage/storage-index.ts +14 -0
  125. package/src/types/types.ts +8 -1
  126. package/test/src/__snapshots__/storage_sync.test.ts.snap +332 -0
  127. package/test/src/change_stream.test.ts +34 -33
  128. package/test/src/change_stream_utils.ts +6 -6
  129. package/test/src/env.ts +1 -0
  130. package/test/src/slow_tests.test.ts +4 -4
  131. package/test/src/storage.test.ts +7 -0
  132. package/test/src/storage_compacting.test.ts +6 -0
  133. package/test/src/storage_sync.test.ts +113 -0
  134. package/test/src/util.ts +20 -7
  135. package/test/tsconfig.json +4 -0
  136. package/tsconfig.tsbuildinfo +1 -1
  137. package/vitest.config.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -1,12 +1,77 @@
1
1
  # @powersync/service-module-mongodb
2
2
 
3
- ## 0.0.0-dev-20241128134723
3
+ ## 0.0.0-dev-20241219110735
4
+
5
+ ### Minor Changes
6
+
7
+ - 697d44b: Moved MongoDB sync bucket storage implementation to the MongoDB module.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [697d44b]
12
+ - Updated dependencies [697d44b]
13
+ - Updated dependencies [a66be3b]
14
+ - Updated dependencies [697d44b]
15
+ - @powersync/service-core@0.0.0-dev-20241219110735
16
+ - @powersync/lib-services-framework@0.0.0-dev-20241219110735
17
+ - @powersync/service-sync-rules@0.0.0-dev-20241219110735
18
+ - @powersync/service-types@0.0.0-dev-20241219110735
19
+
20
+ ## 0.1.7
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies [320e646]
25
+ - Updated dependencies [e3a9343]
26
+ - @powersync/service-core@0.12.2
27
+
28
+ ## 0.1.6
29
+
30
+ ### Patch Changes
31
+
32
+ - 2043447: Use short timeout for testing mongodb connections.
33
+
34
+ ## 0.1.5
35
+
36
+ ### Patch Changes
37
+
38
+ - 889ac46: Fix "BSONObj size is invalid" error during replication.
39
+ - Updated dependencies [889ac46]
40
+ - @powersync/service-core@0.12.1
41
+
42
+ ## 0.1.4
43
+
44
+ ### Patch Changes
45
+
46
+ - Updated dependencies [ebc62ff]
47
+ - @powersync/service-core@0.12.0
48
+ - @powersync/service-types@0.5.0
49
+
50
+ ## 0.1.3
51
+
52
+ ### Patch Changes
53
+
54
+ - Updated dependencies [62e97f3]
55
+ - Updated dependencies [a235c9f]
56
+ - Updated dependencies [8c6ce90]
57
+ - @powersync/service-core@0.11.0
58
+ - @powersync/service-sync-rules@0.22.0
59
+
60
+ ## 0.1.2
61
+
62
+ ### Patch Changes
63
+
64
+ - Updated dependencies [2a4f020]
65
+ - @powersync/service-core@0.10.1
66
+
67
+ ## 0.1.1
4
68
 
5
69
  ### Patch Changes
6
70
 
7
- - Updated dependencies [74548e4]
8
- - @powersync/service-core@0.0.0-dev-20241128134723
9
- - @powersync/service-types@0.0.0-dev-20241128134723
71
+ - Updated dependencies [2c18ad2]
72
+ - Updated dependencies [35c267f]
73
+ - @powersync/service-core@0.10.0
74
+ - @powersync/service-types@0.4.0
10
75
 
11
76
  ## 0.1.0
12
77
 
@@ -0,0 +1 @@
1
+ export * as mongo from './mongo.js';
@@ -0,0 +1,2 @@
1
+ export * as mongo from './mongo.js';
2
+ //# sourceMappingURL=db-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db-index.js","sourceRoot":"","sources":["../../src/db/db-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC"}
@@ -0,0 +1,35 @@
1
+ import * as mongo from 'mongodb';
2
+ import { configFile } from '@powersync/service-types';
3
+ /**
4
+ * Time for new connection to timeout.
5
+ */
6
+ export declare const MONGO_CONNECT_TIMEOUT_MS = 10000;
7
+ /**
8
+ * Time for individual requests to timeout the socket.
9
+ */
10
+ export declare const MONGO_SOCKET_TIMEOUT_MS = 60000;
11
+ /**
12
+ * Time for individual requests to timeout the operation.
13
+ *
14
+ * This is time spent on the cursor, not total time.
15
+ *
16
+ * Must be less than MONGO_SOCKET_TIMEOUT_MS to ensure proper error handling.
17
+ */
18
+ export declare const MONGO_OPERATION_TIMEOUT_MS = 30000;
19
+ /**
20
+ * Same as above, but specifically for clear operations.
21
+ *
22
+ * These are retried when reaching the timeout.
23
+ */
24
+ export declare const MONGO_CLEAR_OPERATION_TIMEOUT_MS = 5000;
25
+ export declare function createMongoClient(config: configFile.MongoStorageConfig): mongo.MongoClient;
26
+ /**
27
+ * Wait up to a minute for authentication errors to resolve.
28
+ *
29
+ * There can be a delay between an Atlas user being created, and that user being
30
+ * available on the database cluster. This works around it.
31
+ *
32
+ * This is specifically relevant for migrations and teardown - other parts of the stack
33
+ * can generate handle these failures and just retry or restart.
34
+ */
35
+ export declare function waitForAuth(db: mongo.Db): Promise<void>;
@@ -0,0 +1,73 @@
1
+ import * as mongo from 'mongodb';
2
+ import * as timers from 'timers/promises';
3
+ import { normalizeMongoConfig } from '../storage/storage-index.js';
4
+ /**
5
+ * Time for new connection to timeout.
6
+ */
7
+ export const MONGO_CONNECT_TIMEOUT_MS = 10000;
8
+ /**
9
+ * Time for individual requests to timeout the socket.
10
+ */
11
+ export const MONGO_SOCKET_TIMEOUT_MS = 60000;
12
+ /**
13
+ * Time for individual requests to timeout the operation.
14
+ *
15
+ * This is time spent on the cursor, not total time.
16
+ *
17
+ * Must be less than MONGO_SOCKET_TIMEOUT_MS to ensure proper error handling.
18
+ */
19
+ export const MONGO_OPERATION_TIMEOUT_MS = 30000;
20
+ /**
21
+ * Same as above, but specifically for clear operations.
22
+ *
23
+ * These are retried when reaching the timeout.
24
+ */
25
+ export const MONGO_CLEAR_OPERATION_TIMEOUT_MS = 5000;
26
+ export function createMongoClient(config) {
27
+ const normalized = normalizeMongoConfig(config);
28
+ return new mongo.MongoClient(normalized.uri, {
29
+ auth: {
30
+ username: normalized.username,
31
+ password: normalized.password
32
+ },
33
+ // Time for connection to timeout
34
+ connectTimeoutMS: MONGO_CONNECT_TIMEOUT_MS,
35
+ // Time for individual requests to timeout
36
+ socketTimeoutMS: MONGO_SOCKET_TIMEOUT_MS,
37
+ // How long to wait for new primary selection
38
+ serverSelectionTimeoutMS: 30000,
39
+ // Avoid too many connections:
40
+ // 1. It can overwhelm the source database.
41
+ // 2. Processing too many queries in parallel can cause the process to run out of memory.
42
+ maxPoolSize: 8,
43
+ maxConnecting: 3,
44
+ maxIdleTimeMS: 60000
45
+ });
46
+ }
47
+ /**
48
+ * Wait up to a minute for authentication errors to resolve.
49
+ *
50
+ * There can be a delay between an Atlas user being created, and that user being
51
+ * available on the database cluster. This works around it.
52
+ *
53
+ * This is specifically relevant for migrations and teardown - other parts of the stack
54
+ * can generate handle these failures and just retry or restart.
55
+ */
56
+ export async function waitForAuth(db) {
57
+ const start = Date.now();
58
+ while (Date.now() - start < 60000) {
59
+ try {
60
+ await db.command({ ping: 1 });
61
+ // Success
62
+ break;
63
+ }
64
+ catch (e) {
65
+ if (e.codeName == 'AuthenticationFailed') {
66
+ await timers.setTimeout(1000);
67
+ continue;
68
+ }
69
+ throw e;
70
+ }
71
+ }
72
+ }
73
+ //# sourceMappingURL=mongo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongo.js","sourceRoot":"","sources":["../../src/db/mongo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,MAAM,MAAM,iBAAiB,CAAC;AAG1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAM,CAAC;AAE/C;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAM,CAAC;AAE9C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAM,CAAC;AAEjD;;;;GAIG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,IAAK,CAAC;AAEtD,MAAM,UAAU,iBAAiB,CAAC,MAAqC;IACrE,MAAM,UAAU,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAChD,OAAO,IAAI,KAAK,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,EAAE;QAC3C,IAAI,EAAE;YACJ,QAAQ,EAAE,UAAU,CAAC,QAAQ;YAC7B,QAAQ,EAAE,UAAU,CAAC,QAAQ;SAC9B;QACD,iCAAiC;QACjC,gBAAgB,EAAE,wBAAwB;QAC1C,0CAA0C;QAC1C,eAAe,EAAE,uBAAuB;QACxC,6CAA6C;QAC7C,wBAAwB,EAAE,KAAM;QAEhC,8BAA8B;QAC9B,2CAA2C;QAC3C,yFAAyF;QACzF,WAAW,EAAE,CAAC;QAEd,aAAa,EAAE,CAAC;QAChB,aAAa,EAAE,KAAM;KACtB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,EAAY;IAC5C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACzB,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,KAAM,EAAE,CAAC;QACnC,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9B,UAAU;YACV,MAAM;QACR,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,CAAC,QAAQ,IAAI,sBAAsB,EAAE,CAAC;gBACzC,MAAM,MAAM,CAAC,UAAU,CAAC,IAAK,CAAC,CAAC;gBAC/B,SAAS;YACX,CAAC;YACD,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
1
  export * from './module/MongoModule.js';
2
+ export * as db from './db/db-index.js';
3
+ export * as storage from './storage/storage-index.js';
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './module/MongoModule.js';
2
+ export * as db from './db/db-index.js';
3
+ export * as storage from './storage/storage-index.js';
2
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AAExC,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import * as bson from 'bson';
3
+ import * as mongo from 'mongodb';
4
+ /**
5
+ * Lock Document Schema
6
+ */
7
+ export type Lock = {
8
+ name: string;
9
+ active_lock?: {
10
+ lock_id: bson.ObjectId;
11
+ ts: Date;
12
+ };
13
+ };
14
+ export type Collection = mongo.Collection<Lock>;
15
+ export type AcquireLockParams = {
16
+ /**
17
+ * Name of the process/user trying to acquire the lock.
18
+ */
19
+ name: string;
20
+ /**
21
+ * The TTL of the lock (ms). Default: 60000 ms (1 min)
22
+ */
23
+ timeout?: number;
24
+ };
25
+ export declare const releaseLock: (collection: Collection, lock_id: string) => Promise<void>;
26
+ export type CreateLockManagerParams = {
27
+ /**
28
+ * Name of the process/user trying to acquire the lock.
29
+ */
30
+ name: string;
31
+ /**
32
+ * The TTL for each lock (ms). Default: 60000 ms (1 min)
33
+ */
34
+ timeout?: number;
35
+ };
36
+ export declare const createMongoLockManager: (collection: Collection, params: CreateLockManagerParams) => framework.locks.LockManager;
@@ -0,0 +1,83 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import * as bson from 'bson';
3
+ const DEFAULT_LOCK_TIMEOUT = 60 * 1000; // 1 minute
4
+ const acquireLock = async (collection, params) => {
5
+ const now = new Date();
6
+ const lock_timeout = params.timeout ?? DEFAULT_LOCK_TIMEOUT;
7
+ const lock_id = new bson.ObjectId();
8
+ await collection.updateOne({
9
+ name: params.name
10
+ }, {
11
+ $setOnInsert: {
12
+ name: params.name
13
+ }
14
+ }, {
15
+ upsert: true
16
+ });
17
+ const expired_ts = now.getTime() - lock_timeout;
18
+ const res = await collection.updateOne({
19
+ $and: [
20
+ { name: params.name },
21
+ {
22
+ $or: [{ active_lock: { $exists: false } }, { 'active_lock.ts': { $lte: new Date(expired_ts) } }]
23
+ }
24
+ ]
25
+ }, {
26
+ $set: {
27
+ active_lock: {
28
+ lock_id: lock_id,
29
+ ts: now
30
+ }
31
+ }
32
+ });
33
+ if (res.modifiedCount === 0) {
34
+ return null;
35
+ }
36
+ return lock_id.toString();
37
+ };
38
+ const refreshLock = async (collection, lock_id) => {
39
+ const lockId = new bson.ObjectId(lock_id);
40
+ const res = await collection.updateOne({
41
+ 'active_lock.lock_id': lockId
42
+ }, {
43
+ $set: {
44
+ 'active_lock.ts': new Date()
45
+ }
46
+ });
47
+ if (res.modifiedCount === 0) {
48
+ throw new Error('Lock not found, could not refresh');
49
+ }
50
+ };
51
+ export const releaseLock = async (collection, lock_id) => {
52
+ const lockId = new bson.ObjectId(lock_id);
53
+ const res = await collection.updateOne({
54
+ 'active_lock.lock_id': lockId
55
+ }, {
56
+ $unset: {
57
+ active_lock: true
58
+ }
59
+ });
60
+ if (res.modifiedCount === 0) {
61
+ throw new Error('Lock not found, could not release');
62
+ }
63
+ };
64
+ export const createMongoLockManager = (collection, params) => {
65
+ return {
66
+ acquire: () => acquireLock(collection, params),
67
+ refresh: (lock_id) => refreshLock(collection, lock_id),
68
+ release: (lock_id) => releaseLock(collection, lock_id),
69
+ lock: async (handler) => {
70
+ const lock_id = await acquireLock(collection, params);
71
+ if (!lock_id) {
72
+ throw new framework.locks.LockActiveError();
73
+ }
74
+ try {
75
+ await handler(() => refreshLock(collection, lock_id));
76
+ }
77
+ finally {
78
+ await releaseLock(collection, lock_id);
79
+ }
80
+ }
81
+ };
82
+ };
83
+ //# sourceMappingURL=MonogLocks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MonogLocks.js","sourceRoot":"","sources":["../../src/locks/MonogLocks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mCAAmC,CAAC;AAC/D,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AA2B7B,MAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,WAAW;AAEnD,MAAM,WAAW,GAAG,KAAK,EAAE,UAAsB,EAAE,MAA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AC9C,CAAC;YAED,IAAI,CAAC;gBACH,MAAM,OAAO,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;YACxD,CAAC;oBAAS,CAAC;gBACT,MAAM,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,12 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import { migrations } from '@powersync/service-core';
3
+ import { configFile } from '@powersync/service-types';
4
+ import { PowerSyncMongo } from '../storage/storage-index.js';
5
+ export declare class MongoMigrationAgent extends migrations.AbstractPowerSyncMigrationAgent {
6
+ store: framework.MigrationStore;
7
+ locks: framework.LockManager;
8
+ protected client: PowerSyncMongo;
9
+ constructor(mongoConfig: configFile.MongoStorageConfig);
10
+ getInternalScriptsDir(): string;
11
+ [Symbol.asyncDispose](): Promise<void>;
12
+ }
@@ -0,0 +1,25 @@
1
+ import { migrations } from '@powersync/service-core';
2
+ import * as path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { createMongoLockManager } from '../locks/MonogLocks.js';
5
+ import { createPowerSyncMongo } from '../storage/storage-index.js';
6
+ import { createMongoMigrationStore } from './mongo-migration-store.js';
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+ const MONGO_LOCK_PROCESS = 'migrations';
10
+ const MIGRATIONS_DIR = path.join(__dirname, '/db/migrations');
11
+ export class MongoMigrationAgent extends migrations.AbstractPowerSyncMigrationAgent {
12
+ constructor(mongoConfig) {
13
+ super();
14
+ this.client = createPowerSyncMongo(mongoConfig);
15
+ this.store = createMongoMigrationStore(this.client.db);
16
+ this.locks = createMongoLockManager(this.client.locks, { name: MONGO_LOCK_PROCESS });
17
+ }
18
+ getInternalScriptsDir() {
19
+ return MIGRATIONS_DIR;
20
+ }
21
+ async [Symbol.asyncDispose]() {
22
+ await this.client.client.close();
23
+ }
24
+ }
25
+ //# sourceMappingURL=MonogMigrationAgent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MonogMigrationAgent.js","sourceRoot":"","sources":["../../src/migrations/MonogMigrationAgent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAkB,MAAM,6BAA6B,CAAC;AACnF,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAE3C,MAAM,kBAAkB,GAAG,YAAY,CAAC;AACxC,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;AAE9D,MAAM,OAAO,mBAAoB,SAAQ,UAAU,CAAC,+BAA+B;IAMjF,YAAY,WAA0C;QACpD,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,MAAM,GAAG,oBAAoB,CAAC,WAAW,CAAC,CAAC;QAEhD,IAAI,CAAC,KAAK,GAAG,yBAAyB,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACvD,IAAI,CAAC,KAAK,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,kBAAkB,EAAE,CAAC,CAAC;IACvF,CAAC;IAED,qBAAqB;QACnB,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;QACzB,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACnC,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import { migrations } from '@powersync/service-core';
2
+ export declare const up: migrations.PowerSyncMigrationFunction;
3
+ export declare const down: migrations.PowerSyncMigrationFunction;
@@ -0,0 +1,30 @@
1
+ import * as mongo from '../../../db/mongo.js';
2
+ import * as storage from '../../../storage/storage-index.js';
3
+ export const up = async (context) => {
4
+ const { service_context: { configuration } } = context;
5
+ const database = storage.createPowerSyncMongo(configuration.storage);
6
+ await mongo.waitForAuth(database.db);
7
+ try {
8
+ await database.bucket_parameters.createIndex({
9
+ 'key.g': 1,
10
+ lookup: 1,
11
+ _id: 1
12
+ }, { name: 'lookup1' });
13
+ }
14
+ finally {
15
+ await database.client.close();
16
+ }
17
+ };
18
+ export const down = async (context) => {
19
+ const { service_context: { configuration } } = context;
20
+ const database = storage.createPowerSyncMongo(configuration.storage);
21
+ try {
22
+ if (await database.bucket_parameters.indexExists('lookup')) {
23
+ await database.bucket_parameters.dropIndex('lookup1');
24
+ }
25
+ }
26
+ finally {
27
+ await database.client.close();
28
+ }
29
+ };
30
+ //# sourceMappingURL=1684951997326-init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1684951997326-init.js","sourceRoot":"","sources":["../../../../src/migrations/db/migrations/1684951997326-init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,sBAAsB,CAAC;AAC9C,OAAO,KAAK,OAAO,MAAM,mCAAmC,CAAC;AAE7D,MAAM,CAAC,MAAM,EAAE,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IACzE,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IACZ,MAAM,QAAQ,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IACtG,MAAM,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACrC,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,iBAAiB,CAAC,WAAW,CAC1C;YACE,OAAO,EAAE,CAAC;YACV,MAAM,EAAE,CAAC;YACT,GAAG,EAAE,CAAC;SACP,EACD,EAAE,IAAI,EAAE,SAAS,EAAE,CACpB,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IAC3E,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IAEZ,MAAM,QAAQ,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IACtG,IAAI,CAAC;QACH,IAAI,MAAM,QAAQ,CAAC,iBAAiB,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3D,MAAM,QAAQ,CAAC,iBAAiB,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const up: () => Promise<void>;
2
+ export declare const down: () => Promise<void>;
@@ -0,0 +1,5 @@
1
+ export const up = async () => {
2
+ // No-op - we don't auto-create sync rules anymore
3
+ };
4
+ export const down = async () => { };
5
+ //# sourceMappingURL=1688556755264-initial-sync-rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1688556755264-initial-sync-rules.js","sourceRoot":"","sources":["../../../../src/migrations/db/migrations/1688556755264-initial-sync-rules.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,EAAE;IAC3B,kDAAkD;AACpD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { migrations } from '@powersync/service-core';
2
+ export declare const up: migrations.PowerSyncMigrationFunction;
3
+ export declare const down: migrations.PowerSyncMigrationFunction;
@@ -0,0 +1,54 @@
1
+ import { storage as core_storage } from '@powersync/service-core';
2
+ import * as mongo from '../../../db/mongo.js';
3
+ import * as storage from '../../../storage/storage-index.js';
4
+ export const up = async (context) => {
5
+ const { service_context: { configuration } } = context;
6
+ const db = storage.createPowerSyncMongo(configuration.storage);
7
+ await mongo.waitForAuth(db.db);
8
+ try {
9
+ // We keep the old flags for existing deployments still shutting down.
10
+ // 1. New sync rules: `active = false, snapshot_done = false, replicating = true, auto_activate = true`
11
+ await db.sync_rules.updateMany({
12
+ active: { $ne: true },
13
+ replicating: true,
14
+ auto_activate: true
15
+ }, { $set: { state: core_storage.SyncRuleState.PROCESSING } });
16
+ // 2. Snapshot done: `active = true, snapshot_done = true, replicating = true, auto_activate = false`
17
+ await db.sync_rules.updateMany({
18
+ active: true
19
+ }, { $set: { state: core_storage.SyncRuleState.ACTIVE } });
20
+ // 3. Stopped: `active = false, snapshot_done = true, replicating = false, auto_activate = false`.
21
+ await db.sync_rules.updateMany({
22
+ active: { $ne: true },
23
+ replicating: { $ne: true },
24
+ auto_activate: { $ne: true }
25
+ }, { $set: { state: core_storage.SyncRuleState.STOP } });
26
+ const remaining = await db.sync_rules.find({ state: null }).toArray();
27
+ if (remaining.length > 0) {
28
+ const slots = remaining.map((doc) => doc.slot_name).join(', ');
29
+ throw new Error(`Invalid state for sync rules: ${slots}`);
30
+ }
31
+ }
32
+ finally {
33
+ await db.client.close();
34
+ }
35
+ };
36
+ export const down = async (context) => {
37
+ const { service_context: { configuration } } = context;
38
+ const db = storage.createPowerSyncMongo(configuration.storage);
39
+ try {
40
+ await db.sync_rules.updateMany({
41
+ state: core_storage.SyncRuleState.ACTIVE
42
+ }, { $set: { active: true, replicating: true } });
43
+ await db.sync_rules.updateMany({
44
+ state: core_storage.SyncRuleState.PROCESSING
45
+ }, { $set: { active: false, replicating: true, auto_activate: true } });
46
+ await db.sync_rules.updateMany({
47
+ $or: [{ state: core_storage.SyncRuleState.STOP }, { state: core_storage.SyncRuleState.TERMINATED }]
48
+ }, { $set: { active: false, replicating: false, auto_activate: false } });
49
+ }
50
+ finally {
51
+ await db.client.close();
52
+ }
53
+ };
54
+ //# sourceMappingURL=1702295701188-sync-rule-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1702295701188-sync-rule-state.js","sourceRoot":"","sources":["../../../../src/migrations/db/migrations/1702295701188-sync-rule-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAc,MAAM,yBAAyB,CAAC;AAE9E,OAAO,KAAK,KAAK,MAAM,sBAAsB,CAAC;AAC9C,OAAO,KAAK,OAAO,MAAM,mCAAmC,CAAC;AAuB7D,MAAM,CAAC,MAAM,EAAE,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IACzE,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IACZ,MAAM,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IAEhG,MAAM,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC/B,IAAI,CAAC;QACH,sEAAsE;QAEtE,uGAAuG;QACvG,MAAM,EAAE,CAAC,UAAU,CAAC,UAAU,CAC5B;YACE,MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE;YACrB,WAAW,EAAE,IAAI;YACjB,aAAa,EAAE,IAAI;SACpB,EACD,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,YAAY,CAAC,aAAa,CAAC,UAAU,EAAE,EAAE,CAC3D,CAAC;QAEF,qGAAqG;QACrG,MAAM,EAAE,CAAC,UAAU,CAAC,UAAU,CAC5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,KAAK,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM;SACzC,EACD,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,CAC9C,CAAC;QAEF,MAAM,EAAE,CAAC,UAAU,CAAC,UAAU,CAC5B;YACE,KAAK,EAAE,YAAY,CAAC,aAAa,CAAC,UAAU;SAC7C,EACD,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,EAAE,CACpE,CAAC;QAEF,MAAM,EAAE,CAAC,UAAU,CAAC,UAAU,CAC5B;YACE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,aAAa,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,YAAY,CAAC,aAAa,CAAC,UAAU,EAAE,CAAC;SACpG,EACD,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,EAAE,CACtE,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { migrations } from '@powersync/service-core';
2
+ export declare const up: migrations.PowerSyncMigrationFunction;
3
+ export declare const down: migrations.PowerSyncMigrationFunction;
@@ -0,0 +1,26 @@
1
+ import * as storage from '../../../storage/storage-index.js';
2
+ export const up = async (context) => {
3
+ const { service_context: { configuration } } = context;
4
+ const db = storage.createPowerSyncMongo(configuration.storage);
5
+ try {
6
+ await db.write_checkpoints.createIndex({
7
+ user_id: 1
8
+ }, { name: 'user_id' });
9
+ }
10
+ finally {
11
+ await db.client.close();
12
+ }
13
+ };
14
+ export const down = async (context) => {
15
+ const { service_context: { configuration } } = context;
16
+ const db = storage.createPowerSyncMongo(configuration.storage);
17
+ try {
18
+ if (await db.write_checkpoints.indexExists('user_id')) {
19
+ await db.write_checkpoints.dropIndex('user_id');
20
+ }
21
+ }
22
+ finally {
23
+ await db.client.close();
24
+ }
25
+ };
26
+ //# sourceMappingURL=1711543888062-write-checkpoint-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1711543888062-write-checkpoint-index.js","sourceRoot":"","sources":["../../../../src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,OAAO,MAAM,mCAAmC,CAAC;AAE7D,MAAM,CAAC,MAAM,EAAE,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IACzE,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IACZ,MAAM,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IAEhG,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,iBAAiB,CAAC,WAAW,CACpC;YACE,OAAO,EAAE,CAAC;SACX,EACD,EAAE,IAAI,EAAE,SAAS,EAAE,CACpB,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IAC3E,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IAEZ,MAAM,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IAEhG,IAAI,CAAC;QACH,IAAI,MAAM,EAAE,CAAC,iBAAiB,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;YACtD,MAAM,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { migrations } from '@powersync/service-core';
2
+ export declare const up: migrations.PowerSyncMigrationFunction;
3
+ export declare const down: migrations.PowerSyncMigrationFunction;
@@ -0,0 +1,28 @@
1
+ import * as storage from '../../../storage/storage-index.js';
2
+ const INDEX_NAME = 'user_sync_rule_unique';
3
+ export const up = async (context) => {
4
+ const { service_context: { configuration } } = context;
5
+ const db = storage.createPowerSyncMongo(configuration.storage);
6
+ try {
7
+ await db.custom_write_checkpoints.createIndex({
8
+ user_id: 1,
9
+ sync_rules_id: 1
10
+ }, { name: INDEX_NAME, unique: true });
11
+ }
12
+ finally {
13
+ await db.client.close();
14
+ }
15
+ };
16
+ export const down = async (context) => {
17
+ const { service_context: { configuration } } = context;
18
+ const db = storage.createPowerSyncMongo(configuration.storage);
19
+ try {
20
+ if (await db.custom_write_checkpoints.indexExists(INDEX_NAME)) {
21
+ await db.custom_write_checkpoints.dropIndex(INDEX_NAME);
22
+ }
23
+ }
24
+ finally {
25
+ await db.client.close();
26
+ }
27
+ };
28
+ //# sourceMappingURL=1727099539247-custom-write-checkpoint-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1727099539247-custom-write-checkpoint-index.js","sourceRoot":"","sources":["../../../../src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,OAAO,MAAM,mCAAmC,CAAC;AAE7D,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAE3C,MAAM,CAAC,MAAM,EAAE,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IACzE,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IACZ,MAAM,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IAEhG,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,wBAAwB,CAAC,WAAW,CAC3C;YACE,OAAO,EAAE,CAAC;YACV,aAAa,EAAE,CAAC;SACjB,EACD,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,CACnC,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAA0C,KAAK,EAAE,OAAO,EAAE,EAAE;IAC3E,MAAM,EACJ,eAAe,EAAE,EAAE,aAAa,EAAE,EACnC,GAAG,OAAO,CAAC;IAEZ,MAAM,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,aAAa,CAAC,OAAwC,CAAC,CAAC;IAEhG,IAAI,CAAC;QACH,IAAI,MAAM,EAAE,CAAC,wBAAwB,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,CAAC;YAC9D,MAAM,EAAE,CAAC,wBAAwB,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { migrations } from '@powersync/lib-services-framework';
2
+ import { Db } from 'mongodb';
3
+ /**
4
+ * A custom store for node-migrate which is used to save and load migrations that have
5
+ * been operated on to mongo.
6
+ */
7
+ export declare const createMongoMigrationStore: (db: Db) => migrations.MigrationStore;
@@ -0,0 +1,49 @@
1
+ import * as path from 'path';
2
+ /**
3
+ * A custom store for node-migrate which is used to save and load migrations that have
4
+ * been operated on to mongo.
5
+ */
6
+ export const createMongoMigrationStore = (db) => {
7
+ const collection = db.collection('migrations');
8
+ return {
9
+ load: async () => {
10
+ const state_entry = await collection.findOne();
11
+ if (!state_entry) {
12
+ return;
13
+ }
14
+ const { _id, ...state } = state_entry;
15
+ /**
16
+ * This is for backwards compatibility. A previous version of the migration tool used to save
17
+ * state as `lastRun`.
18
+ */
19
+ let last_run = state.last_run;
20
+ if ('lastRun' in state) {
21
+ last_run = state.lastRun;
22
+ }
23
+ /**
24
+ * This is for backwards compatibility. A previous version of the migration tool used to include the
25
+ * file extension in migration names. This strips that extension off if it exists
26
+ */
27
+ const extension = path.extname(last_run);
28
+ if (extension) {
29
+ last_run = last_run.replace(extension, '');
30
+ }
31
+ return {
32
+ last_run,
33
+ log: state.log || []
34
+ };
35
+ },
36
+ clear: async () => {
37
+ await collection.deleteMany({});
38
+ },
39
+ save: async (state) => {
40
+ await collection.replaceOne({}, {
41
+ last_run: state.last_run,
42
+ log: state.log
43
+ }, {
44
+ upsert: true
45
+ });
46
+ }
47
+ };
48
+ };
49
+ //# sourceMappingURL=mongo-migration-store.js.map