@powersync/service-core 0.13.0 → 0.14.0

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 (181) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/entry/commands/compact-action.js +14 -14
  3. package/dist/entry/commands/compact-action.js.map +1 -1
  4. package/dist/entry/commands/migrate-action.js +15 -4
  5. package/dist/entry/commands/migrate-action.js.map +1 -1
  6. package/dist/index.d.ts +1 -3
  7. package/dist/index.js +1 -3
  8. package/dist/index.js.map +1 -1
  9. package/dist/migrations/PowerSyncMigrationManager.d.ts +17 -0
  10. package/dist/migrations/PowerSyncMigrationManager.js +21 -0
  11. package/dist/migrations/PowerSyncMigrationManager.js.map +1 -0
  12. package/dist/migrations/ensure-automatic-migrations.d.ts +4 -0
  13. package/dist/migrations/ensure-automatic-migrations.js +14 -0
  14. package/dist/migrations/ensure-automatic-migrations.js.map +1 -0
  15. package/dist/migrations/migrations-index.d.ts +2 -3
  16. package/dist/migrations/migrations-index.js +2 -3
  17. package/dist/migrations/migrations-index.js.map +1 -1
  18. package/dist/routes/configure-fastify.d.ts +12 -12
  19. package/dist/routes/endpoints/admin.d.ts +24 -24
  20. package/dist/storage/BucketStorage.d.ts +49 -1
  21. package/dist/storage/BucketStorage.js +26 -0
  22. package/dist/storage/BucketStorage.js.map +1 -1
  23. package/dist/storage/bson.d.ts +24 -0
  24. package/dist/storage/bson.js +73 -0
  25. package/dist/storage/bson.js.map +1 -0
  26. package/dist/storage/storage-index.d.ts +3 -14
  27. package/dist/storage/storage-index.js +3 -14
  28. package/dist/storage/storage-index.js.map +1 -1
  29. package/dist/sync/sync.js +3 -1
  30. package/dist/sync/sync.js.map +1 -1
  31. package/dist/system/ServiceContext.d.ts +3 -0
  32. package/dist/system/ServiceContext.js +11 -3
  33. package/dist/system/ServiceContext.js.map +1 -1
  34. package/dist/util/config/types.d.ts +2 -2
  35. package/dist/util/utils.d.ts +13 -1
  36. package/dist/util/utils.js +20 -1
  37. package/dist/util/utils.js.map +1 -1
  38. package/package.json +7 -8
  39. package/src/entry/commands/compact-action.ts +19 -14
  40. package/src/entry/commands/migrate-action.ts +17 -4
  41. package/src/index.ts +1 -4
  42. package/src/migrations/PowerSyncMigrationManager.ts +42 -0
  43. package/src/migrations/ensure-automatic-migrations.ts +15 -0
  44. package/src/migrations/migrations-index.ts +2 -3
  45. package/src/storage/BucketStorage.ts +53 -1
  46. package/src/storage/bson.ts +78 -0
  47. package/src/storage/storage-index.ts +3 -15
  48. package/src/sync/sync.ts +3 -1
  49. package/src/system/ServiceContext.ts +17 -4
  50. package/src/util/config/types.ts +2 -2
  51. package/src/util/utils.ts +21 -1
  52. package/test/src/env.ts +0 -1
  53. package/tsconfig.tsbuildinfo +1 -1
  54. package/dist/db/db-index.d.ts +0 -1
  55. package/dist/db/db-index.js +0 -2
  56. package/dist/db/db-index.js.map +0 -1
  57. package/dist/db/mongo.d.ts +0 -35
  58. package/dist/db/mongo.js +0 -73
  59. package/dist/db/mongo.js.map +0 -1
  60. package/dist/locks/LockManager.d.ts +0 -10
  61. package/dist/locks/LockManager.js +0 -7
  62. package/dist/locks/LockManager.js.map +0 -1
  63. package/dist/locks/MongoLocks.d.ts +0 -36
  64. package/dist/locks/MongoLocks.js +0 -81
  65. package/dist/locks/MongoLocks.js.map +0 -1
  66. package/dist/locks/locks-index.d.ts +0 -2
  67. package/dist/locks/locks-index.js +0 -3
  68. package/dist/locks/locks-index.js.map +0 -1
  69. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  70. package/dist/migrations/db/migrations/1684951997326-init.js +0 -33
  71. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  72. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  73. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  74. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  75. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  76. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -56
  77. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  78. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  79. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -29
  80. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  81. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  82. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -31
  83. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  84. package/dist/migrations/definitions.d.ts +0 -18
  85. package/dist/migrations/definitions.js +0 -6
  86. package/dist/migrations/definitions.js.map +0 -1
  87. package/dist/migrations/executor.d.ts +0 -16
  88. package/dist/migrations/executor.js +0 -64
  89. package/dist/migrations/executor.js.map +0 -1
  90. package/dist/migrations/migrations.d.ts +0 -18
  91. package/dist/migrations/migrations.js +0 -110
  92. package/dist/migrations/migrations.js.map +0 -1
  93. package/dist/migrations/store/migration-store.d.ts +0 -11
  94. package/dist/migrations/store/migration-store.js +0 -46
  95. package/dist/migrations/store/migration-store.js.map +0 -1
  96. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  97. package/dist/storage/MongoBucketStorage.js +0 -427
  98. package/dist/storage/MongoBucketStorage.js.map +0 -1
  99. package/dist/storage/mongo/MongoBucketBatch.d.ts +0 -74
  100. package/dist/storage/mongo/MongoBucketBatch.js +0 -683
  101. package/dist/storage/mongo/MongoBucketBatch.js.map +0 -1
  102. package/dist/storage/mongo/MongoCompactor.d.ts +0 -40
  103. package/dist/storage/mongo/MongoCompactor.js +0 -310
  104. package/dist/storage/mongo/MongoCompactor.js.map +0 -1
  105. package/dist/storage/mongo/MongoIdSequence.d.ts +0 -12
  106. package/dist/storage/mongo/MongoIdSequence.js +0 -21
  107. package/dist/storage/mongo/MongoIdSequence.js.map +0 -1
  108. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +0 -9
  109. package/dist/storage/mongo/MongoPersistedSyncRules.js +0 -9
  110. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +0 -1
  111. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +0 -20
  112. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +0 -26
  113. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +0 -1
  114. package/dist/storage/mongo/MongoStorageProvider.d.ts +0 -5
  115. package/dist/storage/mongo/MongoStorageProvider.js +0 -26
  116. package/dist/storage/mongo/MongoStorageProvider.js.map +0 -1
  117. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +0 -38
  118. package/dist/storage/mongo/MongoSyncBucketStorage.js +0 -534
  119. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +0 -1
  120. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +0 -16
  121. package/dist/storage/mongo/MongoSyncRulesLock.js +0 -65
  122. package/dist/storage/mongo/MongoSyncRulesLock.js.map +0 -1
  123. package/dist/storage/mongo/MongoWriteCheckpointAPI.d.ts +0 -20
  124. package/dist/storage/mongo/MongoWriteCheckpointAPI.js +0 -104
  125. package/dist/storage/mongo/MongoWriteCheckpointAPI.js.map +0 -1
  126. package/dist/storage/mongo/OperationBatch.d.ts +0 -35
  127. package/dist/storage/mongo/OperationBatch.js +0 -119
  128. package/dist/storage/mongo/OperationBatch.js.map +0 -1
  129. package/dist/storage/mongo/PersistedBatch.d.ts +0 -46
  130. package/dist/storage/mongo/PersistedBatch.js +0 -223
  131. package/dist/storage/mongo/PersistedBatch.js.map +0 -1
  132. package/dist/storage/mongo/config.d.ts +0 -19
  133. package/dist/storage/mongo/config.js +0 -26
  134. package/dist/storage/mongo/config.js.map +0 -1
  135. package/dist/storage/mongo/db.d.ts +0 -36
  136. package/dist/storage/mongo/db.js +0 -47
  137. package/dist/storage/mongo/db.js.map +0 -1
  138. package/dist/storage/mongo/models.d.ts +0 -163
  139. package/dist/storage/mongo/models.js +0 -27
  140. package/dist/storage/mongo/models.js.map +0 -1
  141. package/dist/storage/mongo/util.d.ts +0 -54
  142. package/dist/storage/mongo/util.js +0 -190
  143. package/dist/storage/mongo/util.js.map +0 -1
  144. package/src/db/db-index.ts +0 -1
  145. package/src/db/mongo.ts +0 -81
  146. package/src/locks/LockManager.ts +0 -16
  147. package/src/locks/MongoLocks.ts +0 -142
  148. package/src/locks/locks-index.ts +0 -2
  149. package/src/migrations/db/migrations/1684951997326-init.ts +0 -38
  150. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  151. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -102
  152. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -34
  153. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -37
  154. package/src/migrations/definitions.ts +0 -21
  155. package/src/migrations/executor.ts +0 -87
  156. package/src/migrations/migrations.ts +0 -142
  157. package/src/migrations/store/migration-store.ts +0 -63
  158. package/src/storage/MongoBucketStorage.ts +0 -541
  159. package/src/storage/mongo/MongoBucketBatch.ts +0 -900
  160. package/src/storage/mongo/MongoCompactor.ts +0 -393
  161. package/src/storage/mongo/MongoIdSequence.ts +0 -24
  162. package/src/storage/mongo/MongoPersistedSyncRules.ts +0 -16
  163. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +0 -50
  164. package/src/storage/mongo/MongoStorageProvider.ts +0 -31
  165. package/src/storage/mongo/MongoSyncBucketStorage.ts +0 -640
  166. package/src/storage/mongo/MongoSyncRulesLock.ts +0 -85
  167. package/src/storage/mongo/MongoWriteCheckpointAPI.ts +0 -154
  168. package/src/storage/mongo/OperationBatch.ts +0 -131
  169. package/src/storage/mongo/PersistedBatch.ts +0 -285
  170. package/src/storage/mongo/config.ts +0 -40
  171. package/src/storage/mongo/db.ts +0 -88
  172. package/src/storage/mongo/models.ts +0 -187
  173. package/src/storage/mongo/util.ts +0 -203
  174. package/test/src/__snapshots__/sync.test.ts.snap +0 -332
  175. package/test/src/bucket_validation.test.ts +0 -143
  176. package/test/src/bucket_validation.ts +0 -60
  177. package/test/src/compacting.test.ts +0 -295
  178. package/test/src/data_storage.test.ts +0 -1569
  179. package/test/src/stream_utils.ts +0 -42
  180. package/test/src/sync.test.ts +0 -511
  181. package/test/src/util.ts +0 -150
@@ -1,110 +0,0 @@
1
- import * as fs from 'fs/promises';
2
- import * as path from 'path';
3
- import { fileURLToPath } from 'url';
4
- import { logger } from '@powersync/lib-services-framework';
5
- import * as db from '../db/db-index.js';
6
- import * as locks from '../locks/locks-index.js';
7
- import * as util from '../util/util-index.js';
8
- import { Direction } from './definitions.js';
9
- import { execute, writeLogsToStore } from './executor.js';
10
- import { createMongoMigrationStore } from './store/migration-store.js';
11
- const DEFAULT_MONGO_LOCK_COLLECTION = 'locks';
12
- const MONGO_LOCK_PROCESS = 'migrations';
13
- const __filename = fileURLToPath(import.meta.url);
14
- const __dirname = path.dirname(__filename);
15
- const MIGRATIONS_DIR = path.join(__dirname, '/db/migrations');
16
- /**
17
- * Loads migrations and injects a custom context for loading the specified
18
- * runner configuration.
19
- */
20
- const loadMigrations = async (dir, runnerConfig) => {
21
- const files = await fs.readdir(dir);
22
- const migrations = files.filter((file) => {
23
- return path.extname(file) === '.js';
24
- });
25
- const context = {
26
- runner_config: runnerConfig
27
- };
28
- return await Promise.all(migrations.map(async (migration) => {
29
- const module = await import(path.resolve(dir, migration));
30
- return {
31
- name: path.basename(migration).replace(path.extname(migration), ''),
32
- up: () => module.up(context),
33
- down: () => module.down(context)
34
- };
35
- }));
36
- };
37
- /**
38
- * Runs migration scripts exclusively using Mongo locks
39
- */
40
- export const migrate = async (options) => {
41
- const { direction, runner_config } = options;
42
- const config = await util.loadConfig(runner_config);
43
- const { storage } = config;
44
- /**
45
- * Try and get Mongo from config file.
46
- * But this might not be available in Journey Micro as we use the standard Mongo.
47
- */
48
- const client = db.mongo.createMongoClient(storage);
49
- logger.info('Connecting to MongoDB');
50
- await client.connect();
51
- const clientDB = client.db(storage.database);
52
- const collection = clientDB.collection(DEFAULT_MONGO_LOCK_COLLECTION);
53
- const manager = locks.createMongoLockManager(collection, {
54
- name: MONGO_LOCK_PROCESS
55
- });
56
- // Only one process should execute this at a time.
57
- logger.info('Acquiring lock');
58
- const lockId = await manager.acquire();
59
- if (!lockId) {
60
- throw new Error('Could not acquire lock_id');
61
- }
62
- let isReleased = false;
63
- const releaseLock = async () => {
64
- if (isReleased) {
65
- return;
66
- }
67
- await manager.release(lockId);
68
- isReleased = true;
69
- };
70
- // For the case where the migration is terminated
71
- process.addListener('beforeExit', releaseLock);
72
- try {
73
- logger.info('Loading migrations');
74
- const migrations = await loadMigrations(MIGRATIONS_DIR, runner_config);
75
- // Use the provided config to connect to Mongo
76
- const store = createMongoMigrationStore(clientDB);
77
- const state = await store.load();
78
- logger.info('Running migrations');
79
- const logStream = execute({
80
- direction: direction,
81
- migrations,
82
- state
83
- });
84
- await writeLogsToStore({
85
- log_stream: logStream,
86
- store,
87
- state
88
- });
89
- }
90
- finally {
91
- logger.info('Releasing lock');
92
- await releaseLock();
93
- logger.info('Closing database');
94
- await client.close(true);
95
- process.removeListener('beforeExit', releaseLock);
96
- logger.info('Done with migrations');
97
- }
98
- };
99
- /**
100
- * Ensures automatic migrations are executed
101
- */
102
- export const ensureAutomaticMigrations = async (params) => {
103
- if (!params.config.migrations?.disable_auto_migration) {
104
- await migrate({
105
- direction: Direction.Up,
106
- runner_config: params.runner_config
107
- });
108
- }
109
- };
110
- //# sourceMappingURL=migrations.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/migrations/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;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EF;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,KAAK,EAAE,MAAgC,EAAE,EAAE;IAClF,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,sBAAsB,EAAE,CAAC;QACtD,MAAM,OAAO,CAAC;YACZ,SAAS,EAAE,SAAS,CAAC,EAAE;YACvB,aAAa,EAAE,MAAM,CAAC,aAAa;SACpC,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC"}
@@ -1,11 +0,0 @@
1
- import { Db } from 'mongodb';
2
- import * as defs from '../definitions.js';
3
- export type MigrationStore = {
4
- load: () => Promise<defs.MigrationState | undefined>;
5
- save: (state: defs.MigrationState) => Promise<void>;
6
- };
7
- /**
8
- * A custom store for node-migrate which is used to save and load migrations that have
9
- * been operated on to mongo.
10
- */
11
- export declare const createMongoMigrationStore: (db: Db) => MigrationStore;
@@ -1,46 +0,0 @@
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
- save: async (state) => {
37
- await collection.replaceOne({}, {
38
- last_run: state.last_run,
39
- log: state.log
40
- }, {
41
- upsert: true
42
- });
43
- }
44
- };
45
- };
46
- //# sourceMappingURL=migration-store.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"migration-store.js","sourceRoot":"","sources":["../../../src/migrations/store/migration-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAQ7B;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,EAAM,EAAkB,EAAE;IAClE,MAAM,UAAU,GAAG,EAAE,CAAC,UAAU,CAAsB,YAAY,CAAC,CAAC;IAEpE,OAAO;QACL,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,OAAO,EAAE,CAAC;YAC/C,IAAI,CAAC,WAAW,EAAE,CAAC;gBACjB,OAAO;YACT,CAAC;YAED,MAAM,EAAE,GAAG,EAAE,GAAG,KAAK,EAAE,GAAG,WAAW,CAAC;YAEtC;;;eAGG;YACH,IAAI,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;YAC9B,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;gBACvB,QAAQ,GAAI,KAAa,CAAC,OAAO,CAAC;YACpC,CAAC;YAED;;;eAGG;YACH,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACzC,IAAI,SAAS,EAAE,CAAC;gBACd,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YAC7C,CAAC;YAED,OAAO;gBACL,QAAQ;gBACR,GAAG,EAAE,KAAK,CAAC,GAAG,IAAI,EAAE;aACrB,CAAC;QACJ,CAAC;QAED,IAAI,EAAE,KAAK,EAAE,KAA0B,EAAE,EAAE;YACzC,MAAM,UAAU,CAAC,UAAU,CACzB,EAAE,EACF;gBACE,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,GAAG,EAAE,KAAK,CAAC,GAAG;aACf,EACD;gBACE,MAAM,EAAE,IAAI;aACb,CACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -1,48 +0,0 @@
1
- import { DisposableObserver } from '@powersync/lib-services-framework';
2
- import { ActiveCheckpoint, BucketStorageFactory, BucketStorageFactoryListener, ParseSyncRulesOptions, PersistedSyncRules, PersistedSyncRulesContent, StorageMetrics, UpdateSyncRulesOptions, WriteCheckpoint } from './BucketStorage.js';
3
- import { PowerSyncMongo } from './mongo/db.js';
4
- import { MongoPersistedSyncRulesContent } from './mongo/MongoPersistedSyncRulesContent.js';
5
- import { MongoSyncBucketStorage } from './mongo/MongoSyncBucketStorage.js';
6
- export declare class MongoBucketStorage extends DisposableObserver<BucketStorageFactoryListener> implements BucketStorageFactory {
7
- private readonly client;
8
- private readonly session;
9
- readonly slot_name_prefix: string;
10
- private readonly storageCache;
11
- readonly db: PowerSyncMongo;
12
- constructor(db: PowerSyncMongo, options: {
13
- slot_name_prefix: string;
14
- });
15
- getInstance(options: PersistedSyncRulesContent): MongoSyncBucketStorage;
16
- configureSyncRules(sync_rules: string, options?: {
17
- lock?: boolean;
18
- }): Promise<{
19
- updated: boolean;
20
- persisted_sync_rules?: undefined;
21
- lock?: undefined;
22
- } | {
23
- updated: boolean;
24
- persisted_sync_rules: MongoPersistedSyncRulesContent;
25
- lock: import("./storage-index.js").MongoSyncRulesLock | undefined;
26
- }>;
27
- slotRemoved(slot_name: string): Promise<void>;
28
- updateSyncRules(options: UpdateSyncRulesOptions): Promise<MongoPersistedSyncRulesContent>;
29
- getActiveSyncRulesContent(): Promise<MongoPersistedSyncRulesContent | null>;
30
- getActiveSyncRules(options: ParseSyncRulesOptions): Promise<PersistedSyncRules | null>;
31
- getNextSyncRulesContent(): Promise<MongoPersistedSyncRulesContent | null>;
32
- getNextSyncRules(options: ParseSyncRulesOptions): Promise<PersistedSyncRules | null>;
33
- getReplicatingSyncRules(): Promise<PersistedSyncRulesContent[]>;
34
- getStoppedSyncRules(): Promise<PersistedSyncRulesContent[]>;
35
- getActiveCheckpoint(): Promise<ActiveCheckpoint>;
36
- getStorageMetrics(): Promise<StorageMetrics>;
37
- getPowerSyncInstanceId(): Promise<string>;
38
- private makeActiveCheckpoint;
39
- /**
40
- * Instance-wide watch on the latest available checkpoint (op_id + lsn).
41
- */
42
- private watchActiveCheckpoint;
43
- private readonly sharedIter;
44
- /**
45
- * User-specific watch on the latest checkpoint and/or write checkpoint.
46
- */
47
- watchWriteCheckpoint(user_id: string, signal: AbortSignal): AsyncIterable<WriteCheckpoint>;
48
- }
@@ -1,427 +0,0 @@
1
- import { SqlSyncRules } from '@powersync/service-sync-rules';
2
- import { wrapWithAbort } from 'ix/asynciterable/operators/withabort.js';
3
- import { LRUCache } from 'lru-cache/min';
4
- import * as mongo from 'mongodb';
5
- import * as timers from 'timers/promises';
6
- import * as locks from '../locks/locks-index.js';
7
- import * as sync from '../sync/sync-index.js';
8
- import * as util from '../util/util-index.js';
9
- import { DisposableObserver, logger } from '@powersync/lib-services-framework';
10
- import { v4 as uuid } from 'uuid';
11
- import { SyncRuleState } from './mongo/models.js';
12
- import { MongoPersistedSyncRulesContent } from './mongo/MongoPersistedSyncRulesContent.js';
13
- import { MongoSyncBucketStorage } from './mongo/MongoSyncBucketStorage.js';
14
- import { generateSlotName } from './mongo/util.js';
15
- export class MongoBucketStorage extends DisposableObserver {
16
- constructor(db, options) {
17
- super();
18
- this.storageCache = new LRUCache({
19
- max: 3,
20
- fetchMethod: async (id) => {
21
- const doc2 = await this.db.sync_rules.findOne({
22
- _id: id
23
- }, { limit: 1 });
24
- if (doc2 == null) {
25
- // Deleted in the meantime?
26
- return undefined;
27
- }
28
- const rules = new MongoPersistedSyncRulesContent(this.db, doc2);
29
- return this.getInstance(rules);
30
- },
31
- dispose: (storage) => {
32
- storage[Symbol.dispose]();
33
- }
34
- });
35
- // Nothing is done here until a subscriber starts to iterate
36
- this.sharedIter = new sync.BroadcastIterable((signal) => {
37
- return this.watchActiveCheckpoint(signal);
38
- });
39
- this.client = db.client;
40
- this.db = db;
41
- this.session = this.client.startSession();
42
- this.slot_name_prefix = options.slot_name_prefix;
43
- }
44
- getInstance(options) {
45
- let { id, slot_name } = options;
46
- if (typeof id == 'bigint') {
47
- id = Number(id);
48
- }
49
- const storage = new MongoSyncBucketStorage(this, id, options, slot_name);
50
- this.iterateListeners((cb) => cb.syncStorageCreated?.(storage));
51
- storage.registerListener({
52
- batchStarted: (batch) => {
53
- // This nested listener will be automatically disposed when the storage is disposed
54
- batch.registerManagedListener(storage, {
55
- replicationEvent: (payload) => this.iterateListeners((cb) => cb.replicationEvent?.(payload))
56
- });
57
- }
58
- });
59
- return storage;
60
- }
61
- async configureSyncRules(sync_rules, options) {
62
- const next = await this.getNextSyncRulesContent();
63
- const active = await this.getActiveSyncRulesContent();
64
- if (next?.sync_rules_content == sync_rules) {
65
- logger.info('Sync rules from configuration unchanged');
66
- return { updated: false };
67
- }
68
- else if (next == null && active?.sync_rules_content == sync_rules) {
69
- logger.info('Sync rules from configuration unchanged');
70
- return { updated: false };
71
- }
72
- else {
73
- logger.info('Sync rules updated from configuration');
74
- const persisted_sync_rules = await this.updateSyncRules({
75
- content: sync_rules,
76
- lock: options?.lock
77
- });
78
- return { updated: true, persisted_sync_rules, lock: persisted_sync_rules.current_lock ?? undefined };
79
- }
80
- }
81
- async slotRemoved(slot_name) {
82
- const next = await this.getNextSyncRulesContent();
83
- const active = await this.getActiveSyncRulesContent();
84
- // In both the below cases, we create a new sync rules instance.
85
- // The current one will continue erroring until the next one has finished processing.
86
- // TODO: Update
87
- if (next != null && next.slot_name == slot_name) {
88
- // We need to redo the "next" sync rules
89
- await this.updateSyncRules({
90
- content: next.sync_rules_content
91
- });
92
- // Pro-actively stop replicating
93
- await this.db.sync_rules.updateOne({
94
- _id: next.id,
95
- state: SyncRuleState.PROCESSING
96
- }, {
97
- $set: {
98
- state: SyncRuleState.STOP
99
- }
100
- });
101
- }
102
- else if (next == null && active?.slot_name == slot_name) {
103
- // Slot removed for "active" sync rules, while there is no "next" one.
104
- await this.updateSyncRules({
105
- content: active.sync_rules_content
106
- });
107
- // Pro-actively stop replicating
108
- await this.db.sync_rules.updateOne({
109
- _id: active.id,
110
- state: SyncRuleState.ACTIVE
111
- }, {
112
- $set: {
113
- state: SyncRuleState.STOP
114
- }
115
- });
116
- }
117
- }
118
- async updateSyncRules(options) {
119
- // Parse and validate before applying any changes
120
- const parsed = SqlSyncRules.fromYaml(options.content, {
121
- // No schema-based validation at this point
122
- schema: undefined,
123
- defaultSchema: 'not_applicable', // Not needed for validation
124
- throwOnError: true
125
- });
126
- let rules = undefined;
127
- await this.session.withTransaction(async () => {
128
- // Only have a single set of sync rules with PROCESSING.
129
- await this.db.sync_rules.updateMany({
130
- state: SyncRuleState.PROCESSING
131
- }, { $set: { state: SyncRuleState.STOP } });
132
- const id_doc = await this.db.op_id_sequence.findOneAndUpdate({
133
- _id: 'sync_rules'
134
- }, {
135
- $inc: {
136
- op_id: 1n
137
- }
138
- }, {
139
- upsert: true,
140
- returnDocument: 'after'
141
- });
142
- const id = Number(id_doc.op_id);
143
- const slot_name = generateSlotName(this.slot_name_prefix, id);
144
- const doc = {
145
- _id: id,
146
- content: options.content,
147
- last_checkpoint: null,
148
- last_checkpoint_lsn: null,
149
- no_checkpoint_before: null,
150
- keepalive_op: null,
151
- snapshot_done: false,
152
- state: SyncRuleState.PROCESSING,
153
- slot_name: slot_name,
154
- last_checkpoint_ts: null,
155
- last_fatal_error: null,
156
- last_keepalive_ts: null
157
- };
158
- await this.db.sync_rules.insertOne(doc);
159
- rules = new MongoPersistedSyncRulesContent(this.db, doc);
160
- if (options.lock) {
161
- const lock = await rules.lock();
162
- }
163
- });
164
- return rules;
165
- }
166
- async getActiveSyncRulesContent() {
167
- const doc = await this.db.sync_rules.findOne({
168
- state: SyncRuleState.ACTIVE
169
- }, { sort: { _id: -1 }, limit: 1 });
170
- if (doc == null) {
171
- return null;
172
- }
173
- return new MongoPersistedSyncRulesContent(this.db, doc);
174
- }
175
- async getActiveSyncRules(options) {
176
- const content = await this.getActiveSyncRulesContent();
177
- return content?.parsed(options) ?? null;
178
- }
179
- async getNextSyncRulesContent() {
180
- const doc = await this.db.sync_rules.findOne({
181
- state: SyncRuleState.PROCESSING
182
- }, { sort: { _id: -1 }, limit: 1 });
183
- if (doc == null) {
184
- return null;
185
- }
186
- return new MongoPersistedSyncRulesContent(this.db, doc);
187
- }
188
- async getNextSyncRules(options) {
189
- const content = await this.getNextSyncRulesContent();
190
- return content?.parsed(options) ?? null;
191
- }
192
- async getReplicatingSyncRules() {
193
- const docs = await this.db.sync_rules
194
- .find({
195
- $or: [{ state: SyncRuleState.ACTIVE }, { state: SyncRuleState.PROCESSING }]
196
- })
197
- .toArray();
198
- return docs.map((doc) => {
199
- return new MongoPersistedSyncRulesContent(this.db, doc);
200
- });
201
- }
202
- async getStoppedSyncRules() {
203
- const docs = await this.db.sync_rules
204
- .find({
205
- state: SyncRuleState.STOP
206
- })
207
- .toArray();
208
- return docs.map((doc) => {
209
- return new MongoPersistedSyncRulesContent(this.db, doc);
210
- });
211
- }
212
- async getActiveCheckpoint() {
213
- const doc = await this.db.sync_rules.findOne({
214
- state: SyncRuleState.ACTIVE
215
- }, {
216
- sort: { _id: -1 },
217
- limit: 1,
218
- projection: { _id: 1, last_checkpoint: 1, last_checkpoint_lsn: 1 }
219
- });
220
- return this.makeActiveCheckpoint(doc);
221
- }
222
- async getStorageMetrics() {
223
- const ignoreNotExiting = (e) => {
224
- if (e instanceof mongo.MongoServerError && e.codeName == 'NamespaceNotFound') {
225
- // Collection doesn't exist - return 0
226
- return [{ storageStats: { size: 0 } }];
227
- }
228
- else {
229
- return Promise.reject(e);
230
- }
231
- };
232
- const active_sync_rules = await this.getActiveSyncRules({ defaultSchema: 'public' });
233
- if (active_sync_rules == null) {
234
- return {
235
- operations_size_bytes: 0,
236
- parameters_size_bytes: 0,
237
- replication_size_bytes: 0
238
- };
239
- }
240
- const operations_aggregate = await this.db.bucket_data
241
- .aggregate([
242
- {
243
- $collStats: {
244
- storageStats: {}
245
- }
246
- }
247
- ])
248
- .toArray()
249
- .catch(ignoreNotExiting);
250
- const parameters_aggregate = await this.db.bucket_parameters
251
- .aggregate([
252
- {
253
- $collStats: {
254
- storageStats: {}
255
- }
256
- }
257
- ])
258
- .toArray()
259
- .catch(ignoreNotExiting);
260
- const replication_aggregate = await this.db.current_data
261
- .aggregate([
262
- {
263
- $collStats: {
264
- storageStats: {}
265
- }
266
- }
267
- ])
268
- .toArray()
269
- .catch(ignoreNotExiting);
270
- return {
271
- operations_size_bytes: Number(operations_aggregate[0].storageStats.size),
272
- parameters_size_bytes: Number(parameters_aggregate[0].storageStats.size),
273
- replication_size_bytes: Number(replication_aggregate[0].storageStats.size)
274
- };
275
- }
276
- async getPowerSyncInstanceId() {
277
- let instance = await this.db.instance.findOne({
278
- _id: { $exists: true }
279
- });
280
- if (!instance) {
281
- const manager = locks.createMongoLockManager(this.db.locks, {
282
- name: `instance-id-insertion-lock`
283
- });
284
- await manager.lock(async () => {
285
- await this.db.instance.insertOne({
286
- _id: uuid()
287
- });
288
- });
289
- instance = await this.db.instance.findOne({
290
- _id: { $exists: true }
291
- });
292
- }
293
- return instance._id;
294
- }
295
- makeActiveCheckpoint(doc) {
296
- return {
297
- checkpoint: util.timestampToOpId(doc?.last_checkpoint ?? 0n),
298
- lsn: doc?.last_checkpoint_lsn ?? null,
299
- hasSyncRules() {
300
- return doc != null;
301
- },
302
- getBucketStorage: async () => {
303
- if (doc == null) {
304
- return null;
305
- }
306
- return (await this.storageCache.fetch(doc._id)) ?? null;
307
- }
308
- };
309
- }
310
- /**
311
- * Instance-wide watch on the latest available checkpoint (op_id + lsn).
312
- */
313
- async *watchActiveCheckpoint(signal) {
314
- const pipeline = [
315
- {
316
- $match: {
317
- 'fullDocument.state': 'ACTIVE',
318
- operationType: { $in: ['insert', 'update'] }
319
- }
320
- },
321
- {
322
- $project: {
323
- operationType: 1,
324
- 'fullDocument._id': 1,
325
- 'fullDocument.last_checkpoint': 1,
326
- 'fullDocument.last_checkpoint_lsn': 1
327
- }
328
- }
329
- ];
330
- // Use this form instead of (doc: SyncRuleDocument | null = null),
331
- // otherwise we get weird "doc: never" issues.
332
- let doc = null;
333
- let clusterTime = null;
334
- await this.client.withSession(async (session) => {
335
- doc = await this.db.sync_rules.findOne({
336
- state: SyncRuleState.ACTIVE
337
- }, {
338
- session,
339
- sort: { _id: -1 },
340
- limit: 1,
341
- projection: {
342
- _id: 1,
343
- last_checkpoint: 1,
344
- last_checkpoint_lsn: 1
345
- }
346
- });
347
- const time = session.clusterTime?.clusterTime ?? null;
348
- clusterTime = time;
349
- });
350
- if (clusterTime == null) {
351
- throw new Error('Could not get clusterTime');
352
- }
353
- if (signal.aborted) {
354
- return;
355
- }
356
- if (doc) {
357
- yield this.makeActiveCheckpoint(doc);
358
- }
359
- const stream = this.db.sync_rules.watch(pipeline, {
360
- fullDocument: 'updateLookup',
361
- // Start at the cluster time where we got the initial doc, to make sure
362
- // we don't skip any updates.
363
- // This may result in the first operation being a duplicate, but we filter
364
- // it out anyway.
365
- startAtOperationTime: clusterTime
366
- });
367
- signal.addEventListener('abort', () => {
368
- stream.close();
369
- }, { once: true });
370
- let lastOp = null;
371
- for await (const update of stream.stream()) {
372
- if (signal.aborted) {
373
- break;
374
- }
375
- if (update.operationType != 'insert' && update.operationType != 'update') {
376
- continue;
377
- }
378
- const doc = update.fullDocument;
379
- if (doc == null) {
380
- continue;
381
- }
382
- const op = this.makeActiveCheckpoint(doc);
383
- // Check for LSN / checkpoint changes - ignore other metadata changes
384
- if (lastOp == null || op.lsn != lastOp.lsn || op.checkpoint != lastOp.checkpoint) {
385
- lastOp = op;
386
- yield op;
387
- }
388
- }
389
- }
390
- /**
391
- * User-specific watch on the latest checkpoint and/or write checkpoint.
392
- */
393
- async *watchWriteCheckpoint(user_id, signal) {
394
- let lastCheckpoint = null;
395
- let lastWriteCheckpoint = null;
396
- const iter = wrapWithAbort(this.sharedIter, signal);
397
- for await (const cp of iter) {
398
- const { checkpoint, lsn } = cp;
399
- // lsn changes are not important by itself.
400
- // What is important is:
401
- // 1. checkpoint (op_id) changes.
402
- // 2. write checkpoint changes for the specific user
403
- const bucketStorage = await cp.getBucketStorage();
404
- if (!bucketStorage) {
405
- continue;
406
- }
407
- const lsnFilters = lsn ? { 1: lsn } : {};
408
- const currentWriteCheckpoint = await bucketStorage.lastWriteCheckpoint({
409
- user_id,
410
- heads: {
411
- ...lsnFilters
412
- }
413
- });
414
- if (currentWriteCheckpoint == lastWriteCheckpoint && checkpoint == lastCheckpoint) {
415
- // No change - wait for next one
416
- // In some cases, many LSNs may be produced in a short time.
417
- // Add a delay to throttle the write checkpoint lookup a bit.
418
- await timers.setTimeout(20 + 10 * Math.random());
419
- continue;
420
- }
421
- lastWriteCheckpoint = currentWriteCheckpoint;
422
- lastCheckpoint = checkpoint;
423
- yield { base: cp, writeCheckpoint: currentWriteCheckpoint };
424
- }
425
- }
426
- }
427
- //# sourceMappingURL=MongoBucketStorage.js.map