MCPcopy
hub / github.com/mongodb/node-mongodb-native / setOption

Function setOption

src/connection_string.ts:583–629  ·  view source on GitHub ↗
(
  mongoOptions: any,
  key: string,
  descriptor: OptionDescriptor,
  values: unknown[]
)

Source from the content-addressed store, hash-verified

581}
582
583function setOption(
584 mongoOptions: any,
585 key: string,
586 descriptor: OptionDescriptor,
587 values: unknown[]
588) {
589 const { target, type, transform } = descriptor;
590 const name = target ?? key;
591
592 switch (type) {
593 case 'boolean':
594 mongoOptions[name] = getBoolean(name, values[0]);
595 break;
596 case 'int':
597 mongoOptions[name] = getIntFromOptions(name, values[0]);
598 break;
599 case 'uint':
600 mongoOptions[name] = getUIntFromOptions(name, values[0]);
601 break;
602 case 'string':
603 if (values[0] == null) {
604 break;
605 }
606 // The value should always be a string here, but since the array is typed as unknown
607 // there still needs to be an explicit cast.
608 // eslint-disable-next-line @typescript-eslint/no-base-to-string
609 mongoOptions[name] = String(values[0]);
610 break;
611 case 'record':
612 if (!isRecord(values[0])) {
613 throw new MongoParseError(`${name} must be an object`);
614 }
615 mongoOptions[name] = values[0];
616 break;
617 case 'any':
618 mongoOptions[name] = values[0];
619 break;
620 default: {
621 if (!transform) {
622 throw new MongoParseError('Descriptors missing a type must define a transform');
623 }
624 const transformValue = transform({ name, options: mongoOptions, values });
625 mongoOptions[name] = transformValue;
626 break;
627 }
628 }
629}
630
631interface OptionDescriptor {
632 target?: string;

Callers 1

parseOptionsFunction · 0.85

Calls 5

isRecordFunction · 0.90
getBooleanFunction · 0.85
getIntFromOptionsFunction · 0.85
getUIntFromOptionsFunction · 0.85
transformFunction · 0.70

Tested by

no test coverage detected