Skip to content
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

Deprecate endpoint+database usage #431

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
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
63 changes: 35 additions & 28 deletions examples/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {getLogger, Logger, parseConnectionString} from 'ydb-sdk';
import {getLogger, Logger} from 'ydb-sdk';
import yargs from 'yargs';


export interface Runner {
(logger: Logger, endpoint: string, database: string, cliParams?: any): Promise<void>;
}
Expand All @@ -16,23 +15,25 @@ export interface Option {
}

export function getCliParams(options?: Option[]) {
const optionsUsage = options && options.length > 0 ? options.map((option) => ` --${option.name}`).join('') : '';
const optionsUsage =
options && options.length > 0 ? options.map((option) => ` --${option.name}`).join('') : '';

const argsBuilder = yargs
.usage(`Usage: $0 (--db <database> --endpoint <endpoint> or --connection-string <connection_string>)${optionsUsage}`);
const argsBuilder = yargs.usage(
`Usage: $0 (--db <database> --endpoint <endpoint> or --connection-string <connection_string>)${optionsUsage}`,
);

argsBuilder.options({
'db': { describe: 'YDB database name', type: 'string'},
'endpoint': {describe: 'YDB database endpoint', type: 'string'},
'connection-string': {describe: 'YDB connection string', type: 'string'},
db: {describe: 'YDB database name', type: 'string'},
endpoint: {describe: 'YDB database endpoint', type: 'string'},
'connection-string': {describe: 'YDB connection string', type: 'string'},
});

options?.forEach((option) => {
argsBuilder.option(option.name, {
describe: option.description,
type: 'string',
demandOption: true,
});
argsBuilder.option(option.name, {
describe: option.description,
type: 'string',
demandOption: true,
});
});

const args = argsBuilder.argv;
Expand All @@ -44,26 +45,30 @@ export function getCliParams(options?: Option[]) {
let endpoint;
let db;
if (connectionStringParam) {
const parsedConnectionString = parseConnectionString(connectionStringParam);
endpoint = parsedConnectionString.endpoint;
db = parsedConnectionString.database;
let cs = new URL(connectionStringParam);
endpoint = cs.origin;
db = cs.pathname || cs.searchParams.get('database') || '';
} else if (endpointParam && dbParam) {
endpoint = endpointParam;
db = dbParam;
} else {
throw new Error('Either --connection-string <connection_string> or --db <database> --endpoint <endpoint> arguments are required');
throw new Error(
'Either --connection-string <connection_string> or --db <database> --endpoint <endpoint> arguments are required',
);
}

const cliParams = {} as any;
options?.forEach((option) => {
cliParams[option.key] = args[option.key] as string;
});
return {endpoint, db, cliParams}
return {endpoint, db, cliParams};
}

export async function mainWithoutLogger(runner: RunnerWithoutLogger, options?: Option[]) {
const {db, endpoint, cliParams} = getCliParams(options)
console.log(`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`)
const {db, endpoint, cliParams} = getCliParams(options);
console.log(
`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`,
);
try {
await runner(endpoint, db, cliParams);
} catch (error) {
Expand All @@ -72,16 +77,18 @@ export async function mainWithoutLogger(runner: RunnerWithoutLogger, options?: O
}

export async function main(runner: Runner, options?: Option[]) {
const {db, endpoint, cliParams} = getCliParams(options)
const {db, endpoint, cliParams} = getCliParams(options);

const logger = getLogger();
logger.info(`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`);
const logger = getLogger();
logger.info(
`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`,
);

try {
await runner(logger, endpoint, db, cliParams);
} catch (error) {
logger.error(error as object);
}
try {
await runner(logger, endpoint, db, cliParams);
} catch (error) {
logger.error(error as object);
}
}

export const SYNTAX_V1 = '--!syntax_v1';
33 changes: 0 additions & 33 deletions src/__tests__/unit/parse-connection-string.test.ts

This file was deleted.

82 changes: 57 additions & 25 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import {ENDPOINT_DISCOVERY_PERIOD} from './constants';
import {TimeoutExpired} from './errors';
import {ISslCredentials, makeSslCredentials} from './utils/ssl-credentials';
import DiscoveryService from "./discovery/discovery-service";
import {TableClient} from "./table";
import {ClientOptions} from "./utils";
import {IAuthService} from "./credentials/i-auth-service";
import SchemeService from "./schema/scheme-client";
import SchemeClient from "./schema/scheme-client";
import {parseConnectionString} from "./utils/parse-connection-string";
import {QueryClient} from "./query";
import {Logger} from "./logger/simple-logger";
import {getDefaultLogger} from "./logger/get-default-logger";
import {TopicClient} from "./topic";
import {RetryStrategy} from "./retries/retryStrategy";
import {RetryParameters} from "./retries/retryParameters";
import {IClientSettings} from "./client/settings";
import {ISslCredentials, makeDefaultSslCredentials} from './utils/ssl-credentials';
import DiscoveryService from './discovery/discovery-service';
import {TableClient} from './table';
import {ClientOptions} from './utils';
import {IAuthService} from './credentials/i-auth-service';
import SchemeService from './schema/scheme-client';
import SchemeClient from './schema/scheme-client';
import {QueryClient} from './query';
import {Logger} from './logger/simple-logger';
import {getDefaultLogger} from './logger/get-default-logger';
import {TopicClient} from './topic';
import {RetryStrategy} from './retries/retryStrategy';
import {RetryParameters} from './retries/retryParameters';
import {IClientSettings} from './client/settings';

export interface IPoolSettings {
minLimit?: number;
Expand All @@ -23,11 +22,18 @@ export interface IPoolSettings {
}

export interface IDriverSettings {
/**
* @deprecated Use connectionString instead
*/
endpoint?: string;

/**
* @deprecated Use connectionString instead
*/
database?: string;
connectionString?: string;
authService: IAuthService;
sslCredentials?: ISslCredentials,
sslCredentials?: ISslCredentials;
poolSettings?: IPoolSettings;
clientOptions?: ClientOptions;
retrier?: RetryStrategy;
Expand All @@ -52,20 +58,46 @@ export default class Driver {
}

constructor(settings: IDriverSettings) {
let secure: boolean = false,
endpoint: string = '',
database: string = '';

this.logger = settings.logger || getDefaultLogger();
let endpoint: string, database: string;
if (settings.connectionString) {
({endpoint, database} = parseConnectionString(settings.connectionString));
} else if (!settings.endpoint) {
throw new Error('The "endpoint" is a required field in driver settings');
} else if (!settings.database) {
throw new Error('The "database" is a required field in driver settings');
} else {

if (settings.endpoint && settings.database) {
settings.logger?.warn(
'The "endpoint" and "database" fields are deprecated. Use "connectionString" instead',
);

endpoint = settings.endpoint;
database = settings.database;

secure = endpoint.startsWith('grpcs://') || endpoint.startsWith('https://');
}

if (settings.connectionString) {
let cs = new URL(settings.connectionString);
endpoint = cs.origin;
database = cs.pathname || cs.searchParams.get('database') || '';

if (!database) {
throw new Error(
'The "database" field is required in the connection string. It should be specified either in the path or as a `database` query parameter.',
);
}

secure = cs.protocol === 'grpcs:' || cs.protocol === 'https:';
}

if (!endpoint || !database) {
throw new Error(
'Either "endpoint" and "database" or "connectionString" must be specified',
);
}

const sslCredentials = makeSslCredentials(endpoint, this.logger, settings.sslCredentials);
const sslCredentials = secure
? settings.sslCredentials ?? makeDefaultSslCredentials()
: undefined;

const retrier = settings.retrier || new RetryStrategy(new RetryParameters(), this.logger);

Expand Down
96 changes: 46 additions & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ export {
getFallbackLogFunction,
} from './logger/deprecated';

export {
Logger,
LogFn,
} from './logger/simple-logger';
export {Logger, LogFn} from './logger/simple-logger';

export {default as Driver, IDriverSettings, IPoolSettings} from './driver';

Expand Down Expand Up @@ -40,53 +37,52 @@ export {Context} from './context';

export {YdbError, StatusCode} from './errors';

export {TableSessionPool} from "./table/table-session-pool";
export {TableSessionPool} from './table/table-session-pool';

export {AlterTableDescription} from "./table/table-session";
export {TableDescription} from "./table/table-session";
export {TableIndex} from "./table/table-session";
export {TableProfile} from "./table/table-session";
export {CachingPolicy} from "./table/table-session";
export {ExecutionPolicy} from "./table/table-session";
export {CompactionPolicy} from "./table/table-session";
export {ReplicationPolicy} from "./table/table-session";
export {PartitioningPolicy} from "./table/table-session";
export {ExplicitPartitions} from "./table/table-session";
export {StoragePolicy} from "./table/table-session";
export {ColumnFamilyPolicy} from "./table/table-session";
export {StorageSettings} from "./table/table-session";
export {Column} from "./table/table-session";
export {TableSession, TableSession as Session} from "./table/table-session";
export {ExecuteScanQuerySettings} from "./table/table-session";
export {ReadTableSettings} from "./table/table-session";
export {BulkUpsertSettings} from "./table/table-session";
export {ExecuteQuerySettings} from "./table/table-session";
export {PrepareQuerySettings} from "./table/table-session";
export {RollbackTransactionSettings} from "./table/table-session";
export {CommitTransactionSettings} from "./table/table-session";
export {BeginTransactionSettings} from "./table/table-session";
export {DescribeTableSettings} from "./table/table-session";
export {DropTableSettings} from "./table/table-session";
export {AlterTableSettings} from "./table/table-session";
export {CreateTableSettings} from "./table/table-session";
export {OperationParams} from "./table/table-session";
export {AUTO_TX} from "./table/table-session";
export {AlterTableDescription} from './table/table-session';
export {TableDescription} from './table/table-session';
export {TableIndex} from './table/table-session';
export {TableProfile} from './table/table-session';
export {CachingPolicy} from './table/table-session';
export {ExecutionPolicy} from './table/table-session';
export {CompactionPolicy} from './table/table-session';
export {ReplicationPolicy} from './table/table-session';
export {PartitioningPolicy} from './table/table-session';
export {ExplicitPartitions} from './table/table-session';
export {StoragePolicy} from './table/table-session';
export {ColumnFamilyPolicy} from './table/table-session';
export {StorageSettings} from './table/table-session';
export {Column} from './table/table-session';
export {TableSession, TableSession as Session} from './table/table-session';
export {ExecuteScanQuerySettings} from './table/table-session';
export {ReadTableSettings} from './table/table-session';
export {BulkUpsertSettings} from './table/table-session';
export {ExecuteQuerySettings} from './table/table-session';
export {PrepareQuerySettings} from './table/table-session';
export {RollbackTransactionSettings} from './table/table-session';
export {CommitTransactionSettings} from './table/table-session';
export {BeginTransactionSettings} from './table/table-session';
export {DescribeTableSettings} from './table/table-session';
export {DropTableSettings} from './table/table-session';
export {AlterTableSettings} from './table/table-session';
export {CreateTableSettings} from './table/table-session';
export {OperationParams} from './table/table-session';
export {AUTO_TX} from './table/table-session';

export {StaticCredentialsAuthService} from "./credentials/static-credentials-auth-service";
export {IamAuthService} from "./credentials/iam-auth-service";
export {MetadataAuthService} from "./credentials/metadata-auth-service";
export {TokenAuthService} from "./credentials/token-auth-service";
export {AnonymousAuthService} from "./credentials/anonymous-auth-service";
export {ITokenService} from "./credentials/i-token-service";
export {IAuthService} from "./credentials/i-auth-service";
export {ModifyPermissionsSettings} from "./schema/scheme-service";
export {DescribePathSettings} from "./schema/scheme-service";
export {ListDirectorySettings} from "./schema/scheme-service";
export {RemoveDirectorySettings} from "./schema/scheme-service";
export {MakeDirectorySettings} from "./schema/scheme-service";
export {StaticCredentialsAuthService} from './credentials/static-credentials-auth-service';
export {IamAuthService} from './credentials/iam-auth-service';
export {MetadataAuthService} from './credentials/metadata-auth-service';
export {TokenAuthService} from './credentials/token-auth-service';
export {AnonymousAuthService} from './credentials/anonymous-auth-service';
export {ITokenService} from './credentials/i-token-service';
export {IAuthService} from './credentials/i-auth-service';
export {ModifyPermissionsSettings} from './schema/scheme-service';
export {DescribePathSettings} from './schema/scheme-service';
export {ListDirectorySettings} from './schema/scheme-service';
export {RemoveDirectorySettings} from './schema/scheme-service';
export {MakeDirectorySettings} from './schema/scheme-service';

export {ParsedConnectionString, parseConnectionString} from "./utils/parse-connection-string";
export {QueryClient, ResultSet, RowType} from "./query";
export {QueryClient, ResultSet, RowType} from './query';

export {SimpleLogger} from "./logger/simple-logger";
export {getDefaultLogger} from "./logger/get-default-logger";
export {SimpleLogger} from './logger/simple-logger';
export {getDefaultLogger} from './logger/get-default-logger';
Loading
Loading