Skip to content

[MongoDB] List Databases #96

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions modules/module-mongodb/src/api/MongoRouteAPIAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as mongo from 'mongodb';

import * as sync_rules from '@powersync/service-sync-rules';
import * as service_types from '@powersync/service-types';
import * as types from '../types/types.js';
import { MongoManager } from '../replication/MongoManager.js';
import { constructAfterRecord, createCheckpoint, getMongoLsn } from '../replication/MongoRelation.js';
import { constructAfterRecord, createCheckpoint } from '../replication/MongoRelation.js';
import * as types from '../types/types.js';
import { escapeRegExp } from '../utils.js';

export class MongoRouteAPIAdapter implements api.RouteAPI {
Expand Down Expand Up @@ -178,13 +178,24 @@ export class MongoRouteAPIAdapter implements api.RouteAPI {
async getConnectionSchema(): Promise<service_types.DatabaseSchema[]> {
const sampleSize = 50;

const databases = await this.db.admin().listDatabases({ authorizedDatabases: true, nameOnly: true });
const databases = await this.db.admin().listDatabases({ nameOnly: true });
const filteredDatabases = databases.databases.filter((db) => {
return !['local', 'admin', 'config'].includes(db.name);
});
return await Promise.all(
const databaseSchemas = await Promise.all(
filteredDatabases.map(async (db) => {
const collections = await this.client.db(db.name).listCollections().toArray();
/**
* 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 (ex) {
return null;
}

const filtered = collections.filter((c) => {
return !['_powersync_checkpoints'].includes(c.name);
});
Expand Down Expand Up @@ -219,6 +230,7 @@ export class MongoRouteAPIAdapter implements api.RouteAPI {
} satisfies service_types.DatabaseSchema;
})
);
return databaseSchemas.filter((schema) => !!schema);
}

private getColumnsFromDocuments(documents: mongo.BSON.Document[]) {
Expand Down
Loading