Skip to content

Allowing PS_MONGO_URI to be a 3-replica set URI #249

New issue

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

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

Already on GitHub? Sign in to your account

Open
whygee-dev opened this issue Apr 27, 2025 · 1 comment
Open

Allowing PS_MONGO_URI to be a 3-replica set URI #249

whygee-dev opened this issue Apr 27, 2025 · 1 comment

Comments

@whygee-dev
Copy link

Currently the logic behind normalizing mongo config does not handle properly a 3-replica set URI

export function normalizeMongoConfig(options: BaseMongoConfigDecoded) {
  let uri = urijs.parse(options.uri);

  const database = options.database ?? uri.path?.substring(1) ?? '';

  const userInfo = uri.userinfo?.split(':');

  const username = options.username ?? userInfo?.[0];
  const password = options.password ?? userInfo?.[1];

  if (database == '') {
    throw new ServiceError(ErrorCode.PSYNC_S1105, `MongoDB connection: database required`);
  }

  delete uri.userinfo;

  const lookupOptions: LookupOptions = {
    reject_ip_ranges: options.reject_ip_ranges ?? []
  };
  const lookup = makeHostnameLookupFunction(uri.host ?? '', lookupOptions);

  return {
    uri: urijs.serialize(uri),
    database,

    username,
    password,

    lookup
  };
}

For example,

mongodb://mongodb-0.mongodb.powersync.svc.cluster.local:27017,mongodb-1.mongodb.powersync.svc.cluster.local:27017,mongodb-2.mongodb.powersync.svc.cluster.local:27017/powersync_test?replicaSet=rs0 resolves to

{
  database: 'mongodb-1.mongodb.powersync.svc.cluster.local:27017,mongodb-2.mongodb.powersync.svc.cluster.local:27017/powersync_test',
}

I suggest using mongodb-connection-string-url

@whygee-dev
Copy link
Author

Opened a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant