-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMongoRouteAPIAdapter.ts
More file actions
427 lines (390 loc) · 14 KB
/
MongoRouteAPIAdapter.ts
File metadata and controls
427 lines (390 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import * as lib_mongo from '@powersync/lib-service-mongodb';
import { mongo } from '@powersync/lib-service-mongodb';
import { api, ParseSyncRulesOptions, ReplicationHeadCallback, SourceTable } from '@powersync/service-core';
import * as sync_rules from '@powersync/service-sync-rules';
import * as service_types from '@powersync/service-types';
import { ServiceAssertionError } from '@powersync/lib-services-framework';
import { MongoLSN } from '../common/MongoLSN.js';
import { MongoManager } from '../replication/MongoManager.js';
import { constructAfterRecord, STANDALONE_CHECKPOINT_ID } from '../replication/MongoRelation.js';
import { CHECKPOINTS_COLLECTION } from '../replication/replication-utils.js';
import * as types from '../types/types.js';
import { escapeRegExp } from '../utils.js';
export class MongoRouteAPIAdapter implements api.RouteAPI {
protected client: mongo.MongoClient;
public db: mongo.Db;
connectionTag: string;
defaultSchema: string;
private isCosmosDb: boolean | null = null;
constructor(protected config: types.ResolvedConnectionConfig) {
const manager = new MongoManager(config);
this.client = manager.client;
this.db = manager.db;
this.defaultSchema = manager.db.databaseName;
this.connectionTag = config.tag ?? sync_rules.DEFAULT_TAG;
}
getParseSyncRulesOptions(): ParseSyncRulesOptions {
return {
defaultSchema: this.defaultSchema
};
}
async shutdown(): Promise<void> {
await this.client.close();
}
async [Symbol.asyncDispose]() {
await this.shutdown();
}
async getSourceConfig(): Promise<service_types.configFile.ResolvedDataSourceConfig> {
return this.config;
}
async getConnectionStatus(): Promise<service_types.ConnectionStatusV2> {
const base = {
id: this.config.id,
uri: lib_mongo.baseUri(this.config)
};
try {
await this.client.connect();
await this.db.command({ hello: 1 });
} catch (e) {
return {
...base,
connected: false,
errors: [{ level: 'fatal', message: e.message }]
};
}
return {
...base,
connected: true,
errors: []
};
}
async executeQuery(query: string, params: any[]): Promise<service_types.internal_routes.ExecuteSqlResponse> {
return service_types.internal_routes.ExecuteSqlResponse.encode({
results: {
columns: [],
rows: []
},
success: false,
error: 'SQL querying is not supported for MongoDB'
});
}
async getDebugTablesInfo(
tablePatterns: sync_rules.TablePattern[],
sqlSyncRules: sync_rules.SyncConfig
): Promise<api.PatternResult[]> {
let result: api.PatternResult[] = [];
const validatePostImages = (schema: string, collection: mongo.CollectionInfo): service_types.ReplicationError[] => {
if (this.config.postImages == types.PostImagesOption.OFF) {
return [];
} else if (!collection.options?.changeStreamPreAndPostImages?.enabled) {
if (this.config.postImages == types.PostImagesOption.READ_ONLY) {
return [
{ level: 'fatal', message: `changeStreamPreAndPostImages not enabled on ${schema}.${collection.name}` }
];
} else {
return [
{
level: 'warning',
message: `changeStreamPreAndPostImages not enabled on ${schema}.${collection.name}, will be enabled automatically`
}
];
}
} else {
return [];
}
};
for (let tablePattern of tablePatterns) {
const schema = tablePattern.schema;
let patternResult: api.PatternResult = {
schema: schema,
pattern: tablePattern.tablePattern,
wildcard: tablePattern.isWildcard
};
result.push(patternResult);
let nameFilter: RegExp | string;
if (tablePattern.isWildcard) {
nameFilter = new RegExp('^' + escapeRegExp(tablePattern.tablePrefix));
} else {
nameFilter = tablePattern.name;
}
// Check if the collection exists
const collections = await this.client
.db(schema)
.listCollections(
{
name: nameFilter
},
{ nameOnly: false }
)
.toArray();
if (tablePattern.isWildcard) {
patternResult.tables = [];
for (let collection of collections) {
const sourceTable = new SourceTable({
id: '', // not used
connectionTag: this.connectionTag,
objectId: collection.name,
schema: schema,
name: collection.name,
replicaIdColumns: [],
snapshotComplete: true
});
let errors: service_types.ReplicationError[] = [];
if (collection.type == 'view') {
errors.push({ level: 'warning', message: `Collection ${schema}.${tablePattern.name} is a view` });
} else {
errors.push(...validatePostImages(schema, collection));
}
const syncData = sqlSyncRules.tableSyncsData(sourceTable);
const syncParameters = sqlSyncRules.tableSyncsParameters(sourceTable);
patternResult.tables.push({
schema,
name: collection.name,
replication_id: ['_id'],
data_queries: syncData,
parameter_queries: syncParameters,
errors: errors
});
}
} else {
const sourceTable = new SourceTable({
id: '', // not used
connectionTag: this.connectionTag,
objectId: tablePattern.name,
schema: schema,
name: tablePattern.name,
replicaIdColumns: [],
snapshotComplete: true
});
const syncData = sqlSyncRules.tableSyncsData(sourceTable);
const syncParameters = sqlSyncRules.tableSyncsParameters(sourceTable);
const collection = collections[0];
let errors: service_types.ReplicationError[] = [];
if (collections.length != 1) {
errors.push({ level: 'warning', message: `Collection ${schema}.${tablePattern.name} not found` });
} else if (collection.type == 'view') {
errors.push({ level: 'warning', message: `Collection ${schema}.${tablePattern.name} is a view` });
} else if (!collection.options?.changeStreamPreAndPostImages?.enabled) {
errors.push(...validatePostImages(schema, collection));
}
patternResult.table = {
schema,
name: tablePattern.name,
replication_id: ['_id'],
data_queries: syncData,
parameter_queries: syncParameters,
errors
};
}
}
return result;
}
async getReplicationLagBytes(options: api.ReplicationLagOptions): Promise<number | undefined> {
// There is no fast way to get replication lag in bytes in MongoDB.
// We can get replication lag in seconds, but need a different API for that.
return undefined;
}
private async detectCosmosDb(): Promise<boolean> {
if (this.isCosmosDb === null) {
const hello = await this.db.command({ hello: 1 });
this.isCosmosDb = hello.internal?.cosmos_versions != null || hello.internal?.documentdb_versions != null;
}
return this.isCosmosDb;
}
async createReplicationHead<T>(callback: ReplicationHeadCallback<T>): Promise<T> {
const session = this.client.startSession();
try {
if (await this.detectCosmosDb()) {
// Cosmos DB: write sentinel to trigger change stream advance
await this.db
.collection(CHECKPOINTS_COLLECTION)
.findOneAndUpdate(
{ _id: STANDALONE_CHECKPOINT_ID as any },
{ $inc: { i: 1 } },
{ upsert: true, returnDocument: 'after', session }
);
// HEAD is unknown — caller must poll storage to determine it
return await callback(null);
}
// Standard MongoDB: existing path
await this.db.command({ hello: 1 }, { session });
const head = session.clusterTime?.clusterTime;
if (head == null) {
throw new ServiceAssertionError(`clusterTime not available for write checkpoint`);
}
const r = await callback(new MongoLSN({ timestamp: head }).comparable);
// Trigger a change on the changestream.
await this.db.collection(CHECKPOINTS_COLLECTION).findOneAndUpdate(
{
_id: STANDALONE_CHECKPOINT_ID as any
},
{
$inc: { i: 1 }
},
{
upsert: true,
returnDocument: 'after',
session
}
);
const time = session.operationTime!;
if (time == null) {
throw new ServiceAssertionError(`operationTime not available for write checkpoint`);
} else if (time.lt(head)) {
throw new ServiceAssertionError(`operationTime must be > clusterTime`);
}
return r;
} finally {
await session.endSession();
}
}
async getConnectionSchema(): Promise<service_types.DatabaseSchema[]> {
const sampleSize = 50;
const databases = await this.db.admin().listDatabases({ nameOnly: true });
const filteredDatabases = databases.databases.filter((db) => {
return !['local', 'admin', 'config'].includes(db.name);
});
const databaseSchemas = await Promise.all(
filteredDatabases.map(async (db) => {
/**
* Filtering the list of database with `authorizedDatabases: true`
* does not produce the full list of databases under some circumstances.
* This catches any potential auth errors.
*/
let collections: mongo.CollectionInfo[];
try {
collections = await this.client.db(db.name).listCollections().toArray();
} catch (e) {
if (lib_mongo.isMongoServerError(e) && e.codeName == 'Unauthorized') {
// Ignore databases we're not authorized to query
return null;
}
throw e;
}
let tables: service_types.TableSchema[] = [];
for (let collection of collections) {
if ([CHECKPOINTS_COLLECTION].includes(collection.name)) {
continue;
}
if (collection.name.startsWith('system.')) {
// system.views, system.js, system.profile, system.buckets
// https://www.mongodb.com/docs/manual/reference/system-collections/
continue;
}
if (collection.type == 'view') {
continue;
}
try {
const sampleDocuments = await this.db
.collection(collection.name)
.aggregate([{ $sample: { size: sampleSize } }])
.toArray();
if (sampleDocuments.length > 0) {
const columns = this.getColumnsFromDocuments(sampleDocuments);
tables.push({
name: collection.name,
// Since documents are sampled in a random order, we need to sort
// to get a consistent order
columns: columns.sort((a, b) => a.name.localeCompare(b.name))
});
} else {
tables.push({
name: collection.name,
columns: []
});
}
} catch (e) {
if (lib_mongo.isMongoServerError(e) && e.codeName == 'Unauthorized') {
// Ignore collections we're not authorized to query
continue;
}
throw e;
}
}
return {
name: db.name,
tables: tables
} satisfies service_types.DatabaseSchema;
})
);
return databaseSchemas.filter((schema) => !!schema);
}
private getColumnsFromDocuments(documents: mongo.BSON.Document[]) {
let columns = new Map<string, { sqliteType: sync_rules.ExpressionType; bsonTypes: Set<string> }>();
for (const document of documents) {
const parsed = constructAfterRecord(document);
for (const key in parsed) {
const value = parsed[key];
const type = sync_rules.sqliteTypeOf(value);
const sqliteType = sync_rules.ExpressionType.fromTypeText(type);
let entry = columns.get(key);
if (entry == null) {
entry = { sqliteType, bsonTypes: new Set() };
columns.set(key, entry);
} else {
entry.sqliteType = entry.sqliteType.or(sqliteType);
}
const bsonType = this.getBsonType(document[key]);
if (bsonType != null) {
entry.bsonTypes.add(bsonType);
}
}
}
return [...columns.entries()].map(([key, value]) => {
const internal_type = value.bsonTypes.size == 0 ? '' : [...value.bsonTypes].join(' | ');
return {
name: key,
type: internal_type,
sqlite_type: value.sqliteType.typeFlags,
internal_type,
pg_type: internal_type
};
});
}
private getBsonType(data: any): string | null {
if (data == null) {
// null or undefined
return 'Null';
} else if (typeof data == 'string') {
return 'String';
} else if (typeof data == 'number') {
if (Number.isInteger(data)) {
return 'Integer';
} else {
return 'Double';
}
} else if (typeof data == 'bigint') {
return 'Long';
} else if (typeof data == 'boolean') {
return 'Boolean';
} else if (data instanceof mongo.ObjectId) {
return 'ObjectId';
} else if (data instanceof mongo.UUID) {
return 'UUID';
} else if (data instanceof Date) {
return 'Date';
} else if (data instanceof mongo.Timestamp) {
return 'Timestamp';
} else if (data instanceof mongo.Binary) {
return 'Binary';
} else if (data instanceof mongo.Long) {
return 'Long';
} else if (data instanceof RegExp) {
return 'RegExp';
} else if (data instanceof mongo.MinKey) {
return 'MinKey';
} else if (data instanceof mongo.MaxKey) {
return 'MaxKey';
} else if (data instanceof mongo.Decimal128) {
return 'Decimal';
} else if (Array.isArray(data)) {
return 'Array';
} else if (data instanceof Uint8Array) {
return 'Binary';
} else if (typeof data == 'object') {
return 'Object';
} else {
return null;
}
}
}