Skip to content

Commit

Permalink
feat: query service WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Zorkaltsev committed Jan 30, 2024
1 parent 1501444 commit 64c9e03
Show file tree
Hide file tree
Showing 26 changed files with 615 additions and 99 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ All notable changes to this project will be documented in this file. See [standa
- decimal value is present as string instead of bigint
(it wasn't working for float values before)
- fix uuid and tz-date types conversion (it wasn't working before)
* signatures of most methods in TableSession are changed:
* signatures of most methods in QuerySession are changed:
- executeQuery
Before: `(query, params, txControl, operationParams?, settings?, collectStats?)`
After: `(query, params, txControl, settings?)`
Expand Down Expand Up @@ -336,7 +336,7 @@ All notable changes to this project will be documented in this file. See [standa

* drop support of old environment variables ([963819a](https://www.github.com/ydb-platform/ydb-nodejs-sdk/commit/963819af9209a45749f5118077f1da4bdb390fa6))
* reorganize signature of SchemeClient's methods ([734d57a](https://www.github.com/ydb-platform/ydb-nodejs-sdk/commit/734d57a2dd7c655cf727b96df415212504339cf8))
* reorganize signatures of TableSession's methods ([431f149](https://www.github.com/ydb-platform/ydb-nodejs-sdk/commit/431f1491bf880f3ba9541d9455d8dd2f2b7849e6))
* reorganize signatures of QuerySession's methods ([431f149](https://www.github.com/ydb-platform/ydb-nodejs-sdk/commit/431f1491bf880f3ba9541d9455d8dd2f2b7849e6))
* use identity names conversion in TypedData ([275598a](https://www.github.com/ydb-platform/ydb-nodejs-sdk/commit/275598aa444e1e977386a3dadd02bbc9ba01f38e))

### [2.9.2](https://www.github.com/ydb-platform/ydb-nodejs-sdk/compare/v2.9.1...v2.9.2) (2022-02-09)
Expand Down Expand Up @@ -431,7 +431,7 @@ All notable changes to this project will be documented in this file. See [standa
* and many other changes in protobufs.

### 1.10.0
* Add `alterTable` method to TableSession class
* Add `alterTable` method to QuerySession class
* Put compiled protobufs to a separate 'ydb-sdk' namespace

### 1.9.0
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions src/__tests__/e2e/query-service/exec-quуry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Driver from '../../../driver';
import {
createTable,
destroyDriver,
fillTableWithData,
initDriver,
Row,
TABLE
} from '../../../test-utils';
import {ReadTableSettings} from '../../../table';
import {TypedValues, TypedData} from '../../../types';
import {QuerySession} from "../../../query/query-session";

// async function execQuery(session: QuerySession): Promise<TypedData[]> {
// const rows: TypedData[] = [];
//
// await session.streamReadTable(TABLE, (result) => {
// if (result.resultSet) {
// rows.push(...Row.createNativeObjects(result.resultSet));
// }
// }, settings);
//
// return rows;
// }

describe('Query service', () => {
let driver: Driver;

beforeAll(async () => {
driver = await initDriver();
});

afterAll(async () => await destroyDriver(driver));

it('Test', async () => {
await driver.tableClient.withSession(async (session) => {
const expectedRows = [
new Row({id: 1, title: 'one'}),
new Row({id: 2, title: 'two'}),
];

await createTable(session);

// TODO: Create few tables

// TODO: Make quries with few datasets

await fillTableWithData(session, expectedRows);

const res = await driver.queryClient.exec({
query: 'SELECT * FROM ${TABLE}',
// Is callback a good name?
callback: (session: QuerySession) => {
// session.beginTransaction(),
// TODO: query -> array
// return;
},
});



// {
// const rows = await execQuery(session, new ReadTableSettings());
// expect(rows).toEqual(expectedRows);
// }
//
// {
// const rows = await readTable(session, new ReadTableSettings().withKeyRange({
// greaterOrEqual: TypedValues.tuple(TypedValues.optional(TypedValues.uint64(1))),
// lessOrEqual: TypedValues.tuple(TypedValues.optional(TypedValues.uint64(2))),
// }));
//
// expect(rows).toEqual(expectedRows);
// }
//
// {
// const rows = await readTable(session, new ReadTableSettings().withKeyRange({
// greater: TypedValues.tuple(TypedValues.optional(TypedValues.uint64(1))),
// lessOrEqual: TypedValues.tuple(TypedValues.optional(TypedValues.uint64(2))),
// }));
//
// expect(rows).toEqual(expectedRows.slice(1));
// }
});
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Driver from '../driver';
import { destroyDriver, initDriver } from '../test-utils';
import Driver from '../../../driver';
import { destroyDriver, initDriver } from '../../../test-utils';
import {
AlterTableDescription,
AlterTableSettings,
Column,
OperationParams,
TableDescription,
TableIndex,
} from '../table/table-session';
import { Types } from '../types';
} from '../../../table';
import { Types } from '../../../types';

const getTableName = () => `table_alter_${Math.trunc(1000 * Math.random())}`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Driver from '../driver';
import Driver from '../../../driver';
import {
createTable,
destroyDriver,
fillTableWithData,
initDriver,
Row,
TABLE
} from '../test-utils';
import {TableSession} from '../table/table-session';
} from '../../../test-utils';
import {TableSession} from '../../../table';
import {Ydb} from 'ydb-sdk-proto';

async function readTable(session: TableSession): Promise<Row[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Driver from '../driver';
import {destroyDriver, initDriver, TABLE} from '../test-utils';
import {Column, TableSession, TableDescription} from '../table/table-session';
import {declareType, TypedData, Types} from '../types';
import {withRetries} from '../retries';
import Driver from '../../../driver';
import {destroyDriver, initDriver, TABLE} from '../../../test-utils';
import {Column, TableSession, TableDescription} from '../../../table';
import {declareType, TypedData, Types} from '../../../types';
import {withRetries} from '../../../retries';

async function createTable(session: TableSession) {
await session.dropTable(TABLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {initDriver, destroyDriver} from '../test-utils';
import {initDriver, destroyDriver} from '../../../test-utils';

describe('Connection', () => {
it('Test GRPC connection', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Driver from '../driver';
import {destroyDriver, initDriver} from '../test-utils';
import {Column, DescribeTableSettings, TableDescription} from '../table/table-session';
import {TypedValues, Types} from '../types';
import Driver from '../../../driver';
import {destroyDriver, initDriver} from '../../../test-utils';
import {Column, DescribeTableSettings, TableDescription} from '../../../table';
import {TypedValues, Types} from '../../../types';
import Long from 'long';
import {Ydb} from 'ydb-sdk-proto';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http';
import Driver from "../driver";
import {destroyDriver, initDriver} from "../test-utils";
import {sleep} from "../utils";
import Driver from "../../../driver";
import {destroyDriver, initDriver} from "../../../test-utils";
import {sleep} from "../../../utils";

const SHUTDOWN_URL = process.env.YDB_SHUTDOWN_URL || 'http://localhost:8765/actors/kqp_proxy?force_shutdown=all';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {parseConnectionString} from '../parse-connection-string';
import {parseConnectionString} from '../../../parse-connection-string';

describe('Parse connection string', () => {
it('test parseConnectionString', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Driver from '../driver';
import Driver from '../../../driver';
import {
createTable,
destroyDriver,
fillTableWithData,
initDriver,
Row,
TABLE
} from '../test-utils';
import {ReadTableSettings, TableSession} from '../table/table-session';
import {TypedValues, TypedData} from '../types';
} from '../../../test-utils';
import {ReadTableSettings, TableSession} from '../../../table';
import {TypedValues, TypedData} from '../../../types';

async function readTable(session: TableSession, settings: ReadTableSettings): Promise<TypedData[]> {
const rows: TypedData[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Endpoint} from '../discovery';
import Driver from '../driver';
import {Endpoint} from '../../../discovery';
import Driver from '../../../driver';
import {
Aborted,
BadRequest,
Expand All @@ -18,11 +18,11 @@ import {
Unavailable,
Undetermined,
YdbError,
} from '../errors';
import {FallbackLogger} from '../logging';
import {RetryParameters, retryable} from '../retries';
import {destroyDriver, initDriver} from '../test-utils';
import {pessimizable} from '../utils';
} from '../../../errors';
import {FallbackLogger} from '../../../logging';
import {RetryParameters, retryable} from '../../../retries';
import {destroyDriver, initDriver} from '../../../test-utils';
import {pessimizable} from '../../../utils';

const logger = new FallbackLogger({level: 'error'});
class ErrorThrower {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Driver from '../driver';
import Driver from '../../../driver';
import {
TABLE,
createTable,
destroyDriver,
fillTableWithData,
initDriver,
Row,
} from '../test-utils';
import {TableSession} from '../table/table-session';
import {TypedData} from '../types';
} from '../../../test-utils';
import {TableSession} from '../../../table';
import {TypedData} from '../../../types';

async function executeScanQuery(session: TableSession): Promise<TypedData[]> {
const query = `SELECT * FROM ${TABLE};`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Long from 'long';
import {google, Ydb} from 'ydb-sdk-proto';
import Driver from '../driver';
import {initDriver, destroyDriver} from '../test-utils';
import {TypedData, TypedValues, Types} from '../types';
import Driver from '../../../driver';
import {initDriver, destroyDriver} from '../../../test-utils';
import {TypedData, TypedValues, Types} from '../../../types';
import NullValue = google.protobuf.NullValue;

describe('Types', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as grpc from '@grpc/grpc-js';
import jwt from 'jsonwebtoken';
import {DateTime} from 'luxon';
import {getOperationPayload, GrpcService, sleep, withTimeout} from './utils';
import {GrpcService, sleep, withTimeout} from './utils';
import {yandex, Ydb} from 'ydb-sdk-proto';
import {ISslCredentials, makeDefaultSslCredentials} from './ssl-credentials';
import IamTokenService = yandex.cloud.iam.v1.IamTokenService;
import AuthServiceResult = Ydb.Auth.LoginResult;
import ICreateIamTokenResponse = yandex.cloud.iam.v1.ICreateIamTokenResponse;
import type {MetadataTokenService} from '@yandex-cloud/nodejs-sdk/dist/token-service/metadata-token-service';
import {retryable} from './retries';
import {getOperationPayload} from "./table/table-utils";

function makeCredentialsMetadata(token: string): grpc.Metadata {
const metadata = new grpc.Metadata();
Expand Down Expand Up @@ -46,7 +47,7 @@ export class AnonymousAuthService implements IAuthService {
interface StaticCredentialsAuthOptions {
/** Custom ssl sertificates. If you use it in driver, you must use it here too */
sslCredentials?: ISslCredentials;
/**
/**
* Timeout for token request in milliseconds
* @default 10 * 1000
*/
Expand Down
3 changes: 2 additions & 1 deletion src/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import EventEmitter from 'events';
import {DateTime} from 'luxon';
import {Ydb} from "ydb-sdk-proto";
import {AuthenticatedService, getOperationPayload, withTimeout} from "./utils";
import {AuthenticatedService, withTimeout} from "./utils";
import {IAuthService} from "./credentials";
import {retryable} from "./retries";
// noinspection ES6PreferShortImport
Expand All @@ -11,6 +11,7 @@ import DiscoveryServiceAPI = Ydb.Discovery.V1.DiscoveryService;
import IEndpointInfo = Ydb.Discovery.IEndpointInfo;
import {Events} from "./constants";
import {ISslCredentials} from './ssl-credentials';
import {getOperationPayload} from "./table/table-utils";


type SuccessDiscoveryHandler = (result: Endpoint[]) => void;
Expand Down
11 changes: 11 additions & 0 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ClientOptions} from './utils';
import {parseConnectionString} from './parse-connection-string';
import {makeSslCredentials, ISslCredentials} from './ssl-credentials';
import {TableClient} from "./table";
import {QueryClient} from "./query/query-sessions-pool";

export interface IPoolSettings {
minLimit?: number;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default class Driver {
private discoveryService: DiscoveryService;

public tableClient: TableClient;
public queryClient: QueryClient;
public schemeClient: SchemeService;

constructor(settings: IDriverSettings) {
Expand Down Expand Up @@ -79,6 +81,15 @@ export default class Driver {
discoveryService: this.discoveryService,
logger: this.logger,
});
this.queryClient = new QueryClient({
database: this.database,
authService: this.authService,
sslCredentials: this.sslCredentials,
poolSettings: this.poolSettings,
clientOptions: this.clientOptions,
discoveryService: this.discoveryService,
logger: this.logger,
});
this.schemeClient = new SchemeClient({
database: this.database,
authService: this.authService,
Expand Down
Loading

0 comments on commit 64c9e03

Please sign in to comment.