@powersync/service-module-mongodb 0.0.0-dev-20250102111825 → 0.0.0-dev-20250108073049

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 (142) hide show
  1. package/CHANGELOG.md +8 -5
  2. package/dist/api/MongoRouteAPIAdapter.d.ts +1 -1
  3. package/dist/api/MongoRouteAPIAdapter.js +4 -3
  4. package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
  5. package/dist/index.d.ts +3 -2
  6. package/dist/index.js +3 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/module/MongoModule.d.ts +0 -1
  9. package/dist/module/MongoModule.js +2 -10
  10. package/dist/module/MongoModule.js.map +1 -1
  11. package/dist/replication/MongoRelation.js +15 -5
  12. package/dist/replication/MongoRelation.js.map +1 -1
  13. package/dist/types/types.d.ts +16 -22
  14. package/dist/types/types.js +4 -24
  15. package/dist/types/types.js.map +1 -1
  16. package/package.json +9 -10
  17. package/src/api/MongoRouteAPIAdapter.ts +4 -4
  18. package/src/index.ts +3 -4
  19. package/src/module/MongoModule.ts +2 -14
  20. package/src/replication/MongoRelation.ts +13 -5
  21. package/src/types/types.ts +8 -34
  22. package/test/src/change_stream.test.ts +2 -4
  23. package/test/src/env.ts +1 -1
  24. package/test/src/mongo_test.test.ts +69 -10
  25. package/test/src/setup.ts +4 -1
  26. package/test/src/slow_tests.test.ts +5 -12
  27. package/test/src/util.ts +5 -38
  28. package/test/tsconfig.json +1 -2
  29. package/tsconfig.json +6 -0
  30. package/tsconfig.tsbuildinfo +1 -1
  31. package/dist/db/db-index.d.ts +0 -1
  32. package/dist/db/db-index.js +0 -2
  33. package/dist/db/db-index.js.map +0 -1
  34. package/dist/db/mongo.d.ts +0 -35
  35. package/dist/db/mongo.js +0 -73
  36. package/dist/db/mongo.js.map +0 -1
  37. package/dist/locks/MongoLockManager.d.ts +0 -25
  38. package/dist/locks/MongoLockManager.js +0 -79
  39. package/dist/locks/MongoLockManager.js.map +0 -1
  40. package/dist/migrations/MongoMigrationAgent.d.ts +0 -12
  41. package/dist/migrations/MongoMigrationAgent.js +0 -25
  42. package/dist/migrations/MongoMigrationAgent.js.map +0 -1
  43. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  44. package/dist/migrations/db/migrations/1684951997326-init.js +0 -30
  45. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  46. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  47. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  48. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  49. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  50. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -54
  51. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  52. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  53. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -26
  54. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  55. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  56. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -28
  57. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  58. package/dist/migrations/mongo-migration-store.d.ts +0 -7
  59. package/dist/migrations/mongo-migration-store.js +0 -49
  60. package/dist/migrations/mongo-migration-store.js.map +0 -1
  61. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  62. package/dist/storage/MongoBucketStorage.js +0 -426
  63. package/dist/storage/MongoBucketStorage.js.map +0 -1
  64. package/dist/storage/implementation/MongoBucketBatch.d.ts +0 -72
  65. package/dist/storage/implementation/MongoBucketBatch.js +0 -681
  66. package/dist/storage/implementation/MongoBucketBatch.js.map +0 -1
  67. package/dist/storage/implementation/MongoCompactor.d.ts +0 -40
  68. package/dist/storage/implementation/MongoCompactor.js +0 -300
  69. package/dist/storage/implementation/MongoCompactor.js.map +0 -1
  70. package/dist/storage/implementation/MongoIdSequence.d.ts +0 -12
  71. package/dist/storage/implementation/MongoIdSequence.js +0 -21
  72. package/dist/storage/implementation/MongoIdSequence.js.map +0 -1
  73. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -9
  74. package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -9
  75. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
  76. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -20
  77. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -26
  78. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
  79. package/dist/storage/implementation/MongoStorageProvider.d.ts +0 -6
  80. package/dist/storage/implementation/MongoStorageProvider.js +0 -34
  81. package/dist/storage/implementation/MongoStorageProvider.js.map +0 -1
  82. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +0 -36
  83. package/dist/storage/implementation/MongoSyncBucketStorage.js +0 -529
  84. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +0 -1
  85. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +0 -16
  86. package/dist/storage/implementation/MongoSyncRulesLock.js +0 -65
  87. package/dist/storage/implementation/MongoSyncRulesLock.js.map +0 -1
  88. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +0 -20
  89. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +0 -104
  90. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +0 -1
  91. package/dist/storage/implementation/OperationBatch.d.ts +0 -34
  92. package/dist/storage/implementation/OperationBatch.js +0 -119
  93. package/dist/storage/implementation/OperationBatch.js.map +0 -1
  94. package/dist/storage/implementation/PersistedBatch.d.ts +0 -46
  95. package/dist/storage/implementation/PersistedBatch.js +0 -223
  96. package/dist/storage/implementation/PersistedBatch.js.map +0 -1
  97. package/dist/storage/implementation/config.d.ts +0 -19
  98. package/dist/storage/implementation/config.js +0 -26
  99. package/dist/storage/implementation/config.js.map +0 -1
  100. package/dist/storage/implementation/db.d.ts +0 -36
  101. package/dist/storage/implementation/db.js +0 -47
  102. package/dist/storage/implementation/db.js.map +0 -1
  103. package/dist/storage/implementation/models.d.ts +0 -139
  104. package/dist/storage/implementation/models.js +0 -2
  105. package/dist/storage/implementation/models.js.map +0 -1
  106. package/dist/storage/implementation/util.d.ts +0 -41
  107. package/dist/storage/implementation/util.js +0 -141
  108. package/dist/storage/implementation/util.js.map +0 -1
  109. package/dist/storage/storage-index.d.ts +0 -14
  110. package/dist/storage/storage-index.js +0 -15
  111. package/dist/storage/storage-index.js.map +0 -1
  112. package/src/db/db-index.ts +0 -1
  113. package/src/db/mongo.ts +0 -81
  114. package/src/locks/MongoLockManager.ts +0 -123
  115. package/src/migrations/MongoMigrationAgent.ts +0 -39
  116. package/src/migrations/db/migrations/1684951997326-init.ts +0 -39
  117. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  118. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -105
  119. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -38
  120. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -40
  121. package/src/migrations/mongo-migration-store.ts +0 -62
  122. package/src/storage/MongoBucketStorage.ts +0 -531
  123. package/src/storage/implementation/MongoBucketBatch.ts +0 -896
  124. package/src/storage/implementation/MongoCompactor.ts +0 -381
  125. package/src/storage/implementation/MongoIdSequence.ts +0 -24
  126. package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -16
  127. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -49
  128. package/src/storage/implementation/MongoStorageProvider.ts +0 -42
  129. package/src/storage/implementation/MongoSyncBucketStorage.ts +0 -612
  130. package/src/storage/implementation/MongoSyncRulesLock.ts +0 -88
  131. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +0 -146
  132. package/src/storage/implementation/OperationBatch.ts +0 -129
  133. package/src/storage/implementation/PersistedBatch.ts +0 -283
  134. package/src/storage/implementation/config.ts +0 -40
  135. package/src/storage/implementation/db.ts +0 -87
  136. package/src/storage/implementation/models.ts +0 -161
  137. package/src/storage/implementation/util.ts +0 -154
  138. package/src/storage/storage-index.ts +0 -14
  139. package/test/src/__snapshots__/storage_sync.test.ts.snap +0 -332
  140. package/test/src/storage.test.ts +0 -7
  141. package/test/src/storage_compacting.test.ts +0 -6
  142. package/test/src/storage_sync.test.ts +0 -113
@@ -1,39 +0,0 @@
1
- import { migrations } from '@powersync/service-core';
2
- import { configFile } from '@powersync/service-types';
3
- import * as mongo from '../../../db/mongo.js';
4
- import * as storage from '../../../storage/storage-index.js';
5
-
6
- export const up: migrations.PowerSyncMigrationFunction = async (context) => {
7
- const {
8
- service_context: { configuration }
9
- } = context;
10
- const database = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
11
- await mongo.waitForAuth(database.db);
12
- try {
13
- await database.bucket_parameters.createIndex(
14
- {
15
- 'key.g': 1,
16
- lookup: 1,
17
- _id: 1
18
- },
19
- { name: 'lookup1' }
20
- );
21
- } finally {
22
- await database.client.close();
23
- }
24
- };
25
-
26
- export const down: migrations.PowerSyncMigrationFunction = async (context) => {
27
- const {
28
- service_context: { configuration }
29
- } = context;
30
-
31
- const database = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
32
- try {
33
- if (await database.bucket_parameters.indexExists('lookup')) {
34
- await database.bucket_parameters.dropIndex('lookup1');
35
- }
36
- } finally {
37
- await database.client.close();
38
- }
39
- };
@@ -1,5 +0,0 @@
1
- export const up = async () => {
2
- // No-op - we don't auto-create sync rules anymore
3
- };
4
-
5
- export const down = async () => {};
@@ -1,105 +0,0 @@
1
- import { storage as core_storage, migrations } from '@powersync/service-core';
2
- import { configFile } from '@powersync/service-types';
3
- import * as mongo from '../../../db/mongo.js';
4
- import * as storage from '../../../storage/storage-index.js';
5
-
6
- interface LegacySyncRulesDocument extends storage.SyncRuleDocument {
7
- /**
8
- * True if this is the active sync rules.
9
- * requires `snapshot_done == true` and `replicating == true`.
10
- */
11
- active?: boolean;
12
-
13
- /**
14
- * True if this sync rules should be used for replication.
15
- *
16
- * During reprocessing, there is one sync rules with `replicating = true, active = true`,
17
- * and one with `replicating = true, active = false, auto_activate = true`.
18
- */
19
- replicating?: boolean;
20
-
21
- /**
22
- * True if the sync rules should set `active = true` when `snapshot_done` = true.
23
- */
24
- auto_activate?: boolean;
25
- }
26
-
27
- export const up: migrations.PowerSyncMigrationFunction = async (context) => {
28
- const {
29
- service_context: { configuration }
30
- } = context;
31
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
32
-
33
- await mongo.waitForAuth(db.db);
34
- try {
35
- // We keep the old flags for existing deployments still shutting down.
36
-
37
- // 1. New sync rules: `active = false, snapshot_done = false, replicating = true, auto_activate = true`
38
- await db.sync_rules.updateMany(
39
- {
40
- active: { $ne: true },
41
- replicating: true,
42
- auto_activate: true
43
- },
44
- { $set: { state: core_storage.SyncRuleState.PROCESSING } }
45
- );
46
-
47
- // 2. Snapshot done: `active = true, snapshot_done = true, replicating = true, auto_activate = false`
48
- await db.sync_rules.updateMany(
49
- {
50
- active: true
51
- },
52
- { $set: { state: core_storage.SyncRuleState.ACTIVE } }
53
- );
54
-
55
- // 3. Stopped: `active = false, snapshot_done = true, replicating = false, auto_activate = false`.
56
- await db.sync_rules.updateMany(
57
- {
58
- active: { $ne: true },
59
- replicating: { $ne: true },
60
- auto_activate: { $ne: true }
61
- },
62
- { $set: { state: core_storage.SyncRuleState.STOP } }
63
- );
64
-
65
- const remaining = await db.sync_rules.find({ state: null as any }).toArray();
66
- if (remaining.length > 0) {
67
- const slots = remaining.map((doc) => doc.slot_name).join(', ');
68
- throw new Error(`Invalid state for sync rules: ${slots}`);
69
- }
70
- } finally {
71
- await db.client.close();
72
- }
73
- };
74
-
75
- export const down: migrations.PowerSyncMigrationFunction = async (context) => {
76
- const {
77
- service_context: { configuration }
78
- } = context;
79
-
80
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
81
- try {
82
- await db.sync_rules.updateMany(
83
- {
84
- state: core_storage.SyncRuleState.ACTIVE
85
- },
86
- { $set: { active: true, replicating: true } }
87
- );
88
-
89
- await db.sync_rules.updateMany(
90
- {
91
- state: core_storage.SyncRuleState.PROCESSING
92
- },
93
- { $set: { active: false, replicating: true, auto_activate: true } }
94
- );
95
-
96
- await db.sync_rules.updateMany(
97
- {
98
- $or: [{ state: core_storage.SyncRuleState.STOP }, { state: core_storage.SyncRuleState.TERMINATED }]
99
- },
100
- { $set: { active: false, replicating: false, auto_activate: false } }
101
- );
102
- } finally {
103
- await db.client.close();
104
- }
105
- };
@@ -1,38 +0,0 @@
1
- import { migrations } from '@powersync/service-core';
2
- import { configFile } from '@powersync/service-types';
3
-
4
- import * as storage from '../../../storage/storage-index.js';
5
-
6
- export const up: migrations.PowerSyncMigrationFunction = async (context) => {
7
- const {
8
- service_context: { configuration }
9
- } = context;
10
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
11
-
12
- try {
13
- await db.write_checkpoints.createIndex(
14
- {
15
- user_id: 1
16
- },
17
- { name: 'user_id' }
18
- );
19
- } finally {
20
- await db.client.close();
21
- }
22
- };
23
-
24
- export const down: migrations.PowerSyncMigrationFunction = async (context) => {
25
- const {
26
- service_context: { configuration }
27
- } = context;
28
-
29
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
30
-
31
- try {
32
- if (await db.write_checkpoints.indexExists('user_id')) {
33
- await db.write_checkpoints.dropIndex('user_id');
34
- }
35
- } finally {
36
- await db.client.close();
37
- }
38
- };
@@ -1,40 +0,0 @@
1
- import { migrations } from '@powersync/service-core';
2
- import { configFile } from '@powersync/service-types';
3
- import * as storage from '../../../storage/storage-index.js';
4
-
5
- const INDEX_NAME = 'user_sync_rule_unique';
6
-
7
- export const up: migrations.PowerSyncMigrationFunction = async (context) => {
8
- const {
9
- service_context: { configuration }
10
- } = context;
11
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
12
-
13
- try {
14
- await db.custom_write_checkpoints.createIndex(
15
- {
16
- user_id: 1,
17
- sync_rules_id: 1
18
- },
19
- { name: INDEX_NAME, unique: true }
20
- );
21
- } finally {
22
- await db.client.close();
23
- }
24
- };
25
-
26
- export const down: migrations.PowerSyncMigrationFunction = async (context) => {
27
- const {
28
- service_context: { configuration }
29
- } = context;
30
-
31
- const db = storage.createPowerSyncMongo(configuration.storage as configFile.MongoStorageConfig);
32
-
33
- try {
34
- if (await db.custom_write_checkpoints.indexExists(INDEX_NAME)) {
35
- await db.custom_write_checkpoints.dropIndex(INDEX_NAME);
36
- }
37
- } finally {
38
- await db.client.close();
39
- }
40
- };
@@ -1,62 +0,0 @@
1
- import { migrations } from '@powersync/lib-services-framework';
2
- import { Db } from 'mongodb';
3
- import * as path from 'path';
4
-
5
- /**
6
- * A custom store for node-migrate which is used to save and load migrations that have
7
- * been operated on to mongo.
8
- */
9
- export const createMongoMigrationStore = (db: Db): migrations.MigrationStore => {
10
- const collection = db.collection<migrations.MigrationState>('migrations');
11
-
12
- return {
13
- load: async () => {
14
- const state_entry = await collection.findOne();
15
- if (!state_entry) {
16
- return;
17
- }
18
-
19
- const { _id, ...state } = state_entry;
20
-
21
- /**
22
- * This is for backwards compatibility. A previous version of the migration tool used to save
23
- * state as `lastRun`.
24
- */
25
- let last_run = state.last_run;
26
- if ('lastRun' in state) {
27
- last_run = (state as any).lastRun;
28
- }
29
-
30
- /**
31
- * This is for backwards compatibility. A previous version of the migration tool used to include the
32
- * file extension in migration names. This strips that extension off if it exists
33
- */
34
- const extension = path.extname(last_run);
35
- if (extension) {
36
- last_run = last_run.replace(extension, '');
37
- }
38
-
39
- return {
40
- last_run,
41
- log: state.log || []
42
- };
43
- },
44
-
45
- clear: async () => {
46
- await collection.deleteMany({});
47
- },
48
-
49
- save: async (state: migrations.MigrationState) => {
50
- await collection.replaceOne(
51
- {},
52
- {
53
- last_run: state.last_run,
54
- log: state.log
55
- },
56
- {
57
- upsert: true
58
- }
59
- );
60
- }
61
- };
62
- };