@powersync/service-core 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 (188) hide show
  1. package/CHANGELOG.md +65 -4
  2. package/dist/auth/KeySpec.d.ts +1 -0
  3. package/dist/auth/KeySpec.js +10 -8
  4. package/dist/auth/KeySpec.js.map +1 -1
  5. package/dist/auth/RemoteJWKSCollector.js +2 -2
  6. package/dist/auth/RemoteJWKSCollector.js.map +1 -1
  7. package/dist/entry/commands/compact-action.js +15 -15
  8. package/dist/entry/commands/compact-action.js.map +1 -1
  9. package/dist/entry/commands/migrate-action.js +15 -4
  10. package/dist/entry/commands/migrate-action.js.map +1 -1
  11. package/dist/index.d.ts +1 -3
  12. package/dist/index.js +1 -3
  13. package/dist/index.js.map +1 -1
  14. package/dist/migrations/PowerSyncMigrationManager.d.ts +17 -0
  15. package/dist/migrations/PowerSyncMigrationManager.js +22 -0
  16. package/dist/migrations/PowerSyncMigrationManager.js.map +1 -0
  17. package/dist/migrations/ensure-automatic-migrations.d.ts +4 -0
  18. package/dist/migrations/ensure-automatic-migrations.js +14 -0
  19. package/dist/migrations/ensure-automatic-migrations.js.map +1 -0
  20. package/dist/migrations/migrations-index.d.ts +2 -3
  21. package/dist/migrations/migrations-index.js +2 -3
  22. package/dist/migrations/migrations-index.js.map +1 -1
  23. package/dist/routes/RouterEngine.js +2 -1
  24. package/dist/routes/RouterEngine.js.map +1 -1
  25. package/dist/routes/configure-fastify.d.ts +28 -28
  26. package/dist/routes/endpoints/admin.d.ts +24 -24
  27. package/dist/storage/BucketStorage.d.ts +41 -1
  28. package/dist/storage/BucketStorage.js +26 -0
  29. package/dist/storage/BucketStorage.js.map +1 -1
  30. package/dist/storage/storage-index.d.ts +2 -14
  31. package/dist/storage/storage-index.js +2 -14
  32. package/dist/storage/storage-index.js.map +1 -1
  33. package/dist/sync/sync.js +12 -3
  34. package/dist/sync/sync.js.map +1 -1
  35. package/dist/system/ServiceContext.d.ts +3 -0
  36. package/dist/system/ServiceContext.js +11 -3
  37. package/dist/system/ServiceContext.js.map +1 -1
  38. package/dist/util/config/types.d.ts +2 -2
  39. package/dist/util/utils.d.ts +14 -1
  40. package/dist/util/utils.js +56 -0
  41. package/dist/util/utils.js.map +1 -1
  42. package/package.json +6 -7
  43. package/src/auth/KeySpec.ts +12 -9
  44. package/src/auth/RemoteJWKSCollector.ts +2 -2
  45. package/src/entry/commands/compact-action.ts +20 -15
  46. package/src/entry/commands/migrate-action.ts +17 -4
  47. package/src/index.ts +1 -4
  48. package/src/migrations/PowerSyncMigrationManager.ts +43 -0
  49. package/src/migrations/ensure-automatic-migrations.ts +15 -0
  50. package/src/migrations/migrations-index.ts +2 -3
  51. package/src/routes/RouterEngine.ts +2 -1
  52. package/src/storage/BucketStorage.ts +44 -1
  53. package/src/storage/storage-index.ts +3 -15
  54. package/src/sync/sync.ts +12 -3
  55. package/src/system/ServiceContext.ts +17 -4
  56. package/src/util/config/types.ts +2 -2
  57. package/src/util/utils.ts +59 -1
  58. package/test/src/auth.test.ts +54 -21
  59. package/test/src/env.ts +0 -1
  60. package/tsconfig.tsbuildinfo +1 -1
  61. package/dist/db/db-index.d.ts +0 -1
  62. package/dist/db/db-index.js +0 -2
  63. package/dist/db/db-index.js.map +0 -1
  64. package/dist/db/mongo.d.ts +0 -35
  65. package/dist/db/mongo.js +0 -73
  66. package/dist/db/mongo.js.map +0 -1
  67. package/dist/locks/LockManager.d.ts +0 -10
  68. package/dist/locks/LockManager.js +0 -7
  69. package/dist/locks/LockManager.js.map +0 -1
  70. package/dist/locks/MongoLocks.d.ts +0 -36
  71. package/dist/locks/MongoLocks.js +0 -81
  72. package/dist/locks/MongoLocks.js.map +0 -1
  73. package/dist/locks/locks-index.d.ts +0 -2
  74. package/dist/locks/locks-index.js +0 -3
  75. package/dist/locks/locks-index.js.map +0 -1
  76. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  77. package/dist/migrations/db/migrations/1684951997326-init.js +0 -33
  78. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  79. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  80. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  81. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  82. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  83. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -56
  84. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  85. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  86. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -29
  87. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  88. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  89. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -31
  90. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  91. package/dist/migrations/definitions.d.ts +0 -18
  92. package/dist/migrations/definitions.js +0 -6
  93. package/dist/migrations/definitions.js.map +0 -1
  94. package/dist/migrations/executor.d.ts +0 -16
  95. package/dist/migrations/executor.js +0 -64
  96. package/dist/migrations/executor.js.map +0 -1
  97. package/dist/migrations/migrations.d.ts +0 -18
  98. package/dist/migrations/migrations.js +0 -110
  99. package/dist/migrations/migrations.js.map +0 -1
  100. package/dist/migrations/store/migration-store.d.ts +0 -11
  101. package/dist/migrations/store/migration-store.js +0 -46
  102. package/dist/migrations/store/migration-store.js.map +0 -1
  103. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  104. package/dist/storage/MongoBucketStorage.js +0 -426
  105. package/dist/storage/MongoBucketStorage.js.map +0 -1
  106. package/dist/storage/mongo/MongoBucketBatch.d.ts +0 -67
  107. package/dist/storage/mongo/MongoBucketBatch.js +0 -643
  108. package/dist/storage/mongo/MongoBucketBatch.js.map +0 -1
  109. package/dist/storage/mongo/MongoCompactor.d.ts +0 -40
  110. package/dist/storage/mongo/MongoCompactor.js +0 -309
  111. package/dist/storage/mongo/MongoCompactor.js.map +0 -1
  112. package/dist/storage/mongo/MongoIdSequence.d.ts +0 -12
  113. package/dist/storage/mongo/MongoIdSequence.js +0 -21
  114. package/dist/storage/mongo/MongoIdSequence.js.map +0 -1
  115. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +0 -9
  116. package/dist/storage/mongo/MongoPersistedSyncRules.js +0 -9
  117. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +0 -1
  118. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +0 -20
  119. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +0 -26
  120. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +0 -1
  121. package/dist/storage/mongo/MongoStorageProvider.d.ts +0 -5
  122. package/dist/storage/mongo/MongoStorageProvider.js +0 -26
  123. package/dist/storage/mongo/MongoStorageProvider.js.map +0 -1
  124. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +0 -38
  125. package/dist/storage/mongo/MongoSyncBucketStorage.js +0 -531
  126. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +0 -1
  127. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +0 -16
  128. package/dist/storage/mongo/MongoSyncRulesLock.js +0 -65
  129. package/dist/storage/mongo/MongoSyncRulesLock.js.map +0 -1
  130. package/dist/storage/mongo/MongoWriteCheckpointAPI.d.ts +0 -20
  131. package/dist/storage/mongo/MongoWriteCheckpointAPI.js +0 -103
  132. package/dist/storage/mongo/MongoWriteCheckpointAPI.js.map +0 -1
  133. package/dist/storage/mongo/OperationBatch.d.ts +0 -35
  134. package/dist/storage/mongo/OperationBatch.js +0 -119
  135. package/dist/storage/mongo/OperationBatch.js.map +0 -1
  136. package/dist/storage/mongo/PersistedBatch.d.ts +0 -46
  137. package/dist/storage/mongo/PersistedBatch.js +0 -213
  138. package/dist/storage/mongo/PersistedBatch.js.map +0 -1
  139. package/dist/storage/mongo/config.d.ts +0 -19
  140. package/dist/storage/mongo/config.js +0 -26
  141. package/dist/storage/mongo/config.js.map +0 -1
  142. package/dist/storage/mongo/db.d.ts +0 -36
  143. package/dist/storage/mongo/db.js +0 -47
  144. package/dist/storage/mongo/db.js.map +0 -1
  145. package/dist/storage/mongo/models.d.ts +0 -156
  146. package/dist/storage/mongo/models.js +0 -27
  147. package/dist/storage/mongo/models.js.map +0 -1
  148. package/dist/storage/mongo/util.d.ts +0 -40
  149. package/dist/storage/mongo/util.js +0 -151
  150. package/dist/storage/mongo/util.js.map +0 -1
  151. package/src/db/db-index.ts +0 -1
  152. package/src/db/mongo.ts +0 -81
  153. package/src/locks/LockManager.ts +0 -16
  154. package/src/locks/MongoLocks.ts +0 -142
  155. package/src/locks/locks-index.ts +0 -2
  156. package/src/migrations/db/migrations/1684951997326-init.ts +0 -38
  157. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  158. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -102
  159. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -34
  160. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -37
  161. package/src/migrations/definitions.ts +0 -21
  162. package/src/migrations/executor.ts +0 -87
  163. package/src/migrations/migrations.ts +0 -142
  164. package/src/migrations/store/migration-store.ts +0 -63
  165. package/src/storage/MongoBucketStorage.ts +0 -540
  166. package/src/storage/mongo/MongoBucketBatch.ts +0 -841
  167. package/src/storage/mongo/MongoCompactor.ts +0 -392
  168. package/src/storage/mongo/MongoIdSequence.ts +0 -24
  169. package/src/storage/mongo/MongoPersistedSyncRules.ts +0 -16
  170. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +0 -50
  171. package/src/storage/mongo/MongoStorageProvider.ts +0 -31
  172. package/src/storage/mongo/MongoSyncBucketStorage.ts +0 -636
  173. package/src/storage/mongo/MongoSyncRulesLock.ts +0 -85
  174. package/src/storage/mongo/MongoWriteCheckpointAPI.ts +0 -151
  175. package/src/storage/mongo/OperationBatch.ts +0 -131
  176. package/src/storage/mongo/PersistedBatch.ts +0 -272
  177. package/src/storage/mongo/config.ts +0 -40
  178. package/src/storage/mongo/db.ts +0 -88
  179. package/src/storage/mongo/models.ts +0 -179
  180. package/src/storage/mongo/util.ts +0 -158
  181. package/test/src/__snapshots__/sync.test.ts.snap +0 -332
  182. package/test/src/bucket_validation.test.ts +0 -142
  183. package/test/src/bucket_validation.ts +0 -116
  184. package/test/src/compacting.test.ts +0 -295
  185. package/test/src/data_storage.test.ts +0 -1499
  186. package/test/src/stream_utils.ts +0 -42
  187. package/test/src/sync.test.ts +0 -511
  188. package/test/src/util.ts +0 -148
@@ -1,47 +0,0 @@
1
- import * as db from '../../db/db-index.js';
2
- import { BSON_DESERIALIZE_OPTIONS } from './util.js';
3
- export function createPowerSyncMongo(config) {
4
- return new PowerSyncMongo(db.mongo.createMongoClient(config), { database: config.database });
5
- }
6
- export class PowerSyncMongo {
7
- constructor(client, options) {
8
- this.client = client;
9
- const db = client.db(options?.database, {
10
- ...BSON_DESERIALIZE_OPTIONS
11
- });
12
- this.db = db;
13
- this.current_data = db.collection('current_data');
14
- this.bucket_data = db.collection('bucket_data');
15
- this.bucket_parameters = db.collection('bucket_parameters');
16
- this.op_id_sequence = db.collection('op_id_sequence');
17
- this.sync_rules = db.collection('sync_rules');
18
- this.source_tables = db.collection('source_tables');
19
- this.custom_write_checkpoints = db.collection('custom_write_checkpoints');
20
- this.write_checkpoints = db.collection('write_checkpoints');
21
- this.instance = db.collection('instance');
22
- this.locks = this.db.collection('locks');
23
- }
24
- /**
25
- * Clear all collections.
26
- */
27
- async clear() {
28
- await this.current_data.deleteMany({});
29
- await this.bucket_data.deleteMany({});
30
- await this.bucket_parameters.deleteMany({});
31
- await this.op_id_sequence.deleteMany({});
32
- await this.sync_rules.deleteMany({});
33
- await this.source_tables.deleteMany({});
34
- await this.write_checkpoints.deleteMany({});
35
- await this.instance.deleteOne({});
36
- await this.locks.deleteMany({});
37
- }
38
- /**
39
- * Drop the entire database.
40
- *
41
- * Primarily for tests.
42
- */
43
- async drop() {
44
- await this.db.dropDatabase();
45
- }
46
- }
47
- //# sourceMappingURL=db.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"db.js","sourceRoot":"","sources":["../../../src/storage/mongo/db.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAa3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AASrD,MAAM,UAAU,oBAAoB,CAAC,MAA6C;IAChF,OAAO,IAAI,cAAc,CAAC,EAAE,CAAC,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC/F,CAAC;AAED,MAAM,OAAO,cAAc;IAezB,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,wBAAwB;SAC5B,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,CAAC,YAAY,CAAC,CAAC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'bson';
3
- /**
4
- * Replica id uniquely identifying a row on the source database.
5
- *
6
- * Can be any value serializable to BSON.
7
- *
8
- * If the value is an entire document, the data serialized to a v5 UUID may be a good choice here.
9
- */
10
- export type ReplicaId = bson.UUID | bson.Document | any;
11
- export interface SourceKey {
12
- /** group_id */
13
- g: number;
14
- /** source table id */
15
- t: bson.ObjectId;
16
- /** source key */
17
- k: ReplicaId;
18
- }
19
- export interface BucketDataKey {
20
- /** group_id */
21
- g: number;
22
- /** bucket name */
23
- b: string;
24
- /** op_id */
25
- o: bigint;
26
- }
27
- export interface CurrentDataDocument {
28
- _id: SourceKey;
29
- data: bson.Binary;
30
- buckets: CurrentBucket[];
31
- lookups: bson.Binary[];
32
- }
33
- export interface CurrentBucket {
34
- bucket: string;
35
- table: string;
36
- id: string;
37
- }
38
- export interface BucketParameterDocument {
39
- _id: bigint;
40
- key: SourceKey;
41
- lookup: bson.Binary;
42
- bucket_parameters: Record<string, SqliteJsonValue>[];
43
- }
44
- export interface BucketDataDocument {
45
- _id: BucketDataKey;
46
- op: OpType;
47
- source_table?: bson.ObjectId;
48
- source_key?: ReplicaId;
49
- table?: string;
50
- row_id?: string;
51
- checksum: number;
52
- data: string | null;
53
- target_op?: bigint | null;
54
- }
55
- export type OpType = 'PUT' | 'REMOVE' | 'MOVE' | 'CLEAR';
56
- export interface SourceTableDocument {
57
- _id: bson.ObjectId;
58
- group_id: number;
59
- connection_id: number;
60
- relation_id: number | string | undefined;
61
- schema_name: string;
62
- table_name: string;
63
- replica_id_columns: string[] | null;
64
- replica_id_columns2: {
65
- name: string;
66
- type_oid?: number;
67
- type?: string;
68
- }[] | undefined;
69
- snapshot_done: boolean | undefined;
70
- }
71
- export interface IdSequenceDocument {
72
- _id: string;
73
- op_id: bigint;
74
- }
75
- export declare enum SyncRuleState {
76
- /**
77
- * New sync rules - needs to be processed (initial replication).
78
- *
79
- * While multiple sets of sync rules _can_ be in PROCESSING,
80
- * it's generally pointless, so we only keep one in that state.
81
- */
82
- PROCESSING = "PROCESSING",
83
- /**
84
- * Sync rule processing is done, and can be used for sync.
85
- *
86
- * Only one set of sync rules should be in ACTIVE state.
87
- */
88
- ACTIVE = "ACTIVE",
89
- /**
90
- * This state is used when the sync rules has been replaced,
91
- * and replication is or should be stopped.
92
- */
93
- STOP = "STOP",
94
- /**
95
- * After sync rules have been stopped, the data needs to be
96
- * deleted. Once deleted, the state is TERMINATED.
97
- */
98
- TERMINATED = "TERMINATED"
99
- }
100
- export interface SyncRuleDocument {
101
- _id: number;
102
- state: SyncRuleState;
103
- /**
104
- * True if initial snapshot has been replicated.
105
- *
106
- * Can only be false if state == PROCESSING.
107
- */
108
- snapshot_done: boolean;
109
- /**
110
- * The last consistent checkpoint.
111
- *
112
- * There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
113
- */
114
- last_checkpoint: bigint | null;
115
- /**
116
- * The LSN associated with the last consistent checkpoint.
117
- */
118
- last_checkpoint_lsn: string | null;
119
- /**
120
- * If set, no new checkpoints may be created < this value.
121
- */
122
- no_checkpoint_before: string | null;
123
- slot_name: string | null;
124
- /**
125
- * Last time we persisted a checkpoint.
126
- *
127
- * This may be old if no data is incoming.
128
- */
129
- last_checkpoint_ts: Date | null;
130
- /**
131
- * Last time we persisted a checkpoint or keepalive.
132
- *
133
- * This should stay fairly current while replicating.
134
- */
135
- last_keepalive_ts: Date | null;
136
- /**
137
- * If an error is stopping replication, it will be stored here.
138
- */
139
- last_fatal_error: string | null;
140
- content: string;
141
- }
142
- export interface CustomWriteCheckpointDocument {
143
- _id: bson.ObjectId;
144
- user_id: string;
145
- checkpoint: bigint;
146
- sync_rules_id: number;
147
- }
148
- export interface WriteCheckpointDocument {
149
- _id: bson.ObjectId;
150
- user_id: string;
151
- lsns: Record<string, string>;
152
- client_id: bigint;
153
- }
154
- export interface InstanceDocument {
155
- _id: string;
156
- }
@@ -1,27 +0,0 @@
1
- export var SyncRuleState;
2
- (function (SyncRuleState) {
3
- /**
4
- * New sync rules - needs to be processed (initial replication).
5
- *
6
- * While multiple sets of sync rules _can_ be in PROCESSING,
7
- * it's generally pointless, so we only keep one in that state.
8
- */
9
- SyncRuleState["PROCESSING"] = "PROCESSING";
10
- /**
11
- * Sync rule processing is done, and can be used for sync.
12
- *
13
- * Only one set of sync rules should be in ACTIVE state.
14
- */
15
- SyncRuleState["ACTIVE"] = "ACTIVE";
16
- /**
17
- * This state is used when the sync rules has been replaced,
18
- * and replication is or should be stopped.
19
- */
20
- SyncRuleState["STOP"] = "STOP";
21
- /**
22
- * After sync rules have been stopped, the data needs to be
23
- * deleted. Once deleted, the state is TERMINATED.
24
- */
25
- SyncRuleState["TERMINATED"] = "TERMINATED";
26
- })(SyncRuleState || (SyncRuleState = {}));
27
- //# sourceMappingURL=models.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"models.js","sourceRoot":"","sources":["../../../src/storage/mongo/models.ts"],"names":[],"mappings":"AAiFA,MAAM,CAAN,IAAY,aAyBX;AAzBD,WAAY,aAAa;IACvB;;;;;OAKG;IACH,0CAAyB,CAAA;IAEzB;;;;OAIG;IACH,kCAAiB,CAAA;IACjB;;;OAGG;IACH,8BAAa,CAAA;IACb;;;OAGG;IACH,0CAAyB,CAAA;AAC3B,CAAC,EAzBW,aAAa,KAAb,aAAa,QAyBxB"}
@@ -1,40 +0,0 @@
1
- import { SqliteJsonValue } from '@powersync/service-sync-rules';
2
- import * as bson from 'bson';
3
- import * as mongo from 'mongodb';
4
- import { OplogEntry } from '../../util/protocol-types.js';
5
- import { BucketDataDocument, ReplicaId } from './models.js';
6
- /**
7
- * Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
8
- * @param lookup
9
- */
10
- export declare function serializeLookup(lookup: SqliteJsonValue[]): bson.Binary;
11
- export declare function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T>;
12
- export declare function generateSlotName(prefix: string, sync_rules_id: number): string;
13
- /**
14
- * Read a single batch of data from a cursor, then close it.
15
- *
16
- * We do our best to avoid MongoDB fetching any more data than this single batch.
17
- *
18
- * This is similar to using `singleBatch: true` in find options.
19
- * However, that makes `has_more` detection very difficult, since the cursor is always closed
20
- * after the first batch. Instead, we do a workaround to only fetch a single batch below.
21
- *
22
- * For this to be effective, set batchSize = limit in the find command.
23
- */
24
- export declare function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{
25
- data: T[];
26
- hasMore: boolean;
27
- }>;
28
- export declare const BSON_DESERIALIZE_OPTIONS: bson.DeserializeOptions;
29
- export declare function mapOpEntry(row: BucketDataDocument): OplogEntry;
30
- /**
31
- * Returns true if two ReplicaId values are the same (serializes to the same BSON value).
32
- */
33
- export declare function replicaIdEquals(a: ReplicaId, b: ReplicaId): boolean;
34
- export declare function replicaIdToSubkey(table: bson.ObjectId, id: ReplicaId): string;
35
- /**
36
- * True if this is a bson.UUID.
37
- *
38
- * Works even with multiple copies of the bson package.
39
- */
40
- export declare function isUUID(value: any): value is bson.UUID;
@@ -1,151 +0,0 @@
1
- import * as bson from 'bson';
2
- import * as crypto from 'crypto';
3
- import * as uuid from 'uuid';
4
- import { ID_NAMESPACE, timestampToOpId } from '../../util/utils.js';
5
- /**
6
- * Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
7
- * @param lookup
8
- */
9
- export function serializeLookup(lookup) {
10
- const normalized = lookup.map((value) => {
11
- if (typeof value == 'number' && Number.isInteger(value)) {
12
- return BigInt(value);
13
- }
14
- else {
15
- return value;
16
- }
17
- });
18
- return new bson.Binary(bson.serialize({ l: normalized }));
19
- }
20
- export function idPrefixFilter(prefix, rest) {
21
- let filter = {
22
- $gte: {
23
- ...prefix
24
- },
25
- $lt: {
26
- ...prefix
27
- }
28
- };
29
- for (let key of rest) {
30
- filter.$gte[key] = new bson.MinKey();
31
- filter.$lt[key] = new bson.MaxKey();
32
- }
33
- return filter;
34
- }
35
- export function generateSlotName(prefix, sync_rules_id) {
36
- const slot_suffix = crypto.randomBytes(2).toString('hex');
37
- return `${prefix}${sync_rules_id}_${slot_suffix}`;
38
- }
39
- /**
40
- * Read a single batch of data from a cursor, then close it.
41
- *
42
- * We do our best to avoid MongoDB fetching any more data than this single batch.
43
- *
44
- * This is similar to using `singleBatch: true` in find options.
45
- * However, that makes `has_more` detection very difficult, since the cursor is always closed
46
- * after the first batch. Instead, we do a workaround to only fetch a single batch below.
47
- *
48
- * For this to be effective, set batchSize = limit in the find command.
49
- */
50
- export async function readSingleBatch(cursor) {
51
- try {
52
- let data;
53
- let hasMore = true;
54
- // Let MongoDB load the first batch of data
55
- const hasAny = await cursor.hasNext();
56
- // Now it's in memory, and we can read it
57
- data = cursor.readBufferedDocuments();
58
- if (!hasAny || cursor.id?.isZero()) {
59
- // A zero id means the cursor is exhaused.
60
- // No results (hasAny == false) means even this batch doesn't have data.
61
- // This should similar results as `await cursor.hasNext()`, but without
62
- // actually fetching the next batch.
63
- //
64
- // Note that it is safe (but slightly inefficient) to return `hasMore: true`
65
- // without there being more data, as long as the next batch
66
- // will return `hasMore: false`.
67
- hasMore = false;
68
- }
69
- return { data, hasMore };
70
- }
71
- finally {
72
- // Match the from the cursor iterator logic here:
73
- // https://github.com/mongodb/node-mongodb-native/blob/e02534e7d1c627bf50b85ca39f5995dbf165ad44/src/cursor/abstract_cursor.ts#L327-L331
74
- if (!cursor.closed) {
75
- await cursor.close();
76
- }
77
- }
78
- }
79
- export const BSON_DESERIALIZE_OPTIONS = {
80
- // use bigint instead of Long
81
- useBigInt64: true
82
- };
83
- export function mapOpEntry(row) {
84
- if (row.op == 'PUT' || row.op == 'REMOVE') {
85
- return {
86
- op_id: timestampToOpId(row._id.o),
87
- op: row.op,
88
- object_type: row.table,
89
- object_id: row.row_id,
90
- checksum: Number(row.checksum),
91
- subkey: replicaIdToSubkey(row.source_table, row.source_key),
92
- data: row.data
93
- };
94
- }
95
- else {
96
- // MOVE, CLEAR
97
- return {
98
- op_id: timestampToOpId(row._id.o),
99
- op: row.op,
100
- checksum: Number(row.checksum)
101
- };
102
- }
103
- }
104
- /**
105
- * Returns true if two ReplicaId values are the same (serializes to the same BSON value).
106
- */
107
- export function replicaIdEquals(a, b) {
108
- if (a === b) {
109
- return true;
110
- }
111
- else if (typeof a == 'string' && typeof b == 'string') {
112
- return a == b;
113
- }
114
- else if (isUUID(a) && isUUID(b)) {
115
- return a.equals(b);
116
- }
117
- else if (a == null && b == null) {
118
- return true;
119
- }
120
- else if (a != null || b != null) {
121
- return false;
122
- }
123
- else {
124
- // There are many possible primitive values, this covers them all
125
- return bson.serialize({ id: a }).equals(bson.serialize({ id: b }));
126
- }
127
- }
128
- export function replicaIdToSubkey(table, id) {
129
- if (isUUID(id)) {
130
- // Special case for UUID for backwards-compatiblity
131
- return `${table.toHexString()}/${id.toHexString()}`;
132
- }
133
- else {
134
- // Hashed UUID from the table and id
135
- const repr = bson.serialize({ table, id });
136
- return uuid.v5(repr, ID_NAMESPACE);
137
- }
138
- }
139
- /**
140
- * True if this is a bson.UUID.
141
- *
142
- * Works even with multiple copies of the bson package.
143
- */
144
- export function isUUID(value) {
145
- if (value == null || typeof value != 'object') {
146
- return false;
147
- }
148
- const uuid = value;
149
- return uuid._bsontype == 'Binary' && uuid.sub_type == bson.Binary.SUBTYPE_UUID;
150
- }
151
- //# sourceMappingURL=util.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/storage/mongo/util.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAGpE;;;GAGG;AAEH,MAAM,UAAU,eAAe,CAAC,MAAyB;IACvD,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtC,IAAI,OAAO,KAAK,IAAI,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AAI,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACrC,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAU;IAC/B,IAAI,KAAK,IAAI,IAAI,IAAI,OAAO,KAAK,IAAI,QAAQ,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,IAAI,GAAG,KAAkB,CAAC;IAChC,OAAO,IAAI,CAAC,SAAS,IAAI,QAAQ,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;AACjF,CAAC"}
@@ -1 +0,0 @@
1
- export * as mongo from './mongo.js';
package/src/db/mongo.ts DELETED
@@ -1,81 +0,0 @@
1
- import * as mongo from 'mongodb';
2
- import * as timers from 'timers/promises';
3
-
4
- import { configFile } from '@powersync/service-types';
5
- import { normalizeMongoConfig } from '../storage/storage-index.js';
6
-
7
- /**
8
- * Time for new connection to timeout.
9
- */
10
- export const MONGO_CONNECT_TIMEOUT_MS = 10_000;
11
-
12
- /**
13
- * Time for individual requests to timeout the socket.
14
- */
15
- export const MONGO_SOCKET_TIMEOUT_MS = 60_000;
16
-
17
- /**
18
- * Time for individual requests to timeout the operation.
19
- *
20
- * This is time spent on the cursor, not total time.
21
- *
22
- * Must be less than MONGO_SOCKET_TIMEOUT_MS to ensure proper error handling.
23
- */
24
- export const MONGO_OPERATION_TIMEOUT_MS = 30_000;
25
-
26
- /**
27
- * Same as above, but specifically for clear operations.
28
- *
29
- * These are retried when reaching the timeout.
30
- */
31
- export const MONGO_CLEAR_OPERATION_TIMEOUT_MS = 5_000;
32
-
33
- export function createMongoClient(config: configFile.PowerSyncConfig['storage']) {
34
- const normalized = normalizeMongoConfig(config);
35
- return new mongo.MongoClient(normalized.uri, {
36
- auth: {
37
- username: normalized.username,
38
- password: normalized.password
39
- },
40
- // Time for connection to timeout
41
- connectTimeoutMS: MONGO_CONNECT_TIMEOUT_MS,
42
- // Time for individual requests to timeout
43
- socketTimeoutMS: MONGO_SOCKET_TIMEOUT_MS,
44
- // How long to wait for new primary selection
45
- serverSelectionTimeoutMS: 30_000,
46
-
47
- // Avoid too many connections:
48
- // 1. It can overwhelm the source database.
49
- // 2. Processing too many queries in parallel can cause the process to run out of memory.
50
- maxPoolSize: 8,
51
-
52
- maxConnecting: 3,
53
- maxIdleTimeMS: 60_000
54
- });
55
- }
56
-
57
- /**
58
- * Wait up to a minute for authentication errors to resolve.
59
- *
60
- * There can be a delay between an Atlas user being created, and that user being
61
- * available on the database cluster. This works around it.
62
- *
63
- * This is specifically relevant for migrations and teardown - other parts of the stack
64
- * can generate handle these failures and just retry or restart.
65
- */
66
- export async function waitForAuth(db: mongo.Db) {
67
- const start = Date.now();
68
- while (Date.now() - start < 60_000) {
69
- try {
70
- await db.command({ ping: 1 });
71
- // Success
72
- break;
73
- } catch (e) {
74
- if (e.codeName == 'AuthenticationFailed') {
75
- await timers.setTimeout(1_000);
76
- continue;
77
- }
78
- throw e;
79
- }
80
- }
81
- }
@@ -1,16 +0,0 @@
1
- import * as bson from 'bson';
2
-
3
- export class LockActiveError extends Error {
4
- constructor() {
5
- super('Lock is already active');
6
- this.name = this.constructor.name;
7
- }
8
- }
9
-
10
- export type LockManager = {
11
- acquire: () => Promise<bson.ObjectId | null>;
12
- refresh: (lock_id: bson.ObjectId) => Promise<void>;
13
- release: (lock_id: bson.ObjectId) => Promise<void>;
14
-
15
- lock: (handler: (refresh: () => Promise<void>) => Promise<void>) => Promise<void>;
16
- };
@@ -1,142 +0,0 @@
1
- import * as mongo from 'mongodb';
2
- import * as bson from 'bson';
3
- import { LockActiveError, LockManager } from './LockManager.js';
4
-
5
- /**
6
- * Lock Document Schema
7
- */
8
- export type Lock = {
9
- name: string;
10
- active_lock?: {
11
- lock_id: bson.ObjectId;
12
- ts: Date;
13
- };
14
- };
15
-
16
- export type Collection = mongo.Collection<Lock>;
17
-
18
- export type AcquireLockParams = {
19
- /**
20
- * Name of the process/user trying to acquire the lock.
21
- */
22
- name: string;
23
- /**
24
- * The TTL of the lock (ms). Default: 60000 ms (1 min)
25
- */
26
- timeout?: number;
27
- };
28
-
29
- const DEFAULT_LOCK_TIMEOUT = 60 * 1000; // 1 minute
30
-
31
- const acquireLock = async (collection: Collection, params: AcquireLockParams) => {
32
- const now = new Date();
33
- const lock_timeout = params.timeout ?? DEFAULT_LOCK_TIMEOUT;
34
- const lock_id = new bson.ObjectId();
35
-
36
- await collection.updateOne(
37
- {
38
- name: params.name
39
- },
40
- {
41
- $setOnInsert: {
42
- name: params.name
43
- }
44
- },
45
- {
46
- upsert: true
47
- }
48
- );
49
-
50
- const expired_ts = now.getTime() - lock_timeout;
51
-
52
- const res = await collection.updateOne(
53
- {
54
- $and: [
55
- { name: params.name },
56
- {
57
- $or: [{ active_lock: { $exists: false } }, { 'active_lock.ts': { $lte: new Date(expired_ts) } }]
58
- }
59
- ]
60
- },
61
- {
62
- $set: {
63
- active_lock: {
64
- lock_id: lock_id,
65
- ts: now
66
- }
67
- }
68
- }
69
- );
70
-
71
- if (res.modifiedCount === 0) {
72
- return null;
73
- }
74
-
75
- return lock_id;
76
- };
77
-
78
- const refreshLock = async (collection: Collection, lock_id: bson.ObjectId) => {
79
- const res = await collection.updateOne(
80
- {
81
- 'active_lock.lock_id': lock_id
82
- },
83
- {
84
- $set: {
85
- 'active_lock.ts': new Date()
86
- }
87
- }
88
- );
89
-
90
- if (res.modifiedCount === 0) {
91
- throw new Error('Lock not found, could not refresh');
92
- }
93
- };
94
-
95
- export const releaseLock = async (collection: Collection, lock_id: bson.ObjectId) => {
96
- const res = await collection.updateOne(
97
- {
98
- 'active_lock.lock_id': lock_id
99
- },
100
- {
101
- $unset: {
102
- active_lock: true
103
- }
104
- }
105
- );
106
-
107
- if (res.modifiedCount === 0) {
108
- throw new Error('Lock not found, could not release');
109
- }
110
- };
111
-
112
- export type CreateLockManagerParams = {
113
- /**
114
- * Name of the process/user trying to acquire the lock.
115
- */
116
- name: string;
117
- /**
118
- * The TTL for each lock (ms). Default: 60000 ms (1 min)
119
- */
120
- timeout?: number;
121
- };
122
-
123
- export const createMongoLockManager = (collection: Collection, params: CreateLockManagerParams): LockManager => {
124
- return {
125
- acquire: () => acquireLock(collection, params),
126
- refresh: (lock_id: bson.ObjectId) => refreshLock(collection, lock_id),
127
- release: (lock_id: bson.ObjectId) => releaseLock(collection, lock_id),
128
-
129
- lock: async (handler) => {
130
- const lock_id = await acquireLock(collection, params);
131
- if (!lock_id) {
132
- throw new LockActiveError();
133
- }
134
-
135
- try {
136
- await handler(() => refreshLock(collection, lock_id));
137
- } finally {
138
- await releaseLock(collection, lock_id);
139
- }
140
- }
141
- };
142
- };
@@ -1,2 +0,0 @@
1
- export * from './LockManager.js';
2
- export * from './MongoLocks.js';