Skip to content

Commit

Permalink
feat: add YDB Query Service client
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Zorkaltsev committed Mar 24, 2024
1 parent 8d433b9 commit f7a5958
Show file tree
Hide file tree
Showing 41 changed files with 6,220 additions and 3,493 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ build/

# secrets
secrets/*

# jest
coverage
7 changes: 0 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [5.2.0](https://github.com/ydb-platform/ydb-nodejs-sdk/compare/v5.1.1...v5.2.0) (2024-02-27)


### Features

* large code files are separated ([f5f9abe](https://github.com/ydb-platform/ydb-nodejs-sdk/commit/f5f9abe2321bfc457827f0df641b9092d110ab7b), [5e57af0](https://github.com/ydb-platform/ydb-nodejs-sdk/commit/5e57af0cbe7ff57e24841cffe1cae6f4ce25dc30))

## [5.1.1](https://github.com/ydb-platform/ydb-nodejs-sdk/compare/v5.1.0...v5.1.1) (2023-09-04)


Expand Down
25 changes: 25 additions & 0 deletions examples/query-service/data-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {declareType, TypedData, Types} from "../../src";

interface IRow {
id: number;
rowTitle: string;
time: Date;
}

export class Row extends TypedData {
@declareType(Types.UINT64)
public id: number;

@declareType(Types.UTF8)
public rowTitle: string;

@declareType(Types.DATETIME)
public time: Date;

constructor(data: IRow) {
super(data);
this.id = data.id;
this.rowTitle = data.rowTitle;
this.time = data.time;
}
}
148 changes: 148 additions & 0 deletions examples/query-service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
process.env.YDB_SDK_PRETTY_LOGS = '1';

import {
Driver,
getCredentialsFromEnv,
Logger,
TypedValues,
} from 'ydb-sdk';
import {Row} from './data-helpers';
import {main} from '../utils';

const TABLE = 'table';

async function createTestTable(driver: Driver) {
await driver.queryClient.do({
fn: async (session) => {
await session.execute({
text: `
DROP TABLE IF EXISTS ${TABLE};
CREATE TABLE ${TABLE}
(
id UInt64,
title Utf8,
time Timestamp,
PRIMARY KEY (id)
);`,
});
}
});
}

async function insert(driver: Driver) {
await driver.queryClient.do({
fn: async (session) => {
await session.execute({
parameters: {
'$id1': TypedValues.uint64(1),
'$title1': TypedValues.text('Some title1'),
'$id2': TypedValues.uint64(2),
'$title2': TypedValues.text('Some title2'),
'$timestamp': TypedValues.timestamp(new Date()),
},
text: `
INSERT INTO ${TABLE} (id, title, time)
VALUES ($id1, $title1, $timestamp);
INSERT INTO ${TABLE} (id, title, time)
VALUES ($id2, $title2, $timestamp);`,
});
}
});
return 2;
}

async function select(driver: Driver) {
const res = await driver.queryClient.do({
fn: async (session) => {
const res = await session.execute({
text: `
SELECT *
FROM ${TABLE};
SELECT * -- double
FROM ${TABLE}; `
});
let rowCount = 0;
for await (const resultSet of res.resultSets) {
console.info(`ResultSet index: ${resultSet.index}`)
for await (const row of resultSet.rows) {
rowCount++;
console.info(`row: ${row}`);
}
}
return rowCount;
}
});
console.info(`rowCount: ${res}`);
}

async function typedSelect(driver: Driver) {
const res = await driver.queryClient.do({
fn: async (session) => {
const res = await session.execute({
text: `
SELECT *
FROM ${TABLE};
SELECT * -- double
FROM ${TABLE}; `
});
let rowCount = 0;
for await (const resultSet of res.resultSets) {
console.info(`ResultSet index: ${resultSet.index}`)
for await (const row of resultSet.typedRows(Row)) {
rowCount++;
console.info(`row: ${row}`);
}
}
return rowCount;
}
});
console.info(`rowCount: ${res}`);
}

async function bulkUpsert(driver: Driver) {
await driver.queryClient.do({
fn: async (session) => {
function* dataGenerator(rowsCount: number) {
for (let id = 1; id <= rowsCount; id++)
yield new Row({
id,
rowTitle: `title_${id}`,
time: new Date(),
})
}

await session.execute({
text: `
UPSERT INTO ${TABLE} (id, title, time)
SELECT id, title, time FROM AS_TABLE($table)`,
parameters: {
'$table': Row.asTypedCollection([...dataGenerator(20)]),
}
});
},
});
}

async function run(logger: Logger, endpoint: string, database: string) {
const authService = getCredentialsFromEnv();
logger.debug('Driver initializing...');
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!await driver.ready(timeout)) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}

createTestTable(driver);
insert(driver);
select(driver);
bulkUpsert(driver);
typedSelect(driver);

// transactions

await driver.destroy();
}

main(run);
15 changes: 15 additions & 0 deletions jest.config.coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const config = require('./jest.config.development');

/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
...config,
collectCoverage: true,
collectCoverageFrom: [
'**/*.{js,ts}',
],
coverageDirectory: './coverage',
};
10 changes: 4 additions & 6 deletions jest.config.development.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module.exports = {
globals: {
'ts-jest': {
tsconfig: 'tsconfig-cjs.json'
}
},
roots: ['<rootDir>/src'],
preset: 'ts-jest',
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.{ts|tsx}?$': ['ts-jest', {
tsConfig: 'tsconfig.json',
}],
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
Expand Down
Loading

0 comments on commit f7a5958

Please sign in to comment.